DOC PREVIEW
UI CS 270 - Interprocess Communication

This preview shows page 1-2-3 out of 9 pages.

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

Unformatted text preview:

Interprocess CommunicationBased on book chapter 12.6How do two processes communicate? We will look at:PipesSockets1Interprocess CommunicationPipes An interprocess communication mechanism allowing two or more processes to send information to each other. They are commonly used from within shells to connect the standard output of one utility to the standard input of another.2Interprocess CommunicationConsider $ who | wc -1option -1 outputs total number of lines in inputboth processes run concurrently; pipe buffers and protects against overflow; suspends reader until more data becomes available...3Unnamed PipesSystem Call: int pipe (int fd [2])pipe () creates an unnamed pipe and returns two file descriptors; the descriptor associated with the "read" end of the pipe is stored in fd[0], the descriptor associated with the "write" end of the pipe is stored in fd[1].4Unnamed PipesThe following rules apply to processes that read from a pipe:If a process reads from a pipe whose write end has been closed, the read () returns a 0, indicating end-of-input.If a process reads from an empty pipe whose write end is still open, it sleeps until some input becomes available.If a process tries to read more bytes from a pipe than are present, all of the current contents are returned and read () returns the number of bytes actually read.5Unnamed PipesThe following rules apply to processes that write to a pipe:If a process writes to a pipe whose read end has been closed, the write fails and the writer is sent a SIGPIPE signal. The default action of this signal is to terminate the writer.If a process writes fewer bytes to a pipe than the pipe can hold, the write () is guaranteed to be atomic; that is, the writer process will complete its system call without being preempted by another process. If a process writes more bytes to a pipe than the pipe can hold, no similar guarantees of atomicity apply.6Unnamed Pipesexecuting the following code will create the data structure shownint fd [2];pipe (fd);7Unnamed PipesUnnamed pipes are usually used for communication between a parent process and its child, with one process writing and the other process reading. The typical sequence of events is as follows:The parent process creates an unnamed pipe using pipe ().The parent process forks.The writer closes its read end of the pipe, and the designated reader closes its write end of the pipe.The processes communicate by using write () and read () calls.Each process closes its active pipe descriptor when finished with it.8Unnamed PipesExample that allows parent to read message from child via a pipenote that child includes NULL terminator as part of the message9$ cat talk.c ...list the program.#include <stdio.h>#define READ 0 /* The index of the read end of the pipe */#define WRITE 1 /* The index of the write end of the pipe */char* phrase = "Stuff this in your pipe and smoke it";main (){ int fd [2], bytesRead; char message [100]; /* Parent process' message buffer */ pipe (fd); /*Create an unnamed pipe */ if (fork () == 0) /* Child, writer */ { close(fd[READ]); /* Close unused end */ write (fd[WRITE],phrase, strlen (phrase) + 1); /* include NULL*/ close (fd[WRITE]); /* Close used end*/ } else /* Parent, reader*/ { close (fd[WRITE]); /* Close unused end */ bytesRead = read (fd[READ], message, 100); printf ("Read %d bytes: %s\n", bytesRead, message); /* Send */ close (fd[READ]); /* Close used end */ }}$ ./talk ...run the program.Read 37 bytes: Stuff this in your pipe and smoke it$ _10Unnamed PipesExample of chaining two programsparent creates pipeeach end attaches its stin or stout to the pipe (via dup2)both processes exec11$ cat connect.c ...list the program.#include <stdio.h>#define READ 0#define WRITE 1main (argc, argv)int argc;char* argv [];{ int fd [2]; pipe (fd); /* Create an unnamed pipe */ if (fork () != 0) /* Parent, writer */ { close (fd[READ]); /* Close unused end */ dup2 (fd[WRITE], 1); /* Duplicate used end to stdout */ close (fd[WRITE]); /* Close original used end */ execlp (argv[1], argv[1], NULL); /* Execute writer program */ perror ("connect"); /* Should never execute */ } else /* Child, reader */ { close (fd[WRITE]); /* Close unused end */ dup2 (fd[READ], 0); /* Duplicate used end to stdin */ close (fd[READ]); /* Close original used end */ execlp (argv[2], argv[2], NULL); /* Execute reader program */ perror ("connect"); /* Should never execute */ }}$ who ...execute "who" by itself.glass pts/1 Feb 15 18:45 (:0.0)$ ./connect who wc ...pipe "who" through "wc". 1 6 42 ...1 line, 6 words, 42 chars.$ _12Named PipesNamed pipes (FIFOs) are less restricted than unnamed pipes, and offer the following advantages:They have a name that exists in the file system.They may be used by unrelated processes.They exist until explicitly deleted.All of the pipe rules mentioned for unnamed pipes apply13Named PipesBecause named pipes exist as special files in the file system, processes using them to communicate need not have a common ancestry as when using unnamed pipes. A named pipe (FIFO) may be created in one of two ways:by using the Linux mkfifo utility or the mkfifo() system callUtility: mkfifo fileNamemkfifo creates a named pipe called fileName.14Named Pipesexample$ mkfifo myPipe ...create pipe.$ chmod ug+rw myPipe ...update permissions.$ ls -l myPipe ...examine attributes. prw-rw---- 1 glass cs 0 Feb 27 12:38 myPipe$ _Note the type of the named pipe is "p" in the ls listing.mkfifo ("myPipe", 0660); /* Create a named pipe */15Named PipesNamed Pipes operation:a special file is added into the file systemonce opened by open(), write() puts data into the FIFO queueread() removes data at end of FIFO queueprocess closes pipe using close()when no longer needed remove pipe from file system using unlink()16Example using a reader and a writer#include <stdio.h>#include <sys/types.h>#include <fcntl.h>/********************************************************************/main (){ int fd; char str[100]; mkfifo ("aPipe", 0660); /* Create named pipe */ fd = open ("aPipe", O_RDONLY); /* Open it for reading */ while (readLine (fd, str)) /* Display received messages */ printf ("%s\n", str); close (fd); /* Close pipe


View Full Document

UI CS 270 - Interprocess Communication

Download 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 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 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?