C Program to Demonstrate fork() and pipe() Last Updated : 10 Jan, 2025 Comments Improve Suggest changes Like Article Like Report fork() is used to create a child process. This child process is a copy of the original(parent) process. It is the primary method of process creation on Unix-like operating systems.( See this article for reference).Syntax:fork(); // It does not take any parameter, it returns // integer values. It may return negative, // positive or zero integer values.pipe(): It is used for inter-process communication in Linux. It is a system function. (See this article for reference)Syntax:int pipe(int pipefd[2]);C program to demonstrate fork() and pipe():Write Linux C program to create two processes P1 and P2. P1 takes a string and passes it to P2. P2 concatenates the received string with another string without using string function and sends it back to P1 for printing.Examples: Other string is: forgeeks.orgInput : www.geeksOutput : www.geeksforgeeks.org Input : www.practice.geeksOutput : practice.geeksforgeeks.orgExplanation: To create child process we use fork(). fork() returns : <0 fail to create child (new) process=0 for child process>0 i.e process ID of the child process to the parent process. When >0 parent process will execute.pipe() is used for passing information from one process to another. pipe() is unidirectional therefore, for two-way communication between processes, two pipes can be set up, one for each direction.Example: int fd[2];pipe(fd);fd[0]; //-> for using read endfd[1]; //-> for using write endInside Parent Process : We firstly close the reading end of first pipe (fd1[0]) then write the string though writing end of the pipe (fd1[1]). Now parent will wait until child process is finished. After the child process, parent will close the writing end of second pipe(fd2[1]) and read the string through reading end of pipe (fd2[0]). Inside Child Process : Child reads the first string sent by parent process by closing the writing end of pipe (fd1[1]) and after reading concatenate both string and passes the string to parent process via fd2 pipe and will exit. Inputwww.geeks C // C program to demonstrate use of fork() and pipe() #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main() { // We use two pipes // First pipe to send input string from parent // Second pipe to send concatenated string from child int fd1[2]; // Used to store two ends of first pipe int fd2[2]; // Used to store two ends of second pipe char fixed_str[] = "forgeeks.org"; char input_str[100]; pid_t p; if (pipe(fd1) == -1) { fprintf(stderr, "Pipe Failed"); return 1; } if (pipe(fd2) == -1) { fprintf(stderr, "Pipe Failed"); return 1; } scanf("%s", input_str); p = fork(); if (p < 0) { fprintf(stderr, "fork Failed"); return 1; } // Parent process else if (p > 0) { char concat_str[100]; close(fd1[0]); // Close reading end of first pipe // Write input string and close writing end of first // pipe. write(fd1[1], input_str, strlen(input_str) + 1); close(fd1[1]); // Wait for child to send a string wait(NULL); close(fd2[1]); // Close writing end of second pipe // Read string from child, print it and close // reading end. read(fd2[0], concat_str, 100); printf("Concatenated string %s\n", concat_str); close(fd2[0]); } // child process else { close(fd1[1]); // Close writing end of first pipe // Read a string using first pipe char concat_str[100]; read(fd1[0], concat_str, 100); // Concatenate a fixed string with it int k = strlen(concat_str); int i; for (i = 0; i < strlen(fixed_str); i++) concat_str[k++] = fixed_str[i]; concat_str[k] = '\0'; // string ends with '\0' // Close both reading ends close(fd1[0]); close(fd2[0]); // Write concatenated string and close writing end write(fd2[1], concat_str, strlen(concat_str) + 1); close(fd2[1]); exit(0); } } Output:Concatenated string www.geeksforgeeks.org Comment More infoAdvertise with us Next Article C Program to Demonstrate fork() and pipe() K Kartik Ahuja Improve Article Tags : C Language C++ system-programming CPP-Library C-Library +1 More Practice Tags : CPP Similar Reads Named Pipe or FIFO with example C program In computing, a named pipe, also known as a FIFO (First In, First Out), is a powerful mechanism for inter-process communication (IPC). Unlike unnamed pipes, which are temporary and exist only as long as the process that created them is running, named pipes provide a persistent communication channel 5 min read Printing source code of a C program itself Printing the source code of a C program itself is different from the Quine problem. Here we need to modify any C program in a way that it prints the whole source code. Recommended: Please try your approach on {IDE} first, before moving on to the solution.ApproachUse predefined macro __FILE__ to get 2 min read C program to copy contents of one file to another file C#include <stdio.h> #include <stdlib.h> // For exit() int main() { FILE *fptr1, *fptr2; char filename[100]; int c; printf("Enter the filename to open for reading: "); scanf("%s", filename); // Open one file for reading fptr1 = fopen(filename, "r"); if (fptr1 == NULL) { printf("Cannot ope 1 min read Implement a Stack in C Programming Stack is the linear data structure that follows the Last in, First Out(LIFO) principle of data insertion and deletion. It means that the element that is inserted last will be the first one to be removed and the element that is inserted first will be removed at last. Think of it as the stack of plate 7 min read C program for pipe in Linux Working and implementation of Pipe in Linux. Prerequisite : Pipe in Linux Approach : Pipe is highly used in Linux. Basically, pipe has 2 parts, one part is for writing and another is used for reading. So, an array of size 2 is taken. a[1] is used for writing and a[0] for reading.After reading from p 1 min read Create Processes with Fork in C++ fork() is a system call that creates a child process from the parent process. Whenever we call fork() from the parent program, a child process is created that has the exact copy of the address space. The important thing to remember is it shares the copy of the address space, not the copy itself. Syn 3 min read Output of C programs | Set 64 (Pointers) Prerequisite : Pointers in C Question 1 : What will be the output of following program? C #include "stdio.h" int main() { char a[] = { 'A', 'B', 'C', 'D' }; char* ppp = &a[0]; *ppp++; // Line 1 printf("%c %c ", *++ppp, --*ppp); // Line 2 } OPTIONS: a)C B b)B A c)B C d)C AOUTPUT: (d) C AExplanati 3 min read Common Memory/Pointer Related bug in C Programs Dereferencing an unknown memory location : C programmers mostly use scanf() function to take input, but sometimes a small mistake can bring a bug or even crash whole program. The syntax for scanf() is scanf("%d", &a);. It might be possible to miss a & and write &a as a so now scanf("%d", 6 min read Output of C Programs | Set 5 Predict the output of below programs Question 1 c int main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } return 0; } Output: Can't be predictedExplanation: The condition in while loop is 1 so at first shot it looks infinite loop. Then there are break and con 5 min read Output of C Programs | Set 3 Predict the output of the below program. Question 1 c #include <stdio.h> int main() { printf("%p", main); getchar(); return 0; } Output: Address of function main. Explanation: Name of the function is actually a pointer variable to the function and prints the address of the function. 3 min read Like