Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Processes – another important abstraction

• First some preliminaries (first video)


• Control flow
• Exceptional control flow
• Asynchronous exceptions (interrupts)

• Processes (second video)


• Creating new processes
Process Management • Fork and wait

(Exception control) • Zombies


• Process Control Box (third video)
• Process life cycle
• Process Control Block
• Process Scheduling

Control Flow Control Flow


• So far, we’ve seen how the flow of control changes as a single program • Processors do only one thing:
executes • From startup to shutdown, a CPU simply reads and executes
• A CPU executes more than one program at a time though – we also need (interprets) a sequence of instructions, one at a time
to understand how control flows across the many components of the • This sequence is the CPU’s control flow (or flow of control)
system
• This abstraction is provided by the operating system layer that not only is
Physical control flow
responsible to load these programs but also provide them all the
required services at the right time. <startup>
• Programs accesses disk drives inst1
• Requests for memory using system calls malloc, calloc etc.
inst2
• Exceptional control flow is the basic mechanism used for: time
inst3
• Transferring control between processes and OS …
• Handling I/O and virtual memory within the OS instn
• Implementing multi-process applications like shells and web servers <shutdown>
• Implementing concurrency 3 4
Altering the Control Flow Exceptional Control Flow
• Up to now: two ways to change control flow: • Exists at all levels of a computer system
• Jumps (conditional and unconditional)
• Low level mechanisms
• Call and return
• Exceptions
Both react to changes in program state
• change processor’s control flow in response to a system event
• Processor also needs to react to changes in system state
(i.e., change in system state, user-generated interrupt)
• user hits “Ctrl-C” at the keyboard
• Combination of hardware and OS software
• user clicks on a different application’s window on the screen
• data arrives from a disk or a network adapter • Higher level mechanisms
• instruction divides by zero • Process context switch
• system timer expires • Through the use of Signals
• Can jumps and procedure calls achieve this? • Implemented by either:
• Jumps and calls are not sufficient – the system needs mechanisms for • OS software
“exceptional” control flow! • C language runtime library

5 6

Exceptions Interrupt Vectors


• An exception is transfer of control to the operating system (OS) in response to
some event (i.e., change in processor state) Exception
numbers

User Process OS code for • Each type of event has a


exception handler 0 unique exception number k
Exception code for
exception
event current_instr. exception processing Table exception handler 1 • k = index into exception table
next_instr. by exception 0
1 (a.k.a. interrupt vector)
handler code for
2 exception handler 2
• return to current_instr ... • Handler k is called each time
• return to next_instr n-1 ... exception k occurs
• abort
code for
• Examples: exception handler n-1
div by 0, page fault, I/O request completes, Ctrl-C
• How does the system know where to jump to in the OS?
7
basically a jump table for exceptions… 8
Asynchronous Exceptions (Interrupts) Synchronous Exceptions
• Caused by events external to the processor • Caused by events that occur as a result of executing an instruction:
• Indicated by setting the processor’s interrupt pin(s) (wire into CPU) • Traps
• Handler returns to “next” instruction • Intentional: transfer control to OS to perform some function
• Examples: system calls, breakpoint traps, special instructions
• Examples:
• Returns control to “next” instruction
• I/O interrupts
• Faults
• hitting Ctrl-C on the keyboard
• Unintentional but possibly recoverable
• clicking a mouse button or tapping a touchscreen • Examples: page faults (recoverable), segment protection faults
• arrival of a packet from a network (unrecoverable), integer divide-by-zero exceptions (unrecoverable)
• arrival of data from a disk • Either re-executes faulting (“current”) instruction or aborts
• Hard reset interrupt • Aborts
• hitting the reset button on front panel • Unintentional and unrecoverable
• Soft reset interrupt • Examples: parity error, machine check (hardware failure detected)
• hitting Ctrl-Alt-Delete on a PC • Aborts current program
9 10

Trap Example: Opening File


Fault Example: Page Fault int a[1000];
• User writes to memory location main ()
• User calls: open(filename, options) {
• Function open executes system call instruction int • That portion (page) of user’s memory a[500] = 13;
is currently on disk }
0804d070 <__libc_open>:
. . .
80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10
804d082: cd 80 int $0x80
804d084: 5b pop %ebx
. . .
User Process OS
User Process OS
exception: page fault
movl
exception Create page and
int load into memory
pop returns
open file
returns

• Page handler must load page into physical memory


• OS must find or create file, get it ready for reading or writing • Returns to faulting instruction: mov is executed again!
• Returns integer file descriptor • Successful on second try
11 12
Fault Example: Invalid Memory Reference Exception Table IA32 (Excerpt)
int a[1000];
main ()
{ Exception Number Description Exception Class
a[5000] = 13;
}
0 Divide error Fault
13 General protection Fault
80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360 fault
14 Page fault Fault
User Process OS
18 Machine check Abort
exception: page fault 32-127 OS-defined Interrupt or trap
movl
128 (0x80) System call Trap
detect invalid address
signal process 129-255 OS-defined Interrupt or trap

• Page handler detects invalid address http://download.intel.com/design/processor/manuals/253665.pdf


• Sends SIGSEGV signal to user process
• User process exits with “segmentation fault”
13 14

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

Processes (Cont.… ) Processes (Cont.… )


• Process provides each program with two key abstractions: • Each process has its own private address space.
• Logical control flow
• Each process seems to have exclusive use of the CPU
• Private virtual address space
• Each process seems to have exclusive use of main memory
• Why are these illusions important?
• How are these illusions maintained?
• Process executions are interleaved (multi-tasking)
• In reality many other programs are running on the system
• Process take turn in using the processor: Each time period that a process
executes a portion of its flow is called a time slice
• Virtual Memory: OS provides a private space for each process
• The private address space is called the virtual address space, which is a
linear array of bytes, addressed by n bit virtual address (videos on
virtual memories)
19 20
Concurrent Processes User View of Concurrent Processes
• Two processes run concurrently (are concurrent) if their instruction • Control flows for concurrent processes are physically disjoint in time
executions (flows) overlap in time • CPU only executes instructions for one process at a time
• Otherwise, they are sequential • However, we can think of concurrent processes as executing in parallel
• Examples:
• Concurrent: A & B, A & C
• Sequential: B & C

Process A Process B Process C


Process A Process B Process C

time
time

21 22

Context Switching Context Switching


• Processes are managed by a shared chunk of OS code called the kernel • Processes are managed by a shared chunk of OS code called the kernel
• Important: the kernel is not a separate process, but rather runs as part of • Important: the kernel is not a separate process, but rather runs as part of
a user process a user process
• Control flow passes from one process to another via a context switch… • Control flow passes from one process to another via a context switch…
(how?) (how?)

Process A Process B Process A Process B

user code user code


saves state of process A
kernel code context switch kernel code context switch
Restores state of process B

time user code time user code


Saves state of process B
kernel code context switch kernel code context switch
restores state of process A

user code user code


23 24
Main Memory
x86 – segments and stacks Before interrupt has occurred
code

Processor Static Data


Registers heap
User Kernel
CS EIP
Process Process
SS ESP stack
DS EAX
ES EBX
ECX
EDX
EIP:
ESI
EDI
CS:
code

Static Data
heap

SS:
ESP: stack
EEIP:
24 25

After interrupt occurs

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

Understanding fork Understanding fork


Process n Process n Child Process m
pid_t pid = fork(); pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) { if (pid == 0) { if (pid == 0) {
printf("hello from child\n"); printf("hello from child\n"); printf("hello from child\n");
} else { } else {
} else { printf("hello from parent\n"); printf("hello from parent\n");
printf("hello from parent\n"); } }
}

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");
} } } }

pid_t pid = fork(); pid_t pid = fork();


if (pid == 0) { if (pid == 0) {
printf("hello from child\n"); printf("hello from child\n");
} else { } else {
printf("hello from parent\n"); printf("hello from parent\n");
} }

33
hello from parent Which one is first? hello from child 34

Fork Example Fork-Exec


• Parent and child both run the same code • Fork-exec model:
• Distinguish parent from child by return value from fork() • Fork() creates a copy of the current process
• Which runs first after the fork() is undefined • Execve() replaces the current process’ code & address space with the
• Start with same state, but each has a private copy code for a different program
• Same variables, same call stack, same file descriptors, same register • There is a whole family of exec calls – see exec(3) and execve(2)
contents, same program counter…
// Example argument: path=“/usr/bin/ls”
void fork1() // argv[0]=“/usr/bin/ls”, argv[1]=“-ahl”, argv[2]=NULL
{ void fork_exec(char *path, char *argv[1])
int x = 1; {
pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) { if (pid != 0) {
printf("Child has x = %d\n", ++x); printf(“Parent: created a child %d\n", pid);
} else { } else {
printf("Parent has x = %d\n", --x); printf(“Child: exec-ing new program now \n");
} execve(path, argv)
printf("Bye from process %d with x = %d\n", getpid(), x); }
} printf(“This line is printed by parent only! \n);
}
35 36
Exec-ing a new program execve: Loading and Running Programs
Stack bottom
Null-terminated
Stack env var strings
• int execve(
char *filename, Null-terminated
Very high-level diagram of what cmd line arg strings
char *argv[],
happens when you run the char *envp[] unused
Heap command ”ls” in a Linux shell: ) envp[n] == NULL
Data • Loads and runs in current process: envp[n-1]
Code: /usr/bin/bash • Executable filename …
fork(): • With argument list argv envp[0]
child argv[argc] == NULL
parent child • And environment variable list envp
argv[argc-1]
Stack • Env. vars: “name=value” strings
Stack Stack (e.g. “PWD=/homes/iws/bpw”) …
argv[0]
exec(): • execve does not return (unless error) Linker vars
• Overwrites code, data, and stack envp
Heap Heap • Keeps pid, open files, a few other items argv
Data Data Data argc
Code: /usr/bin/bash Code: /usr/bin/bash Code: /usr/bin/ls 37 Stack frame for
Autumn 2013 Processes 38
35
main Stack top

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

wait: Synchronizing with Children Summary


• int wait(int *child_status) • Processes
• Suspends current process (i.e. the parent) until one of its children • At any given time, system has multiple active processes
terminates • Only one can execute at a time, but each process appears to have
total control of the processor
• Return value is the pid of the child process that terminated
• OS periodically “context switches” between active processes
• On successful return, the child process is reaped • Implemented using exceptional control flow
• If child_status != NULL, then the int that it points to will be
• Process management
set to a status indicating why the child process terminated
• fork: fork gets us two copies of the same process (but fork()
• NULL is a macro for address 0, the null pointer returns different values to the two processes)
• There are special macros for interpreting this status – see wait(2) • exec: replaces the code and address space of the new program. It is
called once and usually has no return
• If parent process has multiple children, wait() will return when any of • wait or waitpid: is used for synchronization among the children
processes
the children terminates
• exit: is used to terminate the process, it has no return
• waitpid() can be used to wait on a specific child process

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

Static Data Proc 1 Proc 1• …. Proc 1 stack

heap code

Static Data
SS: ESP: Operating System heap
stack
EBP: 0xFFF… stack

• OS periodically “context switches” between active processes to allow


sharing of CPU resources 45 46

Process vs. Context Switches


• Terms often used interchangeably, but there is a difference
• Context Switch
• System switches from running a process to running kernel code
• At least one mode switch is required
• Computationally expensive operation

• 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

You might also like