processes & threads - cornell university · processes & threads cs 4410, operang systems...

39
Processes & Threads CS 4410, Opera5ng Systems Fall 2016 Cornell University Rachit Agarwal Anne Bracy See: Ch 3&4 in OSPP textbook The slides are the product of many rounds of teaching CS 4410 by Professors Sirer, Bracy, Agarwal, George, and Van Renesse. Some content from Markus Püschel at CMU.

Upload: others

Post on 11-Jun-2020

41 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Stack

Whathappenswhen…

25

Mail

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

Stack

Idea!

26

Mail

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

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

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

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

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

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

OpOon#1:KernelThreads

32

•Threadsshareasingleloca5oninmemory•SeparatePCBs(TCBs)foreachthread•PCBshave:• same:base&boundregistervalues• different:PC,SP,registers

Mail

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

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

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

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

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

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

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

MulOthreadingProgrammingModel

39

Rule#1:don’tpresumetoknowtheschedule

SharedResources→Synchroniza5onMasers!thread_share.crevisited(NextWeek!)