Searching in fork() in C++

In this tutorial, we will discuss the searching in the fork() in C++. The fork() function in C++ programming language is a system call that creates a new process, called child process, which in turn runs simultaneously with the parent process which calls the fork() system call. No parameters are passed to this function and it returns an integer value depending upon which process is executed first. These values are:

  • Zero: when the creation of the child process was successful and the execution of the child process is done first.
  • A positive value: when the compiler returned back to the parent call and thus parent process is executed first.
  • A negative value: when child process creation was not successful.

The working of the fork() system call can be illustrated using the following example:

#include <stdio.h> 
#include <sys/types.h> 
#include <unistd.h>
void forkeg() 
{ 
    // call child process for execution because returned value is zero 
    if (fork() == 0) 
        printf("fork() system call for the child process!!!!\n"); 
  
    // call parent process for execution because returned value is positive integer value. 
    else
        printf("fork() system call for the parent process!!!\n"); 
} 
int main() 
{ 
    forkeg(); 
    return 0; 
}
The output of the above program is:
fork() system call for the parent process!!!
fork() system call for the child process!!!!
or
fork() system call for the child process!!!! 
fork() system call for the parent process!!!

Explanation:
A child process is created. The fork() system call returns 0 when the child process is called whereas it returns a positive integer value when the parent process is called.
Here, as we can see, two outputs are possible because the returned value of the fork() system call is dependent on which process is first assigned by the CPU.

Now, we have to perform searching in the fork() where we will search for the key element in the parent process and will print the same element in the child process.
For example:

Input :
Key element = 7;
array[5] = {6, 2, 4, 7, 10};

Output: 
Parent process 
7 is present in the given array
Child process 
Number to search in the child process is 7

This can be illustrated well using the following code:

#include <iostream> 
#include <unistd.h> 
using namespace std; 
   
int main() 
{ 
  
    int key = 7; 
    int ret_value = fork(); 
  
    // Checking value of process id returned by fork 
    if (ret_value > 0)  { 
  
        cout << "Parent process \n"; 
  
        int a[] = { 6, 2, 4, 7, 10, 11 }; 
        int arraysize = 6; 
        int temp; 
        int i; 
  
        for (i = 0; i < arraysize; i++) 
        { 
  
            if (a[i] != key) { 
                temp = 0; 
            } 
  
            else  { 
  
                temp = 1; break;
            } 
        } 
  
        if (temp == 1) { 
  
            cout << "7 is present in the given array"<<endl; 
        } 
  
        else { 
  
            cout << "7 is not present in the given array"; 
            cout << "\n"; 
  
        } 
    } 
  
    // If n is 0 i.e. we are in child process 
    else { 
  
        cout << "Child process \n"; 
        cout << "Number to be searched in the child process is "; 
        cout << key; 
    } 
  
    return 0; 
}
The output of the following program is:
Parent process 
7 is present in the given array
Child process 
Number to be searched in the child process is 7

Explanation:
Now the value of the ret_value variable is dependent on which process is assigned by the CPU first.
fork() system call, when returns a positive value then this implies that searching of the key elements will be done in the parent process.
but if it returns 0 then the element to be searched will be printed in the child process.

That’s all for this tutorial.
I hope it helps.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *