fork
- The fork function is used to start a new process. This kid process is an exact replica of the parent process. On Unix-like operating systems, it is the main technique for creating new processes.
- A new process known as a "child process" is created with the fork system function and runs simultaneously with the process that invoked fork (parent process). Both processes will carry out the next instruction after the fork system call once a new child process has been started. The same CPU registers, program counter, and open files that the parent process utilizes are used by the child process.
- It returns an integer value and requires no arguments. Various values returned by fork are listed below .
- A pipe can be used so that one process writes to it and another process reads from it. This is known as one-way communication. It begins by opening a pipe, a portion of main memory that functions as a "virtual file."
- The producing process, along with all of its offspring, may read from and write on the pipe. This "virtual file" or pipe can be written to by one process, and read from by another process that is connected to it.
- A process is halted until anything is written to the pipe if it tries to read before anything has been written.
- The read and write endpoints of the pipe are given the first two open places that the pipe system call discovers in the process' open file table.
pipe
C Program to Demonstrate fork and pipe:
Create two tasks, Task1 and Task2, within a C program. Task1 passes a string to Task2 upon receiving it. Task2 concatenates the received string with another string without using any string function and returns the resulting string to Task1 for display.
Example:
Other string is: logic practice.com
Input : www.Javat
Output : www.logic practice.com
Input : www.tutorialandexamples.com
Output : tutorialandexamples.logic practice.com
// 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);
}
}
Explanation:
We use a fork to spawn a child process . The fork function provides the following outcomes: 0 indicates failure to create a new child process, 0 for the child process itself, and >0 for the process ID of the child process to the parent process. The parent process will execute when the process ID is greater than 0.
Data can be transferred between processes by utilizing the pipe function. As pipe is one-way, a pair of pipes can be established for bidirectional communication, enabling information exchange in both directions between the processes.
Within the Parent Process: Initially, the reading end of the first pipe (fd1[0]) is closed followed by the transmission of the string through the writing end of the pipe (fd1[1]). Subsequently, the parent process enters a wait state until the child process finishes execution. Upon completion of the child process, the parent closes the writing end of the second pipe (fd2[1]) and proceeds to read the string from the reading end of the pipe (fd2[0]).
In the spawned process, it reads the initial string provided by the parent process by closing the writing side of the pipe (fd1[1]), merges the two strings, transmits the resulting string to the parent process through the fd2 pipe, and finally terminates.