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 processFork: process creates a new child processWait: parent waits for child process to completeExec: child starts running a new programSystem: combines fork, wait, and exec all in one•Communication between processesPipe between two processesRedirecting stdin and stdout•Initial background for the shell assignmentSoftware that provides interface for the userPrimarily used to launch other programs3Creating a New Process4Program vs. Process•ProgramExecutable codeNo dynamic state•ProcessAn instance of a program in executionWith 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 programEach 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 twoWhen process blocks waiting for operation to completeWhen process finishing using its share of the CPU•But, how do multiple processes start runningHow 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 programE.g., shell executing a program entered at command lineOr, even running an entire pipeline of commandsSuch as “wc –l * | sort | uniq -c | sort –nr”•Run a new thread of control for the same programE.g. a Web server handling a new Web requestWhile continuing to allow more requests to arriveEssentially time sharing the computer•Underlying mechanismA process runs “fork” to create a child process(Optionally) child process does “exec” of a new program8Fork System Call•Create a new processChild process inherits state from parent processParent and child have separate copies of that stateParent and child share access to any open filespid = fork();if (pid != 0) { /* in parent */ …} else { /* in child */ …}parentchild9Fork System Call•Fork is called onceBut returns twice, once in each process•Telling which process is which Parent: fork() returns the child’s process IDChild: 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 processChild continues running the parent program… with a copy of the process memory and registers•Need a way to invoke a new programIn 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•PipesProcesses on the same machineOne process spawns the otherUsed mostly for a pipeline of filters•SocketsProcesses on any machinesProcesses created independentlyUsed for client/server communication (e.g., Web)19Pipes20Creating a Pipe21Pipe Examplechildparent22Dupa.out < foo23Dup224Pipes and Stdiochildparent25Pipes and Execchild26A Unix Shell!27Conclusion•System callsAn interface to the operating systemTo perform operations on behalf of a user process•System calls for creating processesFork: process creates a new child processWait: parent waits for child process to completeExec: child starts running a new programSystem: combines fork, wait, and exec all in one•System calls for inter-process communicationPipe: create a pipe with a write end and a read endOpen/close: to open or close a fileDup2: to duplicate a file
View Full Document