Friday, March 11, 2016
Operating System: Creating Child Process and Tracing Output
March 11, 2016
compile and run from terminal
,
computer science
,
Creating Child Process
,
operating system
,
process creation linux
,
process fork tracing
,
system call
The fork system call returns a value which is 0 in child and equal to the child's process identifier or PID in parent. Using this data we can identify which one is the child process and which one is the parent.
No need to use code blocks to run this. Make sure gcc or g++ is installed. If it is not installed then use the steps below to install it in linux mint. For ubuntu check this and for any other system there are there are tutorials on google. Now save the code above in a file named forktest. It can be named anything, but to make it easy to follow this tutorial use the name forktest. Now either open the terminal in the folder by right click or cd to that directory from terminal. Next type this code in the terminal to create the executable, gcc -o forktest forktest.c Finally, type this code in terminal to run it and get the output, ./forktest
As it can be seen below the output executable name can be changed to something else.
Parent has the PID of 2859. After forking we create a child process for which PID is increased by 1. Now fork return 2860 for parent which is equal to the child PID. Next it tries to check if PID returned is 0. But it is not so it goes printf with label 8 and prints the statement. Next it gets to wait and now it is waiting for child to exit. Again for the child with PID 2860 the PID returned is 0. So it now has PID equal to 0. So it enter the outermost if condition. Now there is another fork. We can think of the current child process a parent for the next process after calling fork. So the same procedure follows as above. Lastly when exiting the child processes exit in the reverse order of their start.
Cpp Program Code:
Running the Program:
No need to use code blocks to run this. Make sure gcc or g++ is installed. If it is not installed then use the steps below to install it in linux mint. For ubuntu check this and for any other system there are there are tutorials on google. Now save the code above in a file named forktest. It can be named anything, but to make it easy to follow this tutorial use the name forktest. Now either open the terminal in the folder by right click or cd to that directory from terminal. Next type this code in the terminal to create the executable, gcc -o forktest forktest.c Finally, type this code in terminal to run it and get the output, ./forktest
Screenshot:
As it can be seen below the output executable name can be changed to something else.
Output:
1.Process PID: 2859 2.fork returned: 2860 8.Process waiting with PID: 2859 2.fork returned: 0 3.Process PID: 2860 4.fork returned: 2861 6.Process waiting with PID: 2860 4.fork returned: 0 5.Process exiting with PID: 2861 7.Process exiting with PID: 2860 9.Process exiting with PID: 2859
Explanation:
Parent has the PID of 2859. After forking we create a child process for which PID is increased by 1. Now fork return 2860 for parent which is equal to the child PID. Next it tries to check if PID returned is 0. But it is not so it goes printf with label 8 and prints the statement. Next it gets to wait and now it is waiting for child to exit. Again for the child with PID 2860 the PID returned is 0. So it now has PID equal to 0. So it enter the outermost if condition. Now there is another fork. We can think of the current child process a parent for the next process after calling fork. So the same procedure follows as above. Lastly when exiting the child processes exit in the reverse order of their start.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment