20180913 ******************************************************* Processes ================================= What is a process? A running program, includes its machine state What's part of a process's machine state? Process's state Open files Registers + program counter + stack pointer + frame pointer Address space - the memory for the process With time sharing (multiprogramming), the OS switches between processes every 10-100 milliseconds Pseudo-parallelism True parallelism requires a multi-processor system What is the API in the OS for a process: ----------- Create Destroy Wait Control + Suspend + Kill + Etc. Status Process Creation ~~~~~~~~~~~~~~~~~~~~~~ Processes are created when: 1) User requested a new process (opened an app) 2) A running process wants help (e.g., using fork()) 3) System initialization 4) Starting a batch job (only on mainframes) OS steps to run a programs ----------- 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, building a process tree Windows: No process hierarchy, but the 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 ~~~~~~~~~~~~~~~~~~~~~~ Main process states Blocked Running Ready Possible process state transitions Exercise Implementation of Processes ~~~~~~~~~~~~~~~~~~~~~~ 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. 20180918 ******************************************************* Process API ================================= fork() ~~~~~~~~~~~~~~~~~~~~~~ UNIX: ----------- Makes a clone of the current process (with the same memory image, environment strings and open files) Memory is mapped so that a copy only happens when the child writes to (changes) the memory The child generally calls exec() and replaces the memory 2 steps: needed fro redirecting standard output and error Windows ----------- 1 step: CreateProcess() (so we have a different address space to begin with) man fork Example: fork-example-02.c Possible orderings: 1) [pid: 33070] Original process [pid: 33070] Parent created process with PID 33071 [pid: 33070] Final statement [pid: 33071] Hello, world (from the child process)! [pid: 33071] Final statement 2) [pid: 33070] Original process [pid: 33071] Hello, world (from the child process)! [pid: 33071] Final statement [pid: 33070] Parent created process with PID 33071 [pid: 33070] Final statement 3) [pid: 33070] Original process [pid: 33071] Hello, world (from the child process)! [pid: 33070] Parent created process with PID 33071 [pid: 33071] Final statement [pid: 33070] Final statement 4) [pid: 33070] Original process [pid: 33071] Hello, world (from the child process)! [pid: 33070] Parent created process with PID 33071 [pid: 33070] Final statement [pid: 33071] Final statement 5) [pid: 33070] Original process [pid: 33070] Parent created process with PID 33071 [pid: 33071] Hello, world (from the child process)! [pid: 33070] Final statement [pid: 33071] Final statement 6) [pid: 33070] Original process [pid: 33070] Parent created process with PID 33071 [pid: 33071] Hello, world (from the child process)! [pid: 33071] Final statement [pid: 33070] Final statement wait() ~~~~~~~~~~~~~~~~~~~~~~ Suspends execution until a child process completes exec() ~~~~~~~~~~~~~~~~~~~~~~ Overwrites the current process Example: fork-exec-example.c UNIX Commands =================================