4061 session 13 (2/27)

17
4061 Session 13 (2/27)

Upload: galena-horne

Post on 01-Jan-2016

18 views

Category:

Documents


1 download

DESCRIPTION

4061 Session 13 (2/27). Today. Pipes and FIFOs. Today’s Objectives. Understand the concept of IPC Understand the purpose of anonymous and named pipes Describe where file descriptors point after system calls to pipe, fork, and dup - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 4061 Session 13 (2/27)

4061 Session 13 (2/27)

Page 2: 4061 Session 13 (2/27)

Today

• Pipes and FIFOs

Page 3: 4061 Session 13 (2/27)

Today’s Objectives• Understand the concept of IPC

• Understand the purpose of anonymous and named pipes

• Describe where file descriptors point after system calls to pipe, fork, and dup

• Write C code that allows parent and child processes to communicate with pipes or FIFOs– Specifically: write a “filter”

Page 4: 4061 Session 13 (2/27)

Admin

• Quiz 2: Nice!

Page 5: 4061 Session 13 (2/27)

Inter-Process Communication (IPC)

• Techniques for the exchange of data between processes or threads

Page 6: 4061 Session 13 (2/27)

Why IPC?

Page 7: 4061 Session 13 (2/27)

UNIX IPC

• Lots of ways to do it. We’ll talk about some of the classics in 4061.

• Today:– Pipes and FIFOs

• Later:– Signals, Sockets

Page 8: 4061 Session 13 (2/27)

Pipes

• Aka: anonymous pipes or unnamed pipes

• Very simple IPC

• Remember bash shell: |– Chain stdout of one process with stdin of

another process– E.g.: ps ax | grep java

Page 9: 4061 Session 13 (2/27)

Pipes: Conceptually

Page 10: 4061 Session 13 (2/27)

Pipes: Conceptually

• One-way channel for information– Write to the input end, read from the output

end

• Think of a pipe as a buffer in the operating system that you can read from and write to.– If you fill it up, the writer blocks– If it’s empty, the reader blocks

Page 11: 4061 Session 13 (2/27)

Opening a Pipe• A process issues a command to create a

pipe, and receives two file descriptors.

Page 12: 4061 Session 13 (2/27)

Forking• To communicate with something, fork.

Page 13: 4061 Session 13 (2/27)

Cleaning Up• Close unused descriptors.

Page 14: 4061 Session 13 (2/27)

Details

• What do the file descriptors point to?– Well, files...but a special type (in pipefs).– So we can still use file-oriented calls, such as

read, write, close, etc.– No mount point, so not visible with ls

• If you close one end of a pipe, you can’t reopen it

Page 15: 4061 Session 13 (2/27)

Details

• Often implemented as a circular buffer

Page 16: 4061 Session 13 (2/27)

Shortcut Methods

• popen and pclose

Page 17: 4061 Session 13 (2/27)

FIFOs (a.k.a. Named Pipes)

• (first in first out)

• File (of type FIFO) in the filesystem.– Access permissions

• Kernel structures are set up on open

• Multiple processes can write, only one can read

• Writes (smaller than the buffer) are atomic