In an operating system which uses firstcome firstservice CPU
In an operating system which uses first-come first-service CPU scheduling algorithm and assigns process ids sequentially, the following C++ program compiles and runs successfully without any error. Suppose that the process and its child processes are the only processes running in the system, where the starting process\' id is 100, please answer the following questions.
#include <sys/types.h>
#include <unistd.h>
#include <iostream.h>
int main (void)
{
for (int i = 0; i < 3; i++) {
cout << \"A message line with i = \" << i << endl;
fork();
}
return 0;
}
a) How many lines of \"A message line ...\" (generated by the cout statement) does the program output?
b) How many processes does the program generate including the initial starting process?
c) Would the program generate the same message sequence every time it runs? How about the case that we do NOT know the CPU scheduling algorithm? Justify your answer.
Solution
Note that fork() will create a child process with a copy of exact storage as it has for its parent. And once a copy has been created, 2 processes will run individually.
a) How many lines of \"A message line ...\" (generated by the cout statement) does the program output?
1. Initially, at i = 0, the statement has been printed with i = 0, and a copy of fork will be created with an i value of 0, but continues without printing i value of 0 again.
2. Now two processes will continue from here, i = 1, after incrementing, and 1 is less than 3, and will print 2 i = 1s. and 2 processes will create 2 more copies with fork()s and will be created with an i value of 1, but continues without printing i value of 1 again.
3. Now 4 processes will continue from here i = 2, after incrementing, and 2 is less than 3, and will print 4 i = 2s. and 4 processes will create 4 more copies with fork()s and will be created with i value of 2, but continues without printing i value of 2 again.
4. Now 8 processes will continue from here i = 3, after incrementing, and will come to an end, as 3 is not less than 3. And all 8 processes will come to an end.
So, the number of lines generated on output screen will be: 1 + 2 + 4 = 7.
b. How many processes does the program generate including the initial starting process?
As explained in the first question, the number of processes generated will be:
Initially, 1 process.
When i = 0, 1 more process will be generated. so total 2 processes.
When i = 1, 2 more processes will be generated. so total 4 processes.
When i = 2, 4 more processes will be generated. So total 8 processes.
When i = 3, 8 more processes will be generated. So total 16 processes.
Therefore, the total number of processes generated is 16.
c) Would the program generate the same message sequence every time it runs? How about the case that we do NOT know the CPU scheduling algorithm? Justify your answer.
Yes. It will generate the same message sequence every time it runs. The reason you specified the system is using a first-come-first-serve algorithm. And if the CPU algorithm is not known, it may generate a different sequence.


