1.
2.
3.
Assume the initial process ID to be 2025. Draw the process tree when f1 is executed. Show a
possible output sequence.
f1.c f3.c
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <unistd.h> int main()
#include <sys/wait.h> {
printf("Good luck\n");
int main() }
{
int c1 = fork();
if (c1 == 0 || fork())
{
printf("Linux\n");
char *args[2];
args[0] = strdup("./f3");
args[1] = NULL;
execvp(args[0], args);
}
else if (c1 > 0)
{
waitpid(c1, NULL, 0);
printf("Windows\n");
}
else
{
printf("Android\n");
}
}
More Practice Problems:
4. Consider the following C program. Assume there are no syntax errors and the program
executes correctly. Assume the fork system calls succeed. What is the output printed to the
screen when we execute the below program?
void main(argc, argv) {
for(int i = 0; i < 4; i++) {
int ret = fork();
if(ret == 0)
printf("child %d\n", i);
}
}
5. Consider a parent process P that has forked a child process C in the program below.
int a = 5;
int fd = open(...); // open a file
int ret = fork(); // create a child process
if (ret > 0) { // Parent process
close(fd);
a = 6;
...
}
else if (ret == 0) { // Child process
printf("a = %d\n", a);
read(fd, something);
}
After the new process is forked, suppose that the parent process is scheduled first, before the
child process. Once the parent resumes after fork, it closes the file descriptor and changes the
value of a variable as shown above. Assume that the child process is scheduled for the first time
only after the parent completes these two changes.
(a) What is the value of the variable a as printed in the child process, when it is scheduled next?
Explain.
(b) Will the attempt to read from the file descriptor succeed in the child? Explain.
6. Consider the following pseudocode. Assume all system calls succeed and there are no other
errors in the code.
int ret1 = fork(); //fork1
int ret2 = fork();//fork2
int ret3 = fork(); //fork3
wait();
wait();
wait();
Let us call the original parent process in this program as P. Draw/describe a family tree of P and
all its descendents (children, grand children, and so on) that are spawned during the execution
of this program. Your tree should be rooted at P. Show the spawned descendents as nodes in
the tree, and connect processes related by the parent-child relationship with an arrow from
parent to child. Give names containing a number for descendents, where child processes
created by fork ”i” above should have numbers like ”i1”, ”i2”, and so on. For example, child
processes created by fork3 above should have names C31, C32, and so on.
7. Consider the following sample code from a simple shell program.
command = read_from_user();
int rc = fork();
if(rc == 0) { //child
exec(command);
}
else {//parent
wait();
}
Now, suppose the shell wishes to redirect the output of the command not to STDOUT but to a
file “[Link]”. Show how you would modify the above code to achieve this output redirection. You
can indicate your changes next to the code above.
8. What are the outputs of the program shown below?
int x = 3;
void main() {
while (x > 0) {
int ret = fork();
if (ret == 0) {
printf("x = %d\n", x);
}
else {
wait(NULL);
}
x--;
}
}
9. What are the outputs of the program shown below? Note that the program below invokes the
exec system call with itself as the argument. That is, “[Link]” refers to the executable of the
program shown below itself.
int x = 3;
void main() {
while (x > 0) {
int ret = fork();
if (ret == 0) {
printf("x = %d\n", x);
x--;
execl("./[Link]", "[Link]", "", 0);
}
else {
wait(NULL);
}
}
}
10. Consider the program shown below, with two fork system calls. When the first fork runs,
Parent P (PID: 8268) forks C1 (PID: 8269). When the second fork runs, P forks C2 (PID: 8270),
and C1 forks C3 (PID: 8271). The program below prints 4 lines. Write down these 4 lines (which
may be printed in any order) in the space next to the question.
int main() {
int ret1 = fork();
int ret2 = fork();
printf("%d %d\n", ret1, ret2);
wait(NULL);
wait(NULL);
}