Lecture Slides 08 083-Processes-exec
Lecture Slides 08 083-Processes-exec
Section 8: Processes
What is a process
Creating processes
Fork-Exec
Fork-Exec
University of Washington
Fork-Exec
fork-exec model:
fork() creates a copy of the current process
execve() replaces the current process’ code & address space with
the code for a different program
There is a whole family of exec calls – see exec(3) and execve(2)
exec():
Heap Heap
Data Data Data
Code: /usr/bin/bash Code: /usr/bin/bash Code: /usr/bin/ls
Fork-Exec
University of Washington
void cleanup(void) {
printf("cleaning up\n");
}
void fork6() {
atexit(cleanup);
fork();
exit(0);
}
Fork-Exec
University of Washington
Zombies
Idea
When process terminates, it still consumes system resources
Various tables maintained by OS
Called a “zombie”
A living corpse, half alive and half dead
Reaping
Performed by parent on terminated child
Parent is given exit status information
Kernel discards process
What if parent doesn’t reap?
If any parent terminates without reaping a child, then child will be
reaped by init process (pid == 1)
But in long-running processes we need explicit reaping
e.g., shells and servers
Fork-Exec
University of Washington
Fork-Exec
University of Washington
wait Example
void fork_wait() {
int child_status;
pid_t child_pid;
if (fork() == 0) { HC Bye
printf("HC: hello from child\n");
} else {
child_pid = wait(&child_status); CT Bye
printf("CT: child %d has terminated\n”,
child_pid);
}
printf("Bye\n");
exit(0);
}
Fork-Exec
University of Washington
Fork-Exec
University of Washington
Summary
Processes
At any given time, system has multiple active processes
Only one can execute at a time, but each process appears to have total
control of the processor
OS periodically “context switches” between active processes
Implemented using exceptional control flow
Process management
fork-exec model
Fork-Exec