Processes&Threads
CS4410,Opera5ngSystems
Fall2016CornellUniversity
RachitAgarwalAnneBracy
See:Ch3&4inOSPPtextbook
TheslidesaretheproductofmanyroundsofteachingCS4410byProfessorsSirer,Bracy,Agarwal,George,andVanRenesse.SomecontentfromMarkusPüschelatCMU.
WhatisaProcess?
2
•Aninstanceofaprogram•Anabstrac5onofacomputer:
AddressSpace+Execu5onContext+Environment
AgoodabstracOon:•isportableandhidesimplementa5ondetails•hasanintui5veandeasy-to-useinterface•canbeinstan5atedmany5mes•isefficientandreasonablyeasytoimplement
ProcessManagement
3
Canaprogram…• Createaninstanceofanotherprogram?• Waitforittocomplete?• Stoporresumeanotherrunningprogram?• Senditanasynchronousevent?
Whoshouldbeallowedtostartaprocess?
4
Possibility#1:Onlythekernelmaystartaprocess
Possibility#2:User-levelprocessesmaystartprocesses
SystemCallInterface
5
System CallInterface
Portable OperatingSystem Kernel
PortableOS Library
Web ServersCompilers Source Code Control
Web Browsers Email
Databases Word Processing
x86 ARM PowerPC
10Mbps/100Mbps/1Gbps Ethernet
802.11 a/b/g/n SCSI IDE
Graphics Accelerators LCD Screens
Whysoskinny?
Example:Crea%ngaProcess
Windows:CreateProcess(…);
UNIXfork+exec
BeginningaProcessviaCreateProcess
6
Kernelhasto:•Create&ini5alizePCBinthekernel• Createandini5alizeanewaddressspace• Loadtheprogramintotheaddressspace• Copyargumentsintomemoryinaddressspace• Ini5alizehwcontexttostartexecu5onat“start”• Informschedulerthatnewprocessisreadytorun
[Windows]
AbstractLifeofaProcess
7
New
Runnable Running
Zombie
Waiting
admiSed done
I/OOperaOonI/OCompleOon
dispatch
interrupt,descheduling
DetailsonThursday.
CreateProcess(Simplified)
8
SystemCall
if(!CreateProcess(NULL,//Nomodulename(usecommandline)argv[1],//CommandlineNULL,//ProcesshandlenotinheritableNULL,//ThreadhandlenotinheritableFALSE,//SethandleinheritancetoFALSE0,//NocreationflagsNULL,//Useparent'senvironmentblockNULL,//Useparent'sstartingdirectory&si,//PointertoSTARTUPINFOstructure&pi)//PtrtoPROCESS_INFORMATIONstructure)
[Windows]
Fork+Exec
9
pid=fork();if(pid==0)exec(B);elsewait(pid);
PC
pid?
ProgramA
pid=fork();if(pid==0)exec(B);elsewait(pid);
PC
pid0
ProgramA
pid=fork();if(pid==0)exec(B);elsewait(pid);
PC
pid42
ProgramA
main(){...}
PC
pid0
ProgramB
[UNIX]
Process1
ifandelsebothexecuted!Process1
Process42 Process42
BeginningaProcessviaCreateProcessFork
10
Kernelhasto:•Create&ini5alizePCBinthekernel• Createandini5alizeanewaddressspace
• Loadtheprogramintotheaddressspace• Copyargumentsintomemoryinaddressspace• IniOalizetheaddressspacewithacopyoftheenOrecontentsoftheaddressspaceoftheparent
• Ini5alizehwcontexttostartexecu5onat“start”• InheritexecuOoncontextofparent(e.g.openfiles)
• Informschedulerthatnewprocessisreadytorun[UNIX]
Codeexample
11
/* * Corresponds to Figure 3.5 in the textbook * */
#include <stdio.h> #include <unistd.h>
int main() {
int child_pid = fork();
if (child_pid == 0) { // child process printf("I am process #%d\n", getpid()); return 0; } else { // parent process. printf("I am the parent of process #%d\n", child_pid); return 0; } }
Possibleoutputs?
CreaOngandManagingProcesses
12[UNIX]
fork Create a child process as a clone of the current process. Returns to both parent and child.
exec(prog, args)
Run the application prog in the current process.
exitTell the kernel the current process is complete, and its data structures (stack, heap, code) should be garbage collected.
Why not necessarily PCB?
wait(pid) Pause until the child process has exited.
kill(pid, type)
Send an interrupt of a specified type to a process.
QuesOons
13
•CanUNIXfork()returnanerror?Why?
•CanUNIXexec()returnanerror?Why?
•CanUNIXwait()everreturnimmediately?Why?
WhatisaShell?
14
Jobcontrolsystem• runsprogramsonbehalfoftheuser•allowsprogrammertocreate/managesetofprograms
• sh OriginalUnixshell(StephenBourne,AT&TBellLabs,1977)• csh BSDUnixCshell(tcsh:enhancedcsh
atCMUandelsewhere)• bash “Bourne-Again”Shell
Runsatuser-level.Whatsystemcallsdoesituse?
Built-InUNIXShellCommands
15[UNIX]
jobs List all jobs running in the background + all stopped jobs.
bg <job> Run the application prog in the current process.
fg <job> Change a stopped or running background job to a running in the foreground.
kill <job> Terminate a job.
showinacOon,+exec
Signals
16[UNIX]
ID Name Default Action Corresponding Event
2 SIGINT Terminate Interrupt (e.g., ctrl-c from keyboard)
9 SIGKILL Terminate Kill program (cannot override or ignore)
14 SIGALRM Terminate Timer signal
17 SIGCHLD Ignore Child stopped or terminated
20 SIGTSTP Stop until next SIGCONT
Stop signal from terminal (e.g. ctrl-z from keyboard)
Avirtualizedinterrupt.Allowapplica5onstobehavelikeopera5ngsystems.
youtube?
SendingaSignal
17
Kerneldeliversasignaltoades5na5onprocess
Foroneofthefollowingreasons:• Kerneldetectedasystemevent(e.g.div-by-zero(SIGFPE)ortermina5onofachild(SIGCHLD))• Aprocessinvokedthekillsystemcallreques5ngkerneltosendsignaltoanotherprocess
• debugging• suspension• resump5on• 5merexpira5on
ReceivingaSignal
18
Ades5na5onprocessreceivesasignalwhenitisforcedbythekerneltoreactinsomewaytothedeliveryofthesignal
Threepossiblewaystoreact:1.Ignorethesignal(donothing)2.Terminateprocess(+op5onalcoredump)3.Catchthesignalbyexecu5ngauser-levelfunc5oncalledsignalhandler• Likeahardwareexcep5onhandlerbeingcalledinresponsetoanasynchronousinterrupt
showhandler.c
SignalExample
19
void int_handler(int sig) { printf("Process %d received signal %d\n", getpid(), sig); exit(0); }
int main() { pid_t pid[N]; int i, child_status; signal(SIGINT, int_handler); for (i = 0; i < N; i++) // N forks if ((pid[i] = fork()) == 0) { while(1); //child infinite loop } for (i = 0; i < N; i++) { // parent continues executing printf("Killing proc. %d\n", pid[i]); kill(pid[i], SIGINT); } for (i = 0; i < N; i++) { pid_t wpid = wait(&child_status); if (WIFEXITED(child_status)) // parent checks for each child’s exit printf("Child %d terminated w exit status %d\n", wpid, WEXITSTATUS(child_status)); else printf("Child %d terminated abnormally\n", wpid); } exit(0); }
BlockedSignals
20
Aprocesscanblockthereceiptofcertainsignals•Blockedsignalscanbedelivered,butwillnotbereceivedun5lthesignalisunblocked
Kernelmaintainspendingandblockedbitvectorsinthecontextofeachprocess•blocked:representsthesetofblockedsignals
Canbesetandclearedbyusingthesigprocmaskfunc5on
ProcessGroupsEveryprocessbelongstoexactlyoneprocessgroupShell
Foreground job
Background job #1
Background job #2
Child Child
getpgrp()Returnprocessgroupofcurrentprocesssetpgid()Changeprocessgroupofaprocess/bin/kill–921SendSIGKILLtoprocess24818/bin/kill–9–20SendSIGKILLtoeveryprocessinprocessgroup20
pid=20pgid=20
pid=21pgid=20
pid=22pgid=20
Foregroundprocessgroup20
Backgroundprocessgroup32
Backgroundprocessgroup40
pid=32pgid=32
pid=40pgid=40
pid=10pgid=10
ImplemenOnga(really,reallysimple)Shell
22
void eval(char *cmdline) { char *argv[MAXARGS]; /* argv for execve() */ int bg; /* should the job run in bg or fg? */ pid_t pid; /* process id */
bg = parseline(cmdline, argv); if (!builtin_command(argv)) { if ((pid = Fork()) == 0) { /* child runs user job */ if (execve(argv[0], argv, environ) < 0) { printf("%s: Command not found.\n", argv[0]); exit(0); } }
if (!bg) { /* parent waits for fg job to terminate */ int status; if (waitpid(pid, &status, 0) < 0)
unix_error("waitfg: waitpid error"); } else /* otherwise, don’t wait for bg job */
printf("%d %s", pid, cmdline); } }
AndNow…Threads!
23
WhyThreads?
24
• Programstructure:expressinglogicallyconcurrenttasks• Responsiveness:shioingworktoruninthebackground• Performance:managingI/Odevices
DoesmulO-threadingonlymakesenseonmulOcore?
• Performance:exploi5ngmul5pleprocessors
Stack
Whathappenswhen…
25
Kernel
PCBs
0x00000000
0xFFFFFFFF
Apachewantstorunmul5pleconcurrentcomputa5ons?
Apache
Emacs
ApacheTwoheavyweightaddressspacesfortwoconcurrentcomputa5ons?
Whatisdis5nctabouttheseaddressspaces?
Heap
DataInsns
StackHeap
DataInsns
Stack
Idea!
26
Kernel
PCBs
0x00000000
0xFFFFFFFF
Apache
Emacs
Heap
DataInsns
Stack
Eliminateduplicateaddressspacesandplaceconcurrentcomputa5onsinthesameaddressspace.
Processvs.Thread
27
Process:•AddressSpace•SharedI/Oresources•OneormoreThreads:
Othertermsforthreads:LightweightProcess,ThreadofControl,Task
NotShared:• Registers,PC,SP• Stack
Shared:• Code• Data• Privileges
Stack 2
ThreadMemoryLayout
28
Data
Insns
Stack 1
Stack 3
PC
Thread1
Thread2PC
Thread3PC
SP
SP
SP
ProcessA
(HeapSharedbutsubdivided.)
SimpleThreadAPI
29
voidthread_create
(thread,func,arg)
Create a new thread, storing information about it in thread. Concurrently with the calling thread, thread executes the function func with the argument arg.
voidthread_yield
()
Calling thread voluntarily gives up processor to let other thread(s) run. Scheduler can resume running
the calling thread whenever it chooses to do so.
intthread_join(thread)
Wait for thread to finish if it has not already done so; then return the value passed to thread_exit by that
thread. Note that thread_join may be called only once for each thread.
voidthread_exit
(ret)
Finish the current thread. Store the value ret in the current thread’s data structure. If another thread is already waiting in a call to thread_join, resume it.
CodingExample
30
process_share.cthread_share.c
Threads
31
Lighter-weightthanprocesses• processisanabstractcomputer(CPU,mem,devices,…)• threadisanabstractcore
Threadsneedtobemutuallytrus5ng(Why?)
Idealforconcurrentprogramswherelotsofcodeanddataareshared• Servers,GUIcode,…
OpOon#1:KernelThreads
32
•Threadsshareasingleloca5oninmemory•SeparatePCBs(TCBs)foreachthread•PCBshave:• same:base&boundregistervalues• different:PC,SP,registers
Kernel
PCBs
0x00000000
0xFFFFFFFF
Apache
Emacs
Stack 2Heap
DataInsns
Stack 1
21
MulO-threadedkernelwiththreekernelthreadsandtwosingle-threadeduser-levelprocesses.
33
EachkernelthreadhasitsownTCBanditsownstack.Eachuserprocesshasastackatuser-levelforexecu5ngusercodeandakernelinterruptstackforexecu5nginterruptsandsystemcalls.
AmulO-threadedkernelwith3kernelthreadsand2user-levelprocesses,eachwith2threads.
34
Eachuser-levelthreadhasauser-levelstackandaninterruptstackinthekernelforexecu5nginterruptsandsystemcalls.
OpOon#2:UserThreads
35
•Buildamini-OSinuserspace• RealOSdoesn’tknowaboutmul5plethreads• SinglePCB
•Generallymoreefficientthankernelthreads(Why?)
•Butkernelthreadssimplifysystemcallhandlingandscheduling
(Why?) Mail
Kernel
PCBs
0x00000000
0xFFFFFFFF
Apache
Emacs
stack 2Heap
DataInsns
stack 1“os” stack
ImplemenOngUserThreads(4411-P1!)
36
Userprocesssupports:• ThreadControlBlock(TCB)table
• oneentryperthread• “contextswitch”opera5ons
• save/restorethreadstateinTCB• muchlikekernel-levelcontextswitches
• yield()opera5on:• threadreleasescore,allowsanotherthreadtouseit• Automa5cpre-emp5onnotalwayssupported
• Threadscheduler
UserThreads&SystemCalls
37
•Withuserthreads,aprocessmayhavemul5plesystemscallsoutstandingsimultaneously(oneperthread)•KernelPCBmustsupportthis(Why?)
CooperaOvevs.PreempOveMulOthreading
38
Coopera5ve:threadrunsun5lityieldscontroltoanotherthread—yieldisinthecodeitself+besercontrolofscheduling+simplerreasoningaboutsharedresources–starva5on(slowinterfaces)–mul5-core→reasoningnotsimpleraferall!
[Notcommonthesedays]
Mul5-threading==preemp5vemul5-threadingexceptin11-P1“Non-PrempOveMulOtasking”
MulOthreadingProgrammingModel
39
Rule#1:don’tpresumetoknowtheschedule
SharedResources→Synchroniza5onMasers!thread_share.crevisited(NextWeek!)