UCSC CMPS 105 - 01 - Chapter 14 - Interprocess Communication

Unformatted text preview:

Chapter 14: Interprocess CommunicationPlansIntroductionPipesPipes (cont.)Understanding PipesUnderstanding Pipes (cont.)Using PipesExampleExample: Piping output to child process’ inputUsing Pipes for synchronization and communicationpopen()popen() and pclose()AssignmentFIFOsFIFO detailsExample: Using FIFOs to Duplicate Output StreamsExample: Client-Server Communication Using FIFOsSystem V IPCRendezvousing with IPC StructuresIPC PermissionsIssues w/System V IPCMessage Queuesmsqid_dsLimitsmsgget()msgctl()msgsnd()msgrcv()Semaphoressemid_dssemget()semctl()semop()Shared MemoryChapter 14: InterprocessCommunicationCMPS 105: Systems ProgrammingProf. Scott BrandtT Th 2-3:45Soc Sci 2, Rm. 167Plans This week: Chapter 14 Next week:  Networked IPC Other? Last week Something ReviewIntroduction Interprocess Communication (IPC) enables processes to communicate with each other to share information Pipes (half duplex) FIFOs (named pipes) Stream pipes (full duplex) Named stream pipes Message queues Semaphores Shared Memory Sockets StreamsPipes Oldest (and perhaps simplest) form of UNIX IPC Half duplex Data flows in only one direction Only usable between processes with a common ancestor Usually parent-child Also child-childPipes (cont.) #include <unistd.h> int pipe(intfildes[2]); fildes[0] is open for reading and fildes[1] is open for writing The output of fildes[1] is the input for fildes[0]Understanding Pipes Within a process Writes to fildes[1] can be read on fildes[0] Not very useful Between processes After a fork() Writes to fildes[1] by one process can be read on fildes[0] by the otherUnderstanding Pipes (cont.) Even more useful: two pipes, fildes_aand fildes_b After a fork() Writes to fildes_a[1] by one process can be read on fildes_a[0] by the other, and  Writes to fildes_b[1] by that process can be read on fildes_b[0] by the first processUsing Pipes Usually, the unused end of the pipe is closed by the process If process A is writing and process B is reading, then process A would close fildes[0] and process B would close fildes[1] Reading from a pipe whose write end has been closed returns 0 (end of file) Writing to a pipe whose read end has been closed generates SIGPIPE PIPE_BUF specifies kernel pipe buffer sizeExampleint main(void) {int n, fd[2];pid_t pid;char line[maxline];if(pipe(fd) < 0) err_sys(“pipe error”);if( (pid = fork()) < 0) err_sys(“fork error”);else if(pid > 0) {close(fd[0]);write(fd[1], “hello\n”, 6);} else {close(fd[1]);n = read(fd[0], line, MAXLINE);write(STDOUT_FILENO, line, n);}Example: Piping output to child process’ inputint fd[2];pid_t pid;pipe(fd);pid = fork();if(pid == 0) {dup2(fd[0], STDIN_FILENO);exec(<whatever>);}Using Pipes for synchronization and communication Once you have a pipe or pair of pipes set up, you can use it/them to Signal events (one pipe) Wait for a message Synchronize (one or two pipes) Wait for a message or set of messages You send me a message when you are ready, then I’ll send you a message when I am ready Communicate (one or two pipes) Send messages back and forthpopen() #include <stdio.h> FILE *popen(const char *cmdstring, const char *type); Encapsulates a lot of system calls Creates a pipe Forks Sets up pipe between parent and child (typespecifies direction) Closes unused ends of pipes Turns pipes into FILE pointers for use with STDIO functions (fread, fwrite, printf, scanf, etc.) Execs shell to run cmdstringon childpopen() and pclose() Popen() details Directs output/input to stdin/stdout “r” -> parent reads, “w” -> parent writes int pclose(FILE *fp); Closes the STDIO stream Waits for command to terminate Returns termination status of shellAssignment Simulated audio player with shared memory and semaphores We will discuss this at the end of class todayFIFOs First: Coprocesses – Nothing more than a process whose input and output are both redirected from another process FIFOs – named pipes With regular pipes, only processes with a common ancestor can communicate With FIFOs, any two processes can communicate Creating and opening a FIFO is just like creating and opening a fileFIFO details #include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_tmode); The modeargument is just like in open() Can be opened just like a file When opened, O_NONBLOCK bit is important Not specified: open() for reading blocks until the FIFO is opened by a writer (same for writing) Specified: open() returns immediately, but returns an error if opened for writing and no reader existsExample: Using FIFOs to Duplicate Output Streams Send program 1’s output to both program2 and program3 (p. 447) mkfifo fifo1 prog3 < fifo1 & prog1 < infile | tee fifo1 | prog2Example: Client-Server Communication Using FIFOs Server contacted by multiple clients (p.448) Server creates a FIFO in a well-known place  And opens it read/write Clients send requests on this FIFO Must be < PIP_BUF bytes Issue: How to respond to clients Solution: Clients send PID, server creates per-client FIFOs for responsesSystem V IPC IPC structures for message queues, semaphores, and shared memory segments Each structure is represented by an identifier The identifier specifies which IPC object we are using The identifier is returned when the corresponding structure is created with msgget(), semget(), or shmget() Whenever an IPC structure is created, a keymust be specified Matching keys refer to matching objects This is how two processes can coordinate to use a single IPC mechanism to communicateRendezvousing with IPC Structures Process 1 can specify a key of IPC_PRIVATE This creates a unique IPC structure Process 1 then stores the IPC structure somewhere that Process 2 can read Process 1 and Process 2 can agree on a key ahead of time Process 1 and Process 2 can agree on a pathname and project ID ahead of time and use ftok to generate a unique keyIPC Permissions System V associates an ipc_perm structure with each IPC structure:struct ipc_perm {uid_t uid; // owner’s eff. user IDgid_t gid; // owner’s eff. group IDuid_t cuid;// creator’s eff. user IDgid_t cgid;// creator’s eff. group IDmode_t mode; // access modesulong seq; // slot usage


View Full Document

UCSC CMPS 105 - 01 - Chapter 14 - Interprocess Communication

Download Chapter 14 - Interprocess Communication
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 Chapter 14 - Interprocess Communication 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 Chapter 14 - Interprocess Communication 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?