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 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
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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.
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 GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.or