Experiment 9
Write a Program on pipes (use functions pipe, popen, pclose), named pipes (FIFOs, accessing
FIFO).
CODE:
Program 1: Using pipe function
This program showcases how a parent process and a child process communicate using the pipe
function:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int pipefds[2]; // Array to store file descriptors for the pipe
pid_t pid;
// Create the pipe
if (pipe(pipefds) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
// Fork a child process
pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
// Parent process
if (pid > 0) {
printf("Parent process writing to pipe...\n");
// Close the read end of the pipe in the parent
close(pipefds[0]);
// Write data to the pipe
const char *data = "Hello from the parent!\n";
if (write(pipefds[1], data, strlen(data)) == -1) {
perror("write");
exit(EXIT_FAILURE);
}
// Close the write end of the pipe in the parent
close(pipefds[1]);
printf("Parent process waiting for child...\n");
wait(NULL); // Wait for the child process to finish
} else { // Child process
printf("Child process reading from pipe...\n");
// Close the write end of the pipe in the child
close(pipefds[1]);
// Read data from the pipe
char buffer[100];
int bytes_read = read(pipefds[0], buffer, sizeof(buffer));
if (bytes_read == -1) {
perror("read");
exit(EXIT_FAILURE);
}
// Print the data received
printf("Child process received: %s", buffer);
// Close the read end of the pipe in the child
close(pipefds[0]);
}
return 0;
}
Program 2: Using popen function
This program demonstrates how to use the popen function to execute a command and capture its
output:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
char buffer[1024];
// Open a pipe to the `ls` command
fp = popen("ls", "r");
if (fp == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
// Read the output of the command line by line
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
printf("%s", buffer);
}
// Close the pipe
pclose(fp);
return 0;
}