CSE 3320(hw2).pdf
CSE 3320(hw2).pdf
3.1 Using the program shown in Figure 3.30, explain what the
output will be at LINE A. (10 points)
Answer: The process that invokes the fork () function is referred to as the Parent
process, and the new process created by a fork system call is referred to as the
Child process. Following the creation of the child process, the fork () system call will
be executed by the parent and child processes. The program counter, CPU registers,
and files opened in the parent process are shared by the new process.
Before it is formed, the child process will have the variable "value" set to 5
according to the code above. After adding 15 to it later, it returns 0 to exit. However,
the parent process has no idea what is going on within the kid process. Because of
the wait (NULL) statement, it only waits until the child process has completed its
execution. The parent process has not modified the value of variables "value", so its
value will be "5".
Therefore, the output will be PARENT: value = 5
3.2 Including the initial parent process, how many processes are
created by the program shown in Figure 3.31? (10 points)
Answer:
Chapter 3 Exercises
3.11 Including the initial parent process, how many processes are
created by the program shown in Figure 3.21? (10 points)
ANSWER: The first of this parent's four children will be created by the initial fork()
call, and each of the first children will have three more fork() calls left. This implies
that each will have three offspring, and that each of those three will have two
children, and so on.
3.12 Explain the circumstances under which the line of code
marked printf("LINE J") in Figure 3.22 will be reached. (10 points)
ANSWER: As we know, the return value of the parent process is the child process's
pid and the return value of the child process is zero when the fork function is
successfully executed. Accordingly, the child process only passes the second
judgment and calls "execlp" once the function fork terminates successfully. The code
printf (''LINE J'') will never be reached if the fork fails.
The process begins running a new program and never returns because the exec()
function substitutes the program defined by its inputs for the process's address
space. Printf("Line J") will never be executed if exec() is successfully executed; if it
is unsuccessful, the function returns, and printf("Line J") will be executed.
In other words, printf("Line J") will be run if fork succeeds and execlp fails!
3.13 Using the program in Figure 3.23, identify the values of pid
at lines A, B, C, and D. (Assume that the actual pids of the parent
and child are 2600 and 2603, respectively.) (10 points)
ANSWER: The parent process's return value is the child process's pid when the fork
function is successfully called, and the child process's return value is 0.
Therefore, it's the child process in A and B. B outputs the child process's pid, 2603;
A outputs the function fork's return value, 0.
It is the parent process in C and D. C outputs the function fork's return, 2603; D
outputs the parent process's pid, 2600.
A: 0 B: 2603 C: 2603 D: 2600 in a word.
3.16 Using the program shown in Figure 3.24, explain what the output will be at
lines X and Y. (10 points)
The parent process is SIZE=5 in LINE Y. Thus, the outcomes are as follows: 0, 1, 2,
3, 4.
Since the child process is a duplicate of the parent process, any changes made to its
data will only impact on its own copy and not the parent process.