Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Meeting Summary: CS 326 Lecture/Lab Spring 2025

Date: March 19, 2025
Time: 06:35 PM Pacific Time (US and Canada)
Meeting ID: 813 3845 7711


Quick Recap

  • Shell Programs & Processes:
    Greg explained the functions of various programs within a shell and clarified the difference between programs (static code) and processes (active executions in memory).

  • Process Creation via Forking:
    He described how a new process is created using the fork system call, discussed file descriptor table manipulation in child processes, and noted the complexities involved with multiple processes and threads.

  • Redirection & File System in XV6:
    The lecture covered redirecting command output to a file using shell commands and reviewed the persistent nature of the file system in the XV6 system.


Next Steps

  • Students will attempt to implement the rdir program for file redirection as discussed in class.
  • Students are expected to review and understand concepts of file descriptor tables, forking, and exec.
  • The next class session will include a demonstration of pipes and their implementation.
  • Students should prepare questions about pipes for the upcoming discussion.

Detailed Topics Covered

Understanding Shell Programs and Pipes

Greg discussed the following:

  • Each program in a shell is designed for a specific redirection task, such as redirecting standard input from a file or sending output to a file.
  • The concept of pipes, which enable one command’s output to be used as another command’s input, was introduced.
  • Emphasis was placed on understanding these concepts and their practical implementation within the shell environment.

Explaining Programs and Processes in Operating Systems

Key points included:

  • The distinction between a program (static code) and a process (the execution of that code).
  • An overview of the proc struct in the kernel, highlighting important fields like the process ID (PID) and the file descriptor table.
  • A description of a running process’s structure, which includes both user space memory segments (code, stack, data, and heap) and the kernel-managed proc struct.
  • The introduction of the Control-P command, which enables viewing running processes.

Unix Process Model and Forking

Discussion points:

  • An explanation of early Unix process models, which separate the creation of a process from the execution of a program.
  • The concept of forking followed by executing a new program in the forked process.
  • How this design offers flexibility in manipulating the file descriptor table for redirection.
  • A brief history of Unix and the C programming language at Bell Labs, emphasizing the longevity and adaptability of the Unix process model.

Forking Process and File Descriptor Manipulation

Highlights:

  • The process of creating a new process via fork, along with the differences between the parent and the forked (child) process.
  • The ability to manipulate the file descriptor table before or after the fork.
  • A review of a previous redirection example, noting a mistake where a file was opened twice, which could cause a race condition if both parent and child processes created the same file.

File Descriptor Management Best Practices

Best practices discussed include:

  • Opening files only once to avoid complications in multi-process/thread environments.
  • Redirecting standard output to a file descriptor and ensuring that unnecessary file descriptors are closed for proper cleanup.
  • Maintaining tidiness in file descriptor management to prevent unforeseen issues.

Addressing Duplicate Command Execution Issue

Key discussion points:

  • A problematic scenario was detailed where a command was executed twice—once in the child process and once in the parent—leading to potential issues.
  • The execution process using the Exec function (which takes a path and an array of arguments) was clarified.
  • An explanation was provided on how string literals are assigned to the argument array.
  • The demonstration included using the ls command with parameters and outlined some limitations of the shell.

File Descriptor Manipulation in Child Processes

Topics covered:

  • The importance of understanding the file descriptor table layout and ensuring unused slots are closed before new ones are created.
  • The use of pipes for redirecting output was explained.
  • A demonstration showed how a file opened in the parent process is closed to allow the child process to manage the file descriptor.
  • The significance of the fork function’s return value (the process ID, not a file descriptor) was emphasized.
  • Guidance was provided on modifying code to achieve desired file descriptor manipulation results.

File Descriptor Manipulation within a Child Process

Additional clarifications:

  • When a new file descriptor is needed, the operating system searches for the first available slot in the table.
  • In the child process, standard output can be redirected to a new file descriptor in a simplified manner.
  • Closing a file descriptor frees up its entry, making it available for future use.
  • Each process maintains its own file descriptor table, which is inherited by the child process during a fork.

Process Structure and Memory Usage

Discussion points:

  • The overall process structure, including the parent process, the proc table, and the allocation of user space memory, was covered.
  • It was explained that when a process is forked, a copy is made, leaving the original process unchanged.
  • Considerations were made regarding the balance of process counts and open files—while these can be increased, doing so might result in higher memory usage.
  • Special attention was given to the standard output, critical for communication with consoles, files, or network connections.
  • The discussion concluded with an explanation of the various components that constitute a process, including kernel structures, file descriptor tables, process IDs, and memory segments.

Redirecting Output to a File

Key points discussed:

  • The shell’s process of redirecting command output to a file was reviewed.
  • By default, command output is sent to the console, but it can be redirected using shell syntax (e.g., “ls > food.txt”).
  • The usage of the specialized command tee, which allows simultaneous output to the console and a file, was introduced.
  • Although the shell can run any command, a specific example was given where it consistently executed the “ls” command and redirected its output to food.txt.

xv6 File System and Security

Final discussion topics:

  • The persistent file system in xv6 was explained, including its creation via the mkfs command and the reuse of an existing file system image.
  • It was noted that all programs, including those on iOS and Android, exhibit process behaviors similar to those in the xv6 system.
  • Security concerns across various operating systems were touched upon, with specific mention of a known vulnerability in Unix and the security challenges faced by Windows.
  • The session concluded with a note that the next class would focus on pipes.