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

Last Updated :

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
Comment
Next Article
Mithlesh Upadhyay Published 07 May, 2023 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • Find the Nth Triacontakaihenagonal Number

    Given a number N, the task is to find Nth triacontakaihenagonal number. A triacontakaihenagonal number is class of figurate number. It has 31 – sided polygon called triacontakaihenagon.…

    1 min read
  • Check if a Number is a Tetracontagon Number

    Given an integer N, the task is to check if it is a Tetracontagon number or not. Tetracontagon number is class of figurate number. It has 40 –…

    2 min read
  • Find max rational number in given Array

    Given the Numerator and Denominator of N Rational Numbers, the task is to find the maximum Rational Number from the array. Examples: Input: N = 2 num[] =…

    2 min read
  • Check if a Number is Pentacontagon

    Given an integer N, the task is to check if it is a pentacontagon number or not. Pentacontagon number is class of figurate number. It has 50 –…

    2 min read
  • Check if a Number is Triacontakaidigon

    Given an integer N, the task is to check if it is a Triacontakaidigon number or not. Triacontakaidigon number is class of figurate number. It has 32 –…

    2 min read
  • Check if a Number is Hexacontagon

    Given an integer N, the task is to check if it is a hexacontagon number or not. Hexacontagon number is class of figurate number. It has 60 –…

    2 min read