0% found this document useful (0 votes)
13 views7 pages

CS604 Assignment.1

The document contains an assignment for an Operating Systems course, detailing the creation of child processes to compute factorials and demonstrating process management in C. It includes code for both original and modified versions of a program, showcasing the use of fork(), wait(), and execlp() functions. The assignment is intended for Spring 2025 and is associated with a student ID and name.

Uploaded by

duanoor227
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)
13 views7 pages

CS604 Assignment.1

The document contains an assignment for an Operating Systems course, detailing the creation of child processes to compute factorials and demonstrating process management in C. It includes code for both original and modified versions of a program, showcasing the use of fork(), wait(), and execlp() functions. The assignment is intended for Spring 2025 and is associated with a student ID and name.

Uploaded by

duanoor227
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/ 7

Operating Systems (CS604)

Assignment # 01

Spring 2025

BC230406442

Question No 01

Part a): Create 3 child processes and perform factorial

// File: part_a.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/wait.h>

// Function to calculate factorial

int factorial(int n) {

if (n == 0 || n == 1) return 1;

return n * factorial(n - 1);

int main() {

pid_t pid;

int i;

for (i = 1; i <= 3; i++) {


pid = fork();

if (pid == 0) {

// Child process

printf("\nChild %d:\n", i);

printf("PID = %d, PPID = %d\n", getpid(), getppid());

int num = i + 2; // small number for factorial (e.g., 3, 4, 5)

int result = factorial(num);

printf("Student ID: BC230406442 | Student Name: Hadia Azhar\n");

printf("Factorial of %d is: %d\n", num, result);

exit(0);

} else if (pid < 0) {

perror("Fork failed");

exit(1);

// Parent waits for all children

for (i = 0; i < 3; i++) {

wait(NULL);

}
printf("\nParent: All child processes completed successfully.\n");

return 0;

Part (b): Two versions — original and modified

Original Program with wait():

//part_b_original.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/wait.h>

int main() {

pid_t pid1, pid2;


pid1 = fork();

if (pid1 == 0) {

// First child

printf("Child 1: PID = %d, PPID = %d\n", getpid(), getppid());

printf("Child 1 terminating.\n");

exit(0);

pid2 = fork();

if (pid2 == 0) {

// Second child

printf("Child 2: PID = %d, PPID = %d\n", getpid(), getppid());

printf("Child 2 terminating.\n");

exit(0);

// Parent waits for both children

wait(NULL);

wait(NULL);

printf("Parent: Both child processes terminated.\n");


return 0;

Modified Version Using execlp() & Zombie Creation

//part_b_modified.c

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

int main() {

pid_t pid1, pid2;

pid1 = fork();

if (pid1 == 0) {
// First child runs `ls -l`

printf("Child 1: PID = %d, executing 'ls'\n", getpid());

execlp("ls", "ls", "-l", NULL); // Executes ls

perror("execlp failed"); // Only runs if execlp fails

exit(1); // Exit with error

pid2 = fork();

if (pid2 == 0) {

// Second child

printf("Child 2: PID = %d, PPID = %d\n", getpid(), getppid());

printf("Child 2 terminating.\n");

exit(0);

// Parent sleeps instead of waiting

sleep(10); // Allows time to run `ps` and observe zombie children

printf("Parent: Done sleeping. Check for zombie processes using 'ps'.\n");

return 0;
}

You might also like