class12 ppt User level view of processes Exceptions Context switches Higher level control flow mechanisms Topics System level programming II Processes Feb 24 2000 The course that gives CMU its Zip 15 213 class12 ppt Time 2 startup inst1 inst2 inst3 instn shutdown Physical control flow CS 213 S 00 this sequence is the system s physical control flow or flow of control From startup to shutdown a CPU simply reads and executes a sequence of instructions one at a time Control flow class12 ppt Time Process A 3 Process B Process C CS 213 S 00 one process for each program that runs during the system s lifetime Modern operating systems partition the physical control flow into a collection of logical control flows called processes task or jobs Each process is an instance of a running program Processes class12 ppt Time Process A Concurrent A B A C Sequential B C Examples 4 Process B Process C CS 213 S 00 Two processes run concurrently are concurrent if their flows overlap in time Otherwise they are sequential Concurrent processes class12 ppt Time Process A 5 Process B CS 213 S 00 Process C Control flows for concurrent processes are physically disjoint in time However we can think of concurrent processes are running in parallel with each other User view of concurrent processes 6 MB MA class12 ppt Process B Process A MC CS 213 S 00 Process C Provides the illusion that the process has its own memory A process must take explicit steps to share part of its address space with other processes The OS also provides each process with its own address space Separate address spaces class12 ppt 7 if fork 0 printf hello from child n else printf hello from parent n CS 213 S 00 creates a new process child process that is identical to the calling process parent process returns 0 to the child process returns child s pid to the parent process int fork void Fork Creating new processes class12 ppt 8 main atexit cleanup if fork 0 printf hello from child n else printf hello from parent n exit void cleanup void printf cleaning up n CS 213 S 00 exits a process atexit registers functions to be executed upon exit void exit int status Exit Destroying processes class12 ppt 9 CS 213 S 00 if fork 0 printf hello from child n else printf hello from parent n wait child status printf child has terminated n exit main int child status suspends current process until one of its children terminates return value the pid of the child process that terminated if child status NULL then the object it points to will be set to a status indicating why the child process terminated int wait int child status Wait Synchronizing with children class12 ppt 10 CS 213 S 00 main if fork 0 execl usr bin cp cp foo bar 0 wait NULL printf copy completed n exit returns 1 if error otherwise doesn t return loads and runs executable at path with args arg0 arg1 path is the complete path of an executable arg0 becomes the name of the process typically arg0 is either identical to path or else it contains only the executable filename from path real arguments to the executable start with arg1 etc list of args is terminated by a char 0 argument int execl char path char arg0 char arg1 0 Exec Running new programs class12 ppt 11 child child grandchild shell Daemon e g snmp init 1 0 grandchild CS 213 S 00 child Linux process hierarchy class12 ppt init 1 0 12 CS 213 S 00 process 1 user mode process fork and exec sbin init process 0 handcrafted kernel process 1 Pushing reset button loads the PC with the address of a small bootstrap program 2 Bootstrap program loads the boot block disk block 0 3 Boot block program loads kernel e g vmunix 4 Boot block program passes control to kernel 5 Kernel handcrafts the data structures for process 0 Unix Startup Step 1 getty Daemons e g ftpd httpd class12 ppt init 1 etc inittab 0 13 CS 213 S 00 forks a getty get tty or get terminal for the console init forks new processes as per the etc inittab file Unix Startup Step 2 class12 ppt login init 1 0 14 CS 213 S 00 getty execs a login program Unix Startup Step 3 class12 ppt tcsh init 1 0 15 CS 213 S 00 login gets user s login and password if OK it execs a shell if not OK it execs another getty Unix Startup Step 4 class12 ppt 16 CS 213 S 00 for commands not in the shell command language if fork 0 execl cmd cmd 0 if background process retpid wait status read command line until EOF while read stdin buffer numchars parse command line if command line contains background process TRUE else background process FALSE Example Loading and running programs from a shell class12 ppt case 1 error fprintf fork error n exit 0 17 CS 213 S 00 default parent closes its copy of worker and repeats close worker sockfd void main master sockfd sl passivesock port create master socket while TRUE worker sockfd sl acceptsock master sockfd await request switch fork case 0 child closes its master and manipulates worker close master sockfd code to read and write to from worker socket goes here exit 0 Example Concurrent network server class12 ppt Time Process A Process B 18 user code kernel code user code kernel code user code CS 213 S 00 context switch context switch Question How does the control flow change from one process to another the kernel is not a separate process but rather runs as part of user process Processes are managed by a shared piece of OS code called the kernel Implementing processes class12 ppt 19 CS 213 S 00 only instructions can change control flow difficult for the CPU to react to internal or external events data arriving from a disk or a network adapter divide by zero user hitting ctl c no way for the OS to preempt one process for another processes must explicitly pass control to the OS cooperative multitasking a la Windows 3 1 These are insufficient for a useful system jumps call and return using the stack discipline So far in class we ve discussed two mechanisms for changing the control flow Altering the control flow 20 CS 213 S 00 process context switch process receiving a signal nonlocal jumps setjmp longjmp these higher level mechanisms are implemented by either OS software context switch and signals Language runtime library nonlocal jumps class12 ppt Higher level mechanisms exceptions change in control flow in response to an internal or external event implemented as a combination of both hardware and OS software Low level mechanism Advanced mechanisms for altering control flow class12 ppt IDE disk controller Memory disk Interrupt controller Processor disk 21 Modem Printer Network Network
View Full Document