![]() |
VOOZH | about |
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]);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.org
Input : www.geeks
Output : www.geeksforgeeks.org
Input : www.practice.geeks
Output : practice.geeksforgeeks.org
Explanation:
Example:
int fd[2];
pipe(fd);
fd[0]; //-> for using read end
fd[1]; //-> for using write end
Inside 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.
Input
www.geeksOutput:
Concatenated string www.geeksforgeeks.org