Bushramemon - 2075 - 4326 - 1 - Lab 13
Bushramemon - 2075 - 4326 - 1 - Lab 13
Process Management
Objective
To write a program in ‘C’ for understanding Pipe Processing.
Pipe Processing
In computing, a pipeline is a set of data processing elements connected in series, where the output of
one element is the input of the next one. The elements of a pipeline are often executed in parallel or in time-
sliced fashion; in that case, some amount of buffer storage is often inserted between elements.
Pipelining LINX
The pipe () system calls in operating systems is used to create a unidirectional communication channel (pipe)
between two related processes. A pipeline is the original software pipeline: a set of processes chained by their
standard streams, so that the output of each process (stdout) feeds directly as input (stdin) to the next one.
Each connection is implemented by an anonymous pipe. Filter programs are often used in this configuration.
Parent Process:
wait(); is used by the parent process to wait for the child process to complete. It's necessary to
7
prevent the parent from finishing before the child can read from the pipe.
Child Process:
Executed when p is 0.
8
The child process closes the write-end of the pipe with close(fd[1]); as it only needs to read.
It reads the data sent by the parent process into buffer using read(fd[0], buffer, 100);.
write(1, buffer, n); writes the received data to the standard output (console).
Return from Main:
9
return 0; signifies the successful completion of the program.
This C program code for inter-process communication using a pipe. Here's a step-by-step
implementation of how it works:
#include <stdlib.h> //for utility functions such as type conversions, memory allocation,algorithms etc
int main() {
char buffer[100];
// parent
if( p > 0 ){
printf("parent\n");
close(fd[0]);
write(fd[1],"hello child\n",13);
wait();
// child
else{
printf("bbb\n");
close(fd[1]);
printf("%d\n",n);
write(1,buffer,n); }return 0;}
Another way
Here's the basic syntax of creating a pipe in C:
Where:
pipefd is an integer array of size 2, which will hold two file descriptors after the pipe is created:
pipefd[0] - This file descriptor is for reading from the pipe (the read end).
pipefd[1] - This file descriptor is for writing to the pipe (the write end).
Implementationin C:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
if (pipe(fd) == -1) {
exit(EXIT_FAILURE);
if (pid == -1) {
perror("Fork failed");
exit(EXIT_FAILURE);
if (pid == 0) {
// Child process
close(fd[0]); // Close the read end of the pipe
} else {
// Parent process
char buffer[100];
return 0;