Skip to content
Related Articles

Related Articles

Improve Article

searching in fork()

  • Difficulty Level : Easy
  • Last Updated : 05 Dec, 2017

Prerequisite – fork() in C, sorting in fork()
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 = 10;
array[5] = {3, 8, 4, 10, 80};

Output: 
Parent process 
key is  present in array
Child process 
numbers to be search is 10

Explanation – Here, we had used fork( ) function to create two processes one child and one parent process.

  • fork() returns value greater than 0 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, it is not necessary that child process will execute first or parent process will be first allotted CPU, any process may get CPU assigned, at some quantum time. Moreover process id may differ during different executions.



Code –




// C++ program to demonstrate searching
// in parent and printing result
// in child processes using fork()
#include <iostream>
#include <unistd.h>
using namespace std;
  
// Driver code
int main()
{
  
    int key = 10;
    int id = fork();
  
    // Checking value of process id returned by fork
    if (id > 0)  {
  
        cout << "Parent process \n";
  
        int a[] = { 3, 8, 4, 10, 80 };
        int n = 5;
        int flag;
        int i;
  
        for (i = 0; i < n; i++)
        {
  
            if (a[i] != key) {
                flag = 0;
            }
  
            else  {
  
                flag = 1;
            }
        }
  
        if (flag == 1) {
  
            cout << "key is not present in array";
        }
  
        else {
  
            cout << "key is present in array";
            cout << "\n";
  
        }
    }
  
    // If n is 0 i.e. we are in child process
    else {
  
        cout << "Child process \n";
        cout << "numbers to be search is ";
        cout << key;
    }
  
    return 0;
}

Output –

Parent process 
key is  present in array
Child process 
numbers to be search is 10

This article is contributed by Pushpanjali Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :