what is the difference btw a process keeping track of ... layout of a process data/bss code/text...

9
1/26/18 1 1 CS 355 Operating Systems Processes, Unix Processes and System Calls 2 Process User types command like run fooat keyboard I/O device driver for keyboard and screen Command is parsed by command shell Executable program file foois located on disk file system, I/O device driver for disk Contents are loaded into memory and control transferred ==> process comes alive! disk device driver, relocating loader, memory management During execution, process may call OS to perform I/O: console, disk, printer, etc. system call interface, I/O device drivers When process terminates, memory is reclaimed memory management 3 What is the Difference btw a Process and a Program? A process is a program in execution with associated data and execution context Each running instance of a program is a separate process 4 Keeping Track of Processes For each process, OS maintains a data structure, called the process control block (PCB). The PCB provides a way of accessing all information relevant to a process: This data is either contained directly in the PCB, or else the PCB contains pointers to other system tables. All current processes (PCBs) are stored in a system table called the process table. Either a linked list or an array, usually a linked list. 5 Process States Possible process states Running: executing Blocked: waiting for I/O Ready: waiting to be scheduled 6 When are processes created? System initialization (many daemons for processing email, printing, web pages etc.) Execution of a process creation system call by a running process (fork or CreateProcess) User request to create a new process (executing new applications) List of all active processes: ps (Unix), Ctl-Alt-Del (Windows)

Upload: vanngoc

Post on 30-Mar-2018

219 views

Category:

Documents


2 download

TRANSCRIPT

1/26/18

1

1

CS355OperatingSystems

Processes,UnixProcessesandSystemCalls

2

Process• Usertypescommandlike“runfoo” atkeyboard

– I/Odevicedriverforkeyboardandscreen• Commandisparsedbycommandshell• Executableprogramfile“foo” islocatedondisk

– filesystem,I/Odevicedriverfordisk• Contentsareloadedintomemoryandcontroltransferred==>processcomesalive!– diskdevicedriver,relocatingloader,memorymanagement

• Duringexecution,processmaycallOStoperformI/O:console,disk,printer,etc.– systemcallinterface,I/Odevicedrivers

• Whenprocessterminates,memoryisreclaimed– memorymanagement

3

WhatistheDifferencebtwaProcessandaProgram?

• Aprocessisaprograminexecutionwithassociateddataandexecutioncontext

• Eachrunninginstanceofaprogramisaseparateprocess

4

KeepingTrackofProcesses

• Foreachprocess,OSmaintainsadatastructure,calledtheprocesscontrolblock(PCB).ThePCBprovidesawayofaccessingallinformationrelevanttoaprocess:– ThisdataiseithercontaineddirectlyinthePCB,orelsethePCBcontainspointerstoothersystemtables.

• Allcurrentprocesses(PCBs)arestoredinasystemtablecalledtheprocesstable.– Eitheralinkedlistoranarray,usuallyalinkedlist.

5

ProcessStates

• Possibleprocessstates– Running:executing– Blocked:waitingforI/O– Ready:waitingtobescheduled

6

Whenareprocessescreated?

• Systeminitialization(manydaemons forprocessingemail,printing,webpagesetc.)

• Executionofaprocesscreationsystemcallbyarunningprocess(fork orCreateProcess)

• Userrequesttocreateanewprocess(executingnewapplications)

Listofallactiveprocesses:ps (Unix),Ctl-Alt-Del (Windows)

1/26/18

2

7

Whenareprocessesterminated?

• Normalexit(voluntary)

• Errorexit(voluntary)

• Fatalerror(involuntary),duetobugs

• Killedbyanotherprocess(involuntary)

8

ProcessContext/Image• Mainmemory– logicallydistinctregionsofmemory:

– textsegment:containsexecutablecode(typicallyread-only)

– data/bss segment:initializedanduninitializedglobals,staticsandconstants

– heap:storageareafordynamicallyallocateddatastructure,e.g.,lists,trees

– stack:run-timestackofactivationrecords• Registers:generalregisters,PC,SP,PSW,

segmentationregisters• Otherinformation:

– openfilestable,statusofongoingI/O– processstatus(running,ready,blocked),userid,...

9

