Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Write a C program that simulates a parallel task execution scenario using process forking. The program should create a specified number of child processes, each

Write a C program that simulates a parallel task execution scenario using process forking. The program should create a specified number of child processes, each performing a unique task. The parent process should wait for all child processes to complete before displaying the final result.
Requirements:
Your program should take an integer input n from the user, where n represents the number of child processes to be created. n should be less than 5.
Each child process should perform a different task, such as computing the factorial of a number, finding prime numbers in a range, or any computationally intensive operation.
The parent process should display a message before creating (forking) the child processes.
Each child process should print its own identifier (PID) and the task it is performing.
After completing their tasks, each child process should print a completion message.
The parent process should wait for all child processes to finish before displaying a final message.
Additional Considerations:
Use the fork() system call to create child processes.
You may use other relevant system calls or functions as needed (e.g., wait(), exit()).
Ensure proper error handling for system calls.
Design the code to guarantee the creation of exactly n children, ensuring that each child executes its intended task. Properly manage the process creation to avoid any unintended duplication or omission of child processes.
#include
#include
#include
#include
// Function prototypes for child tasks
void task1();
void task2();
void task3();
int main(){
int n;
printf("Enter the number of child processes to create: ");
scanf("%d", &n);
if (n <1|| n >5){
printf("Number of child processes must be between 1 and 5.
");
exit(EXIT_FAILURE);
}
printf("Parent process (PID: %d) is creating %d child processes.
", getpid(), n);
// Forking child processes
for (int i =0; i < n; ++i){
pid_t pid = fork();
if (pid ==-1){
// Error handling for fork
perror("fork");
exit(EXIT_FAILURE);
} else if (pid ==0){
// Child process
// Perform task based on child process ID
switch (i){
case 0:
task1();
break;
case 1:
task2();
break;
case 2:
task3();
break;
// Add more cases for additional tasks if needed
}
exit(EXIT_SUCCESS);
}
}
// Parent process waits for all child processes to complete
int status;
pid_t child_pid;
while ((child_pid = wait(&status))>0){
printf("Child process with PID %d completed.
", child_pid);
}
// Final message from parent
printf("All child processes have completed.
");
return 0;
}
// Sample task functions (replace with your own tasks)
void task1(){
printf("Child process (PID: %d) is computing the factorial of 5.
", getpid());
// Perform factorial calculation here
}
void task2(){
printf("Child process (PID: %d) is finding prime numbers up to 20.
", getpid());
// Find prime numbers here
}
void task3(){
printf("Child process (PID: %d) is performing a custom task.
", getpid());
// Custom task here
}
ample output:
$ ./parallel_execution
Enter the number of child processes to create: 3
Parent process (PID: 1234) is creating 3 child processes.
Child 1(PID: 1235) is computing the factorial of 5.
Child 2(PID: 1236) is finding prime numbers up to 20.
Child 3(PID: 1237) is performing a custom task.
Child 1(PID: 1235) completed its task. Result: 120
Child 2(PID: 1236) completed its task. Result: 235711131719
Child 3(PID: 1237) completed its task. Result: Custom task completed.
All child processes have completed. Parent (PID: 1234) is displaying the final message.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions