+ All Categories
Home > Documents > System Programming: Communication through pipes

System Programming: Communication through pipes

Date post: 17-Feb-2022
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
21
System Programming: Communication through pipes Raymond Namyst Dept. of Computer Science University of Bordeaux, France https://gforgeron.gitlab.io/progsys/ 1
Transcript
Page 1: System Programming: Communication through pipes

System Programming:Communication through pipes

Raymond NamystDept. of Computer Science

University of Bordeaux, France

https://gforgeron.gitlab.io/progsys/

1

Page 2: System Programming: Communication through pipes

The concept of pipe

• Major mechanism used by the shell• ls | grep pattern• ./prog | cat -n | less• Etc.

• Some operating systems (MS-DOS) implement pipes using files• Example: ls | grep pattern

• The output of “ls” is redirected to a temporary file• The system waits for the termination of “ls”• “grep pattern” is executed, with its input redirected from the file

• No parallelism• Max file size limit can be reached

2

Page 3: System Programming: Communication through pipes

The concept of pipe

• In Unix systems, pipes are special objects allocated in the kernel• FIFO ordering• Fixed capacity

3

Page 4: System Programming: Communication through pipes

The concept of pipe

• In Unix systems, pipes are special objects allocated in the kernel• FIFO ordering• Fixed capacity

• The “pipe” syscallint pipe(int fildes[2]);

• Creates a pipe and returns two file descriptors• One for reading (fildes[0]) and one for writing (fildes[1])

4

fildes[1] fildes[0]

Page 5: System Programming: Communication through pipes

The concept of pipe

• In Unix systems, pipes are special objects allocated in the kernel• FIFO ordering• Fixed capacity

• The “pipe” syscallint pipe(int fildes[2]);

• Creates a pipe and returns two file descriptors• One for reading (fildes[0]) and one for writing (fildes[1])

• Reading and writing are done using usual read/write• No lseek

5

write read

Page 6: System Programming: Communication through pipes

int tube[2];pipe (tube);

- 6

process

filedescriptor

table

0

12

3

4

5

6

7

3tube

Opened files table

Mode: RD_ONLYOffset: 0Ref counter: 1File info:

4

Mode: WR_ONLYOffset: 0Ref counter: 1File info: write read

Page 7: System Programming: Communication through pipes

int tube[2];pipe (tube);

- 7

process

filedescriptor

table

0

12

3

4

5

6

7

3tube 4

write read

Simplified view

Page 8: System Programming: Communication through pipes

Trying out our first pipe

• pipe.c

8

Page 9: System Programming: Communication through pipes

Pipe are intended to allow communication between processes• But pipes created with “pipe” are anonymous

• No way to share their name• Look at mkfifo if named pipes are really what you need

• Fortunately, pipes are inherited when forking new processes…

9

Page 10: System Programming: Communication through pipes

int tube[2];pipe (tube); fork();

- 10

parent

filedescriptor

table

0

12

3

4

5

6

7

3tube 4

child

0

12

3

4

5

6

7

3tube 4

write read

Page 11: System Programming: Communication through pipes

Sending characters through a pipe

• Hand-made implementation of• Line numbering + to upper case

• pipe-n-fork.c

11

father child

file

Touppercat -n

stdout

Page 12: System Programming: Communication through pipes

The concept of pipe

• When both sides of a pipe are opened• read is blocking if the pipe is empty• write is blocking if the pipe is full

12

Page 13: System Programming: Communication through pipes

The concept of pipe

• When both sides of a pipe are opened• read is blocking if the pipe is empty• write is blocking if the pipe is full

• Pipe closed on the write side• read returns 0 (end of file)

13

Page 14: System Programming: Communication through pipes

The concept of pipe

• When both sides of a pipe are opened• read is blocking if the pipe is empty• write is blocking if the pipe is full

• Pipe closed on the write side• read returns 0 (end of file)

• Pipe closed on the read side• write raises an exception (“Broken pipe”)

14

Page 15: System Programming: Communication through pipes

Sending characters through a pipe

• Redirections to only use STDIN/STDOUT • pipe-n-redir.c

15

father child

file

Touppercat -n

stdout

Page 16: System Programming: Communication through pipes

Sending characters through a pipe

• Redirections to only use STDIN/STDOUT • Exec to use legacy

• cat -n• tr a-z A-Z

• pipe-n-exec.c

16

father child

file

tr a-z A-Zcat -n

stdout

Page 17: System Programming: Communication through pipes

Sending characters through a pipe

• Problem:• Child still output characters after

shell prompt

• One solution would be to swap roles of father & child…

17

father child

file

tr a-z A-Zcat -n

stdout

Page 18: System Programming: Communication through pipes

Sending characters through a pipe

• Problem:• Child still output characters after

shell prompt

• But the shell acts differently: it creates two childs!

• ultimate-pipe.c

18

Child 1 Child 2

file

tr a-z A-Zcat -n

stdoutFather

fork fork

Page 19: System Programming: Communication through pipes

Setting up a chain of processes

• chain.c

19

P1 PN-1

Cmd N-1Cmd 1

stdout

P0

Cmd 0

stdin

Page 20: System Programming: Communication through pipes

Atomicity of read/write

• What happens if multiple processes simultaneously write into (or read from) the same pipe?• Atomic if size < PIPE_BUF

• (typically 512 bytes)

20

writeread

writeread

read

Page 21: System Programming: Communication through pipes

Additional resourcesavailable on

http://gforgeron.gitlab.io/progsys/

21


Recommended