MemoryLayoutofaProcess

data/bss

Code/Text

Heap

Freespace

Stack

CPU

(Virtual)Memory

PSW

ProgramCounter

StackPointer

Recall:Wheredolocalvariablesgo?Wheredoglobalvariablesgo?Howaboutcommandlineargumentsandenv vars?Howisinformationexchangedbetweenaprogramanditssubroutines?

10

PrograminMemory

• Whatisstoredinmemory?– Code– Constants– Globalandstaticvariables– Localvariables– Dynamicmemory(malloc)

int SIZE;

char* f(void) {char *c;

SIZE = 10;c = malloc(SIZE);return c;

}

global

local

constdynamic

0

0xffffffff

virtual address space

11 Lec11

MemoryLayout

• Howismemoryorganized?– Code– Text– Constants– Data– Globalandstaticvariables– BSS– Localvariables– Stack– Dynamicmemory(malloc)– Heap

int SIZE;

char* f(void) {char *c;

SIZE = 10;c = malloc(SIZE);return c;

}

global

local

constdynamic

0

0xffffffff

TextDataBSS

Heap

Stack

12

ATypicalStackFrame

• int foo(int arg1, int arg2);

• TwolocalvarsCallee savedregisters(asneeded)

temporarystorage

Caller'sEBP

Stack

localvar1localvar2

Returnaddressarg1arg2

EBP-8EBP-4

EBP+8EBP+12

Callersavedregisters(asneeded)

ESP-->

1/26/18

3

13

WhendoesOSgetinvoked?

• Operatingsystemgetscontrolintwoways– AuserprocesscallsOSviaasystemcall– Aninterruptabortsthecurrentuserprocess

• SystemstackcanbeusedtoexchangeinformationbetweenOSanduserprocesses

• Recall:ModebitinPSWissetonlywhenOSisrunning

14

Interrupts• Aninterruptionofthenormalprocessingofprocessor.

• InterruptscausetheCPUtosuspenditscurrentcomputationandtakeupsomenewtask.

• Reasonsforinterruptsand/ortraps:– controlofasynchronousI/Odevices– CPUscheduling– exceptionalconditions(e.g.,div.byzero,pagefault,illegalinstruction)arisingduringexecution

• InterruptsareessentiallywhatdrivesanOS.WecanviewtheOSasanevent-drivensystem,whereaninterruptisanevent.

• Boundinginterrupthandlinglatencyisimportantforreal-timesystems.

15

InterruptHandling• Theservicingofaninterruptisknownasinterrupthandling.

• Anintegerisassociatedwitheachtypeofinterrupt.Whenaninterruptoccurs,thecorrespondingintegerissuppliedtotheOSusuallybythehardware(inaregister).

• TheOSmaintainsatable,knownastheinterruptvector,thatassociateseachinterrupt'sidwiththestartingaddressofitsserviceroutine.

• InterruptNo. InterruptHandler• 0 clock• 1 disk• 2 tty• 3 dev• 4 soft• 5 other

16

Interruptinterrupts!• OnIntelPentium,hardwareinterruptsarenumbered0to255– 0:divideerror 1:debugexception– 2:nullinterrupt 6:invalidopcode– 12:stackfault 14:pagefault– 16:floating-pointerror 19-31:Intelreserved(notavailable?)– 32-255:maskable interrupts(devicecontrollers,softwaretraps)

• Issue:canCPUbeinterruptedwhileanOSinterrupthandlerisexecuting?

• Maskable interruptscanbedisabled,plusthereisapriorityamonginterrupts

• Ifinterrupted-enabledflagisset,raceconditionmayoccurwhenahigherpriorityinterruptinterruptsaloweroneinthewrongtime

17

Typicalinterrupthandlingsequence• InterruptinitiatedbyI/OdevicesignalingCPU,byexceptionalconditionarising,throughexecutionofspecialinstruction,etc.

• CPUsuspendsexecutionofcurrentinstructionstreamandsavesthestateoftheinterruptedprocess(onsystemstack).

• Thecauseoftheinterruptisdeterminedandtheinterruptvectorisconsultedinordertotransfercontroltotheappropriateinterrupthandler.

