BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
DEPARTMENT OF COMPUTER ENGINEERING
SUBJECT: Operating System
Name Devansh Shah
UID no. 2023300203
Experiment 1
AIM : Write a program that creates exactly 16 copies of itself by calling fork ()
only twice within a loop. The program should also print a tree of the pids.
Code: #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
// Function to print process tree
void print_process_tree(pid_t parent, int level) {
for (int i = 0; i < level; i++) {
printf(" "); // Indentation for hierarchy
}
printf("PID: %d, Parent PID: %d\n", getpid(), parent);
fflush(stdout);
}
int main() {
pid_t pid;
int level = 0;
for (int i = 0; i < 2; i++) {
pid = fork();
if (pid < 0) {
perror("Fork failed");
exit(EXIT_FAILURE);
} else if (pid > 0) {
// Parent process
wait(NULL); // Ensure orderly execution
} else {
// Child process
level++; // Increase indentation level
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
DEPARTMENT OF COMPUTER ENGINEERING
SUBJECT: Operating System
}
}
print_process_tree(getppid(), level);
sleep(1); // Allow time for all processes to print
return 0;
}
OUTPUT:
Post Lab Questions
Q1 Write another program using fork(). The child process should print
“hello”; the parent process should print “goodbye”. You should try to
ensure that the child process always prints first; can you do this without
calling wait() in the parent?
Program #include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
pid_t pid = fork();
if (pid < 0) {
// Fork failed
perror("Fork failed");
return 1;
}
else if (pid == 0) {
// Child process
printf("hello\n");
}
else {
// Parent process
sleep(1);
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
DEPARTMENT OF COMPUTER ENGINEERING
SUBJECT: Operating System
printf("goodbye\n");
}
return 0;
}
Result
The answer is yes
Question 2 Write a C program that calls fork(). Before calling fork(), have the main
process access a variable (e.g., x) and set its value to something (e.g., 100).
What value is the variable in the child process? What happens to the
variable when both the child and parent change the value of x?
Program #include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
int x;
printf("Enter a value for x: ");
scanf("%d", &x);
pid_t pid = fork();
if (pid < 0) {
perror("Fork failed");
return 1;
} else if (pid == 0) {
// Child process
printf("Child process: x = %d\n", x);
x += 10; // Modify x in child process
printf("Child process (after change): x = %d\n", x);
} else {
// Parent process
printf("Parent process: x = %d\n", x);
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
DEPARTMENT OF COMPUTER ENGINEERING
SUBJECT: Operating System
x -= 10; // Modify x in parent process
printf("Parent process (after change): x = %d\n", x);
}
return 0;
}
Result The variable in the child process initially has the same value as in the
parent process because fork() creates a separate copy of the parent's
memory space. If the parent sets x = 100 before fork(), the child will also see
x = 100 initially.
When both the child and parent change the value of x, their changes do not
affect each other because they have separate memory spaces. Any
modification made by the child is only reflected in the child process, while
modifications in the parent affect only the parent. For example, if the child
increments x to 110 and the parent decrements x to 90, these changes
remain isolated within their respective processes.
Question 3 Now write a C program that uses wait() to wait for the child process to
finish in the parent.
What does wait() return? What happens if you use wait() in the child?
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid < 0) {
perror("Fork failed");
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
DEPARTMENT OF COMPUTER ENGINEERING
SUBJECT: Operating System
exit(1);
}
if (pid == 0) { // Child process
printf("Child process (PID: %d) is running.\n", getpid());
sleep(2); // Simulate some work in the child process
printf("Child process exiting.\n");
exit(0);
} else { // Parent process
int status;
printf("Parent process (PID: %d) waiting for child (PID: %d) to
finish.\n", getpid(), pid);
wait(&status); // Parent waits for the child to finish
printf("Child process finished. Parent continues execution.\n");
}
return 0;
}
Result
wait() returns the process ID of the terminated child.
If used in the child, wait() will fail since the child has no child processes to
wait for.
CONCLUSION: This experiment demonstrates how the fork() system call creates a
hierarchical process tree structure. By calling fork() within a loop, we
generate multiple processes that each have a unique process ID (PID) and
parent process ID (PPID). The structure shows how process creation
propagates, forming a binary tree with 16 total processes. Proper use of
wait() ensures an orderly execution, avoiding orphaned or zombie
processes.