Processes Fork Fork Example 1 2 3 Fork Again
Total Page:16
File Type:pdf, Size:1020Kb
11/12/15 fork pid_t fork() 1. Clone current parent process to create identical child process, including all state (memory, registers, program counter, …). Processes 2. Continue executing both copies with one difference: • returns 0 to the child process • returns child’s process ID (pid) to the parent process pid_t pid = fork(); Focus: if (pid == 0) { Process model printf("hello from child\n"); Process management case study: Unix/Linux/Mac OS X } else { (Windows is a little different.) printf("hello from parent\n"); } fork is unique: called in one process, returns in two processes! (once in parent, once in child) 3 fork example fork again Process n Parent and child continue from private copies of same state. pid_t pid = f ork( ); Memory contents (code, globals, heap, stack, etc.), if (p id = = 0) { Register contents, program counter, file descriptors… pr intf ("he llo from chi ld\n "); } els e { 1 pr intf ("he llo from par ent\ n"); } Child Process m Only difference: return value from fork() Relative execution order of parent/child after fork() undefined pid_t pid = f ork( ); à m pid_t pid = f ork( ); à 0 if (p id = = 0) { if (p id = = 0) { pr intf ("he llo from chi ld\n "); pr intf ("he llo from chi ld\n "); } els e { } els e { void fork1() { 2 pr intf ("he llo from par ent\ n"); pr intf ("he llo from par ent\ n"); int x = 1; } } pid_t pid = fork(); if (pid == 0) { pid_t pid = f ork( ); pid_t pid = f ork( ); if (p id = = 0) { if (p id = = 0) { printf("Child has x = %d\n", ++x); 3 pr intf ("he llo from chi ld\n "); pr intf ("he llo from chi ld\n "); } else { } els e { } els e { pr intf ("he llo from par ent\ n"); pr intf ("he llo from par ent\ n"); printf("Parent has x = %d\n", --x); } } } printf("Bye from process %d with x = %d\n", getpid(), x); hello from parent hello from child Which prints first? } 4 5 1 11/12/15 When you run the command ls fork-exec Exec-ing a new program in a shell: fork-exec model: Stack fork() clone current process Code/state of shell process. execv() replace process code and context (registers, memory) with a fresh program. 1 See man 3 execv, man 2 execv e Heap Replaced by code/state of ls. Data Copy of code/state // Example arguments: path="/usr/bin/ls”, Code: /usr/bin/bash of shell process. // argv[0]="/usr/bin/ls”, argv[1]="-ahl", argv[2]=NULL fork(): void fork_exec(char* path, char* argv[]) { parent child child pid_t pid = fork(); Stack if (pid != 0) { Stack Stack printf("Parent: created a child %d\n”, pid); } else { exec(): printf("Child: exec-ing new program now\n"); 2 2 3 execv(path, argv); Heap Heap } Data Data Data printf("This line printed by parent only!\n"); Code: /usr/bin/bash Code: /usr/bin/bash Code: /usr/bin/ls } 6 Code/state of shell process. 7 execv: load/start program exit: end a process Stack bottom Null-terminated int execv(char* filename, env var strings void exit(int status) End process with status: 0 = normal, nonzero = error. char* argv[]) Null-terminated argument strings atexit() registers functions to be executed upon exit loads/starts program in current process: unused Executable filename envp[n] == NULL With argument list argv envp[n-1] … overwrites code, data, and stack envp[0] Keeps pid, open files, a few other items argv[argc] == NULL does not return argv[argc-1] … unless error argv[0] Linker vars envp argv Also sets up environment. See also: execve. argc Stack frame for main Stack top 8 9 2 11/12/15 Zombies! wait for child processes to terminate Terminated process still consumes system resources pid_t waitpid(pid_t pid, int* stat, int ops) Various tables maintained by OS Suspend current process (i.e. parent) until child with pid ends. A living corpse, half alive and half dead On success: Reaping with wait/waitpid Return pid when child terminates. Parent waits to reap child once child terminates Reap child. Parent receives child exit status. Kernel discards process. If stat != NULL, waitpid saves termination reason where it points. What if parent doesn’t reap? If any parent terminates without reaping a child, then child will be reaped See also: man 3 waitpid by init process (pid == 1) But in long-running processes we need explicit reaping e.g., shells and servers 10 11 waitpid example HCBye Error-checking CTBye Check return results of system calls for errors! (No exceptions.) void fork_wait() { Read documentation for return values. int child_status; pid_t child_pid == fork(); Use perror to report error, then exit. if (child_pid == 0) { printf("HC: hello from child\n"); void perror(char* message) } else { Print "message: reason that last system call failed." if (-1 == waitpid(child_pid, &child_status, 0) { perror("waitpid"); exit(1); } printf("CT: child %d has terminated\n”, child_pid); } printf("Bye\n"); exit(0); } 12 3 11/12/15 Examining Processes on Linux (demo) Summary ps Processes pstree System has multiple active processes top Each process appears to have total control of the processor. OS periodically “context switches” between active processes /proc Implemented using exceptional control flow Process management fork, execv, waitpid 14 16 4.