1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstdli
ID: 3589116 • Letter: 1
Question
1 #include <iostream>
2 #include <string>
3 #include <cstdio>
4 #include <cstdlib>
5 #include <unistd.h>
6
7 using std::cout;
8 using std::endl;
9 using std::string;
10 int main() {
11
12 pid_t pid;
13 println_now("At the start");
14
15 if ((pid = fork()) < 0) {
16 perror("FORK ERROR");
17 }
18
19 for (int i = 0; i < 3; ++i) {
20 cout << "i = " << i << ", "
21 << "my.pid = " << getpid() << ", "
22 << "fork = " << pid << ", "
23 << "ppid = " << getppid() << endl;
24 }
25 return EXIT_SUCCESS;
26 }
a.) (pretty fast, multiprocessor and the processor utilization is minimal. Also assume that the shell's process number that executes your program is 12345, when the program is executed it was given process number 23456 and the forked off child's process number was 34567. What output will you see (be explicit) and why? Give all lines of output.
b.) If you change line 19 to "19. for (int i = 0; i < 200; ++i) { " what output will you see around 200 printouts on the stdout and why? Give at least 5 to 10 lines of sample output.
c.) If we would inject a new line of code at line 11 that would be: " cout.setf(std::ios_base::unitbuf);" and the line 19 is same "for (int i = 0; i < 200; ++i) { " what output will you see around 200 printouts on the stdout? Again, be explicit and give 5-10 lines of what a sample output would look like.
Explanation / Answer
a)
At the start:
i = 0, my.pid = 8668, fork = 8669, ppid = 8329
i = 1, my.pid = 8668, fork = 8669, ppid = 8329
i = 2, my.pid = 8668, fork = 8669, ppid = 8329
i = 0, my.pid = 8669, fork = 0, ppid = 8668
i = 1, my.pid = 8669, fork = 0, ppid = 1
i = 2, my.pid = 8669, fork = 0, ppid = 1
Here in the code we are using fork so it will create child process. Here in linux system parent will exicute first. so and after that child. now first 3 lines are stdout by parent.
(i = 0, my.pid = 8668, fork = 8669, ppid = 8329
i = 1, my.pid = 8668, fork = 8669, ppid = 8329
i = 2, my.pid = 8668, fork = 8669, ppid = 8329)
here my.pid will give parent process id , fork will give child process id and ppid will give shell process id.which will same for three lines .
Now child will execute so that last 3 lines are stdout of child .
(i = 0, my.pid = 8669, fork = 0, ppid = 8668
i = 1, my.pid = 8669, fork = 0, ppid = 1
i = 2, my.pid = 8669, fork = 0, ppid = 1)
in child process fork will give 0 according to fork system call.In 1st line we can see that and pid will child process id and ppid is parent process id.now in at execution time parent process complited so child will become orphan process and it's parent will be init process so init process pid is 1. so child process ppid will also be 1.
b)when we change line 19 is same "for (int i = 0; i < 200; ++i) and exicute it i am displying some part of output here.
i = 0, my.pid = 8725, fork = 8726, ppid = 8329
i = 1, my.pid = 8725, fork = 8726, ppid = 8329
i = 1, my.pid = 8726, fork = 0, ppid = 8725
i = 43, my.pid = 8725, fork = 8726, ppid = 8329
i = 2, my.pid = 8726, fork = 0, ppid = 8725
i = 44, my.pid = 8725, fork = 8726, ppid = 8329
i = 75, my.pid = 8726, fork = 0, ppid = 8725
i = 96, my.pid = 8725, fork = 8726, ppid = 8329
i = 76, my.pid = 8726, fork = 0, ppid = 8725
i = 77, my.pid = 8726, fork = 0, ppid = 8725
i = 78, my.pid = 8726, fork = 0, ppid = 8725
i = 198, my.pid = 8726, fork = 0, ppid = 8725
i = 199, my.pid = 8726, fork = 0, ppid = 8725
- After fork child and parent process performing loop saparately so two process want to get stdout file. So that there will be race condition will happen if parant will get resource first then it will print line or child will do that.And important point is when time slice will be complite for parent or child.It will switch over to other one.where fork have positive value it is parent and where it is 0 it is child line.
c)When we use cout.setf(std::ios_base::unitbuf); it will automatically flush the buffer.
i = 4, my.pid = 8828, fork = 8829, ppid = 8329
i = 5, my.pid = 8828, fork = 8829, ppid = 8329
i = 6, my.pid = 8828, fork = 8829i = , ppid = 8329
i = 7, my.pid = 8828, fork = 8829, ppid = 8329
i = 80, , my.pid = my.pid = 88288829, , fork = fork = 88290, , ppid = ppid = 83298828
i = 9, my.pid = 8828, fork =
8829, ppid = i = 83291
, i = my.pid = 108829, , my.pid = fork = 88280, , fork = ppid = 88298828,
ppid = i = 83292
, i = my.pid = 118829, , my.pid = fork = 88280, , fork = ppid = 88298828,
So output will unpriductable some time parent will put somthing in stdout buffer or some time child so it will flush when it will ful what ever in it that will be print on the screen.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.