Professional Documents
Culture Documents
Operating System
Operating System
5 6
Summary
• Exceptions
• Events that require non-standard control flow
• Generated externally (interrupts) or internally (traps and faults)
• After an exception is handled, one of three things may happen:
• Re-execute the current instruction
• Resume execution with the next instruction
• Abort the process that caused the exception
Process Management
(Context switch)
15
Processes – another important abstraction What is a process?
• First some preliminaries (first video) • Why are we learning about processes?
• Control flow • Processes are another abstraction in our computer system – the
• Exceptional control flow process abstraction provides an interface between the program and
• Asynchronous exceptions (interrupts) the underlying CPU + memory.
• Processes (second video) • What do processes have to do with exceptional control flow (previous
Creating new processes video)?
Fork-exec model • Exceptional control flow is the mechanism that the OS uses to enable
Zombies multiple processes to run on the same system.
• Process Scheduling (third video)
• Process life cycle • What is a program? A processor? A process?
• Process Control Block
• Definition: A process is an instance of a running program
• Process Scheduling
• One of the most important ideas in computer science
• Not the same as “program” or “processor”
17 18
time
time
21 22
Static Data
heap
SS:
ESP: stack
EEIP:
24 25
User Kernel
Process Process
Process Management
(System calls)
26
Creating New Processes & Programs fork: Creating New Processes
• fork-exec model: • pid_t fork(void)
• fork() creates a copy of the current process • creates a new process (child process) that is identical to the calling
• execve() replaces the current process’ code & address space with process (parent process), including all state (memory, registers, etc.)
the code for a different program • returns 0 to the child process
• returns child’s process ID (pid) to the parent process
• fork() and execve() are system calls pid_t pid = fork();
• Note: process creation in Windows is slightly different from Linux’s if (pid == 0) {
fork-exec model printf("hello from child\n");
} else {
printf("hello from parent\n");
• Other system calls for process management: }
• getpid()
• exit()
• wait() / waitpid() • fork is unique (and often confusing) because it is called once but
returns twice
29 30
31 32
Understanding fork Understanding fork
Process n Child Process m Process n Child Process m
pid_t pid = fork(); pid_t pid = fork(); pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) { if (pid == 0) { if (pid == 0) { if (pid == 0) {
printf("hello from child\n"); printf("hello from child\n"); printf("hello from child\n"); printf("hello from child\n");
} else { } else { } else { } else {
printf("hello from parent\n"); printf("hello from parent\n"); printf("hello from parent\n"); printf("hello from parent\n");
} } } }
pid_t pid = fork(); pid_t pid = fork(); pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) { if (pid == 0) { if (pid == 0) { if (pid == 0) {
pid = m printf("hello from child\n"); pid = 0 printf("hello from child\n"); pid = m printf("hello from child\n"); pid = 0 printf("hello from child\n");
} else { } else { } else { } else {
printf("hello from parent\n"); printf("hello from parent\n"); printf("hello from parent\n"); printf("hello from parent\n");
} } } }
33
hello from parent Which one is first? hello from child 34
execve: Loading and Running Programs execve: Loading and Running Programs
Stack bottom
Null-terminated
env var strings
Null-terminated
cmd line arg strings
envp[n] == NULL unused envp[n] == NULL
envp[n-1] envp[n] == NULL envp[n-1] “PWD=/homes/iws/dirname”
… envp[n-1] … “PRINTER=ps581”
envp[0] … envp[0] “USER=dirname”
envp[0]
argv[argc] == NULL
argv[argc] == NULL argv[argc-1] argv[argc] == NULL
argv[argc-1] … argv[argc-1] “/usr/include”
… argv[0] … “-l”
argv[0] Linker vars argv[0] “ls”
envp
argv
argc
Stack frame for
39 40
main Stack top
exit: Ending a process Zombies
• void exit(int status) • Idea
• Exits a process • When process terminates, it still consumes system resources
• Status code: 0 is used for a normal exit, nonzero for abnormal exit • Various tables maintained by OS
• atexit() registers functions to be executed upon exit • Called a “zombie”
• A living corpse, half alive and half dead
• Reaping
void cleanup(void) { • Performed by parent on terminated child
printf("cleaning up\n"); • Parent is given exit status information
}
• Kernel discards process
void fork6() { • What if parent doesn’t reap?
atexit(cleanup); • If any parent terminates without reaping a child, then child will be
fork(); function pointer reaped by init process (pid == 1)
exit(0); • But in long-running processes we need explicit reaping
}
• e.g., shells and servers
41 42
43 44
Recap of Processes Recap of Process Context
• Processes • Process Context code
• Before launching an executable, the program is passively residing in • Minimal set of state information that must be stored Static Data
the Disk Drive and Becomes active when loaded into main memory to allow a process to be stopped and restarted. heap
• At any instance, multiple active processes are running concurrently • Information stored in the Processor
stack
• Each process is allocated an address space by the OS that contains • Contents of Registers
different segments used by a program • Program Counter OR the Instruction Pointer (EIP) code
• A range of accessible addresses + the state associated with them • This information is stored in the main memory
EIP:
Static Data
CS: 0x000… heap
code
heap code
Static Data
SS: ESP: Operating System heap
stack
EBP: 0xFFF… stack
• Process Switch
• Operating system switches from one process to another Kernel Mode Vs. User Mode
• Requires saving the state of one process and restoring the state of another
process
• Two context switches required
• Into the kernel
• Out of the kernel
47