0% found this document useful (0 votes)
23 views6 pages

Bc190401190 - CS604 - 1

The document contains a programming assignment for CS604, detailing two parts involving process creation in C using fork and execlp. Part A includes a program that calculates factorials in child processes, while Part B demonstrates the use of execlp to execute the 'ls' command in a child process. The document also includes screenshots of the output for both parts.

Uploaded by

az7168852
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)
23 views6 pages

Bc190401190 - CS604 - 1

The document contains a programming assignment for CS604, detailing two parts involving process creation in C using fork and execlp. Part A includes a program that calculates factorials in child processes, while Part B demonstrates the use of execlp to execute the 'ls' command in a child process. The document also includes screenshots of the output for both parts.

Uploaded by

az7168852
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

ID: Bc190401190

CS604
Assignment No.1
Spring 2025
Question 1
Solution: - Part A
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int factorial(int n) {
if (n <= 1) return 1;
else 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("Child Process %d:\n", i);
printf("PID: %d, PPID: %d\n", getpid(), getppid());
printf("Student ID: Bc190401190, Name: Sehar Iqbal\n");
int num = 6 - i; // Use different numbers for each child
int fact = factorial(num);
printf("Factorial of %d is: %d\n\n", num, fact);
exit(0);
}
}

// Parent waits for all children


for (i = 0; i < 3; i++) {
wait(NULL);
}
printf("Parent Process:\nPID: %d\nAll child processes completed.\n", getpid());
return 0;
}

Screenshot of Output (Part A):


Question 1 - Part B Before (Initial Version)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
pid_t pid1, pid2;

pid1 = fork();

if (pid1 == 0) {
printf("Child Process 1 (execlp):\nPID: %d, PPID: %d\n", getpid(), getppid());
execlp("ls", "ls", NULL);
perror("execlp failed");
exit(1);
} else {
pid2 = fork();

if (pid2 == 0) {
printf("Child Process 2:\nPID: %d, PPID: %d\n", getpid(), getppid());
printf("Message: Child 2 is exiting.\n");
exit(0);
} else {
sleep(10); // Let zombies be inspected
printf("\nParent Process:\nPID: %d\nSleeping... (Inspect zombie using 'ps aux | grep
<PID>')\n", getpid());
}
}
return 0;
}
Screenshot of Output (Part B before - Initial):

Question 1 - Part B After (Modified Version with execlp)


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

int main() {
pid_t pid1, pid2;

// Create first child process


pid1 = fork();
if (pid1 < 0) {
perror("Fork failed for child 1");
exit(1);
}

if (pid1 == 0) {
// Inside first child process
printf("Child Process 1:\n");
printf("PID: %d, PPID: %d\n", getpid(), getppid());

// Run execlp to execute the 'ls' command


printf("Running ls using execlp...\n");
execlp("ls", "ls", NULL);

// If execlp fails
perror("execlp failed for child 1");
exit(1);
} else {
// Create second child process
pid2 = fork();
if (pid2 < 0) {
perror("Fork failed for child 2");
exit(1);
}

if (pid2 == 0) {
// Inside second child process
printf("Child Process 2:\n");
printf("PID: %d, PPID: %d\n", getpid(), getppid());
printf("Child Process 2 is exiting...\n");
exit(0); // Exit child 2
} else {
// Inside parent process
printf("Parent Process: Waiting for children to terminate.\n");
waitpid(pid1, NULL, 0); // Wait for child 1
waitpid(pid2, NULL, 0); // Wait for child 2

printf("Parent Process:\n");
printf("PID: %d\n", getpid());
printf("Both child processes have terminated.\n");
}
}

return 0;
}

Screenshot of Output (Part B - After):

Screenshot of Zombie Process:

You might also like