Operating System Structure CSCI 4061 Introduction to Operating Systems Applications Instructor: Abhishek Chandra Operating System Hardware 2 Questions Operating System Structure How does the OS manage different resources, applications, and events? What kind of functions and services does the OS provide to applications? User Programs Shells, Tools and Utilities Processes, File System, Virtual Memory, Threads, Sockets CPU, Memory, Disks, Devices Applications Operating System Hardware 3 4 1
Processes: Outline Process Definition Process Structure and States Process Creation and Execution Process Termination and Waiting What is a Process? A program in execution Basic unit of work Enables multiprogramming Provides a protection boundary 5 6 Program vs. Process Program: Passive entity Set of instructions A binary file Process: Active entity Executing path of instructions Live set of resources (CPU cycles, memory, files) Multiple processes can correspond to the same program What does a process contain? Program text: Binary code Program Counter: Pointer to current instruction Other registers (e.g., stack pointer) Data: Memory required for variables, functions Other objects: File descriptors, signals, locks Accounting information 7 8 2
Process Memory Layout Process Context High Address Args, environment Stack Heap Activation Records (function params, local vars, saved registers) Dynamic Memory Current state of process activity Determined by: Program counter value Function call stack Data values (data segment, bss, and heap) Register values bss Uninitialized Static Data Data Segment Initialized Static Data Low Address Program Text Binary Code 9 10 Multiprogramming OS multiplexes system resources among multiple processes Each process is allowed to run on the CPU for a short duration (quantum) Process has to give up the CPU if: its time quantum expires it has to wait for an event (I/O, signal, lock) Process States New: Being created Running: Executing instructions on the CPU Blocked: Waiting for an event Ready: Waiting to be assigned a CPU Done: Finished executing 11 12 3
Process Life Cycle new created quantum expired ready running done terminated Process Identity Each process has a unique identifier called pid OS keeps track of each process through its pid pid_t getpid(); Returns the pid for the current process event completion scheduled blocked wait for event 13 14 Processes in Unix Parent-child relation Each process has a parent init process: grand-daddy of all processes pid=1, ever-running process pid_t getppid(); Returns pid of parent Process Tree in Unix 1 2 5 3 4 6 7 8 15 16 4
Process Lifecycle Process is created Parent clones itself Process executes a program Loads program from a file Executes the code Process exits Parent might wait for the child to finish Process Creation: fork pid_t fork(); Parent process executes fork It creates an (almost) identical copy of itself New (child) process inherits a new copy of the parent s whole state and context: Code, data, open files Program counter, stack Two clones exist immediately after fork Most systems employ copy-on-write 17 18 Process Creation: fork Returning from fork A rgs, env ironment Stack Heap Data Segment, bss Program Text fork parent child A rgs, env ironment Stack Heap Data Segment, bss Program Text A rgs, env ironment Stack Heap Data Segment, bss Program Text fork() returns twice Once in parent and once in child Value returned by fork is different for each process Returns 0 to child process Returns pid of child to parent Returns -1 to parent if error Both processes resume from same point after fork(), but with different return values 19 20 5
Process Creation: fork pid_t child_pid; int x=1; child_pid=fork(); if (child_pid>0) /* This is parent process */ printf ( I m parent process: x=%d\n, x); else if (child_pid==0) /* This is child process */ printf ( I m child process: x=%d\n, x); Process Creation Example 1 pid_t child_pid; int i, n=3; for (i=0; i<n; i++) { child_pid=fork(); if (child_pid == 0) break; } 21 22 Process Creation Example 2 pid_t child_pid; int i, n=3; for (i=0; i<n; i++) { child_pid=fork(); if (child_pid!= 0) break; } Process Execution: exec fork creates identical clones But what if the child wants to execute some other program? int execv(char *path, char *argv[]); 6 variations of exec Use the same system call execve Differ in parameters 23 24 6
Process Execution: exec Process Execution: exec exec overwrites the memory image inherited from the parent Process starts from scratch with a new state: New code, program counter, data, stack New arguments, (sometimes) environment Retains some things from before exec: Open files parent A rgs, env 1 Stack 1 Heap 1 Data Segment 1, bss 1 Program Text 1 A rgs, env 1 Stack 1 exec A rgs, env 2 Stack 2 child Heap 1 Data Segment 1, bss 1 Program Text 1 Heap 2 Data Segment 2, bss 2 Program Text 2 25 26 Process Execution: exec pid_t child_pid; int x=1; child_pid=fork(); if (child_pid>0) /* This is parent process */ printf ( I m parent process: x=%d\n, x); else if (child_pid==0) /* This is child process */ { execv( /bin/ls, ls, l, NULL); printf ( I m child process: x=%d\n, x); } Normal Process Termination main function falls off the end return from main exit, _Exit or _exit void exit(int status); status value of 0 corresponds to successful completion OS cleans up all process state Releases memory, file pointers, registers, etc. Flushes print buffers 27 28 7
Abnormal Process Termination Call to abort() Receives a signal it does not catch Signals are software interrupts E.g.: Ctrl-C, segmentation fault OS does not call user-installed exit handlers Core dump may be produced Waiting for a Child: wait Sometime parent wants to wait for the child to finish execution Example: ls l Shell waits until command is executed pid_t wait(int *status); The parent suspends execution wait() returns when a child exits Returns pid of exited child status is pointer to child s exit status 29 30 Waiting for a Child: wait Waiting for a Specific Child: waitpid parent wait A process may have many children, but may want to wait for a specific child pid_t waitpid(pid_t pid, int *status, int opt); fork child exec exit pid: process id of specific child status: exit status of child opt: WNOHANG means no waiting, return 0 if child still running 31 Time 32 8
Process Groups Process group: Multiple processes having a single group leader By default, each process belongs to the process group of its parent waitpid allows you to wait for any process belonging to a process group: Negative pid indicates group_pid waitpid(-group_pid, NULL, 0); Uses of wait Synchronization Allows parent to synchronize its execution with the child/children Useful for interactive applications like the shell Reaping OS removes a process only when its parent waits for it Need to notify the exit status of the process 33 34 Orphans and Zombies Orphan: Running process whose parent dies before it finishes Zombie: Terminated process whose parent hasn t waited on it System does not remove child completely until parents does a wait Orphans are adopted by init process init does wait from time to time Eventually reaped Background Processes Shell spawns a process but does not wait for it E.g.: mozilla & Here, the parent does not wait for the child parent fork child exec 35 36 9
Daemons Forever running background processes Similar to shell Get some input Do something useful Print results, log errors if required Differences from shell: Each implements specific service May not be interactive Examples: Web server (httpd), print server (lpd), ssh daemon (sshd) Processes Summary Process Definition and Layout Process States fork() and exec() exit() and wait() 37 38 10