Searching in parent and printing result in child processes using fork()

By | May 7, 2023

In C/C++, the fork() function is used to generate a new process known as the child process. We can do searching within a fork() process.

Problem statement – Write a program to search the key element in parent process and print the key to be searched in child process.

Examples:

Input :
Key=7;
array[5] = { 2, 5, 4, 7, 90 };

Output: 
Parent process 
Key is present in the array.
Child process 
Number to be searched is 7

Explanation – Here, we had used fork( ) function to create a new process. Through one process call this fork() function, two processes will be returned from the system call. The Process which call fork() is called the parent process and the newly spawned process is called the child process.The fork() is the only system call which returns two return values.

  • fork() returns value greater than 0 (i.e. PID(Process ID) of child process) for parent process so we can perform the searching operation.
  • for child process fork() returns 0 so we can perform the print the value of key to be searched.
  • Here we are using a simple searching algorithm to search the key element in the given array.
  • We are using the returned values of fork() to know which process is a child or which is a parent process.

Note – At some instance of time, We cant predict that which process first gets the chance to execute i.e. It can be parent process or it can be child process. Its the matter of processor allotment which is dependent upon the scheduler and the scheduler implemented algorithm that can be FCFS, Round Robin Algorithm etc. Any process may get CPU assigned, at some quantum time. Moreover process id may differ during different executions.

Implementation:

// C++ program to demonstrate searching
// in parent and printing result
// in child processes using fork()
#include <iostream>
#include <unistd.h>

using namespace std;

int main()
{
int key = 7;
int id = fork();
if (id > 0) {
    // Parent process

    cout << "Parent process \n";

    int a[] = { 2, 5, 4, 7, 90 };
    int n = 5;
    bool flag = false;
    int i;

    for (i = 0; i < n; i++) {
        if (a[i] == key) {
            flag = true;
            break;
        }
    }

    if (flag) {
        cout << "Key is present in the array.\n";
    } else {
        cout << "Key is not present in the array.\n";
    }
} else {
    // Child process

    cout << "Child process \n";
    cout << "Number to be searched is " << key << endl;
}

return 0;
}

Output:

Parent process 
Key is present in the array.
Child process 
Number to be searched is 7
Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.