Professional Documents
Culture Documents
• Key States:
• Ready – In memory, ready to execute
• Blocked – In memory, waiting for an event
• Blocked Suspend – On disk, waiting for an event
• Ready Suspend – On disk, ready to execute
Modes of Execution
• User mode
• Less-privileged mode
• User programs typically execute in this mode
• System mode, control mode, or kernel mode
• More-privileged mode
• Kernel of the operating system
• Flags
• Interrupt vectors
Process Control Block
•Process identification
• Identifiers
• Numeric identifiers that may be stored with the
process control block include
• Identifier of this process
• Identifier of the process that created this process
(parent process)
• User identifier
Process Control Block
• int 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"); Fork is interesting
} else { (and often confusing)
printf("hello from parent\n"); because it is called
} once but returns twice
exit: Destroying Process
• void exit(int status)
• exits a process
• Normally return with status 0
• atexit() registers functions to be executed upon exit
void cleanup(void) {
printf("cleaning up\n");
}
void fork6() {
atexit(cleanup);
fork();
exit(0);
}
Zombies
• Idea
• When process terminates, still consumes system resources
• Various tables maintained by OS
• Called a “zombie”
• 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
wait: Synchronizing with children
• int wait(int *child_status)
• suspends current process until one of its children terminates
• return value is 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 terminated
• Exceptions
• Events that require nonstandard control flow
• Generated externally (interrupts) or internally (traps and faults)
• Processes
• At any given time, system has multiple active processes
• Only one can execute at a time, though
• Each process appears to have total control of processor + private memory space
• Spawning Processes
• Call to fork
• One call, two returns
• Terminating Processes
• Call exit
• One call, no return
• Reaping Processes
• Call wait or waitpid
Concurrency
• Key idea:
• separate the concept of a process (address space, etc.)
• …from that of a minimal “thread of control” (execution state:
PC, etc.)
• This execution state is usually called a thread
Threads and processes
• Most modern OS’s (MacOS, Windows, modern UNIX) therefore support two
entities:
• the process, which defines the address space and general process
attributes (such as open files, etc.)
• the thread, which defines a sequential execution stream within a
process
• A thread is bound to a single process / address space
• address spaces, however, can have multiple threads executing within
them
• sharing data between threads is cheap: all see the same address space
• creating threads is cheap too!
• Threads become the unit of scheduling
• processes / address spaces are just containers in which threads execute