Program Translation and Execution II: Processes Oct 1, 1998ProcessesForkExitWaitExecExample: Concurrent network serverProcess hierarchyUnix startup (1)Unix startup (2)Unix startup (3)Unix startup (4)Loading and running programs from a shellProcess memory image (Alpha)Kernel block diagramUser and kernel modesSystem call interfaceHardware controlProcess control: Context of a processProcess control: Context switchProcess control: Process statesProcess states and state transitionsSetjmp/LongjmpSetjmp/Longjmp exampleProgram Translation and Execution II:ProcessesOct 1, 1998Topics•User-level view of processes•Implementation of processes•setjmp/longjmpclass12.ppt15-213Introduction to Computer SystemsCS 213 F’98– 2 –class12.pptProcessesA process is an instance of a running program•runs concurrently with other processes (multitasking)•managed by a shared piece of OS code called the kernel–kernel is not a separate process, but rather runs as part of some user process•each process has its own data space and process id (pid)•data for each protected protected from other processesProcess AProcess Buser codekernel codeuser codekernel codeuser codeTimeJust a stream of instructions!CS 213 F’98– 3 –class12.pptForkint 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 F’98– 4 –class12.pptExitvoid exit(int status)•exits a process•atexit() function registers functions to be executed on 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 F’98– 5 –class12.pptWaitint wait(int child_status)•waits for a child to terminate and returns status and pidmain() { 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 F’98– 6 –class12.pptExecint execl(char *path, char *arg0, char *arg1, ...)•loads and runs executable at path with args arg0, arg1, ...•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 F’98– 7 –class12.pptExample: Concurrent network servervoid main() { master_sockfd = sl_passivesock(port); /* create master socket */ while (1) { slave_sockfd = sl_acceptsock(master_sockfd); /* await request */ switch (fork()) { case 0: /* child closes its master and manipulates slave */ close(master_sockfd); /* code to read and write to/from slave socket goes here */ exit(0); default: /* parent closes its copy of slave and repeats */ close(slave_sockfd); case -1: /* error */ fprintf("fork error\n"); exit(0); } }}CS 213 F’98– 8 –class12.pptProcess hierarchyshellchildchildchildgrandchildgrandchildinit (1)(0)Daemone.g. snmpCS 213 F’98– 9 –class12.pptUnix startup (1)init (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 F’98– 10 –class12.pptUnix startup (2)init [1][0]forks a getty (get tty or get terminal)for the consolegettyDaemonse.g. snmp/etc/inittabinit forks new processes as perthe /etc/inittab fileCS 213 F’98– 11 –class12.pptUnix startup (3)init [1][0]getty execs a login programloginCS 213 F’98– 12 –class12.pptUnix startup (4)init [1][0]login gets user’s login and passw if OK, it execs a shellif not OK, it execs another gettytcshCS 213 F’98– 13 –class12.pptLoading and running programsfrom a shell/* read command line until EOF */while (read(stdin, buffer, numchars)) { <parse command line> if (<command line contains ‘&’ >) amper = 1; else amper = 0; } /* for commands not in the shell command language */ if (fork() == 0) { execl(cmd, cmd, 0) } if (amper == 0) retpid = wait(&status);}CS 213 F’98– 14 –class12.pptProcess memory image (Alpha)Reserved for kernelReserved for shared librariesand dynamic loaderAvailable for heapHeap (via malloc() or sbrk()Grows upBss segmentData segmentText segmentStackNot accessibleAvailable for stackGrows down to zeroNot accessible by convention(64KB)$gp$sp0x0000 0000 0000 00000x0000 0000 0000 ffff0x0000 0000 0001 00000x0000 0000 1fff ffff0x0000 0001 2000 00000x0000 03ff 7fff ffff0x0000 03ff 8000 00000x0000 03ff ffff ffff0x0000 0400 0000 00000xffff fbff ffff ffff0xffff fc00 0000 00000xffff ffff ffff ffffCS 213 F’98– 15 –class12.pptKernel block diagramhardware (processor and devices)hardware control (interrupt and exception handlers)device driversbuffer cachechar blockfile system process controlsystem call interfacelibrariesUser programsUser-levelKernel levelKernel levelHw levelCS 213 F’98– 16 –class12.pptUser and kernel modes User mode•Process can–execute its own instructions and access its own data.•Process cannot–execute kernel instructions or privileged instructions (e.g. halt)–access kernel data or data from other processes.Kernel mode•Process can –execute kernel instructions and privileged instructions–access kernel and user addresses Processes transition from user to kernel mode via•interrupts and exceptions •system calls (traps)CS 213 F’98– 17 –class12.pptSystem call interfaceSystem calls (traps) are expected program events•e.g., fork(), exec(), wait(), getpid()User code•call user-level library function, •executes special syscall instruction–e.g. syscall(id)•switch from user mode to kernel mode•transfer control to kernel system call interfaceSystem call interface •find entry in syscall table corresponding to id•determine number of parameters•copy parameters from user member to kernel memory•save current process context
View Full Document