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.
Other string is: forgeeks.org Input : www.geeks Output : www.geeksforgeeks.org Input : www.practice.geeks Output : practice.geeksforgeeks.org
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; pipe(fd); fd; //-> for using read end fd; //-> for using write end
Inside Parent Process : We firstly close the reading end of first pipe (fd1) then write the string though writing end of the pipe (fd1). Now parent will wait until child process is finished. After the child process, parent will close the writing end of second pipe(fd2) and read the string through reading end of pipe (fd2).
Inside Child Process : Child reads the first string sent by parent process by closing the writing end of pipe (fd1) and after reading concatenate both string and passes the string to parent process via fd2 pipe and will exit.
Input : www.geeks Output : Concatenated string www.geeksforgeeks.org
This article is contributed by Kartik Ahuja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Named Pipe or FIFO with example C program
- pipe() System call
- fork() and Binary Tree
- fork() and memory shared b/w processes created using it
- Difference between fork() and exec()
- Calculation in parent and child process using fork()
- Factorial calculation using fork() in C for Linux
- fork() in C
- Fork() Bomb
- C vs BASH Fork bomb
- Creating multiple process using fork()
- sorting in fork()
- searching in fork()
- Create n-child process from same parent process using fork() in C
- Creating child process using fork() in Python
- fork() to execute processes from bottom to up using wait()
- Fork() - Practice questions
- Fork CPP | Course Structure
- C program to detect tokens in a C program
- C program to print a string without any quote (singe or double) in the program