DOC PREVIEW
Harvey Mudd CS 105 - Processes

This preview shows page 1-2-3-25-26-27 out of 27 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 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 27 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 27 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 27 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 27 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 27 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ProcessesSlide 2Logical Control FlowsConcurrent ProcessesUser View: Concurrent ProcessesContext SwitchingPrivate Address Spacesfork: Creating new processesFork Example #1Fork Example #2Fork Example #3Fork Example #4Fork Example #5exit: Destroying ProcessZombiesZombie ExampleNonterminating Child Examplewait: Synchronizing with childrenSlide 19Wait ExampleWaitpidWait/Waitpid Example Outputsexec: Running new programsSummarizingSummarizing (cont.)Fork() RevisitedExec() RevisitedProcessesProcessesTopicsTopicsProcess context switchesCreating and destroying processescs105CS 105“Tour of the Black Holes of Computing!”– 2 –CS 105ProcessesProcessesDef: A Def: A processprocess is an instance of a running program. is an instance of a running program.One of the most profound ideas in computer science.Not the same as “program” or “processor”Process provides each program with two key Process provides each program with two key abstractions:abstractions:Logical control flowEach program seems to have exclusive use of the CPU.Private address spaceEach program seems to have exclusive use of main memory.How are these Illusions maintained?How are these Illusions maintained?Process executions interleaved (multitasking)Address spaces managed by virtual memory system– 3 –CS 105Logical Control FlowsLogical Control FlowsTimeProcess A Process B Process CEach process has its own logical control flow– 4 –CS 105Concurrent ProcessesConcurrent ProcessesTwo processes Two processes run concurrentlyrun concurrently ( (are concurrent)are concurrent) if their if their flows overlap in time.flows overlap in time.Otherwise, they are Otherwise, they are sequential.sequential. Examples:Examples:Concurrent: A & B, A & CSequential: B & CTimeProcess A Process B Process C– 5 –CS 105User View: Concurrent ProcessesUser View: Concurrent ProcessesControl flows for concurrent processes are physically Control flows for concurrent processes are physically disjoint in time. (Except on multi-CPU machines.)disjoint in time. (Except on multi-CPU machines.)However, we can think of concurrent processes as However, we can think of concurrent processes as running in parallel with each other.running in parallel with each other.TimeProcess A Process B Process C– 6 –CS 105Context SwitchingContext SwitchingProcesses are managed by a shared chunk of OS code Processes are managed by a shared chunk of OS code called the called the kernelkernelImportant: the kernel is not a separate process, but rather runs as part of some user processControl flow passes from one process to another via a Control flow passes from one process to another via a context switch.context switch.Process AcodeProcess Bcodeuser codekernel codeuser codekernel codeuser codeTimecontext switchcontext switch– 7 –CS 105Private Address SpacesPrivate Address SpacesEach process has its own private address space.Each process has its own private address space.kernel virtual memory(code, data, heap, stack)memory mapped region forshared librariesrun-time heap(managed by malloc)user stack(created at runtime)unused0%esp (stack pointer)memoryinvisible touser codebrk0xc00000000x080480000x40000000read/write segment(.data, .bss)read-only segment(.init, .text, .rodata)loaded from the executable file0xffffffff– 8 –CS 105fork: Creating new processesfork: Creating new processesint fork(void)int fork(void)creates a new process (child process) that is identical to the calling process (parent process)returns 0 to the child processreturns child’s pid to the parent process if (fork() == 0) { printf("hello from child\n");} else { printf("hello from parent\n");}Fork is interesting(and often confusing)because it is calledonce but returns twice– 9 –CS 105Fork Example #1Fork Example #1void fork1(){ int x = 1; pid_t pid = fork(); if (pid == 0) {printf("Child has x = %d\n", ++x); } else {printf("Parent has x = %d\n", --x); } printf("Bye from process %d with x = %d\n", getpid(), x);}Key PointsKey PointsParent and child both run same codeDistinguish parent from child by return value from forkStart with same state, but each has private copyIncluding shared output file descriptorRelative ordering of their print statements undefined– 10 –CS 105Fork Example #2Fork Example #2void fork2(){ printf("L0\n"); fork(); printf("L1\n"); fork(); printf("Bye\n");}Key PointsKey PointsBoth parent and child can continue forkingL0 L1L1ByeByeByeBye– 11 –CS 105Fork Example #3Fork Example #3void fork3(){ printf("L0\n"); fork(); printf("L1\n"); fork(); printf("L2\n"); fork(); printf("Bye\n");}Key PointsKey PointsBoth parent and child can continue forkingL1 L2L2ByeByeByeByeL1 L2L2ByeByeByeByeL0– 12 –CS 105Fork Example #4Fork Example #4void fork4(){ printf("L0\n"); if (fork() != 0) {printf("L1\n"); if (fork() != 0) { printf("L2\n"); fork();} } printf("Bye\n");}Key PointsKey PointsBoth parent and child can continue forkingL0 L1ByeL2ByeByeBye– 13 –CS 105Fork Example #5Fork Example #5void fork5(){ printf("L0\n"); if (fork() == 0) {printf("L1\n"); if (fork() == 0) { printf("L2\n"); fork();} } printf("Bye\n");}Key PointsKey PointsBoth parent and child can continue forkingL0 ByeL1ByeByeByeL2– 14 –CS 105exit: Destroying Processexit: Destroying Processvoid exit(int status)void exit(int status)exits a processNormally return with status 0 (success)atexit() registers functions to be executed upon exitvoid cleanup(void) { printf("cleaning up\n");}void fork6() { atexit(cleanup); fork(); exit(0);}– 15 –CS 105ZombiesZombiesIdeaIdeaWhen process terminates, still consumes system resourcesVarious tables maintained by OS (to store exit status)Called a “zombie”Living corpse, half alive and half deadReapingReapingPerformed by parent on terminated childParent is given exit status informationKernel discards processWhat if Parent Doesn’t Reap?What if Parent Doesn’t Reap?If any parent terminates without reaping a child, then child will be reaped by init processOnly need explicit reaping for long-running processesE.g., shells and servers– 16 –CS 105linux> ./forks 7 &[1] 6639Running Parent, PID = 6639Terminating Child, PID = 6640linux> ps PID TTY TIME CMD 6585 ttyp9 00:00:00 tcsh 6639 ttyp9 00:00:03 forks 6640 ttyp9 00:00:00


View Full Document

Harvey Mudd CS 105 - Processes

Documents in this Course
Processes

Processes

25 pages

Load more
Download Processes
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 Processes 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 Processes 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?