Open In App

searching in fork()

Improve
Improve
Like Article
Like
Save
Share
Report

In C or C++, the fork() system call is used to create a new process, called the child process, which is an exact copy of the calling process, called the parent process. The child process starts executing from the point where the fork() system call was called.

Searching within a fork() process, depends on how you’ve implemented the search algorithm and the data structure you’re using for storing the data.

For example, if you’re using a linear search algorithm, you can search through the data in the child process by iterating through the data and comparing each element to the search key.

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 – 

CPP




// 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

If you’re using a more advanced search algorithm, such as a binary search, you can split the data into smaller segments and search through each segment in a separate child process. This is known as a divide and conquer approach and it can improve the performance of the search process.

It’s also important to keep in mind that forking a process can be a resource-intensive operation, and it’s important to carefully manage the number of child processes that are created, to avoid overloading the system.

Another thing to keep in mind is that when you’re searching in a fork() process, the child process inherits the same memory space of the parent process. This means that any changes made to the data in the child process will also be visible in the parent process. So, it’s important to make sure that the child process does not modify the data while searching, unless it’s intended.

One way to avoid this issue is to use mmap() system call to create a memory-mapped file and map it to a separate address space for the child process. This way the child process can access the data without modifying it.

In conclusion, searching in a fork() process can be done by using different algorithms and data structures depending on the problem you’re solving, and it’s important to use inter-process communication mechanisms to share the data and results between the parent and child processes. It’s also important to be aware of the potential issues that can arise when using fork() and to manage the resources accordingly.



Last Updated : 25 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads