Outline
❚ Inter-Process communication (IPC) IPC and Unix Special Files ❚ Pipe and Its Operations ❚ FIFOs: Named Pipes (USP Chapters 6 and 7) Ø Allow Un-related Processes to Communicate ❚ Ring of Communicating Processes Ø Steps to Create A Ring with Pipes Instructor: Dr. Tongping Liu Ø A Ring with Multiple Processes
1 2
How Can Processes Communicate? Interprocess Communication (IPC)
❚ The openfork example ❚ IPC is a set of methods to exchange data among myfd = open("my.dat", O_RDONLY); multiple processes. fork(); read(myfd, &c, 1); ❚ Reasons for cooperating processes: What are the problems when Ø Information sharing parent/child processes try to Ø Computation speedup communicate through a Ø Modularity shared file? Ø Convenience
1. Keep expanding (overhead) 2. Slow? ❚ Cooperating processes need interprocess 3. Extra synchronization 4. Can’t write at the same time communication (IPC)
3 4
1 Outline Pipe: Communication Buffer
❚ Inter-Process communication (IPC) ❚ Create a pipe: system call pipe() ❚ Pipe and Its Operations #include
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 After Two dup2() Functions Example 6.5, page 188: ls -l | sort -n +4
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 Named Pipes
❚ Inter-Process communication (IPC) ❚ Named pipes are more powerful than ordinary pipes ❚ Pipe and Its Operations ❚ FIFOs: Named Pipes ❚ Communication is bidirectional Ø Allow Un-related Processes to Communicate Ø The same fd can be utilized for both read and write ❚ Ring of Communicating Processes Ø Steps to Create A Ring with Pipes ❚ No parent-child/sibling relationship is necessary Ø A Ring with Multiple Processes between communicating processes ❚ Provided on both UNIX and Windows systems
15
4 Named Pipes (FIFO) Named Pipes (FIFO)
❚ Named pipes allow two unrelated processes to ❚ How does it work? communicate with each other Ø Uses an access point (a file on the file system) ❚ They are also known as FIFOs (first-in, first-out) Ø Two unrelated processes opens this file to communicate Ø One process writes and the other reads, thus it is a half- duplex communication
File System reads writes Process 2 Named Pipe Process 1 (FIFO)
file
FIFOs: Named Pipes Parent/Child Processes with a FIFO
❚ Create a FIFO Program 6.4 on page 194 [USP] #include
5 Parent/Child Processes with a FIFO (cont.) Parent/Child Processes with a FIFO (cont.)
Program 6.6 on page 196 [USP] Program 6.5 on page 195 [USP]
21 22
Using FIFO in Client-Server Model Server: Receive and Output
❚ Client-Server Communication Program 6.7 on page 197 [USP] Ø 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
❚ 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.
Server process that creates a FIFO and open it.
23 Whatever received from the FIFO will be write to standard output.24
6 Client: Write to a Named FIFO from Server Benefits of Named Pipes (FIFO)
Program 6.8 on page 198 [USP] ❚ 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 Outline
❚ Named pipes can only be used for communication ❚ Inter-Process communication (IPC) among processes on the same host machine. ❚ Pipe and Its Operations ❚ Named pipes can be created only in the local file ❚ FIFOs: Named Pipes system of the host, (i.e. it cannot create a named Ø Allow Un-related Processes to Communicate pipe on the NFS file system.) ❚ Ring of Communicating Processes Ø Steps to Create A Ring with Pipes ❚ Careful programming is required for the client and Ø A Ring with Multiple Processes server, in order to avoid deadlocks.
❚ Named pipe data is a byte stream.
28
7 An Example of Unidirectional Ring Ring within A Single Process
❚ A ring of 5 nodes ❚ Code section for a single process ring without error Ø Nodes represent processes check Ø Links represent a unidirectional pipe ❚ How do the processes int fd[2]; communicate ? pipe(fd); Ø Message with target PID dup2(fd[0], STDIN_FILENO); Ø Receive targeted message dup2(fd[1], STDOUT_FILENO); Ø Forward other messages close(fd[0]); close(fd[1]);
29 30
Ring within A Single Process (cont.) Ring within A Single Process (cont.)
❚ Status after pipe(fd) ❚ Status after both dup2(…, …)
31 32
8 Ring within A Single Process (cont.) Results of A Single Process Ring
❚ Status after both close(…) ❚ 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 A Ring of Two Processes
❚ What will be the output of the following code? ❚ Code section without error check Ø Using file pointers that has buffer
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.) A Ring of Two Processes (cont.)
❚ Status after the 2nd pipe () ❚ Status after fork()
39 40
10 A Ring of Two Processes (cont.) A Ring of Two Processes (cont.)
❚ After dup2() ❚ After close()
41 42
A Ring of n Processes (no error check) Token Management
Program 7.1 on page 223 [USP] ❚ 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