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

Lab 11

The document provides an overview of the IPC system call 'pipe' used for inter-process communication, detailing its functionality, syntax, and examples. It explains how pipes facilitate one-way communication between processes, allowing one to write and another to read data, and includes programming tasks for practical implementation. The tasks involve creating programs that utilize pipes for various scenarios, including parent-child communication and error handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab 11

The document provides an overview of the IPC system call 'pipe' used for inter-process communication, detailing its functionality, syntax, and examples. It explains how pipes facilitate one-way communication between processes, allowing one to write and another to read data, and includes programming tasks for practical implementation. The tasks involve creating programs that utilize pipes for various scenarios, including parent-child communication and error handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Operating System

Lab 11

Topic: IPC System Call

Lab Instructor: Muhammad Rizwan

Session: Fall 2023


School of Systems and Technology
UMT Lahore Pakistan

1
System call: Pipe ()
Conceptually, a pipe is a connection between two processes, such that the standard
output from one process becomes the standard input of the other process. Pipes are
useful for communication between related processes (inter-process communication).
 Pipe is one-way communication only i.e we can use a pipe such that One process
write to the pipe, and the other process reads from the pipe. It opens a pipe, which
is an area of main memory that is treated as a “virtual file”.
 The pipe can be used by the creating process, as well as all its child processes, for
reading and writing. One process can write to this “virtual file” or pipe and another
related process can read from it.
 If a process tries to read before something is written to the pipe, the process is
suspended until something is written.
 The pipe system call finds the first two available positions in the process’s open file
table and allocates them for the read and write ends of the pipe.

Syntax
int pipe(int fds[2]);

Parameters :
fd[0] will be the fd(file descriptor) for the
read end of pipe.
fd[1] will be the fd for the write end of pipe.
Returns : 0 on Success. -1 on error.

Pipes behave FIFO(First in First out), Pipe behave like a queue data structure. Size
of read and write don’t have to match here.

Example: 01

#include <unistd.h>

2
#include <iostream>
using namespace std;
int main()
{
char msg1[] = "message 1";
char msg2[] = "message 2";
char buffer[10];
int fd[2];
if(pipe(fd)>=0){
write(fd[1],msg1, sizeof(msg1));
write(fd[1],msg2, sizeof(msg2));
write(fd[1],"message",8);
for(int i=0; i<3; i++)
{
read(fd[0],buffer,10);
cout << buffer << endl;
}
}
return 0;
}

Output:

Example: 02

#include <unistd.h>
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
using namespace std;
int main()
{
char buffer[10];
//file descriptor: fd[0] is reading end , fd[1] is writing end
int fd[2];
//creating pipe & checking its status
if (pipe(fd)==-1){
cout << "Pipe failed.\n";

3
exit(0);
}
int pid = fork();
if (pid>0){
//Blocking reading end of the pipe.
close(fd[0]);
//writing message in pipe.
write(fd[1], "Message 1", 10);
}
else{
//Blocking writing end of the pipe.
close(fd[1]);
//reading message in pipe.
read(fd[0],buffer,10);
cout << buffer << endl;
}
return 0;

}
Output:

TASKS

4
1. Write a program where the parent process writes multiple messages into a pipe, and the
child process reads them one by one. The parent should write 5 different messages into the
pipe, and the child should read and print all of them.

2. Extend the previous task by forking multiple child processes. Each child process should read a
message from the pipe, modify it (like, add a function and apply convert it to uppercase), and
then print it.

3. Implement a producer-consumer problem using pipes where: The parent process is the
producer and writes a series of integers into the pipe. A child process is the consumer
and reads the integers, performs some processing on them (e.g., doubling the value),
and prints the result.

4. Create a program that implements a pipe between a parent and child process, and
introduces error handling at different stages. Specifically:
 Check for errors when creating the pipe.
 Check for errors during read/write operations.
 Implement proper closing of the pipe file descriptors.

5. Write a program where the parent process dynamically generates messages (e.g., based on
user input) and sends them through the pipe to the child process. The child process reads the
message, processes it (e.g., appends a timestamp), and sends it back to the parent.

You might also like