You are on page 1of 8

CSC-341 Assignment #2

1. Using the program shown below, explain what the output will be
at LINE A, explain.

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int value = 5; int
main()
{
pid_t pid; pid = fork(); if (pid == 0)
{ /* child process */ value += 15;
return 0;
}
else if (pid > 0) { /* parent process */
wait(NULL);
printf("PARENT: value = %d",value); /* LINE A */ return 0;
}
}

Answer:

At LINE A, the output will be `PARENT: value = 5`.

1. The program creates a child process using the `fork()` system call. After the fork, the child and parent
processes have separate memory spaces, but they initially share the same value of `value`, which is `5`.

2. In the child process (`pid == 0`), `value` is incremented by 15 (`value += 15`), so `value` becomes `20`.

3. In the parent process (`pid > 0`), it waits for the child process to terminate using the `wait(NULL)`
system call. This ensures that the parent process waits until the child process has finished executing
before continuing.
4. After the child process has finished executing and the parent process resumes execution, the `printf`
statement at LINE A is executed. However, since the parent process has its own copy of `value`, which
was not modified, it prints the original value of `value`, which is `5`.

So, the output of the program will be `PARENT: value = 5`.

1. Process Creation:
• The fork() system call creates a new child process that's a copy of the parent process.
• Both processes share the same code and memory, including the variable value (initialized to 5).
2. Child Process Execution:
• The if (pid == 0) block executes only in the child process.
o It increments value by 15, making value 20 in the child's memory space.
o The child process then terminates (return 0).
3. Parent Process Execution:
• The else if (pid > 0) block executes only in the parent process.
• The wait(NULL) call makes the parent process pause until the child process finishes.
• Critical point: The child's modification of value doesn't affect the parent's memory space. Each
process has its own separate copy of variables.
• When the parent resumes execution, value still holds 5 in its memory.
• LINE A: The parent prints "PARENT: value = 5", reflecting the value in its memory.
Therefore, the output at LINE A will be:
PARENT: value = 5

Key Points:
• Process creation via fork() creates a separate memory space for the child process.
• Changes made to variables in the child process don't affect the parent's variables, and vice versa.
• This concept is crucial for understanding process isolation and communication in operating
systems.
2. Write a C program that forks a child process that ultimately becomes a zombie
process. This zombie process must remain in the system for at least 10 seconds. (25
Points)
Answer:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main() {
pid_t pid;

// Fork a child process


pid = fork();

if (pid < 0) {
// Fork failed
fprintf(stderr, "Fork failed.\n");
return 1;
} else if (pid == 0) {
// Child process
printf("Child process is running.\n");
exit(0); // Child exits immediately, becoming a zombie
} else {
// Parent process
printf("Parent process is running.\n");
sleep(10); // Parent waits for 10 seconds

// After waiting, check if the child process is a zombie


int status;
pid_t terminated_pid = waitpid(pid, &status, WNOHANG);
if (terminated_pid == 0) {
printf("Child process with PID %d is still a zombie.\n", pid);
} else {
printf("Child process with PID %d is no longer a zombie.\n", pid);
}
}

return 0;
}
1. We fork a child process.
2. In the child process, it prints a message and then exits immediately, becoming a zombie.
3. In the parent process, it waits for 10 seconds using the `sleep()` function.
4. After waiting, it checks if the child process is still a zombie using `waitpid()` with the `WNOHANG`
option. If the child process is still a zombie, it prints a message indicating so; otherwise, it prints a
message indicating that the child process is no longer a zombie.

3. Using the program below, 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.) (25
Points)
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

int main()
{
pid_t pid, pid1;
/* fork a child process */ pid = fork();
if (pid lt== 0) { /* error occurred */
fprintf(stderr, "Fork Failed"); return 1;
}
else if (pid == 0) { /* child process */ pid1 =
getpid(); printf("child: pid = %d",pid); /* A */
printf("child: pid1 = %d",pid1); /* B */
}
else { /* parent process */ pid1 = getpid();
printf("parent: pid = %d",pid); /* C */
printf("parent: pid1 = %d",pid1); /* D */
wait(NULL);
}
return 0;
}

Answer:

In the provided program, let's identify the values of `pid` at lines A, B, C, and D.

#include <sys/types.h>

#include <stdio.h>

#include <unistd.h>

int main() {

pid_t pid, pid1;

/* fork a child process */

pid = fork();

if (pid < 0) {

/* error occurred */

fprintf(stderr, "Fork Failed");

return 1;

} else if (pid == 0) {

/* child process */

pid1 = getpid();
printf("child: pid = %d\n", pid); /* A */

printf("child: pid1 = %d\n", pid1); /* B */

} else {

/* parent process */

pid1 = getpid();

printf("parent: pid = %d\n", pid); /* C */

printf("parent: pid1 = %d\n", pid1); /* D */

wait(NULL);

return 0;

Assuming the actual pids of the parent and child are 2600 and 2603,
respectively:

- At line A: `pid` in the child process is `0`.

- At line B: `pid1` in the child process is `2603`.

- At line C: `pid` in the parent process is `2603`.

- At line D: `pid1` in the parent process is `2600`.

These values correspond to the behaviour of the program when the parent and
child processes are executing their respective sections of code.
4. When a process creates a new process using the fork() operation, which of the following
states is shared between the parent process and the child process? Explain. (25Points)

a. Stack
b. Heap
c. Shared memory segments

Answer:
State which is shared between the parent and child process after a fork() operation:
Shared State: Memory Segments
Out of the options, the state that is shared between the parent and child process after a fork() operation
is:
c. Shared memory segments
Explanation:

When a process forks, it creates a copy of its address space. This copy includes most of the process's
memory, including:

• Code segment: Instructions for the program


• Data segment: Global and static variables
However, the following are not shared by default:
• Stack: Each process has its own stack for storing local variables and function call frames. This
prevents processes from interfering with each other's function calls and local data.
• Heap: The heap is a dynamically allocated memory region where processes allocate memory at
runtime. While both processes can potentially allocate memory from the same heap using system
calls like malloc, they don't inherently share specific heap allocations.
Shared Memory Segments: An Explicit Choice
• Shared memory segments are explicitly created regions of memory that processes can agree to
share.
• To use shared memory segments, processes need to use system calls like shmget, shmat, and
shmdt to create, attach to, and detach from the shared memory segment, respectively.
In summary:
• fork() creates a copy of the parent's memory, including shared memory segments.
• The stack and heap are not directly shared, but processes can allocate from the same heap or
explicitly create shared memory segments.

You might also like