Write a program to find sum of even numbers in parent process and sum of odd numbers in child process.
Input : 1, 2, 3, 4, 5 Output : Parent process Sum of even no. is 6 Child process Sum of odd no. is 10
Explanation: Here, we had used fork() function to create two processes one child and one parent process.
- For child process fork() returns 0 so we can calculate sum of all odd numbers in child process.
fork() returns value greater than 0 for parent process so we can calculate sum
- for all even numbers there by just simply checking the value returned by fork().
Parent process Sum of even no. is 30 Child process Sum of odd no. is 25
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 firstname.lastname@example.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.
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Factorial calculation using fork() in C for Linux
- Creating child process using fork() in Python
- Creating multiple process using fork()
- Exit status of a child process in Linux
- fork() and memory shared b/w processes created using it
- fork() to execute processes from bottom to up using wait()
- What all is inherited from parent class in C++?
- fork() and Binary Tree
- C program to demonstrate fork() and pipe()
- Difference between fork() and exec()
- fork() in C
- C vs BASH Fork bomb
- sorting in fork()
- searching in fork()
- Fork() - Practice questions
- Fork CPP | Course Structure
- Inter-process Communication using a shared stack
- How to execute zombie and orphan process in a single program?
- Double forking to prevent Zombie process
- Process control instructions in 8086 microprocessor