Input : 5, 2, 3, 1, 4 Output : Parent process sorted numbers are 1, 2, 3, 4, 5 Child process numbers to sort are 5, 2, 3, 1, 4
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 sorting operation.
- for child process fork() returns 0 so we can perform the printing operation.
- Here we are using a simple sorting algorithm to sort the numbers in the desired order.
- 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.
Output : Parent process sorted numbers are 1 2 3 4 5 6 7 8 9 10 Child process numbers to be sorted are 1 6 3 4 9 2 7 5 8 10
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 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- fork() in C
- Fork CPP | Course Structure
- searching in fork()
- Fork() - Practice questions
- C vs BASH Fork bomb
- Difference between fork() and exec()
- fork() and Binary Tree
- C program to demonstrate fork() and pipe()
- Factorial calculation using fork() in C for Linux
- Creating multiple process using fork()
- Multiple calculations in 4 processes using fork()
- fork() to execute processes from bottom to up using wait()
- Creating child process using fork() in Python
- fork() and memory shared b/w processes created using it
- Calculation in parent and child process using fork()
- Sorting a Map by value in C++ STL
- Sorting a vector in C++
- C++ Program for Topological Sorting
- Sorting 2D Vector in C++ | Set 1 (By row and column)
- Structure Sorting (By Multiple Rules) in C++
Improved By : AyushSaxena