To solve this problem, we need to write a C program that:
- Accepts a number (
N) between 1 and 10 via command line arguments.
- Creates a chain of
N processes such that:
- Each process (except the last one) is a parent of exactly one child process.
- The last process is not the parent of any process.
- Each child process prints its own process ID (
PID) and its parent's PID.
Here's how we can approach this:
- Read and validate the command line argument.
- Use a loop to create a chain of child processes.
- In each iteration, the current process will create a child process.
- The parent process will wait for the child to finish and then exit.
- The child process will print its own PID and parent's PID, decrement the count, and continue the loop if more processes need to be created.
- Ensure that the last process does not create any child processes.
Below is the complete C program that implements the above logic:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char *argv[]) {
// Check if exactly one command line argument is provided
if(argc != 2) {
fprintf(stderr, "Usage: %s N\n", argv[0]);
exit(1);
}
// Convert the command line argument to an integer
int N = atoi(argv[1]);
// Validate that the input is between 1 and 10
if(N < 1 || N > 10) {
fprintf(stderr, "Error: N must be between 1 and 10\n");
exit(1);
}
// The initial count of processes to create (excluding the current process)
int count = N - 1;
// Print the PID and parent's PID of the initial process
printf("My PID: %d, Parent PID: %d\n", getpid(), getppid());
// Loop to create the chain of processes
while(count > 0) {
pid_t pid = fork(); // Create a child process
if(pid < 0) {
// Fork failed
perror("fork");
exit(1);
} else if(pid == 0) {
// Child process
printf("My PID: %d, Parent PID: %d\n", getpid(), getppid());
count--; // Decrement the count
// If count > 0, the loop continues to create the next child
// If count == 0, this is the last process, and the loop exits
} else {
// Parent process
wait(NULL); // Wait for the child process to finish
exit(0); // Exit after child has finished
}
}
// The last process (when count == 0) reaches here and exits
return 0;
}
Explanation:
-
Reading Command Line Argument:
We first check if the user has provided exactly one command line argument. If not, we display the usage message and exit.
if(argc != 2) {
fprintf(stderr, "Usage: %s N\n", argv[0]);
exit(1);
}
We then convert the argument to an integer and ensure it's between 1 and 10.
int N = atoi(argv[1]);
if(N < 1 || N > 10) {
fprintf(stderr, "Error: N must be between 1 and 10\n");
exit(1);
}
-
Creating the Chain of Processes:
We initialize count as N - 1 because the initial process counts as one.
int count = N - 1;
We print the PID and parent's PID of the initial process.
printf("My PID: %d, Parent PID: %d\n", getpid(), getppid());
We enter a loop that continues as long as count > 0. Inside the loop:
- We use
fork() to create a child process.
- If
fork() returns 0, we're in the child process.
- The child process prints its own PID and parent's PID.
- Decrements
count and continues the loop if more processes need to be created.
- If
fork() returns a positive value, we're in the parent process.
- The parent process waits for the child to finish using
wait(NULL); and then exits.
This ensures that each process (except the last one) creates exactly one child process, and only the child continues the loop.
while(count > 0) {
pid_t pid = fork();
if(pid < 0) {
perror("fork");
exit(1);
} else if(pid == 0) {
// Child process
printf("My PID: %d, Parent PID: %d\n", getpid(), getppid());
count--; // Decrement the count
} else {
// Parent process
wait(NULL); // Wait for the child process
exit(0); // Exit after child finishes
}
}
-
Exiting the Last Process:
When count becomes 0, the last process exits the loop and reaches the end of main, where it exits normally.
Sample Output:
If you run the program with N = 3, you might get an output similar to:
My PID: 12345, Parent PID: 6789
My PID: 12346, Parent PID: 12345
My PID: 12347, Parent PID: 12346
(Note: The actual PIDs will vary each time you run the program.)
This output shows that each child process prints its own PID and its parent's PID, and forms a chain of three processes, as required.
Compiling and Running the Program:
To compile the program, save it in a file called process_chain.c and compile using:
gcc -o process_chain process_chain.c
Run the program by specifying a number between 1 and 10:
./process_chain 5