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

Assignment solution of cs604

The document contains an assignment solution for CS604 by student Mehak Adeel, detailing C programming code for calculating factorials using child processes and demonstrating process management with fork and execlp. It includes three parts: the first part calculates the factorial of a number in multiple child processes, the second part creates two child processes with simple exit messages, and the third part modifies one child to execute a command using execlp. The output sections indicate the expected behavior of the processes and the parent process's response to their completion.

Uploaded by

Mehak Adeel
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)
8 views

Assignment solution of cs604

The document contains an assignment solution for CS604 by student Mehak Adeel, detailing C programming code for calculating factorials using child processes and demonstrating process management with fork and execlp. It includes three parts: the first part calculates the factorial of a number in multiple child processes, the second part creates two child processes with simple exit messages, and the third part modifies one child to execute a command using execlp. The output sections indicate the expected behavior of the processes and the parent process's response to their completion.

Uploaded by

Mehak Adeel
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

Assignment solution of cs604

Student Name: Mehak Adeel Student ID:


BC2330215528

Question no.01:

Solution: (Part 1)
#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <unistd.h>

#include <sys/wait.h>

int factorial(int n) {

if (n <= 1) return 1;

return n * factorial(n - 1);

int main() {

pid_t pid;

int i;

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

pid = fork();

if (pid == 0) {

// Child process

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


int num = 5;

int result = factorial(num);

printf("Student ID: BC230215528 | Student Name: Mehak Adeel\n");

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

exit(0);

} else if (pid < 0) {

perror("Fork failed");

exit(1);

// Parent process waits for all children

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

wait(NULL);

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

return 0;

Output:
Solution: (Part B) (step 01 Base version (no execlp)

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <unistd.h>

#include <sys/wait.h>

int main() {

pid_t pid1, pid2;

pid1 = fork();
if (pid1 == 0) {

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

printf("Child 1: Exiting...\n");

exit(0);

pid2 = fork();

if (pid2 == 0) {

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

printf("Child 2: Exiting...\n");

exit(0);

// Parent waits for both children

wait(NULL);

wait(NULL);

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

return 0;

Output:
Solution: (Part B) (step 02 Modified version using execlp())
#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <unistd.h>

#include <sys/wait.h>

int main() {

pid_t pid1, pid2;

pid1 = fork();

if (pid1 == 0) {

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

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

perror("execlp failed");

exit(1);

pid2 = fork();

if (pid2 == 0) {

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

printf("Child 2: Exiting...\n");

exit(0);

}
// No wait() here

sleep(10); // Allows you to run `ps` in another terminal to check zombie state

printf("Parent Process: Observed children behavior.\n");

return 0;

Output:

You might also like