In: Computer Science
Based on the below code: Answer the following questions and explain your answers:
i)Presume that the code is instantiated in a process and it has JUST returned from its own call to fork() and there is now a clone of that process that is itself “just about” to pick up execution. Where will the clone child pick up its execution?
ii)How does the clone child know it’s a clone? How does the parent process know it is not a clone, or at least not the clone if just made by calling fork()?
iii)Compile and run the program from the command line. Scroll through the whole history of outputs and describe how that two processes (original and clone) seem to be writing to the same terminal. Do you notice anything that isodd? Explain why you might be seeing this oddness.
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv)
{ pid_t fork_returned_pid;
int count;
fork_returned_pid = fork();
if (fork_returned_pid < 0)
{ printf("The fork() didn't work! Terminate\n");
exit (0); }
for (count = 0; count < 1000; count++)
{ if (fork_returned_pid == 0)
printf(" child says: count = %d\n", count);
else
printf("parent says: count = %d\n", count);
}
}
Please find the properly indented code which will help to understand the program flow.
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv)
{
pid_t fork_returned_pid;
int count;
fork_returned_pid = fork();
if (fork_returned_pid < 0)
{
printf("The fork() didn't work! Terminate\n");
exit (0);
}
for (count = 0; count < 1000; count++)
{
if (fork_returned_pid == 0)
printf(" child says: count = %d\n", count);
else
printf("parent says: count = %d\n", count);
}
}
Question 1 :
Child clone will pick up its execution after the fork process.
In our example, child process will start execution from line 4 of
int main
function.
Question 2 :
Below line is crucial to understand the program.
fork_returned_pid = fork();
When we run the program, it runs in main_thread ( let's assume pid = 5 ). When the above line is executed, a new thread is created. Let's call it new_thread ( let's assume pid = 6750 ).
The fork returns the pid of the child in the parent process, and return 0 in the child process
According to the statement above,
In Main Thread
fork_returned_pid = fork() // In main_thread , after execution of this line, fork_returned_pid = 6750
In Child Thread
fork_returned_pid = fork() // In child_thread , after execution of this line, fork_returned_pid = 0
From that line, both the threads will execute indpendently.
Question 3
:
As this is multi-threading program, different compilers may show
different output. Even on single compiler running the program
multiple times may show different output. Why ? Because, Understand
the below steps
Let's say CPU started executing main_thread.
CPU created new_thread.
CPU executed few lines from main_thread.
CPU may switch to new_thread. and execute few more statements in
the new_thread.
Then again CPU may switch back to main_thread. and this goes on
untill both of those are properly executed.
You as a User can't predict how many statements will be executed by
CPU if it is inside main_thread or if it is inside
new_thread.
Why Such Odd behaviour ? Why CPU don't guarantee how many
statements will be executed ?
Because Multi-threading is not meant to guarantee order of execution among process. Multi threading is meant to be used to run two independelty programs. THe focus is to complete the both programs, not the order in which program gets executed. We need multi threading so CPU doesn't sit Ideal. If we decide the CPU should run first thread for x amount of lines, it clearly voids the advantage of multithreading.
How to run and analyse the programs
// Save the above code as first.c
// Compile the given file on gcc compiler. Successful execution of this command creates object file named 'first'
gcc first.c
// Run the object file created in above step.
gcc -o first first.c
Try to run this object file multiple times, you may see different output everytime you run.
Screenshots of program at different instance of time :
In Below screenshot we can see, both the threads are switched by CPU after execution of one Single statement. So we get output from parent and child alternatively.
In Below Screenshot you can see only child is getting executed.
In Below screenshot you can see only parent is getting executed.
So order totally depends upon CPU which can't be predicted.