CS 208 s21 — The Process Model
1 Video
Here is a video lecture for part of the material outlined below. The entire outline covers CSPP sections 8.2–8.4 (p. 732–754).
Link to the Panopto viewer: https://carleton.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=9a85af12-c218-47ea-8927-ac700169cd3d
2 Processes
- an instance of a program in execution
- each process has the illusion of exclusive use of the processor and memory
2.1 Logical Control Flow
- processes take turns using the processor
- a process is temporarily suspended or preempted when other processes take their turns
- this switching is hidden from the processes
- only detectable through precise measurment of time between instructions
2.2 Concurrent Flows
- process flows overlap
- switching between concurrent processes is called multitasking or time slicing
- independent of whether there are multiple processors
- two processes running simultaneously on different processors are said to be running in parallel
2.3 Private Address Space
- illusion of uniform layout of exclusive memory for each process
2.4 User and Kernel Modes
- typically control via mode bit in some special register
- kernel mode can execute any instruction, access any memory
- user mode prevented from executing privilaged instructions or accessing the kernel area of memory
- instead must operate indirectly through system calls
- exception handlers run in kernel mode
2.5 Context Switches
- kernel maintains the context for each process
- everything needed to restart a preempted process
- register values, instruction pointer, user's stack, and other data structures
- kernel switches from one process to another via context switch: saving current process context and loading in a previously saved one for a different process
- the decisions for doing these switches are collectively called scheduling
- contexting switching often occurs when a process is waiting for I/O (e.g., disk read) or when an interrupt occurs
3 System Call Error Handling
- good practice to always check for errors after making a system call
- possible to define convenient wrapper functions that abstract the error checking
4 Process Control
4.1 Obtaining Process IDs
- each process has a unique id (PID)
getpidreturns PID of calling process,getppidreturns PID of parent of calling process (i.e., process that created calling process)
4.2 Creating and Terminating Processes
- a process is either running, stopped, or terminated
- a stopped process is suspended until it receives a SIGCONT signal and resumes
- a process is terminated by a signal, by returning from
main, or by callingexitexittakes anintdetermines the exit status of the process- exit status also set by the return value from
main - exit status used to indicate whether a process completed successfully or ended in some kind of error state
- a parent process creates a new child process by calling
fork- child process get a separate copy of the parent's private address space and access to any file descriptors open when
forkwas called (differeny PIDs)- including code, child runs parent's code from the point of the
forkcall
- including code, child runs parent's code from the point of the
forkis weird: called once, returns twice (in the parent and in the child)- returns child's PID to parent, returns 0 to child (providing a way for the code to check which process it is)
- kernel free to interleave the execution of parent and child, meaning we can't assume anything about which will run first, etc.
- multiple calls to
forkcan get very difficult to reason about
- child process get a separate copy of the parent's private address space and access to any file descriptors open when
4.3 Reaping Child Processes
- a terminated process persists until it is reaped by its parent
- reaping passes the child's exit status to the parent
- an un-reaped process is called a zombie
- when a parent terminates, it's non-terminated child processes become children of the
initprocessintireaps any zombie children- necessary for long-running processes like shells to reap children, as zombies still take up memory
- a process waits for a child to terminate or stop by calling
waitpid- by default it suspends calling process until a child process in its wait set terminates
- returns immediately if a child has already terminated
4.3.1 Wait Set
waitpidtakespidarugment- if
pid > 0, wait set is single process with that PID - if
pid = -1, wait set is all child processes
- if
4.3.2 Modifying Default Behavior
- set
optionsarugment to combinations (i.e.,or-ing) of:- WNOHANG: return immediately with value 0 if no child process has terminated yet
- WUNTRACED: wait for child process to terminate or stop
- WCONTINUED: wait for child process to terminate or continue via SIGCONT signal
4.3.3 Checking the Exit Status of a Reaped Child
- provide non-null pointer
statusargument,waitpidwill store child's exit status as the value pointed to bystatus wait.hprovides macros for interpreting exit status
4.3.4 Error Conditions
waitpidreturns -1 if there are no children or if it was interrupted
4.4 Putting Processes to Sleep
sleepstops process for a given number of secondspausestops process until it receives a signal
4.5 Loading and Running Programs
- function
execveruns and executable with an argument list and environment variable list- does not return unless there's an error
- global variable
environalso references environment variables - functions
getenv,setenv, andunsetenvmanipulate environment array
4.6 Using fork and execve
- neat mini-shell example, p. 754-56