Write a program to create 4 processes: parent process and its child process which perform various tasks :
- Parent process count the frequency of a number
- 1st child sort the array
- 2nd child find total even number(s) in a given array
- 3rd child calculate the sum of even numbers in an array
Example –
Input : 2, 4, 6, 7, 9, 0, 1, 5, 8, 3 Output : Parent process : the key to be searched is 7 the frequency of 7 is 1 1st child process : the sorted array is 0 1 2 3 4 5 6 7 8 9 2nd child process : Total even no are: 5 3rd child process : the sum is :45
Explanation – Here, we had used fork() function to create 4 processes three child and one parent process. So, here we use two fork() function which create 4 process n1=fork() and n2 = fork()
- if n1 and n2 is greater than zero then it is parent process which counts the frequency of a number.
- if n1 is equal to zero and n2 is greater than zero then it is 1st child process which sorts the given array.
- if n1 is greater than zero and n2 is equal to zero than it is 2nd child process which finds the total even numbers in the array.
- if n1 and n2 both equal to zero then it is 3rd child calculates the sum of all elements in an array.
Code –
// C++ code to demonstrate the calculation // in parent and its 3 child processes using fork() #include <iostream> #include <unistd.h> using namespace std; int main() { int a[10] = { 2, 4, 6, 7, 9, 0, 1, 5, 8, 3 }; int n1, n2, i, j, key, c, temp; n1 = fork(); n2 = fork(); // if n1 is greater than zero // and n2 is greater than zero // then parent process executes if (n1 > 0 && n2 > 0) { int c = 0; cout << "Parent process :" << endl; // key to be searched is 7 key = 7; cout << "the key to be searched is " << key << endl; for (i = 0; i < 10; i++) { if (a[i] == key) // frequency of key c++; } cout << "the frequency of " << key << " is " << c << endl; } // else if n1 is zero // and n2 is greater than zero // then 1st child process executes else if (n1 == 0 && n2 > 0) { cout << "1st child process :" << endl; for (i = 0; i < 10; i++) { for (j = 0; j < 9; j++) { if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } cout << "the sorted array is" << endl; for (i = 0; i < 10; i++) { cout << a[i] << " " ; } cout << endl; } // else if n1 is greater than zero // and n2 is zero // then 2nd child process executes else if (n1 > 0 && n2 == 0) { int f = 0; cout << "2nd child process :" << endl; for (i = 0; i < 10; i++) { // counting total even numbers if (a[i] % 2 == 0) { f++; } } cout << " Total even no are: " << f << " " ; cout << endl; } // else if n1 is zero // and n2 is zero // then 3rd child process executes else if (n1 == 0 && n2 == 0) { cout << "3rd child process :" << endl; int sum = 0; // summing all given keys for (i = 0; i < 10; i++) { sum = sum + a[i]; } cout << "the sum is :" << sum << endl; } return 0; } |
Output –
Parent process : the key to be searched is 7 the frequency of 7 is 1 1st child process : the sorted array is 0 1 2 3 4 5 6 7 8 9 2nd child process : Total even no are: 5 3rd child process : the sum is :45
This article is contributed by Shivani Baghel. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen 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.or
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!