processes - computer scienceporter/courses/comp530/f18/slides/processes.pdf · • a process is the...

37
COMP 530: Operating Systems Processes Don Porter Portions courtesy Emmett Witchel 1

Upload: others

Post on 25-Aug-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Processes

DonPorter

PortionscourtesyEmmettWitchel

1

Page 2: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

App

Whatisaprocess?

2-2

Hardware

Libraries

Kernel

User

Super-visor

App

Libraries

Daemon

Libraries

SystemCallTable(350—1200)

Intuitively,oneofthese

Page 3: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Whatisaprocess?• Aprocessisaprogramduringexecution.

– Program=staticfile(image)– Process=executingprogram=program+executionstate.

• Aprocessisthebasicunitofexecutioninanoperatingsystem– Eachprocesshasanumber,itsprocessidentifier(pid).

• Differentprocessesmayrundifferentinstancesofthesameprogram– E.g.,myjavac andyourjavac processboth runtheJavacompiler

• Ataminimum,processexecutionrequiresfollowingresources:– Memory tocontaintheprogramcodeanddata– AsetofCPUregisterstosupportexecution

3

Page 4: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

• Wewriteaprogramine.g.,Java.• Acompilerturnsthatprogramintoaninstructionlist.• TheCPUinterpretstheinstructionlist(whichismoreagraphofbasic

blocks).

void X (int b) {

if(b == 1) {

int main() {

int a = 2;X(a);

}

Programtoprocess

Page 5: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

void X (int b) {

if(b == 1) {

int main() {

int a = 2;X(a);

}

Whatyouwrote:

void X (int b) {

if(b == 1) {

int main() {

int a = 2;X(a);

} Code

main; a = 2X; b = 2

Heap

Stack

ProcessinmemoryWhatisinmemory:

Data

Page 6: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Wheretoprocessescomefrom?• WhenItype‘./a.out’,thebinaryruns,right?

– Reallyonlytrueforstaticbinaries(moreonthislater)

• Inrealityaloader setsuptheprogram– Usuallyauser-levelprogram– Canalsobein-kernel,orsplitbetweenboth

6

Page 7: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Wheretoprocessescomefrom?• Inordertorunaprogram,theloader:

– readsandinterpretstheexecutablefile– setsuptheprocess’smemorytocontainthecode&datafrom

executable– pushes“argc”,“argv” onthestack– setstheCPUregistersproperly&calls“_start()”

• Programstartsrunningat_start()_start(args) {

initialize_java();ret = main(args);exit(ret)

}

“process” isnowrunning;nolongerthinkof“program”

• Whenmain()returns,OScalls“exit()”whichdestroystheprocessandreturnsallresources

7WhatbookkeepingdoestheOSneedforprocesses?

Page 8: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

• A process has code.– OS must track program counter (code location).

• A process has a stack.– OS must track stack pointer.

• OS stores state of processes’ computation in a process control block (PCB).– E.g., each process has an identifier (process identifier,

or PID)• Data (program instructions, stack & heap) resides

in memory, metadata is in PCB (which is a kernel data structure in memory)

Keepingtrackofaprocess

Page 9: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

ContextSwitching• TheOSperiodicallyswitchesexecutionfromoneprocesstoanother

• Calleda contextswitch,becausetheOSsavesoneexecutioncontextandloadsanother

Page 10: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Whatcausescontextswitches?• WaitingforI/O(disk,network,etc.)

– MightaswellusetheCPUforsomethinguseful– Calledablockedstate

• Timerinterrupt(preemptivemultitasking)– Evenifaprocessisbusy,weneedtobefairtootherprograms

• Voluntaryyielding(cooperativemultitasking)• Afewothers

– Synchronization,IPC,etc.

Page 11: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Processlifecycle• Processesarealwayseither:

– Executing– Waitingtoexecute, or– Blockedwaitingforanevent tooccur

11

RunningReady

Blocked

Start Done

Page 12: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Operating System

“SystemSoftware”

User Process 1

User Program 2User Process 2

User Process n

...Process 1 Process 2OS I/O

Device

k: read()

k+1:

startIO()

endio{ interrupt

main{

main{

}

read{

}

}

schedule()

Memory

savestate schedule()

restorestate

savestate

Processcontexts

Page 13: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

1. Ready2. Running3. Blocked4. Zombie5. Exited

WhenaprocessiswaitingforI/O,whatisitsstate?

Page 14: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

CPUScheduling• Problemofchoosingwhichprocesstorunnext

– Andforhowlonguntilthenextprocessruns

• Whybother?– Improveperformance:amortizecontextswitchingcosts– Improveuserexperience:e.g.,lowlatencykeystrokes– Priorities:favor“important”workoverbackgroundwork– Fairness

14Wewillcovertechniqueslater

Page 15: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Whendoesschedulinghappen?• Whenaprocessblocks• WhenadeviceinterruptstheCPUtoindicateaneventoccurred(possiblyun-blockingaprocess)

• WhenaprocessyieldstheCPU

• Preemptivescheduling:SettingatimertointerrupttheCPUaftersometime– PlacesanupperboundonhowlongaCPU-boundprocesscanrunwithoutgivinganotherprocessaturn

• Non-preemptivescheduling:ProcessesmustexplicitlyyieldtheCPU

15

Page 16: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

• OS uses PCBs to represent a process• Every resource is represented with a queue• OS puts PCB on an appropriate queue.

– Ready to run queue.– Blocked for IO queue (Queue per device).– Zombie queue.

• When CPU becomes available, choose from ready to run queue

• When an event occurs, remove waiting process from blocked queue, move to ready queue.

Schedulingprocesses

Page 17: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Consider a Web server:get network message (URL) from clientfetch URL data from diskcompose responsesend response

How well does this web server perform?With many incoming requests?

That access data all over the disk?

Whyusemultipleprocessesinoneapp?

AsingleprocesscannotoverlapCPUandI/O

Page 18: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Consider a Web serverget network message (URL) from clientcreate child process, send it URL

Childfetch URL data from diskcompose responsesend response

NowthechildcanblockonI/O,parentkeepsworkingDifferentchildrencanblockonreadingdifferentfiles

Howdoesserverknowifchildsucceededorfailed?

Whyusemultipleprocessesinoneapp?

Page 19: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

• Aftertheprogramfinishesexecution,itcallsexit()• Thissystemcall:

– takesthe“result” of theprogramasanargument– closesallopen files,connections, etc.– deallocates memory– deallocates mostoftheOSstructuressupporting theprocess– checksifparentisalive:

v Ifso,itholdstheresultvalueuntilparentrequestsit;inthiscase,processdoesnotreallydie,but itentersthezombie/defunct state

v Ifnot,itdeallocates alldatastructures,theprocessisdead

• Processterminationistheultimategarbagecollection

Orderlytermination:exit()

Webserverex:Childusesexitcodeforsuccess/failure

Page 20: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

• Childreturnsavaluetoparentviaexit()• Theparentreceivesthisvaluewithwait()

• Specifically,wait():– Blockstheparentuntil childfinishes(needawaitqueue)– Whenachildcallsexit(), theOSunblocks theparentandreturns thevalue

passedbyexit()asaresultof thewait()call(alongwiththepid ofthechild)– Iftherearenochildrenalive,wait()returnsimmediately

Thewait()systemcall

Page 21: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Zombies!!!

21

• Aparentcanwaitindefinitelytocallwait()• TheOStostoretheexitcodeforafinishedchilduntiltheparentcallswait()

• Hack:KeepPCBfordeadprocessesarounduntil:– Parentcallswait(),or– Parentexit()s(don’tneedtowait()ongrandkids)

• Andthatisazombie(donestate)– Willnotbescheduledagain

Page 22: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Wheredoprocessescomefrom?(redux)• Parent/childmodel• Anexistingprogramhastospawnanewone

– MostOSes haveaspecial‘init’programthatlaunchessystemservices,logondaemons,etc.

– Whenyoulogin(viaaterminalorssh),theloginprogramspawnsyourshell

Page 23: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Approach1:WindowsCreateProcess• InWindows,whenyoucreateanewprocess,youspecifytheprogram– Andcanoptionallyallowthechildtoinheritsomeresources(e.g.,anopenfilehandle)

Page 24: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Approach2:Unixfork/exec()• InUnix,aparentmakesacopy ofitselfusingfork()

– Childinheritseverything,runssameprogram– Onlydifferenceisthereturnvaluefromfork()

• Childgets0;parentgetschildpid

• Aseparateexec()systemcallloadsanewprogram– Likegettingabraintransplant

• Someprograms,likeourwebserverexample,fork()clones(withoutcallingexec()).– Commoncaseisprobablyfork+exec

Page 25: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

• The exec() call allows a process to “load” a different program and start execution at main (actually _start).

• It allows a process to specify the number of arguments (argc) and the string argument array (argv).

• If the call is successful– it is the same process …– but it runs a different program !!

• Code, stack & heap is overwritten– Sometimes memory mapped files are preserved.

• Exec does not return!

Programloading:exec()

Page 26: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

In the parent process:main()…int rv =fork(); // create a childif(0 == rv) { // child continues here

exec_status = exec(“calc”, argc, argv0, argv1, …);printf(“Something is horribly wrong\n”); exit(exec_status);

} else { // parent continues hereprintf(“Shall I be mother?”);…child_status = wait(rv);

}

Exec should not return

fork()+exec()example

Page 27: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

pid = 127open files = “.history”last_cpu = 0

pid = 128open files = “.history”last_cpu = 0

int rv = fork();if(rv == 0) {close(“.history”);exec(“/bin/calc”);} else {wait(rv);

int rv = fork();if(rv == 0) {close(“.history”);exec(“/bin/calc”);} else {wait(rv);

Process Control Blocks (PCBs)

OSUSER

int rv = fork();if(rv == 0) {close(“.history”);exec(“/bin/calc”);} else {wait(rv);

int rvc_main(){irvq = 7;do_init();ln = get_input();exec_in(ln);

pid = 128open files = last_cpu = 0

int rv = fork();if(rv == 0) {close(“.history”);exec(“/bin/calc”);} else {wait(rv);

Ashellforksandexecsacalculator

Page 28: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

pid = 127open files = “.history”last_cpu = 0

pid = 128open files = “.history”last_cpu = 0

int shell_main() {int a = 2;… Code

main; a = 2

Heap

Stack

0xFC0933CA

int shell_main() {int a = 2;… Code

main; a = 2

Heap

Stack

0xFC0933CA

int calc_main() {int q = 7;… Code

Heap

Stack

0x43178050

pid = 128open files =last_cpu = 0

OSUSER

Process Control Blocks (PCBs)

Ashellforksandthenexecsacalculator

Page 29: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Whyseparatefork&exec?• Keyissue:Inheritance offiledescriptors,environment,etc.– Or,makingtheshellwork

• Rememberhowtheshellcandoredirection?– ./warmup <testinput.txt– Filehandle0(stdin)isopenedtoreadtestinput.txt

• Theparent(shell)openstestinput.txt beforefork()– Thechild(warmup)inheritsthisopenfilehandle

• Evenafterexec()

29

Page 30: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

• Decoupling fork and exec lets you do anything to the child’s process environment without adding it to the CreateProcess API.

int rv = fork(); // create a childIf(0 == rv) { // child continues here

// Do anything (unmap memory, close net connections…)exec(“program”, argc, argv0, argv1, …);

}fork() creates a child process that inherits:Ø identical copy of all parent’s variables & memoryØ identical copy of all parent’s CPU registers (except one)

Parent and child execute at the same point after fork() returns:Ø by convention, for the child, fork() returns 0Ø by convention, for the parent, fork() returns the pid of the child

Theconvenienceofseparatefork/exec

Page 31: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

TheCreateProcess alternative• Windowsdoesallowyoutocreateaprocessthatisinitiallysuspended– Youcanalsochangememoryandhandlesofanotherprocess

– Andthenunblockit

• Somewhatisomorphic– Butabitcumbersome– Andpronetosecurityissues(loadingthreadsandlibrariesinanotherapp!)

31

Page 32: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

• Simple implementation of fork():– allocate memory for the child process– copy parent’s memory and CPU registers to child’s– Expensive !!

• In 99% of the time, we call exec() after calling fork()– the memory copying during fork() operation is useless– the child process will likely close the open files & connections– overhead is therefore high

Atwhatcost,fork()?

Anyideastoimprovethis?

Page 33: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Protool:vfork• Ifyouknowyouaregoingtocallexec()almostimmediately:

– CreateanewPCB,stack,registerstate– Butnotanewcopyofthefullmemory

• YoucanchangeOSstateandcallexecsafely• Youcannot:

– Returnfromthefunctionthatcalledfork()– Touchtheheap– Probablyotherstuff

• Whydoesitimproveperformance?Avoidscopies

• Unfortunateexampleofimplementationinfluenceoninterface– CurrentLinux&BSD4.4haveitforbackwardscompatibility

33

Page 34: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Copy-on-writefork(preview)• Idea:writeprotecteverythinginmemoryafterafork()– Detectandcopyonlywhatyoutouch,untiltheexec()– Afterexec(),removewriteprotectionfromchildmemory

• Commoncase:execquickly– Someoverheadtosettingcopy-on-write,butcheaperthancopyingeverything

• Uncommoncase:forkneverexecs– Eventuallycopyeverything

• Wewillseemoreaboutthislater…

34

Page 35: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

OSmustincludecallstoenablespecialcontrolofaprocess:

• Prioritymanipulation:– nice(),whichspecifiesbaseprocesspriority (initialpriority)– InUNIX,processprioritydecaysastheprocessconsumesCPU

• Debuggingsupport:– ptrace(),allowsaprocesstobeputundercontrolofanotherprocess– Theotherprocesscansetbreakpoints, examineregisters,etc.

• Alarmsandtime:– Sleepputsaprocessonatimerqueuewaitingforsomenumberofseconds,

supporting analarmfunctionality

Processcontrol

Page 36: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

while(! EOF) {read inputhandle regular expressionsint rv = fork(); // create a childif(rv == 0) { // child continues here

exec(“program”, argc, argv0, argv1, …);}else { // parent continues here…}

Translates<CTRL-C>tothekill()systemcallwithSIGKILL

Translates<CTRL-Z>tothekill()systemcallwithSIGSTOP

Allowsinput-output redirections,pipes,andalotofotherstuffthatwewillseelater

Tyingitalltogether:TheUnixshell

Page 37: processes - Computer Scienceporter/courses/comp530/f18/slides/processes.pdf · • A process is the basic unit of execution in an operating system – Each process has a number, its

COMP530:OperatingSystems

Summary• Understandwhataprocessis• Thehigh-levelideaofcontextswitchingandprocessstates

• Howaprocessiscreated• ProsandconsofdifferentcreationAPIs

– Intuitionofcopy-on-writeforkandvfork