processes & threads - cornell university · processes & threads cs 4410, operang systems...
TRANSCRIPT
![Page 1: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/1.jpg)
Processes&Threads
CS4410,Opera5ngSystems
Fall2016CornellUniversity
RachitAgarwalAnneBracy
See:Ch3&4inOSPPtextbook
TheslidesaretheproductofmanyroundsofteachingCS4410byProfessorsSirer,Bracy,Agarwal,George,andVanRenesse.SomecontentfromMarkusPüschelatCMU.
![Page 2: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/2.jpg)
WhatisaProcess?
2
•Aninstanceofaprogram•Anabstrac5onofacomputer:
AddressSpace+Execu5onContext+Environment
AgoodabstracOon:•isportableandhidesimplementa5ondetails•hasanintui5veandeasy-to-useinterface•canbeinstan5atedmany5mes•isefficientandreasonablyeasytoimplement
![Page 3: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/3.jpg)
ProcessManagement
3
Canaprogram…• Createaninstanceofanotherprogram?• Waitforittocomplete?• Stoporresumeanotherrunningprogram?• Senditanasynchronousevent?
![Page 4: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/4.jpg)
Whoshouldbeallowedtostartaprocess?
4
Possibility#1:Onlythekernelmaystartaprocess
Possibility#2:User-levelprocessesmaystartprocesses
![Page 5: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/5.jpg)
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
![Page 6: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/6.jpg)
BeginningaProcessviaCreateProcess
6
Kernelhasto:•Create&ini5alizePCBinthekernel• Createandini5alizeanewaddressspace• Loadtheprogramintotheaddressspace• Copyargumentsintomemoryinaddressspace• Ini5alizehwcontexttostartexecu5onat“start”• Informschedulerthatnewprocessisreadytorun
[Windows]
![Page 7: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/7.jpg)
AbstractLifeofaProcess
7
New
Runnable Running
Zombie
Waiting
admiSed done
I/OOperaOonI/OCompleOon
dispatch
interrupt,descheduling
DetailsonThursday.
![Page 8: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/8.jpg)
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]
![Page 9: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/9.jpg)
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
![Page 10: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/10.jpg)
BeginningaProcessviaCreateProcessFork
10
Kernelhasto:•Create&ini5alizePCBinthekernel• Createandini5alizeanewaddressspace
• Loadtheprogramintotheaddressspace• Copyargumentsintomemoryinaddressspace• IniOalizetheaddressspacewithacopyoftheenOrecontentsoftheaddressspaceoftheparent
• Ini5alizehwcontexttostartexecu5onat“start”• InheritexecuOoncontextofparent(e.g.openfiles)
• Informschedulerthatnewprocessisreadytorun[UNIX]
![Page 11: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/11.jpg)
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?
![Page 12: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/12.jpg)
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.
![Page 13: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/13.jpg)
QuesOons
13
•CanUNIXfork()returnanerror?Why?
•CanUNIXexec()returnanerror?Why?
•CanUNIXwait()everreturnimmediately?Why?
![Page 14: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/14.jpg)
WhatisaShell?
14
Jobcontrolsystem• runsprogramsonbehalfoftheuser•allowsprogrammertocreate/managesetofprograms
• sh OriginalUnixshell(StephenBourne,AT&TBellLabs,1977)• csh BSDUnixCshell(tcsh:enhancedcsh
atCMUandelsewhere)• bash “Bourne-Again”Shell
Runsatuser-level.Whatsystemcallsdoesituse?
![Page 15: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/15.jpg)
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
![Page 16: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/16.jpg)
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?
![Page 17: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/17.jpg)
SendingaSignal
17
Kerneldeliversasignaltoades5na5onprocess
Foroneofthefollowingreasons:• Kerneldetectedasystemevent(e.g.div-by-zero(SIGFPE)ortermina5onofachild(SIGCHLD))• Aprocessinvokedthekillsystemcallreques5ngkerneltosendsignaltoanotherprocess
• debugging• suspension• resump5on• 5merexpira5on
![Page 18: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/18.jpg)
ReceivingaSignal
18
Ades5na5onprocessreceivesasignalwhenitisforcedbythekerneltoreactinsomewaytothedeliveryofthesignal
Threepossiblewaystoreact:1.Ignorethesignal(donothing)2.Terminateprocess(+op5onalcoredump)3.Catchthesignalbyexecu5ngauser-levelfunc5oncalledsignalhandler• Likeahardwareexcep5onhandlerbeingcalledinresponsetoanasynchronousinterrupt
showhandler.c
![Page 19: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/19.jpg)
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); }
![Page 20: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/20.jpg)
BlockedSignals
20
Aprocesscanblockthereceiptofcertainsignals•Blockedsignalscanbedelivered,butwillnotbereceivedun5lthesignalisunblocked
Kernelmaintainspendingandblockedbitvectorsinthecontextofeachprocess•blocked:representsthesetofblockedsignals
Canbesetandclearedbyusingthesigprocmaskfunc5on
![Page 21: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/21.jpg)
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
![Page 22: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/22.jpg)
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); } }
![Page 23: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/23.jpg)
AndNow…Threads!
23
![Page 24: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/24.jpg)
WhyThreads?
24
• Programstructure:expressinglogicallyconcurrenttasks• Responsiveness:shioingworktoruninthebackground• Performance:managingI/Odevices
DoesmulO-threadingonlymakesenseonmulOcore?
• Performance:exploi5ngmul5pleprocessors
![Page 25: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/25.jpg)
Stack
Whathappenswhen…
25
Kernel
PCBs
0x00000000
0xFFFFFFFF
Apachewantstorunmul5pleconcurrentcomputa5ons?
Apache
Emacs
ApacheTwoheavyweightaddressspacesfortwoconcurrentcomputa5ons?
Whatisdis5nctabouttheseaddressspaces?
Heap
DataInsns
StackHeap
DataInsns
![Page 26: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/26.jpg)
Stack
Idea!
26
Kernel
PCBs
0x00000000
0xFFFFFFFF
Apache
Emacs
Heap
DataInsns
Stack
Eliminateduplicateaddressspacesandplaceconcurrentcomputa5onsinthesameaddressspace.
![Page 27: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/27.jpg)
Processvs.Thread
27
Process:•AddressSpace•SharedI/Oresources•OneormoreThreads:
Othertermsforthreads:LightweightProcess,ThreadofControl,Task
NotShared:• Registers,PC,SP• Stack
Shared:• Code• Data• Privileges
![Page 28: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/28.jpg)
Stack 2
ThreadMemoryLayout
28
Data
Insns
Stack 1
Stack 3
PC
Thread1
Thread2PC
Thread3PC
SP
SP
SP
ProcessA
(HeapSharedbutsubdivided.)
![Page 29: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/29.jpg)
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.
![Page 30: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/30.jpg)
CodingExample
30
process_share.cthread_share.c
![Page 31: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/31.jpg)
Threads
31
Lighter-weightthanprocesses• processisanabstractcomputer(CPU,mem,devices,…)• threadisanabstractcore
Threadsneedtobemutuallytrus5ng(Why?)
Idealforconcurrentprogramswherelotsofcodeanddataareshared• Servers,GUIcode,…
![Page 32: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/32.jpg)
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
![Page 33: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/33.jpg)
MulO-threadedkernelwiththreekernelthreadsandtwosingle-threadeduser-levelprocesses.
33
EachkernelthreadhasitsownTCBanditsownstack.Eachuserprocesshasastackatuser-levelforexecu5ngusercodeandakernelinterruptstackforexecu5nginterruptsandsystemcalls.
![Page 34: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/34.jpg)
AmulO-threadedkernelwith3kernelthreadsand2user-levelprocesses,eachwith2threads.
34
Eachuser-levelthreadhasauser-levelstackandaninterruptstackinthekernelforexecu5nginterruptsandsystemcalls.
![Page 35: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/35.jpg)
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
![Page 36: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/36.jpg)
ImplemenOngUserThreads(4411-P1!)
36
Userprocesssupports:• ThreadControlBlock(TCB)table
• oneentryperthread• “contextswitch”opera5ons
• save/restorethreadstateinTCB• muchlikekernel-levelcontextswitches
• yield()opera5on:• threadreleasescore,allowsanotherthreadtouseit• Automa5cpre-emp5onnotalwayssupported
• Threadscheduler
![Page 37: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/37.jpg)
UserThreads&SystemCalls
37
•Withuserthreads,aprocessmayhavemul5plesystemscallsoutstandingsimultaneously(oneperthread)•KernelPCBmustsupportthis(Why?)
![Page 38: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/38.jpg)
CooperaOvevs.PreempOveMulOthreading
38
Coopera5ve:threadrunsun5lityieldscontroltoanotherthread—yieldisinthecodeitself+besercontrolofscheduling+simplerreasoningaboutsharedresources–starva5on(slowinterfaces)–mul5-core→reasoningnotsimpleraferall!
[Notcommonthesedays]
Mul5-threading==preemp5vemul5-threadingexceptin11-P1“Non-PrempOveMulOtasking”
![Page 39: Processes & Threads - Cornell University · Processes & Threads CS 4410, Operang Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides](https://reader034.vdocument.in/reader034/viewer/2022052310/5f0579967e708231d4132528/html5/thumbnails/39.jpg)
MulOthreadingProgrammingModel
39
Rule#1:don’tpresumetoknowtheschedule
SharedResources→Synchroniza5onMasers!thread_share.crevisited(NextWeek!)