Answered step by step
Verified Expert Solution
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 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 eg 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 task; void task; void task; int main int n; printfEnter the number of child processes to create: ; scanfd &n; if n n printfNumber of child processes must be between and ; exitEXITFAILURE; printfParent process PID: d is creating d child processes. getpid n; Forking child processes for int i ; i n; i pidt pid fork; if pid Error handling for fork perrorfork; exitEXITFAILURE; else if pid Child process Perform task based on child process ID switch i case : task; break; case : task; break; case : task; break; Add more cases for additional tasks if needed exitEXITSUCCESS; Parent process waits for all child processes to complete int status; pidt childpid; while childpid wait&status printfChild process with PID d completed. childpid; Final message from parent printfAll child processes have completed. ; return ; Sample task functions replace with your own tasks void task printfChild process PID: d is computing the factorial of getpid; Perform factorial calculation here void task printfChild process PID: d is finding prime numbers up to getpid; Find prime numbers here void task printfChild process PID: d is performing a custom task. getpid; Custom task here ample output: $ parallelexecution Enter the number of child processes to create: Parent process PID: is creating child processes. Child PID: is computing the factorial of Child PID: is finding prime numbers up to Child PID: is performing a custom task. Child PID: completed its task. Result: Child PID: completed its task. Result: Child PID: completed its task. Result: Custom task completed. All child processes have completed. Parent PID: is displaying the final message.
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
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 eg 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 task;
void task;
void task;
int main
int n;
printfEnter the number of child processes to create: ;
scanfd &n;
if n n
printfNumber of child processes must be between and
;
exitEXITFAILURE;
printfParent process PID: d is creating d child processes.
getpid n;
Forking child processes
for int i ; i n; i
pidt pid fork;
if pid
Error handling for fork
perrorfork;
exitEXITFAILURE;
else if pid
Child process
Perform task based on child process ID
switch i
case :
task;
break;
case :
task;
break;
case :
task;
break;
Add more cases for additional tasks if needed
exitEXITSUCCESS;
Parent process waits for all child processes to complete
int status;
pidt childpid;
while childpid wait&status
printfChild process with PID d completed.
childpid;
Final message from parent
printfAll child processes have completed.
;
return ;
Sample task functions replace with your own tasks
void task
printfChild process PID: d is computing the factorial of
getpid;
Perform factorial calculation here
void task
printfChild process PID: d is finding prime numbers up to
getpid;
Find prime numbers here
void task
printfChild process PID: d is performing a custom task.
getpid;
Custom task here
ample output:
$ parallelexecution
Enter the number of child processes to create:
Parent process PID: is creating child processes.
Child PID: is computing the factorial of
Child PID: is finding prime numbers up to
Child PID: is performing a custom task.
Child PID: completed its task. Result:
Child PID: completed its task. Result:
Child PID: completed its task. Result: Custom task completed.
All child processes have completed. Parent PID: is displaying the final message.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started