More on Processes
Process Life Cycle
- process life cycle
- kernel needs to track which event a process is waiting on (see proc.h)
Process APIs
- fork
- creates a new process that is an exact copy of the calling process
- child's virtual address space is identical to the parent at the time of fork
- after fork, updates to parent's or child's address space is only visible to that process
- separate physical memory, separate memory translation table
- child's execution states are the same as parent's
- where should the child start executing?
- how do we know which one is parent & which one is child?
- diffferent
%rax
, parent gets the child's pid, child gets a 0
- child inherits parent's OS resources (e.g. same set of open files)
- easy way for parent to configure the child's OS resources
- exec
- loads a new program into the current process
- does not create a new process (same pid & OS resources)
- replaces the address space and execution states of the current process
- new code, data, heap, and stack region
- new instruction pointer & stack pointer
- what should happen to the old address space?
- takes a list of string args to pass to the new program
- process's view:
int main(int argc, char** argv)
- argv[0] = program name, argv[1..] = command line arg pointers
- OS sets up the users's stack with the command line args and the argv array (null terminated)
- often used in combination with fork to create a new process and executes a new program
- shell uses fork + exec to implement redirect, how?
- fork/exec combo
- simple semantics: process creation is separated from program loading
- any cost to this combo?
- what happens to the forked memory upon an exec?
- can we do better? copy-on-write (COW) fork!
- COW fork
- shares the same physical memory for as long as processes are only doing reads
- upon a write, actually makes the copy so that processes can write independently
- how might we detect a write to shared memory?
- maps writable memory as read only, writes will then cause page faults (exception!)
- is fork really a simple semantic?
- exit
- terminates the current process, does not return!
- process scheduling state => terminated
- how does a process stop execution?
- must free up in-use OS resources
- open files should be closed, virtual address space deallocated
- what about its kernel stack? can a process free its own kernel stack?
- wait
- waits for a child process to exit, does not return until the child exits
- what should hapen if no child has exited?
- what should happen if a child already exited?
- kernel needs to track the parent child relationship
- parent responsible for cleaning up the rest of child's resources
- what if parent exits without waiting on its children?