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

CS604 Assignment 1 (Noshaba Nadeem)

Uploaded by

olayboy782
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)
9 views7 pages

CS604 Assignment 1 (Noshaba Nadeem)

Uploaded by

olayboy782
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

CS604 Assignment 1

Noshaba Nadeem
Bc230424161
Solution (a):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int factorial(int n) {
if (n == 0 || n == 1) return 1;
return n * factorial(n - 1);
}

int main() {
pid_t child_pid;
int status;

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


child_pid = fork();

if (child_pid == -1) {
perror("fork failed");
exit(1);
}

if (child_pid == 0) {
printf("Child %d: PID = %d, PPID = %d\n", i+1, getpid(), getppid());
int num = i + 4;
int result = factorial(num);

printf("Student ID: Bc230424161, Student Name: Noshaba Nadeem \n");


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

exit(0);
}
}

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


wait(&status);
}

printf("All child processes have completed. Parent exiting.\n");


return 0;
}
Screenshot:
Solution (b)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main() {
pid_t child1, child2;
int status;

child1 = fork();

if (child1 == -1) {
perror("First fork failed");
exit(1);
}
if (child1 == 0) {
printf("First child: PID = %d, PPID = %d\n", getpid(), getppid());
printf("First child exiting\n");
exit(0);
}

child2 = fork();

if (child2 == -1) {
perror("Second fork failed");
exit(1);
}

if (child2 == 0) {
printf("Second child: PID = %d, PPID = %d\n", getpid(), getppid());
printf("Second child exiting\n");
exit(0);
}

waitpid(child1, &status, 0);


waitpid(child2, &status, 0);

printf("Both children have terminated. Parent exiting.\n");


return 0;
}
Modified Program (with execlp and zombie behavior):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int main() {
pid_t child1, child2;

// Create first child


child1 = fork();

if (child1 < 0) {
perror("First fork failed");
exit(1);
}

if (child1 == 0) {
// In first child process
printf("First child: PID = %d, PPID = %d\n", getpid(), getppid());
printf("First child executing 'ls -l'\n");
if (execlp("ls", "ls", "-l", NULL) == -1) {
perror("execlp failed in first child");
exit(1);
}
}

// Create second child


child2 = fork();

if (child2 < 0) {
perror("Second fork failed");
exit(1);
}

if (child2 == 0) {
// In second child process
printf("Second child: PID = %d, PPID = %d\n", getpid(), getppid());
printf("Second child exiting\n");
exit(0);
}

// Parent process
printf("Parent process: PID = %d\n", getpid());
printf("Parent sleeping for 15 seconds...\n");
printf("Open another terminal and run: ps -elf | grep defunct\n");
sleep(15);

// No wait() — children will become zombies


printf("Parent exiting after sleep (children may still be zombies if not waited on)\n");

return 0;
}

You might also like