Section 2 Processes January 27rd, 2017 Taught by Joshua Don
Quick 61C Review char *string = “hello”; 0xFFF..FF 0x000..00 Stack Heap Text char *string = “hello”; int main(int argc, char* argv[]) { int x = 10; char *another_string = malloc(10); } Where is string? argc? x? another_string?
Quick 61C Review char *string = “hello”; 0xFFF..FF 0x000..00 Stack Heap Text char *string = “hello”; int main(int argc, char* argv[]) { int x = 10; char *another_string = malloc(10); } Where is string? Text argc? Stack x? Stack another_string? Stack, but it points to a loc on the heap
Dual Mode Operation Two modes of operation. User and kernel mode. We want to protect ourselves from user programs which can’t be trusted and other processes from each other. Examples of procedures that are in kernel mode? How do we get in kernel mode?
Dual Mode Operation Two modes of operation. User and kernel mode. We want to protect ourselves from user programs which can’t be trusted and other processes from each other. Examples of procedures that are in kernel mode? Scheduling Handling interrupts Filesystem management How do we get in kernel mode? Syscall (a programmatic interface to the kernel) Interrupt (asynchronous event ie. Timer going off) Exception (synchronous event ie. Divide by 0)
Fork
Fork Process A 0xFFF..FF Stack int main() { . . . pid_t p = fork() } Heap Text int main() { . . . pid_t p = fork() }
Fork continued Process A 0xFFF..FF Stack int main() { . . . Heap Text int main() { . . . pid_t p = fork() } Fork!!!!
Fork continued Process A Process B (PID = 1) 0xFFF..FF 0x000..00 Stack Heap Text 0xFFF..FF 0x000..00 Stack Heap Text int main() { . . . pid_t p = fork() } int main() { . . . pid_t p = fork() }
Fork continued Process A Process B (PID = 1) 0xFFF..FF 0x000..00 Stack Heap Text 0xFFF..FF 0x000..00 Stack Heap Text int main() { . . . pid_t p = fork() } int main() { . . . pid_t p = fork() } 1
Wait
wait() function signature is wait(int *status) “waits” for any child process to terminate. wait(&status) same as waitpid(-1, &status, 0) waitpid generally used to wait on a specific child (first argument to waitpid is the PID of the process to wait on) -1 in this case means “any child”, so it is equivalent to wait()
Wait continued int main() { int status; pid_t p = fork() if (p != 0){ Process A Process B (PID = 1) 0xFFF..FF 0x000..00 Stack Heap Text 0xFFF..FF 0x000..00 Stack Heap Text int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } exit(0); int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } exit(0);
Wait continued int main() { int status; pid_t p = fork() if (p != 0){ Process A Process B (PID = 1) 0xFFF..FF 0x000..00 Stack Heap Text 0xFFF..FF 0x000..00 Stack Heap Text int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } exit(0); int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } exit(0);
Wait continued int main() { int status; pid_t p = fork() if (p != 0){ Process A Process B (PID = 1) 0xFFF..FF 0x000..00 Stack Heap Text 0xFFF..FF 0x000..00 Stack Heap Text int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } exit(0); int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } exit(0);
Wait continued int main() { int status; pid_t p = fork() if (p != 0){ Process A Process B (PID = 1) 0xFFF..FF 0x000..00 Stack Heap Text 0xFFF..FF 0x000..00 Stack Heap Text int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } exit(0); int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } exit(0);
Wait continued int main() { int status; pid_t p = fork() if (p != 0){ Process A Process B (PID = 1) 0xFFF..FF 0x000..00 Stack Heap Text 0xFFF..FF 0x000..00 Stack Heap Text int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } exit(0); int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } exit(0);
Wait continued int main() { int status; pid_t p = fork() if (p != 0){ Process A Process B (exit code = 0) 0xFFF..FF 0x000..00 Stack Heap Text int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } exit(0);
Exec
Exec Process B Process A 0xFFF..FF 0x000..00 Stack Heap Text 0xFFF..FF int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } else { exec(“/bin/ls”); } exit(0); int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } else { exec(“/bin/ls”); } exit(0);
Exec continued Process B Process A 0xFFF..FF 0x000..00 Stack Heap Text int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } else { exec(“/bin/ls”); } exit(0); int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } else { exec(“/bin/ls”); } exit(0);
Exec continued Process B Process A 0xFFF..FF 0x000..00 Stack Heap Text int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } else { exec(“/bin/ls”); } exit(0); int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } else { exec(“/bin/ls”); } exit(0);
Exec continued Process B Process A int main() { // code for // /bin/ls 0xFFF..FF 0x000..00 Stack Heap Text 0xFFF..FF 0x000..00 Stack Heap Text Process A Process B int main() { int status; pid_t p = fork() if (p != 0){ // parent wait(&status); } else { exec(“/bin/ls”); } exit(0); int main() { // code for // /bin/ls exit(0); }