0% found this document useful (0 votes)
2 views

13-Pipe

Uploaded by

shashank.skr1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

13-Pipe

Uploaded by

shashank.skr1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Operating Systems (CS3000)

Lecture – 16
(Inter Process Communication - 2)

Dr. Jaishree Mayank


Assistant Professor
Department of Computer Sc. and Engg.
Message Passing using Pipes
• Pipe is a communication between parent and child process
• Communication is achieved by one process writing into the pipe and
other reading from the pipe
• To achieve the pipe system call, create two descriptors, one to write
into the file and another to read from the file.
• Patent  Child

Write
Pipe
Read

2
Creating Pipe between Parent and Child process
• int pipe(int pipefd[2]);
• a file descriptor is used to access the two ends of the pipe: one end
for reading and one end for writing

 pipefd[0] is the file descriptor for reading.


 pipefd[1] is the file descriptor for writing.
• Returns zero on success
• Returns -1 in case of failure

5
Message Passing using Pipes
• ssize_t read(int fd, void *buf, size_t count)

 The file descriptor to read from.


 A pointer to a buffer where the read data will be stored.
 The maximum number of bytes to read.

• Returns the number of bytes read


• Returns -1 in case of failure

6
Message Passing using Pipes
• ssize_t write(int fd, void *buf, size_t count)


The file descriptor to write to.

A pointer to a buffer where the write data will be stored.

The maximum number of bytes to write.
• Return the number of bytes written
• Return zero in case nothing is written
• Return -1 in case of failure

7
Message Passing using Pipes
• _int close(int fd)


Closing the pipe end.

• Return zero on success


• Return -1 in case of failure

8
Message Passing using Pipes
• Algorithm
• Step 1 − Create a pipe
• Step 2 − Create a child process
• Step 3 − Parent process writes to the pipe
• Step 4 − Child process retrieves the message from the pipe and
writes it to the standard output
• Step 5 − Repeat step 3 and step 4 once again

9
Two-way Communication Using Pipes
• If both the parent and the child needs to write and read from the
pipes simultaneously
• Two pipes are required

10
Two-way Communication Using Pipes
• Algorithm
• Step 1 − Create pipe1 for the parent process to write and the child
process to read
• Step 2 − Create pipe2 for the child process to write and the parent
process to read
• Step 3 − Close the unwanted ends of the pipe from the parent and
child side
• Step 4 − Parent process to write a message and child process to
read and display on the screen
• Step 5 − Child process to write a message and parent process to
read and display on the screen

11
Pipe1.c

12
Pipe2.c

13
Thank You
Any Questions?

14

You might also like