20180917 ******************************************************* What is a process? A running program, including its machine state What's part of a process's machine state? Process state PID Address space - the memory the process Registers * Program counter * Stack pointer * Frame pointer With time sharing (multiprogramming), the OS switches between processes every 10-100 millisecond Pseudo-parallelism True parallelism requires a multi-processors system What is the API in the OS for a process: ----------- Create Destroy Wait Control + Kill + Suspend + Etc. Status Process Creation ~~~~~~~~~~~~~~~~~~~~~~ Processes are created when: 1) System initialization 2) The user requests a new process 3) A running process wants help (e.g., using fork()) 4) Starting a batch job (only on mainframes) OS steps to run a program ----------- Load (from disk) the code and any static data (initialized variables) into memory Allocate memory for the stack Initialize the stack (with argc and argv) Allocate some memory for the heap Setup file descriptors (stdin, stdout, stderr) Start executing main() Process Hierarchies ----------- UNIX: init starts running after booting, a new process for each terminal, builds a process tree Windows: No process hierarchy, but a parent process is given a special token (handle) to control the child process Process Termination ----------- Processes are terminated when: 1) Normal completion exit() or ExitProcess() 2) Error 3) Fatal error (involuntary) 4) Killed (involuntary) kill() or TerminateProcess() Process States ~~~~~~~~~~~~~~~~~~~~~~ Exercise Process Table ~~~~~~~~~~~~~~~~~~~~~~ OS tracks processes in the process table (aka Process Control Block (PCB)) Stores a process's + Program counter + Stack pointer + Memory allocation + Open files + Scheduling information + etc. Process API ~~~~~~~~~~~~~~~~~~~~~~ How many lines will be printed? #include #include int main(){ fork(); fork(); fork(); fprintf(stderr, "All done!\n"); return 0; } 20180919 ******************************************************* UNIX Commands: ../unixCommands.txt Process API ~~~~~~~~~~~~~~~~~~~~~~ fork() ----------- Makes a clone of the current process (with the same memory image, environment strings and open files) Memory is mapped so that it only makes a copy when the child writes to (changes) its memory The child generally calls exec() and replaces the memory