0% found this document useful (0 votes)
523 views4 pages

Lab 07

This document describes a lab assignment on implementing interprocess communication (IPC) using pipes in Linux. The objectives are to: 1. Create pipes to allow communication between related processes. Pipes allow unidirectional data flow and synchronize processes. 2. Complete two tasks: (1) Compute the factorial of a number by having the parent write the number to a pipe for the child to read and calculate, (2) Have the parent read from one file and write to a pipe for the child to read from the pipe and write to another file. 3. Use system calls like pipe(), read(), write(), close() to create and use pipes between processes for communication and synchronization. An

Uploaded by

majidshafi
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)
523 views4 pages

Lab 07

This document describes a lab assignment on implementing interprocess communication (IPC) using pipes in Linux. The objectives are to: 1. Create pipes to allow communication between related processes. Pipes allow unidirectional data flow and synchronize processes. 2. Complete two tasks: (1) Compute the factorial of a number by having the parent write the number to a pipe for the child to read and calculate, (2) Have the parent read from one file and write to a pipe for the child to read from the pipe and write to another file. 3. Use system calls like pipe(), read(), write(), close() to create and use pipes between processes for communication and synchronization. An

Uploaded by

majidshafi
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/ 4

LAB 07

Summary

Items Description
Course Title Operating System
Lab Title Interprocess Communication using Pipes
Duration 3 Hours
Operating System Linux Operating System
/Tool/Language
Objective Implement the Interprocess Communication using pipes

Inter Process Communication

Interprocess communication (IPC) is a mechanism for processes to communicate and


to synchronize their actions. It is a set of programming interfaces that allows a
programmer to coordinate activities among different program processes that can run
concurrently in an operating system. This allows a program to handle many user
requests at the same time. Since even a single user request may result in multiple
processes running in the operating system on the user's behalf, the processes need to
communicate with each other. The IPC interfaces make this possible. Each IPC
method has its own advantages and limitations so it is not unusual for a single
program to use all of the IPC methods.

IPC methods include pipes and named pipes; message queuing; semaphores; shared


memory; and sockets.

Pipes:

A pipe is a communication channel between two ends. It is mostly used to


communicate between processes running within a computer. It is a communication
device that permits unidirectional communication. Data written to the “write end” of
the pipe is read back from the “read end”. Pipes are serial devices; the data is always
read from the pipe in the same order it was written.

A pipe’s data capacity is limited. If the writer process writes faster than the reader
process consumes the data, and if the pipe cannot store more data, the writer process
blocks until more capacity becomes available. If the reader tries to read but no data is
available, it blocks until data becomes available. Thus, the pipe automatically
synchronizes the two processes.

Creating Pipes:
To create a pipe, invoke the pipe command. Supply an integer array of size 2.The call
to pipe stores the reading file descriptor in array position 0 and the writing file
descriptor in position 1. For example, consider the code:

int pipe_fds[2];
int read_fd;
int write_fd;

pipe (pipe_fds);
read_fd = pipe_fds[0];
write_fd = pipe_fds[1];

Data written to the file descriptor read_fd can be read back from write_fd.

To create a simple pipe with C, we make use of the pipe () system call. It takes a
single argument, which is an array of two integers. If successful, the array will contain
two new file descriptors to be used for the pipeline.

System call: pipe ();


Prototype: int pipe (int fd[2] );

It returns 0 on success, -1 on error.

NOTE:
o fd[0] is set up for reading
o fd[1] is set up for writing

The first integer in the array (element 0) is set up and opened for reading, while the
second integer (element 1) is set up and opened for writing. Visually speaking, the
output of fd1 becomes the input for fd0.

Some of the system calls that are used in piping are:

Open Open or create a file


Read Read from a pipe
Write Write data to a pipe
Close Close/destroy a pipe
Pipe Create a pipe for IPC
Example:

//Parent creates pipe, forks a child, child writes into pipe, and parent reads from pipe

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
main()
{
int pipefd[2], pid, n, rc, nr, status;
char *testString = "Hello, world!\n";
char buf[1024];
rc = pipe (pipefd);
if (rc < 0)
{
perror("pipe");
}
pid = fork ();
if (pid < 0)
{
perror("fork");
}
if (pid == 0)
{ /* Child’s Code */
close(pipefd[0]);
write(pipefd[1], testString, strlen(testString));
close(pipefd[1]);

}
else /* Parent’s Code */
{
close(pipefd[1]);
n = strlen(testString);
nr = read(pipefd[0], buf, n);
rc = write(1, buf, nr);
wait(&status);
printf("End!\n");
}
return(0);
}
LAB TASKS

Task 1:

o Compute the Factorial of a number using IPC (PIPE implementation).

o Parent creates pipe


o Forks a child
o Parent writes into pipe (the number whose factorial is to be calculated,
take the number from the user)
o Child reads from pipe and compute the Factorial of a number written by
Parent

Task 2:

o Using pipes, parent read data from one file, and child write data into another
file.

You might also like