0% found this document useful (0 votes)
31 views

CSE 3320(hw2).pdf

cse 3320 homework 2

Uploaded by

wizard gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

CSE 3320(hw2).pdf

cse 3320 homework 2

Uploaded by

wizard gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CSE 3320 Homework Assignment 2 (Spring 2025)

Due date: 11:59pm 2/11 (Tuesday)


40 (free) points + 10 points X 6 = 100 points
SGG Book (the 10th Edition)

1. Practice Exercises (p154)

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:

- The initial process is P0.


- The first `fork()` call generates process P1.
- The second `fork ()` call results in processes P2 and P3, with P0 creating P2 and
P1 creating P3 since both act as parent processes at this stage.
- The third `fork ()` call produces processes P4, P5, P6, and P7. Specifically, P0
creates P4, P1 creates P6, P2 creates P5, and P3 creates P7.
- Each time `fork ()` is executed, a new child process is spawned, duplicating the
parent process, including its data and code.
We can calculate this using formula as well: total no of process= 2^n(where n is the
number of fork system calls)
There are 3 fork system calls then total no of process are 2^3=8

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)

ANSWER: The child process in LINE X is SIZE=5 (forked by the parent). As a


result, the outcomes are 0, -1, -4, -9, and -16.

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.

You might also like