1) 7 processes will be created as each fork() call duplicates the current process. With 3 iterations of fork(), 2^3 = 8 processes are created.
2) The output of Line A will be "PARENT: value = 5" as the child process increments its copy of value to 20 but the parent's value is unchanged.
3) Two ways to avoid duplicating the core image during fork() only to overwrite it during execve() are: 1) Call execve() directly in the parent without forking. 2) Use vfork() instead of fork() which does not duplicate the memory space until execve() is called.
1. National University of Singapore
School of Computing
CS2106 Tutorial 2 Semester 1 10/11
1. Adapted from SGG Problem 3.9
Including the initial parent process, how many processes are created by the following pro-
gram?
int main()
{
for (int i = 0; i < 3; i++)
fork();
}
2. Adapted from SGG Problem 3.13
Explain the output of Line A in the following program.
int value = 5;
int main()
{
pid_t pid;
pid = fork();
if (pid == 0) {
value += 15;
} else if (pid > 0) {
wait(NULL);
printf("PARENT: value = %dn", value); /* LINE A */
}
return 0;
}
3. A common code sequence is to call fork() to create a child process with a copy of the
parent process’s core image, followed by execve() to replace the child’s core image with
the core image of another executable.
You might have noticed the inefficiency here: we spend the effort to duplicate the core
image of the parent process to the child process in fork(), only to have the core image
overwritten by execve().
Suggest two ways where we can avoid this wasted effort.
4. What are the advantages of implementing the Web browser using multiple processes (one
process per tab) versus multiple threads (one thread per tab)?