System-level programming II:ProcessesFeb 24, 2000Topics• User-level view of processes• Exceptions• Context switches• Higher level control flow mechanismsclass12.ppt15-213“The course that gives CMU its Zip!”CS 213 S’00– 2 –class12.pptControl flow<startup>inst1inst2inst3…instn<shutdown>From startup to shutdown, a CPU simply reads and executes a sequence of instructions, one at a time.• this sequence is the system’s physical control flow (or flow of control).Physical control flowTimeCS 213 S’00– 3 –class12.pptProcessesModern 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.• one process for each program that runs during the system’s lifetime. TimeProcess A Process B Process CCS 213 S’00– 4 –class12.pptConcurrent processesTwo processes run concurrently (are concurrent) if their flows overlap in time.Otherwise, they are sequential.Examples:• Concurrent: A & B, A&C• Sequential: B & CTimeProcess A Process B Process CCS 213 S’00– 5 –class12.pptUser view of concurrent processesControl flows for concurrent processes are physically disjoint in time.However, we can think of concurrent processes are running in parallel with each other.TimeProcess A Process B Process CCS 213 S’00– 6 –class12.pptSeparate address spacesThe OS also provides each process with its own address space.• A process must take explicit steps to share part of its address space with other processes.Provides the illusion that the process has its own memory.Process A Process B Process CMAMBMCCS 213 S’00– 7 –class12.pptFork(): Creating new processesint fork(void)• 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 if (fork() == 0) {printf(“hello from child\n”);} else { printf(“hello from parent\n”);}CS 213 S’00– 8 –class12.pptExit(): Destroying processes void exit(int status)• exits a process• atexit() registers functions to be executed upon exitvoid cleanup(void) {printf(“cleaning up\n”);}main() {atexit(cleanup);if (fork() == 0) {printf(“hello from child\n”);} else { printf(“hello from parent\n”);} exit();}CS 213 S’00– 9 –class12.pptWait(): Synchronizing with childrenint wait(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 terminatedmain() {int child_status; if (fork() == 0) {printf(“hello from child\n”);}else {printf(“hello from parent\n”);wait(&child_status);printf(“child has terminated\n”);}exit();}CS 213 S’00– 10 –class12.pptExec(): Running new programsint execl(char *path, char *arg0, char *arg1, …, 0)• 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• returns -1 if error, otherwise doesn’t return!main() {if (fork() == 0) {execl(“/usr/bin/cp”,“cp”,“foo”,“bar”,0);}wait(NULL);printf(“copy completed\n”);exit();}CS 213 S’00– 11 –class12.pptLinux process hierarchyshellchildchildchildgrandchildgrandchildinit [1][0]Daemone.g. snmpCS 213 S’00– 12 –class12.pptUnix Startup: Step 1init [1][0]process 0: handcrafted kernel processprocess 1: user mode processfork() and exec(/sbin/init)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.CS 213 S’00– 13 –class12.pptUnix Startup: Step 2init [1][0]forks a getty (get tty or get terminal)for the consolegettyDaemonse.g. ftpd, httpd/etc/inittabinit forks new processes as perthe /etc/inittab fileCS 213 S’00– 14 –class12.pptUnix Startup: Step 3init [1][0]getty execs a login programloginCS 213 S’00– 15 –class12.pptUnix Startup: Step 4init [1][0]login gets user’s login and password if OK, it execs a shellif not OK, it execs another gettytcshCS 213 S’00– 16 –class12.pptExample: Loading and running programs from a shell/* read command line until EOF */while (read(stdin, buffer, numchars)) {<parse command line>if (<command line contains ‘&’>)background_process = TRUE;else background_process = FALSE;/* for commands not in the shell command language */if (fork() == 0) {execl(cmd, cmd, 0)}if (!background_process)retpid = wait(&status);}CS 213 S’00– 17 –class12.pptExample: Concurrent network servervoid 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);default: /* parent closes its copy of worker and repeats */close(worker_sockfd);case -1: /* error */fprintf("fork error\n");exit(0);}}}CS 213 S’00– 18 –class12.pptImplementing processesProcesses are managed by a shared piece of OS code called the kernel• the kernel is not a separate process, but rather runs as part of user processQuestion: How does the control flow change from one process to another?Process A Process Buser codekernel codeuser codekernel codeuser codeTimecontext switchcontext switchCS 213 S’00– 19 –class12.pptAltering the control flowSo far in class, we’ve discussed two mechanisms for changing the control flow:• jumps • call and return using the stack discipline.These are insufficient for a useful system• 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.CS 213
View Full Document