0% found this document useful (0 votes)
48 views5 pages

Bushramemon - 2075 - 4326 - 1 - Lab 13

This C program code demonstrates process communication using pipes. It creates a pipe, forks a child process, and has the parent write a message to the child through the pipe. The child then reads the message from the pipe and writes it to standard output. Key steps include creating the pipe file descriptors, forking the child process, having the parent write and child read with the appropriate file descriptor, and closing the unused file descriptors.

Uploaded by

Sahar Soomro
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)
48 views5 pages

Bushramemon - 2075 - 4326 - 1 - Lab 13

This C program code demonstrates process communication using pipes. It creates a pipe, forks a child process, and has the parent write a message to the child through the pipe. The child then reads the message from the pipe and writes it to standard output. Key steps include creating the pipe file descriptors, forking the child process, having the parent write and child read with the appropriate file descriptor, and closing the unused file descriptors.

Uploaded by

Sahar Soomro
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

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 and child sharing a pipe


When we use fork in any process, file descriptors remain open across child process and also parent process. If
we call fork after creating a pipe, then the parent and child can communicate via the pipe.
Procedure for doing the experiment – Step by Step
Step Number Details
Start the program
Include Necessary Libraries:
<stdio.h> is included for standard input/output functions like printf and scanf.
1
<stdlib.h> provides utility functions such as type conversions and memory allocation.
<unistd.h> is required for POSIX operating system API, including read and write system calls.

2 Define main Function: This is where the program execution starts.

Create File Descriptors for Pipe:

int fd[2]; declares an array of two file descriptors.


3 fd[1] is used to write to the pipe and fd[0] to read from it.
pipe(fd); creates a pipe. Pipes are a form of IPC (Inter-Process Communication) that allows data
to be transferred from one process to another.

Buffer Declaration and Process ID:


4
A buffer char buffer[100]; is declared to store the data read from the pipe.
pid_t p; declares a variable to store the process ID.
Forking to Create Child Process:

p = fork(); creates a child process.


5
returns twice: once in the parent process, where it returns the child's PID, and once in the child
process, where it returns 0.

Parent Process:

6 The if (p > 0) block is executed by the parent process.


close(fd[0]); closes the read-end of the pipe since the parent will only write.
The parent sends a message to the child using write(fd[1], "Hello child\n", 13);.

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 <stdio.h> // for Input/Output function i.e. printf,scanf

#include <stdlib.h> //for utility functions such as type conversions, memory allocation,algorithms etc

#include <unistd.h> //for read and write system calls

int main() {

int fd[2]; //File descriptor // fd[1] to write, fd[0] to read

pipe(fd); //creating a pipe

char buffer[100];

pid_t p; //Process ID is been declared

p = fork(); // Calling fork process to crete child

// parent

if( p > 0 ){

printf("parent\n");

close(fd[0]);

printf("passing value to child");

write(fd[1],"hello child\n",13);

wait();

// child

else{

printf("bbb\n");

close(fd[1]);

int n; // number of character

n = read(fd[0],buffer,100); // return max number of byte to read

printf("%d\n",n);
write(1,buffer,n); }return 0;}

Another way
Here's the basic syntax of creating a pipe in C:

int pipe(int pipefd[2]);

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() {

int fd[2]; // File descriptors for the pipe

if (pipe(fd) == -1) {

perror("Pipe creation failed");

exit(EXIT_FAILURE);

// Fork a child process

pid_t pid = fork();

if (pid == -1) {

perror("Fork failed");

exit(EXIT_FAILURE);

if (pid == 0) {

// Child process
close(fd[0]); // Close the read end of the pipe

char message[] = "Hello from the child process!\n";

write(fd[1], message, sizeof(message));

close(fd[1]); // Close the write end of the pipe in the child

} else {

// Parent process

close(fd[1]); // Close the write end of the pipe

char buffer[100];

read(fd[0], buffer, sizeof(buffer));

printf("Parent received: %s", buffer);

close(fd[0]); // Close the read end of the pipe in the parent

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy