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;
}
OutputParent 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.
Similar Reads
sorting in fork()
Prerequisite â Introduction of fork(), sorting algorithms Problem statement â Write a program to sort the numbers in parent process and print the unsorted numbers in child process. For example : Input : 5, 2, 3, 1, 4 Output : Parent process sorted numbers are 1, 2, 3, 4, 5 Child process numbers to s
2 min read
C++ Program For Linear Search
Linear search algorithm is the simplest searching algorithm that is used to find an element in the given collection. It simply compares the element to find with each element in the collection one by one till the matching element is found or there are no elements left to compare.In this article, we w
4 min read
Binary Search Tree in C++
A Binary Search Tree (BST) is a type of binary tree in which the data is organized and stored in a sorted order. Unlike, a binary tree that doesn't follow a specific order for node placement, in a binary search tree all the elements on the left side of a node are smaller than the node itself, and el
10 min read
C++ Program For Searching An Element In A Linked List
Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise. bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then functi
4 min read
Multiple calculations in 4 processes using fork()
Write a program to create 4 processes: parent process and its child process which perform various tasks : Parent process count the frequency of a number1st child sort the array2nd child find total even number(s) in a given array3rd child calculate the sum of even numbers in an arrayExample - Input :
8 min read
String C/C++ Programs
C program to swap two StringsC Program to Sort an array of names or stringsC Program to Check if a Given String is PalindromeC/C++ Program for Return maximum occurring character in the input stringC/C++ Program for Remove all duplicates from the input string.C/C++ Program for Print all the duplicate
3 min read
How to Read Binary Search Tree from File in C++?
A binary search tree is a hierarchical data structure in which for every node in the tree, the value of all nodes in the left subtree is less than the node's value and the value of all nodes in the right subtree is greater than the node's value. This property of the binary search tree makes it effic
4 min read
Program for Deadlock Free Condition in Operating System
Deadlock occurs when two or more processes are stuck, each holding a resource while waiting for another, creating a cyclic dependency. This halts system progress indefinitely. Deadlocks occur when all four conditions are present which are Mutual Exclusion, Hold and Wait, No Preemption and Circular W
6 min read
C++ Program for BFS Traversal
In C++, breadth First Search (BFS) is a method used to navigate through tree or graph data structures. It begins at the starting point or any chosen node, within the structure. Examines the neighboring nodes at the current level before progressing to nodes, at deeper levels.. In this article, we wil
6 min read
C++ Program For Detecting Loop In A Linked List
Given a linked list, check if the linked list has loop or not. Below diagram shows a linked list with a loop. The following are different ways of doing this. Solution 1: Hashing Approach: Traverse the list one by one and keep putting the node addresses in a Hash Table. At any point, if NULL is reach
11 min read