processes and threads ......processes and threads prof.bracyand0van0renesse cs04410...
TRANSCRIPT
-
ProcessesandThreads
Prof. Bracy and0Van0RenesseCS04410
Cornell0University
based&on&slides&originally&created&by&Prof.&Sirer 1
Fun0Starts0Here!
What0is0involved0in0“starting0a0program”0or0“running0a0program”?! which0are0misnomers…How0can0I0run0multiple0processes0on0one0computer?
2
What0is0a0program?Program0is0a0file0containing:! executable0code0(machine0instructions)! data0(information0manipulated0by0these0instructions)0that0together0describe0a0computation0Resides0on0diskObtained0through0compilation0and0linking
3
Preparing0a0Program
Source
files
compiler/
assembler
Object
files
Linker
PROGRAM
An&executable&file
in&a&standard& format,
such&as&ELF&on&Linux,&
Microsoft&PE&on&Windows
Header
Code
Initialized&data
BSS
Symbol&table
Line&numbers
Ext.&refs
static&
libraries
(libc)
4
-
Process0!=0ProgramHeader
Code
Initialized&data
BSS
Symbol&table
Line&numbers
Ext.&refs
Code
Initialized&data
BSS
Heap
Stack
DLL’s
mapped& segments
Executable
Process.address.space
Program&is&passive
• Code&+&data
Process&is&running&program
• stack,®s,&program&counter
Example:
We&both&run&IE:
T Same&program&
T Separate&processes
5
What0is0a0Process?
A0process0is0an0abstraction of0a0computerRecap:0A0good0abstraction! is0portable0and0hides0implementation0details! has0an0intuitive0and0easyWtoWuse0interface! can0be0instantiated0many0times! is0efficient0and0reasonably0easy0to0implement
6
Basic0Process0Abstraction
ENVIRONMENT
(files,&etc.)ADDRESS
SPACE
REGISTERS
CPU
STATE
7
Typical0Virtual Address0Space0Layout0(of0a0single0process)
CODE
HEAP
STACK
0
(same0for0kernel0except0kernel0is0mapped0in0disjoint0range)
DATA
allocated
free
8
-
Process0Interface
CreateProcess(initial0state)0" processIDGetStatus(processID)0" runningStatusTerminate(processID)WaitFor(processID)0" completionStatusListProcesses()0" [0pid1,0pid2,0…0]
9
Kernel0implements0processes!
P1
OS&KERNEL
P2 P3
Supervisor&Mode
User&Mode
10
Emulation…
One0option0is0for0the0hardware0to0simulate0multiple0instances0of0the0same0or0other0hardware+ Useful0for0debugging,0emulation0of0ancient0hardware,0etc.– Too0inefficient0for0modernWday0daily0use
11
CPU0runs0each0process0directly
But0somehow each0process0has0its0own! Registers! Memory! I/O0resources! “thread0of0control”
12
-
Physical0RAM0Layout
0x0KERNEL
P2
P1
P3
allocated
free
13
Computer0Architecture0preWMMU
CPU
address&bus
data&bus
MEM
14
Computer0Architecture0with0MMU
CPU
address&bus
data&bus
MEMMMU
15MMU&=&Memory&Management&Unit
virtual physical
For0now:0Very0Simple0MMUTwo0registers:0Base0and0BoundOperation:! supervisor0mode:
# addr_out :=0addr_in &00x7FFFFFFF
! user0mode:# if0addr_in
-
Back0to0Software:00Process0Control0Block (10per0process)Process0IdentifierProcess0arguments0(for0identification0with0ps etc.)Process0status0(runnable,0waiting,0zombie,0…)User0Identifier0(for0security)! beware:0superuser ≠0supervisor
RegistersBase0/0BoundScheduling0/0accounting0info…
17
Abstract0life0of0a0process
New
Runnable
Zombie
interrupt&TTT
descheduling
dispatchRunning
Waiting
18
Invariants
Supervisor0mode0" PC0points0to0kernel0codeEquivalently:0PC0points0to0user0code0" user0modeUser0code0runs0with0interrupts0enabled
For0simplicity0(for0now):! Kernel0code0runs0with0interrupts0disabled! Single0core
19
createProcess(initial0state)
Allocate0memory0for0address0spaceInitialize0address0space! program0vs fork! program0≠0processAllocate0ProcessIDAllocate0process0control0blockPut0process0control0block0on0the0run0queue
20
-
Running0a0programEvery0OS0provides0a0“loader”0that0is0capable0of0converting0a0given0program0into0an0executing0instance,0a0process! A0program0in0execution0is0called0a0process
The0loader:! reads0and0interprets0the0executable0file! Allocates0memory0for0the0new0process0and0sets0process’s0memory0to0contain0code0&0data0from0executable
! pushes0“argc”,0“argv”,0 “envp”0on0the0stack! sets0the0CPU0registers0properly0&0jumps0to0the0entry0point
21
How0does0a0process0terminate?
! Exit(processStatus)! Terminate(ProcessID)! Executing0an0illegal0instruction! Using0up0quota! Accessing0illegal0memory0addresses
22
TimeWSharing
Illusion:0multiple0processes0run0at0same0timeReality:0only0one0process0runs0at0a0time! For0no0more0than0a0few010s0of0millisecondsBut0this0can0happen0in0parallel0to0another0process0waiting0for0I/O!
23
Context0Switching
Kernel0runsSwitch0to0process01Trap0to0kernelSwitch0to0another0(or0same)0processTrap0to0kerneletc. Context3switches
P1 K P2 K P2 KK P1
trap dispatch
24
-
Kernel0Operation0(conceptual)
Initialize0devicesInitialize0“First0Process”For0ever! while0device0interrupts0pending
# handle0device0interrupts! while0system0calls0pending
# handle0system0calls! if0run0queue0is0nonWempty
# select0a0runnable0process0and0switch0to0it
! otherwise# wait0for0device0interrupt 25
Dispatch:0kernel0" process
CurProc :=0&PCB0of0current0processSet0user0base/bound0registerRestore0process0registersExecute0ReturnFromInterrupt instruction! Sets0user0mode! Enables0interrupts! Restores0program0counter
26
Trap:0process0" kernel
Hardware:! Disables0interrupts! Sets0supervisor0mode! Saves0user0PC0and0SP0on0kernel0stack
# why0not0on0process0stack?
! Sets0kernel0stack0pointer! Sets0PC0to0trap0handlerSoftware:! Save0process0registers0in0PCB0of0CurProc! Handle0trap0(syscall,0device0interrupt,0etc.)
27
Causes0for0traps
Clock0interruptDevice0interruptSystem0callPrivileged0instructionDivide0by0zeroBad0memory0access…
28
-
Questions0about0system0calls
How0does0a0process0specify0what0system0call0to0invoke0and0what0parameters0to0use?How0does0the0kernel0protect0itself0and0other0processes?How0does0the0kernel0return0a0result0to0the0process?How0does0the0kernel0prevent0accidentally0returning0privacy0sensitive0data?
29
Class0Project
Implement0sleep(delay)0system0call
30
How0Much0To0AbstractUnix0and0Windows0provide0processes0that0look0like0idealized0machines,0with0nice0looking0file0abstractions,0network0abstractions,0graphical0windows,0etc.Xen,0KVM,0etc.0provide0processes0that0look0just0like0real0hardware! virtualization
Requires0different0kinds0of0things0from0kernels! Unix/Windows:0 implement0files,0network0protocols,0window0management
! Xen/KVM/…:0emulate0hardware
31
Virtual0Machine0Abstraction(“type01”)
Virtual&Machine&Monitor&kernel
Unix&Kernel Windows&NT&Kernel
P1 P2 P3 P4 P5
32
-
Things0to0emulate
Supervisor0modeMMU0registersDevice0registers…
Hardware0can0help! MultiWlevel0supervisor! MultiWlevel0MMU! …
FLASH&/&ROM
BLOCK&OF&RAM
BLOCK&OF&RAM
DEVICE®ISTERS
BITMAP& /&SCREEN
33
Processes0Under0Unix/Linuxfork()0system0call0to0create0a0new0process! Old0process0called0parent,0new0process0called0child
int fork()0clones the0invoking0process:! Allocates0a0new0PCB0and0process0ID! Allocates0a0new0address0space! copies0the0parent’s0address0space0into0the0child’s! in0parent,0fork()0 returns0PID0of0child! in0child,0fork()0 returns0a0zero.int fork()0returns0TWICE!
34
Example
int main(int argc, char **argv)
{ int parentPid = getpid();
int pid = fork();
if (pid == 0) {printf(“The child of %d is %d\n”, parentPid, getpid());
exit(0);
} else {
printf(“My child is %d\n”, pid);
exit(0);
}
}What does this program print?
35
Bizarre…(both0if0and0else0are0executed)$ cc a.c
$ ./a.out
The child of 23873 is 23874
My child is 23874
Parent
Child
Kernel
fork()
retsys
v0=0v0=23874
36
-
execve()0fork()0gets0us0a0new0address0space
int execve(char0*programName,0…)0completes0the0picture! throws0away0the0contents0of0the0calling0address0space! replaces0it0with0the0program0in0file0named0by0programName! starts0executing0at0header.startPC! PCB0remains0the0same0otherwise0(same0PID)
Pros:0Clean,0simpleCon:0duplicate0operations
37
Process0Termination,0part01Process0executes0last0statement0and0calls0exit())syscall! Process’0resources0are0deallocated by0operating0system
Parent0may0terminate0execution0of0child0process0(kill)! Task0assigned0to0child0is0no0longer0required! If0parent0is0exiting
# Some0OSes don’t0allow0child0to0continue0if0parent0terminates! All0children0terminated0W cascading0termination
38
Process0Termination,0part02
Process0first0goes0into0“zombie0state”Parent0can0wait0for0zombie0children! Syscall:0wait()0" (pid,0exit0status)After0wait()0returns,0PCB0of0child0is0garbage0collected
39
Class0Project
Write0a0simple0Linux0command0line0interpreter
40
-
Multiple0Cores
Modern0computers0often0have0several0if0not0dozens0of0coresEach0core0has0its0own0registers,0but0cores0share0memory0and0devicesCores0can0run0user0processes0in0parallelCores0need0to0synchronize0access0to0PCBs0and0devices! we’ll0learn0later0how0to0do0synchronizing
41
MultiWCore0Architecture
BUS
CORE&1 CORE&2 CORE&3
RAM FLASH/&
ROM
SCREEN
BUFFER
DISK
42
Abstraction…multiWthreaded0process
THREAD&1 THREAD&2
ENVIRONMENT(files,&etc.)
ADDRESS&SPACE
(MEMORY)
CPU&+®isters
43
THREAD&3
Why0MultiWthreading?Simpler,0more0efficient0for0a0process0to0take0advantage0of0multicore0machines! Instead0of0starting0multiple0processes,0each0with0its0own0address0space0and0a0single0thread0running0on0a0single0core
Not0just0for0CPU0parallelism:0I/O0parallelism0can0be0achieved0even0if0I/O0operations0are0“blocking”Program0structuring:0for0example,0servers0dealing0with0concurrent0incoming0eventsMight0well0have0more0threads0than0cores!!
44
-
45
Processes0and0Address0Spaces• What0happens0when0Apache0wants0to0run0multiple0concurrent0computations0?
• Two0heavyweight0address0spaces0for0two0concurrent0computations0?
Kernel
0x0
kernel
PCBs
EMACS
Apache
Mail
physical&memory
Kernel
0x0
kernel
PCBs
EMACS
Apache
Mail
physical&memory
Apache?
46
Processes0and0Address0SpacesEliminate0duplicate0address0spaces0and0place0concurrent0computations0in0the0same0address0space
Kernel
0x0
kernel
PCBs
EMACS
Apache
Mail
physical&memory
Kernel
0x0
kernel
PCBs
EMACS
Apache
Mail
physical&memory
$
Architecture
Process0consists0of! One0address0space0containing0chunks0of0memory! Shared0I/O0resources! Multiple0threads
# Each0with0its0own0registers,0in0particular0PC0and0SP# Each0has0its0own0stack0 in0the0address0space# Code0and0data0is0shared
Other)terms)for)threads! Lightweight)Process! Thread)of)Control! Task
47
Memory0Layout
CODE
DATA
STACK&1
STACK&3
STACK&2
SP
PC
48
-
Threads
LighterWweight0than0processes! process0is0abstract0computer (CPU,0mem,0devices,0…)! thread0is0abstract0core
Threads0need0to0be0mutually0trusting! Why?
Ideal0for0concurrent0programs0where0lots0of0code0and0data0are0shared! Servers,0GUI0code,0…
49 50
Sharing
What’s0shared0between0threads?! code0and0data0(address0space)! Privileges! Almost0everything0in0the0process
What0don’t0they0share?! PC,0registers,0stack0pointer,0and0stack
51
Separation0of0Thread0and0Process0conceptsConcurrency0(multiWthreading)00is0useful0for:! improving0program0structure! handling0concurrent0events0(e.g.,0web0requests)! building0parallel0programs
" multiWthreading0is0useful0even0on0a0uniprocessor
To0be0useful,0thread0operations0have0to0be0fast
Option0#1:0Kernel0threadsKernel0threads! Threads0share0a0single0location0in0memory! Separate0PCBs0for0each0thread! Each0PCB0has0same:0base0&0bound0register0values! Each0PCB0has0different:0 PC,0SP,0registers
52
Kernel
0x0
kernel
PCBs
EMACS
Apache
Mail
physical&memory
PCB
PCB
-
Option0#2:0User0ThreadsBuild0a0miniature0O.S.0in0user0space! OS0doesn’t0know0about0multiple0 threads! Single0PCBGenerally0more0efficient0than0kernel0threads! Why?
But…0kernel0threads0simplify0system0call0handling0and0scheduling! Why?
Kernel
0x0
kernel
PCBs
EMACS
Apache
Mail
physical&memory
User0Thread0Implementation0(P1!)User0process0supports:! Thread0Control0Block0(TCB)0table
# one0entry0per0thread! “context0switch”0operations
# save/restore0thread0state0in0TCB#much0like0kernelWlevel0context0switches
! yield()0operation:# thread0releases0core,0allows0another0thread0to0use0it# Automatic0preWemption0not0always0supported
! Thread0scheduler54
System0calls
With0user0threads,0a0process0may0have0multiple0systems0calls0outstanding0simultaneously0(one0per0thread)Kernel0PCB0must0support0this! why?
55
Things0to0Think0about
Coordination! How0do0cores0/0threads0synchronize0access0to0shared0memory0and0devices?Scheduling! Which0runnable0process0/0thread0runs0when?
56