DOC PREVIEW
Duke CPS 110 - Unix process-oriented system calls

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture context switch =Timer interrupt goes off + Save current lecture state (ESC out of current powerpoint slideshow) + Load state of other lecture (choose powerpoint window) + Reset timer + Run new lecture (Click on slideshow)Lecture Thread:Unix process-oriented system callsUnix Process Model• Simple and powerful primitives for process creation and initialization.– fork syscall creates a child process as (initially) a clone of the parent– parent program runs in child process to set it up for exec– child can exit, parent can wait for child to do so.• Rich facilities for controlling processes by asynchronous signals.– notification of internal and/or external events to processes or groups– the look, feel, and power of interrupts and exceptions– default actions: stop process, kill process, dump core, no effect– user-level handlersUnix Process Controlint pid;int status = 0;if (pid = fork()) {/* parent */…..pid = wait(&status);} else {/* child */…..exit(status);}Parent uses waitto sleep until the child exits; wait returns childpidand status.Waitvariants allow wait on a specific child, or notification of stops and other signals.Child process passes status back to parent on exit , to report success/failure.The fork syscallreturns a zero to the child and the child process ID to the parent.Fork creates an exact copy of the parent process.Child Discipline• After a fork, the parent program (not process) has complete control over the behavior of its child process.• The child inherits its execution environment from the parent...but the parent program can change it.– sets bindings of file descriptors with open, close, dup– pipe sets up data channels between processes• Parent program may cause the child to execute a different program, by calling exec* in the child context.Exec, Execve, etc.• Children should have lives of their own.• Exec* “boots” the child with a different executable image.– parent program makes exec* syscall (in forked child context) to run a program in a new child process– exec* overlays child process with a new executable image– restarts in user mode at predetermined entry point (e.g., crt0)– no return to parent program (it’s gone)– arguments and environment variables passed in memory– file descriptors etc. are unchangedFork/Exit/Wait ExampleOS resourcesforkparent fork childwait exitChild process starts as clone of parent: incrementrefcounts on shared resources.Parent and child execute independently: memory states and resources may diverge.On exit, release memory and decrementrefcountson shared resources.Child enters zombie state: process is dead and most resources are released, but process descriptor remains until parent reaps exit status via wait.Parent sleeps in waituntil child stops or exits. “join”Fork/Exec/Exit/Wait Exampleforkparent forkchildwaitexitint pid = fork();Create a new process that is a clone of its parent.exec*(“program” [, argvp, envp]);Overlay the calling process virtual memory with a new program, and transfer control to it.exit(status);Exit with status, destroying the process.int pid = wait*(&status);Wait for exit (or other status change) of a child. execinitialize child contextJoin Scenarios• Several cases must be considered for join (e.g., exit/wait).– What if the child exits before the parent joins?• “Zombie” process object holds child status and stats.– What if the parent continues to run but never joins?• Danger of filling up memory with zombie processes?• Parent might have specified it was not going to wait or that it would ignore its child’s exit. Child status can be discarded.– What if the parent exits before the child?• Orphans become children of init (process 1).– What if the parent can’t afford to get “stuck” on a join?• Asynchronous notification (we’ll see an example later).Unix Signals• Signals notify processes of internal or external events.– the Unix software equivalent of interrupts/exceptions– only way to do something to a process “from the outside”– Unix systems define a small set of signal types• Examples of signal generation:– keyboard ctrl-c and ctrl-z signal the foreground process– synchronous fault notifications, syscall errors – asynchronous notifications from other processes via kill– IPC events (SIGPIPE, SIGCHLD)– alarm notificationssignal == “upcall”Process Handling of Signals1. Each signal type has a system-defined default action.• abort and dump core (SIGSEGV, SIGBUS, etc.)• ignore, stop, exit, continue 2. A process may choose to block (inhibit) or ignoresome signal types.3. The process may choose to catch some signal types by specifying a (user mode) handler procedure.• specify alternate signal stack for handler to run on• system passes interrupted context to handler• handler may munge and/or return to interrupted contextPredefined Signals (a Sampler)Sw termination sent by killQuitSIGTERMChild status changeIgnoreSIGCHLDAlarm clockQuitSIGALRMOut of range addrDumpSIGSEGVKill (can not be caught, blocked, or ignoredQuitSIGKILLIllegal instructionDumpSIGILLInterruptQuitSIGINTDescriptionDefault actionNameUser’s View of Signalsint alarmflag=0;alarmHandler (){ printf(“An alarm clock signal was received\n”);alarmflag = 1;}main(){signal (SIGALRM, alarmHandler);alarm(3);printf(“Alarm has been set\n”);while (!alarmflag) pause ();printf(“Back from alarm signal handler\n”);}Suspends calleruntil signalInstructs kernel to send SIGALRM in3 secondsSets up signal handlerYet Another User’s Viewmain(argc, argv)int argc; char* argv[];{int pid;signal (SIGCHLD,childhandler);pid = fork ();if (pid == 0) /*child*/{ execvp (argv[2], &argv[2]); }else {sleep (5);printf(“child too slow\n”);kill (pid, SIGINT);}}childhandler(){ int childPid, childStatus;childPid = wait (&childStatus);printf(“child done in time\n”);exit;}SIGCHLD sentby child on termination;if SIG_IGN, dezombieCollects statusWhat does this do?Files (& everything else)• Descriptors are small unsigned integers used as handles to manipulate objects in the system, all of which resemble files.• open with the name of a file returns a descriptor• read and write, applied to a descriptor, operate at the current position of the file offset. lseek repositions it.• Pipes are unnamed, unidirectional I/O stream created by pipe.• Devices are special files, created by mknod, with ioctl used for parameters of specific device.• Sockets


View Full Document

Duke CPS 110 - Unix process-oriented system calls

Download Unix process-oriented system calls
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Unix process-oriented system calls and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Unix process-oriented system calls 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?