• Interrupthandlerperformswhateverprocessingisnecessarytodealwiththeinterrupt.

• PreviousCPUstateisrestored(popped)fromsystemstack,andCPUreturnscontroltointerruptedtask.

18

Example:ServicingaTimerInterrupt• TimerdeviceisusedinCPUschedulingtomakesurecontrolisreturnedtosystemeverysooften(e.g.,1/60sec.)

• Typically,timerhasasingleregisterthatcanbeloadedwithanintegerindicatingaparticulartimedelay(#ofticks).

• Onceloaded,timercountsdownandwhen0isreached,aninterruptisgenerated.

• Interrupthandlermightdothefollowing:– updatetime-of-dayinformation– signalanyprocessesthatare"asleep"andawaitingthisalarm– calltheCPUscheduler

• Controlreturnstousermode,possiblytoadifferentprocessthantheoneexecutingwhentheinterruptoccurred.

1/26/18

4

19

Example:ServicingaDiskInterrupt

• Whendiskcontrollercompletesprevioustransfer,itgeneratesaninterrupt.

• Interrupthandlerchangesthestateofaprocessthatwaswaitingforjust-completedtransferfromwait-statetoready-state.

• ItalsoexaminesqueueofI/Orequeststoobtainnextrequest.

• I/Oisinitiatedonnextrequest.• CPUschedulercalled.• Controlreturnedtousermode.

20

SystemCalls• Toobtain"directaccess"tooperatingsystemservices,user

programsmustmakesystemcalls– filesystem– I/Oroutines– memoryallocate&freeroutines

• Systemcallsarespecial,andinfact,aretreatedlikeinterrupts.• Programsthatmakesystemcallsaretraditionallycalled"system

programs"andweretraditionallyimplementedinassemblylanguage– Win32APIinWindows

• Eachinstructionsethasaspecialinstructionformakingsystemcalls:

SVC (IBM360/370)trap (PDP11)tw (PowerPC)- trapwordtcc (Sparc)break (MIPS)

21

Timeout

• Interrupts,traps,systemcalls.What’sthedifference?

22

SystemCalls

ApplicationProgram

LibraryRoutine

OSRoutine

DeviceController

Software Hardware

OSModebit=1

UserModebit=0

SystemAPI

SpecialInstructions

TRAP

23

Example• count = read(fd, buffer, nbytes);

Returnsthenumberofbytesreadfromtheopenfiledescriptorfd intothebufferbuffer.

24

Stepsinread() SystemCall• Userprogrampushesparametersontostack1-3• UserprogramexecutesCALLinstructiontoinvoke

libraryroutinereadinassemblylanguage4• Readroutinesetsuptheregisterforsystemcall

number5• ReadroutineexecutesTRAPinstructiontoinvoke

OS6• Hardwaresetsthemode-bitto1,savesthestate

oftheexecutingreadroutine,andtransferscontroltoafixedlocationinkernel6

• Kernelcode,usingatablelook-upbasedonsystemcallnumber7,transferscontrol(dispatches)tocorrectsystemcallhandler8

1/26/18

5

25

Stepsinread()SystemCall(cont)• OSroutinecopiesparametersfromuserstack,setsupdevicedriverregisters,andexecutesthesystemcallusingprivilegedinstructions9

• OSroutinecanfinishthejobandreturn,ordecidetosuspendthecurrentuserprocesstoavoidwaiting9

• UponreturnfromOS,hardwareresetsthemode-bit9

• Controltransferstothereadlibraryroutineandallregistersarerestored9

• Libraryroutineterminates,transferringcontrolbacktooriginaluserprogram10

• Userprogramincrementsstackpointertocleartheparameters11

26

Issues

• Canasystemcallinterruptanothersystemcall?

• Canasystemcallinterruptaninterrupt?• Cananinterruptinterruptasystemcall?

27

CreatingprocessesinUNIX• ImportantsourceofinformationonUNIXisman

• AUnixprocessiscreatedbyanother.– anewlycreatedprocessisthe“child” ofthe“parent”processthatcreatedit

– everyprocesshasexactlyoneparent– aprocessmaycreateanynumberofchildprocesses

• processeshaveauniquePID(processID)– indextothePCBintheprocesstable

• ProcessesarecreatedinUNIXwiththefork()systemcall.

28

ProcessHierarchies• Processesformahierarchy

– UNIXcallsthisaprocessgroup

• Signalscanbesenttoallprocessesofagroup

• Windowshasnoconceptofprocesshierarchy– allprocessesarecreatedequal

29

Initialization

• Attherootofthefamilytreeofprocessesisthespecialprocessinit:– createdaspartofthebootstrappingprocedure– pid =1– amongotherthings,init spawnsachildtolistentoeachterminal,sothatausermaylogon.

– do"man init” tolearnmoreaboutit– allprocessesarechildrenofinit

30

UNIXProcessControlUNIXprovidesanumberofsystemcallsforprocesscontrolincluding:

– fork - usedtocreateanewprocess– exec - tochangetheprogramaprocessisexecuting– exit - usedbyaprocesstoterminateitselfnormally– abort - usedbyaprocesstoterminateitselfabnormally– kill - usedbyoneprocesstokillorsignalanother– wait - towaitforterminationofachildprocess– sleep - suspendexecutionforaspecifiedtimeinterval– getpid - getprocessid– getppid - getparentprocessid

1/26/18

6

31

Thefork SystemCall

• Thefork() systemcallcreatesa"clone"ofthecallingprocess.

• Identicalineveryrespectexcept– theparentprocessisreturnedanon-zerovalue(namely,thepid ofthechild)

– thechildprocessisreturnedzero.– Thepid returnedtotheparentcanbeusedbyparentinawait orkill systemcall.

• Whatgoodisthis?– writecodetobehavedifferentlyifyouarechild

32

Example1. #include <unistd.h>2. main(){3. pid_t pid;4. printf(“Just one process so far\n”);5. pid = fork();6. if (pid == 0) /* code for child */7. printf(“I’m the child\n”);8. else if (pid > 0) /* code for parent */9. printf(“The parent, child pid =%d\n”,10. pid);11. else /* error handling */12. printf(“Fork returned error code\n”);13. }

33

Exampleint main(){

int x=0;fork();x++;printf(“The value of x is %d\n”, x);

}

Whatistheoutput?

34

ReplacingaProcess:exec

• Theexec systemcallreplacesaprocesswithanewprogram– itdoesnotcreateanynewprocess– thenewprogramisspecifiedbythenameofthefilecontainingtheexecutableandarguments

• Thecallingprocessstopsrunningassoonasitcallsexec iftheexecutablecanberun

• Usuallyrunsafterfork()

35

SpawningApplicationspid_t pid;if ( ( pid = fork() ) == 0 ) {/*child code: replace executable image*/execv("/usr/games/tetris", "-easy”);

} else if (pid > 0) {/*parent code: wait for child to terminate*/wait( &status );

}

36

wait()

• Oftenaprocessneedstowaitforachildtofinishfirst– whattheshellusuallydoeswhenyoutypesomething

• wait() blocksuntiloneofthecaller’schildrenterminatesthenreturnswithstatusorerror

• Ifstatusisavailableformorethanonechild,theorderinwhichitisreportedisunspecified

• pid_t waitpid(pid_t pid, int*stat_loc, int options);

1/26/18

7

37

Example#include <sys/types.h>#include <sys/wait.h>#include <stdlib.h>int main() {int status;if (fork() == 0) exit(EXIT_SUCCESS);wait (&status); pexit(status); if (fork() == 0) abort(); /* SIGABRT */wait (&status); pexit(status); if (fork() == 0) status /= 0; /* SIGFPE */wait (&status); pexit(status);

}

38

Examplevoid pexit(int stat) {if (WIFEXITED(status))printf(“Normal termination, exit status = %d\n”, WEXITSTATUS(status));

if (WIFSIGNALED(status))printf(“Abormal termination, signal number = %d\n”, WTERMSIG(status));

if (WIFSTOPPED(status))printf(“Stopped, signal number = %d\n”,WSTOPSIG(status));

}

39

waitpid()

• pid_t waitpid(pid_t pid, int*stat_loc, int options);

• waitpid() willwaituntilaspecified(bypid)childterminates

• waitpid() alsohastheoptiontonotblock– pid == -1 waitsforanychild– option == NOHANG non-blocking– option == 0 blocking– waitpid(-1,&status,0) == wait(&staus)

40

Howshellexecutesacommand

q whenyoutypeacommand,theshellforks acloneofitselfq thechildprocessmakesanexec call,whichcausesittostop

executingtheshellandstartexecutingyourcommandq theparentprocess,stillrunningtheshell,waitsforthechildto

terminate

fork wait

exitexec Requiredjob

Parentshell

Child

41

exec andFriends• exec doesnotreturn,i.e.theprogramcallingexec isgone

forever!• exec isreallyafamilyofsystemcalls,eachdifferslightlyin

thewaytheprocessargumentsaregiven– execl, execlp, execle, execv, execcvp, execve

• Thel’sexpectalistofpointerstostringsasarguments– const char *exename, const char *arg0, constchar *arg1, …, const char *argn

• Thev’sexpectanarrayofpointerstostringsasarguments• Thep’swillduplicatetheshell’spathsearchingeffort• Thee’sallowanadditionalparameterspecifyingthe

environmentvariables• Alleventuallymakeacalltoexecve (whichisthereal

systemcall),theothersareClibfrontends42

ProcessTermination• Aself-terminatingprocesssendsitsparentaSIGCHLDsignalandwaitsforterminationstatuscodetobeaccepted

• Aprocesscanbekilledbyanotherprocessusingkill:– kill(pid, sig) - sendssignalsig toprocesswithprocess-idpid.OnesignalisSIGKILL(terminatethetargetprocessimmediately).

• Whenaprocessterminates,alltheresourcesitownsarereclaimedbythesystem:– PCBreclaimed– itsmemoryisdeallocated– allopenfilesclosedandOpenFileTablereclaimed.

1/26/18

8

43

OrphansandZombies

• Orphanprocess– aprocesswhoseparentdiedbeforeitdid– getsadoptedbyinit– killinginit isequivalenttokillingtheentireOS

• Zombieprocess– aprocessthatiswaitingforitsparenttoacceptisstatuscode– showsupwith‘Z’ inps

44

SystemCallsandErrorHandling• Allsystemcallsreturn–1iffailed• errno – theglobalvariablethatholdstheinteger

errorcodeforthelastsystemcall• perror(const char *str) – alibraryfunction

whichdescribesthelastsystemcallerror

• Everyprocesshaserrno initializedto0• Asuccessfulsystemcallneveraffectserrno• Afailedsystemcallalwaysoverwriteserrno• #include <sys/errno.h>

45

Example#include <stdio.h>#include <errno.h>#include <unistd.h>

int main(int argc, char **argv) {int rv; char str[50];printf(“Before execlp, pid=%d\n”, getpid());rv = execlp(“blah”, (char *) 0);sprintf(“%s failed”, argv[0]);if (rv == -1)perror(str);

return 0;}

46

SignalsandSignalHandlers• AUnixsystemdefinesafixedsetofsignalsthatcan

beraised byoneprocess,therebycausingotherprocessestobeinterruptedandto(optionally)catchthesignal.

• Interruptsalsoraisesignals• <sys/signal.h> listssignalhandlingfunctions• man 7 signal givesthelistof#define signals

– mansections:1general,2syscalls,3Clib,7conventions,8sysadmincommands

• Whenasignalisreceived,itispassedontoitshandler,whichcanbeusercreatedandthenregistered

47

SomeSystemCallsForProcessManagement

48

SomeSystemCallsForFileManagement

1/26/18

9

49

SomeSystemCallsForDirectoryManagement

50

SomeSystemCallsForMiscellaneousTasks

51

UnixandWin32SystemCalls

52

DevilisintheDetails

• Basicprinciplesofsystemcalls,signalsandinterruptsarenotdifficult

• Detailsofsyscall trapsandinterruptsvaryfromCPUtoCPU–whatisputonthestackandinwhatorder– addressspacesissues,etc

• ThatiswhyOSwritersmustalsobeexpertsoftheCPUarchitecture

• Andwhyyoushouldreadmanualpagesofthesystemcallsinexcess