13-Pipe
13-Pipe
Lecture – 16
(Inter Process Communication - 2)
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
5
Message Passing using Pipes
• ssize_t read(int fd, void *buf, size_t count)
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.
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