0% found this document useful (0 votes)
17 views3 pages

Day 9

The document explains the pipe() system call in C, which facilitates inter-process communication by creating a unidirectional channel between a parent and child process. It details the usage of file descriptors for reading and writing data, along with example code for sending and receiving integers through a pipe. Additionally, it provides practice exercises for implementing various functionalities using pipes in C programs.

Uploaded by

pratikbarainda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Day 9

The document explains the pipe() system call in C, which facilitates inter-process communication by creating a unidirectional channel between a parent and child process. It details the usage of file descriptors for reading and writing data, along with example code for sending and receiving integers through a pipe. Additionally, it provides practice exercises for implementing various functionalities using pipes in C programs.

Uploaded by

pratikbarainda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

pipe() system call:

pipe() is a system call used in C (and other languages) to allow inter-process


communication (IPC) between a parent and its child process. It creates a
unidirectional communication channel, meaning data flows in one direction only —
from one end to another.

When you create a pipe, the kernel gives you two file descriptors:

● fd[0] — for reading from the pipe


● fd[1] — for writing to the pipe

Descriptor pipedes[0] is for reading and pipedes[1] is for writing. Whatever is written
into pipedes[1] can be read from pipedes[0].
To check if a pipe has been successfully created, you can verify the return value
of the pipe() system call. The pipe()function returns 0 if the pipe creation is
successful and -1 if there is an error.

Command to send data via pipe:


write(fd[1], &data, sizeof(data));
Example:

If you want to send an integer value num = 25:

int num = 25;


write(fd[1], &num, sizeof(num));

Command to read data via a pipe in C:


read(fd[0], &data, sizeof(data));

Example:

If you want to receive an integer:

int num;
read(fd[0], &num, sizeof(num));

Write a C program where the parent process sends a number


to the child process using PIPE and the child process prints
the number.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int main() {
int pipefd[2]; // Declare the pipe array: pipefd[0] = read end, pipefd[1] = write end
pid_t pid; // Declare a variable for process ID
int number = 42; // Number that the parent will send to the child

// Create a pipe: if pipe creation fails, return an error


if (pipe(pipefd) == -1) {
perror("Pipe failed"); // Error message if pipe creation fails
return 1; // Return with error code 1
}

// Create a child process using fork()


pid = fork();

// Check if the fork operation fails


if (pid < 0) {
perror("Fork failed"); // Error message if fork fails
return 1; // Return with error code 1
}
else if (pid == 0) { // This block is executed by the child process
// Child process starts here

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

int received_number; // Variable to store the received number from the pipe
read(pipefd[0], &received_number, sizeof(received_number)); // Read the
number from pipe

// Print the received number in the child process


printf("Child Process: Received number = %d\n", received_number);

close(pipefd[0]); // Close the read end of the pipe after reading


}
else { // This block is executed by the parent process
// Parent process starts here
close(pipefd[0]); // Close unused read end of the pipe in the parent process

// Print the number that will be sent to the child


printf("Parent Process: Sending number = %d to child\n", number);

// Write the number to the pipe, so the child can read it


write(pipefd[1], &number, sizeof(number)); // Write the number to the pipe

close(pipefd[1]); // Close the write end of the pipe after writing


}

return 0; // Program ends successfully


}

Practice:

1. Write a C program where the parent process send a number to child


process using PIPE and child process compute whether that number is
a prime number or not.
2. Write a C program to create a child process. Parent process send a
string to child process using PIPE then child process will calculate how
many, alphabet, numeric and special characters (#, &) are there?
3. Write a C program using fork() system call that generates the Fibonacci
sequence in the parent process and prime checking in the child
process.
4. Write a C program to create a child process. Parent process send a
string to child process using PIPE then the child process will check the
string is palindrome or not.
5. Write a C program where parent process send a number to child
process using PIPE and child process compute the Fibonacci series up
to that number.

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