Outline IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Inter-Process communication (IPC) Pipe and Its Operations FIFOs: Named Pipes Ø Allow Un-related Processes to Communicate Ring of Communicating Processes Ø Steps to Create A Ring with Pipes Ø A Ring with Multiple Processes 1 2 How Can Processes Communicate? The openfork example myfd = open("my.dat", O_RDONLY); fork(); read(myfd, &c, 1); What are the problems when parent/child processes try to communicate through a shared file? 1. Keep expanding (overhead) 2. Slow? 3. Extra synchronization 4. Can t write at the same time Interprocess Communication (IPC) IPC is a set of methods to exchange data among multiple processes. Reasons for cooperating processes:" Ø Information sharing" Ø Computation speedup" Ø Modularity" Ø Convenience "" Cooperating processes need interprocess communication (IPC)" 3 4 1
Outline Inter-Process communication (IPC) Pipe and Its Operations FIFOs: Named Pipes Ø Allow Un-related Processes to Communicate Ring of Communicating Processes Ø Steps to Create A Ring with Pipes Ø A Ring with Multiple Processes Pipe: Communication Buffer Create a pipe: system call pipe() #include <unistd.h> int pipe(int fildes[2]); Ø Create a unidirectional communication buffer with two file descriptors: fildes[0] for read and fildes[1] for write Ø Data write and read on a first-in-first-out basis Ø No external or permanent name, and can only be accessed through two file descriptors Ø The pipe can only be used by the process that created it and its descendants (i.e., child & grand-child processes) 5 6 Ordinary Pipes Pipe: Read and Write Read Ø Not necessarily atomic: may read less bytes Ø Blocking: if no data, but write file descriptor still opens Ø If empty, and all file descriptors for the write end are closed à read sees end-of-file and returns 0 Write Ø Atomic for at most PIPE_BUF bytes (512, 4K, or 64K) Ø Blocking: if buffer is full, and read file descriptors open Ø When all file descriptors referring to the read end of a pipe are closed à cause a SIGPIPE signal for the calling process 8 2
Program 6.1, parentwritepipe.c, page 185 Parent/Child File Descriptor Tables Which process write? Which process read? Which process write/read first? 9 10 Pipes and Redirection Example 6.5, page 188: ls -l sort -n +4 After Two dup2() Functions 11 12 3
After Close Unnecessary File Descriptors Problems of Pipe Pros Ø simple Ø flexible Ø efficient communication Cons: Ø no way to open an already-existing pipe. This makes it impossible for two arbitrary processes to share the same pipe, unless the pipe was created by a common ancestor process. 13 14 Outline Inter-Process communication (IPC) Pipe and Its Operations FIFOs: Named Pipes Ø Allow Un-related Processes to Communicate Ring of Communicating Processes Ø Steps to Create A Ring with Pipes Ø A Ring with Multiple Processes Named Pipes Named pipes are more powerful than ordinary pipes " Communication is bidirectional" Ø The same fd can be utilized for both read and write" " No parent-child/sibling relationship is necessary between communicating processes " Provided on both UNIX and Windows systems" 15 4
Named Pipes (FIFO) Named pipes allow two unrelated processes to communicate with each other They are also known as FIFOs (first-in, first-out) Named Pipes (FIFO) How does it work? Ø Uses an access point (a file on the file system) Ø Two unrelated processes opens this file to communicate Ø One process writes and the other reads, thus it is a halfduplex communication reads Process 2 File System Named Pipe (FIFO) writes Process 1 file FIFOs: Named Pipes Create a FIFO #include <sys/stat.h> int mkfifo(const char *path, mode_t mode); *path specific to the named pipe, which appears in the same directory as ordinary file Can be accessed by all processes Should be opened before read/write operations Removing a FIFO: same as removing a file Ø rm or unlink A named pipe is a special file that has no contents on the file system. 19 Parent/Child Processes with a FIFO Program 6.4 on page 194 [USP] Only the name of the FIFO is passed to the parent and child routines. 20 5
Parent/Child Processes with a FIFO (cont.) Program 6.5 on page 195 [USP] Parent/Child Processes with a FIFO (cont.) Program 6.6 on page 196 [USP] 21 22 Using FIFO in Client-Server Model Client-Server Communication Ø Client processes request service from a server. Ø A server process waits for requests from clients. A process can act as both a client and a server Server: Receive and Output Program 6.7 on page 197 [USP] When processes are exchanging data via the FIFO, the kernel passes all data internally without writing it to the file system. Thus, it is much faster than sockets, due to not using network resources. 23 Server process that creates a FIFO and open it. Whatever received from the FIFO will be write to standard output. 24 6
Client: Write to a Named FIFO from Server Program 6.8 on page 198 [USP] Benefits of Named Pipes (FIFO) Named pipes are very simple to use. mkfifo is a thread-safe function. Can server and client on two different machines? No synchronization mechanism is needed when using named pipes. Write to a FIFO is guaranteed to be atomic as far as the size is less than 4K. Named pipes have permissions (read and write) associated with them, unlike anonymous pipes. client process opens the FIFO and writes the string to the pipe. 25 Limitations of Named Pipes Named pipes can only be used for communication among processes on the same host machine. Named pipes can be created only in the local file system of the host, (i.e. it cannot create a named pipe on the NFS file system.) Careful programming is required for the client and server, in order to avoid deadlocks. Outline Inter-Process communication (IPC) Pipe and Its Operations FIFOs: Named Pipes Ø Allow Un-related Processes to Communicate Ring of Communicating Processes Ø Steps to Create A Ring with Pipes Ø A Ring with Multiple Processes Named pipe data is a byte stream. 28 7
An Example of Unidirectional Ring A ring of 5 nodes Ø Nodes represent processes Ø Links represent a unidirectional pipe How do the processes communicate? Ø Message with target PID Ø Receive targeted message Ø Forward other messages Ring within A Single Process Code section for a single process ring without error check int fd[2]; pipe(fd); dup2(fd[0], STDIN_FILENO); dup2(fd[1], STDOUT_FILENO); close(fd[0]); close(fd[1]); 29 30 Ring within A Single Process (cont.) Status after pipe(fd) Ring within A Single Process (cont.) Status after both dup2(, ) 31 32 8
Ring within A Single Process (cont.) Status after both close( ) Results of A Single Process Ring What will be the output of the following code? Ø Write first, then read It prints out {0,1,,9} on the screen! 33 34 Results of A Single Process Ring What will be the outputs of the following code? Ø Read first, then write The program hangs on the first read because no writes on the pipe 35 36 9
Results of A Single Process Ring What will be the output of the following code? Ø Using file pointers that has buffer A Ring of Two Processes Code section without error check The program may hang on the scanf(), since the printf() buffers its output. Putting an fflush(stdout) after the printf() will get he output. 37 38 A Ring of Two Processes (cont.) Status after the 2 nd pipe () A Ring of Two Processes (cont.) Status after fork() 39 40 10
A Ring of Two Processes (cont.) After dup2() A Ring of Two Processes (cont.) After close() 41 42 A Ring of n Processes (no error check) Program 7.1 on page 223 [USP] Token Management What is a token? Where does the token come from? How does the token is used? What if the token is lost (or can the token be lost)? Parent process break; child process create the next pipe and new process; 43 44 11
Outline Inter-Process communication (IPC) Pipe and Its Operations FIFOs: Named Pipes Ø Allow Un-related Processes to Communicate Ring of Communicating Processes Ø Steps to Create A Ring with Pipes Ø A Ring with Multiple Processes 45 12