Operating Systems Lecture 23: System calls for process management in xv6

Operating Systems Lecture 23: System calls for process management in xv6

Understanding Process Management in xv6

Overview of Process Creation in xv6

  • The lecture focuses on system calls for process management within the xv6 operating system, starting with an introduction to the process tree and creation.
  • Upon booting, xv6 initiates the "init" process, which subsequently forks to create the shell process that serves as the user interface for command execution.

Shell Functionality and Command Execution

  • The shell prompts users for commands; when a command is entered (e.g., ls), it forks a child process to execute this command while waiting for completion before returning to prompt.
  • Certain commands like cd are executed directly by the shell without creating a new child process since they need to affect the parent shell's working directory.

Code Structure of xv6 Shell

  • The main function of the xv6 shell is initiated by "init" after booting. It includes standard input/output setup and reads user commands into a buffer.
  • For cd, the shell invokes its own change directory system call without forking; other commands lead to forked child processes where execution occurs via an exec system call.

System Calls and Kernel Interaction

  • System calls available in user programs are defined in user.h, which acts similarly to C library headers but is specific to xv6 as it does not utilize standard libraries.
  • When invoking a system call like fork, there’s a transition from user space into kernel space using an interrupt instruction (int), which facilitates access to privileged OS functions.

Mechanism of System Call Invocation

  • Each system call has an associated number pushed into a register, allowing the kernel to identify which function needs execution upon receiving control from user space.
  • This mechanism ensures that all interactions with kernel-level functions are properly managed through controlled entry points, maintaining security and stability within the operating system.

Fork System Call Implementation

Forking a Child Process in xv6

Understanding the Fork System Call

  • The fork function is invoked by a parent process to create a new child process, transitioning into kernel mode to execute the fork code.
  • A new entry for the child process is allocated in the process table (p table array), and the parent's memory state is copied to this new child.
  • The child's size matches that of the parent, and its pointer is set correctly. The child's state is marked as runnable for scheduling.
  • Additional attributes such as open files and process name are copied from the parent, and a unique PID is assigned to the child.
  • Upon completion, the parent returns with the child's PID while initializing certain registers so that the child returns 0.

Executing New Code with Exec System Call

Overview of Exec Functionality

  • The exec system call replaces an existing process's memory image with a new executable, discarding old data structures like stack and heap.
  • All previous code and data are erased; a fresh memory image containing new code, stack, and heap is created for execution.
  • The page table now points to this newly created memory image, allowing execution of updated instructions.

Process Termination via Exit System Call

Key Actions During Process Exit

  • When calling exit, a process cleans up its resources including any open files before terminating itself.
  • If there are children processes, they are re-parented to init, ensuring proper management of orphaned processes.
  • The exiting process marks itself as "zombie," indicating it has completed cleanup but still needs its state fully cleaned by its parent.

Cleaning Up After Child Processes with Wait System Call

Mechanism of Waiting for Child Termination

  • The wait system call allows a parent to check if any of its children have terminated or become zombies in order to clean up their states.

Process Management in xv6

Overview of Process Management System Calls

  • The discussion begins with a description of a waiting mechanism for processes, where if no "dead child" (terminated process) is found, the parent process will invoke a sleep function to wait until one becomes available.
  • A summary of system calls related to process management in xv6 is provided, specifically focusing on the fork system call. When invoked, it creates a new child process and marks it as runnable in the process table.
  • The child's memory image is initialized as a copy of the parent's memory. This includes duplicating various states from the parent into the child, allowing it to be scheduled by the scheduler when appropriate.
  • The exit system call is explained; when called, it marks the calling process as a zombie and relinquishes control. The wait system call allows parents to clean up after their zombie children once they terminate.
Video description

For more information, please visit: https://www.cse.iitb.ac.in/~mythili/os/