DOC PREVIEW
Princeton COS 217 - Processes and Pipes

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:

Processes and PipesGoals of Today’s LectureCreating a New ProcessProgram vs. ProcessLife Cycle of a ProcessMany Processes Running “Concurrently”Why Start a New Process?Fork System CallSlide 9Example: What Output?ForkWaitExecuting a New ProgramCombining Fork() and Exec()SystemCommunication Between ProcessesSlide 17Interprocess CommunicationPipesCreating a PipePipe ExampleDupDup2Pipes and StdioPipes and ExecA Unix Shell!Conclusion1Processes and PipesCOS 217Professor Jennifer Rexford2Goals of Today’s Lecture•Creating a new processFork: process creates a new child processWait: parent waits for child process to completeExec: child starts running a new programSystem: combines fork, wait, and exec all in one•Communication between processesPipe between two processesRedirecting stdin and stdout•Initial background for the shell assignmentSoftware that provides interface for the userPrimarily used to launch other programs3Creating a New Process4Program vs. Process•ProgramExecutable codeNo dynamic state•ProcessAn instance of a program in executionWith its own control flow (illusion of a processor)… & private address space (illusion of memory)State including code, data, stack, registers, instruction pointer, open file descriptors, …Either running, waiting, or ready…•Can run multiple instances of the same programEach as its own process, with its own process ID5Life Cycle of a Process•Running: instructions are being executed•Waiting: waiting for some event (e.g., I/O finish) •Ready: ready to be assigned to a processorCreate Ready Running TerminationWaiting6Many Processes Running “Concurrently”•Multiple processes sharing the CPU•Processor switches context between the twoWhen process blocks waiting for operation to completeWhen process finishing using its share of the CPU•But, how do multiple processes start runningHow are they invoked in the first place?CPUCPUCPUI/OI/O I/O1:CPUCPUCPUI/OI/O I/O2:7Why Start a New Process?•Run a new programE.g., shell executing a program entered at command lineOr, even running an entire pipeline of commandsSuch as “wc –l * | sort | uniq -c | sort –nr”•Run a new thread of control for the same programE.g. a Web server handling a new Web requestWhile continuing to allow more requests to arriveEssentially time sharing the computer•Underlying mechanismA process runs “fork” to create a child process(Optionally) child process does “exec” of a new program8Fork System Call•Create a new processChild process inherits state from parent processParent and child have separate copies of that stateParent and child share access to any open filespid = fork();if (pid != 0) { /* in parent */ …} else { /* in child */ …}parentchild9Fork System Call•Fork is called onceBut returns twice, once in each process•Telling which process is which Parent: fork() returns the child’s process IDChild: fork() returns a 0pid = fork();if (pid != 0) { /* in parent */ …} else { /* in child */ …}10Example: What Output?int main(){ pid_t pid; int x = 1; pid = fork(); if (pid != 0) { printf(“parent: x = %d\n”, --x); exit(0); } else { printf(“child: x = %d\n”, ++x); exit(0); }}11Fork12Wait13Executing a New Program•Fork copies the state of the parent processChild continues running the parent program… with a copy of the process memory and registers•Need a way to invoke a new programIn the context of the newly-created child process•Exampleexeclp(“ls”, “ls”, “-l”, NULL);fprintf(stderr, “exec failed\n”);exit(1);programnull-terminated list of arguments(to become “argv[]”)14Combining Fork() and Exec()15System16Communication Between Processes17Communication Between Processesdifferent machinessame machine18Interprocess Communication•PipesProcesses on the same machineOne process spawns the otherUsed mostly for a pipeline of filters•SocketsProcesses on any machinesProcesses created independentlyUsed for client/server communication (e.g., Web)19Pipes20Creating a Pipe21Pipe Examplechildparent22Dupa.out < foo23Dup224Pipes and Stdiochildparent25Pipes and Execchild26A Unix Shell!27Conclusion•System callsAn interface to the operating systemTo perform operations on behalf of a user process•System calls for creating processesFork: process creates a new child processWait: parent waits for child process to completeExec: child starts running a new programSystem: combines fork, wait, and exec all in one•System calls for inter-process communicationPipe: create a pipe with a write end and a read endOpen/close: to open or close a fileDup2: to duplicate a file


View Full Document

Princeton COS 217 - Processes and Pipes

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

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