You are on page 1of 19

PIPES() Functions

Pipes()
• A pipe is a connection between two processes, such that the standard
output from one process becomes the standard input of the other
process. In UNIX Operating System,
• Pipes are useful for communication between related processes(inter-
process communication)
“virtual file”.

• 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.
Pipe()
Pipe Reading and writing
• pipe() function creates a unidirectional pipe for IPC.
• On success it return two file descriptors pipefd[0] and pipefd[1].
pipefd[0] is the reading end of the pipe.
• So, the process which will receive the data should use this file
descriptor.
• pipefd[1] is the writing end of the pipe.
• So, the process that wants to send the data should use this file
descriptor.
1.1 Child write and parent receive
#include <unistd.h>
int main()
{
int pid, pip[2];
char instring[20];
pipe(pip);
pid = fork();
if (pid == 0) /* child : sends message to parent*/
{
/* send 7 characters in the string, including end-of-string */
write(pip[1], "Hi Mom!", 7);
}
else
/* parent : receives message from child */
{
/* read from the pipe */
read(pip[0], instring, 7);
}
}
Example:
• // C program to illustrate
• // pipe system call in C
• #include <stdio.h>
• #include <unistd.h>
• #define MSGSIZE 16
• char* msg1 = "hello, world #1";
• char* msg2 = "hello, world #2";
• char* msg3 = "hello, world #3";
• int main()
•{
• char inbuf[MSGSIZE];
• int p[2], i;

• if (pipe(p) < 0)
• exit(1);

• /* continued */

• /* write pipe */

• write(p[1], msg1, MSGSIZE);


• write(p[1], msg2, MSGSIZE);
• write(p[1], msg3, MSGSIZE);


• for (i = 0; i < 3; i++) {
• /* read pipe */
• read(p[0], inbuf, MSGSIZE);
• printf("% s\n", inbuf);
• }
• return 0;
•}
2. Example
• The program below creates a child process. The parent process will
establish a pipe and will send the data to the child using writing end of
the pipe and the child will receive that data and print on the screen
using the reading end of the pipe

• /Q. Program to send a message from parent process to child process


using pipe()
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
int fd[2],n;
char buffer[100];
pid_t p;
pipe(fd); //creates a unidirectional pipe with two end fd[0] and fd[1]
p=fork();
• if(p>0) //parent
{
printf("Parent Passing value to child\n");
write(fd[1],"hello\n",6); //fd[1] is the write end of the pipe
\\wait();
}
else // child
{
printf("Child printing received value\n");
n=read(fd[0],buffer,100); //fd[0] is the read end of the pipe
write(1,buffer,n);
}
}
Difference b/w process & thread
S.N. Process Thread

1 Process is heavy weight or resource Thread is light weight, taking lesser resources
intensive. than a process.

2 Context switching is slow Context switching is fast

3 In multiple processing environments, each All threads can share same set of open files,
process executes the same code but has child processes.
its own memory and file resources.
Process thread
4 If one process is blocked, then other process wil not While one thread is blocked and waiting, a second thread in
block. the same task can run.

5 Multiple processes without using threads use more Multiple threaded processes use fewer resources.
resources.

6 In multiple processes each process operates One thread can read, write or change another thread's
independently of the others. data.
Process:

You might also like