Top Banner
1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu 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 2 How Can Processes Communicate? The openfork example myfd = open("my.dat", O_RDONLY); fork(); read(myfd, &c, 1); 3 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) 4
12

IPC and Unix Special Files - UMass...1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Outline Inter-Process communication (IPC) Pipe and Its Operations

Mar 06, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: IPC and Unix Special Files - UMass...1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Outline Inter-Process communication (IPC) Pipe and Its Operations

1

1

IPC and Unix Special Files

(USP Chapters 6 and 7)

Instructor: Dr. Tongping Liu

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

2

How Can Processes Communicate?

❚  The openfork example myfd = open("my.dat", O_RDONLY); fork(); read(myfd, &c, 1);

3

What are the problems when parent/child processes try to communicate through a shared file?

1.  Keep expanding (overhead)2.  Slow?3.  Extra synchronization4.  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)"

4

Page 2: IPC and Unix Special Files - UMass...1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Outline Inter-Process communication (IPC) Pipe and Its Operations

2

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

5

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)

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

Page 3: IPC and Unix Special Files - UMass...1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Outline Inter-Process communication (IPC) Pipe and Its Operations

3

Program 6.1, parentwritepipe.c, page 185

9

Which process write?Which process read? Which process write/read first?

Parent/Child File Descriptor Tables

10

Pipes and Redirection

11

Example 6.5, page 188:ls -l | sort -n +4

After Two dup2() Functions

12

Page 4: IPC and Unix Special Files - UMass...1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Outline Inter-Process communication (IPC) Pipe and Its Operations

4

After Close Unnecessary File Descriptors

13

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.

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

15

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"

Page 5: IPC and Unix Special Files - UMass...1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Outline Inter-Process communication (IPC) Pipe and Its Operations

5

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 half-duplex communication

File System Named Pipe

(FIFO) Process 1 Process 2

writes reads

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

19

A named pipe is a special file that has no contents on the file system.

Parent/Child Processes with a FIFO

20

Only the name of the FIFO is passed to the parent and child routines.

Program 6.4 on page 194 [USP]

Page 6: IPC and Unix Special Files - UMass...1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Outline Inter-Process communication (IPC) Pipe and Its Operations

6

Parent/Child Processes with a FIFO (cont.)

21

Program 6.5 on page 195 [USP]

Parent/Child Processes with a FIFO (cont.)

22

Program 6.6 on page 196 [USP]

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

❚  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: Receive and Output

24

Program 6.7 on page 197 [USP]

Server process that creates a FIFO and open it. Whatever received from the FIFO will be write to standard output.

Page 7: IPC and Unix Special Files - UMass...1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Outline Inter-Process communication (IPC) Pipe and Its Operations

7

Client: Write to a Named FIFO from Server

25

Program 6.8 on page 198 [USP]

client process opens the FIFO and writes the string to the pipe.

Can server and client on two different machines?

Benefits of Named Pipes (FIFO)

❚  Named pipes are very simple to use.

❚  mkfifo is a thread-safe function.

❚  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.

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.

❚  Named pipe data is a byte stream.

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

28

Page 8: IPC and Unix Special Files - UMass...1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Outline Inter-Process communication (IPC) Pipe and Its Operations

8

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

29

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]);

30

Ring within A Single Process (cont.)

❚  Status after pipe(fd)

31

Ring within A Single Process (cont.)

❚  Status after both dup2(…, …)

32

Page 9: IPC and Unix Special Files - UMass...1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Outline Inter-Process communication (IPC) Pipe and Its Operations

9

Ring within A Single Process (cont.)

❚  Status after both close(…)

33

Results of A Single Process Ring

❚  What will be the output of the following code? Ø Write first, then read

34

It prints out {0,1,…,9} on the screen!

Results of A Single Process Ring

❚  What will be the outputs of the following code? Ø Read first, then write

35

The program hangs on the first read because no writes on the pipe

36

Page 10: IPC and Unix Special Files - UMass...1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Outline Inter-Process communication (IPC) Pipe and Its Operations

10

Results of A Single Process Ring

❚  What will be the output of the following code? Ø Using file pointers that has buffer

37

The program may hang on the scanf(), since the printf() buffers its output. Putting an fflush(stdout) after the printf() will get he output.

A Ring of Two Processes

❚  Code section without error check

38

A Ring of Two Processes (cont.)

❚  Status after the 2nd pipe ()

39

A Ring of Two Processes (cont.)

40

❚  Status after fork()

Page 11: IPC and Unix Special Files - UMass...1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Outline Inter-Process communication (IPC) Pipe and Its Operations

11

A Ring of Two Processes (cont.)

❚  After dup2()

41

A Ring of Two Processes (cont.)

❚  After close()

42

A Ring of n Processes (no error check)

43

Parent process break; �child process create the next pipe and new process;

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

44

Page 12: IPC and Unix Special Files - UMass...1 1 IPC and Unix Special Files (USP Chapters 6 and 7) Instructor: Dr. Tongping Liu Outline Inter-Process communication (IPC) Pipe and Its Operations

12

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