Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need help with my program #include #include #include #include #include / / Function to compute factorial int factorial ( int n ) { if (
Need help with my program
#include
#include
#include
#include
#include
Function to compute factorial
int factorialint n
if n n
return ;
else
return n factorialn ;
Function to check if a number is prime
int isPrimeint num
if num
return ;
for int i ; i i num; i
if num i
return ;
return ;
int main
pidt childpid;
int n;
Prompt user for the number of child processes to create
printfEnter the number of child processes to create less than : ;
scanfd &n;
Validate input
if n n
printfInvalid input. Please enter a number between and
;
return ;
Display initial message
printfParent process PID: d is creating d child processes.
getpid n;
Create child processes
for int i ; i n; i
childpid fork;
if childpid
perrorFork failed";
exitEXITFAILURE;
else if childpid
This code will be executed by the child process
Print child's identifier PID and the task it is performing
printfChild d PID: d is performing a custom task.
i getpid;
Perform different tasks based on child number
switch i
case :
printfChild d PID: d is computing the factorial of
i getpid;
printfChild d PID: d completed its task. Result: d
i getpid factorial;
break;
case :
printfChild d PID: d is finding prime numbers up to
i getpid;
printfChild d PID: d completed its task. Result: i getpid;
for int j ; j ; j
if isPrimej
printfd j;
printf
;
break;
case :
printfChild d PID: d is performing a custom task.
i getpid;
printfChild d PID: d completed its task. Result: Custom task completed.
i getpid;
break;
Add more cases for additional child processes if needed
exitEXITSUCCESS;
This code will be executed by the parent process
Wait for all child processes to finish
for int i ; i n; i
waitNULL;
printfAll child processes have completed. Parent PID: d is displaying the final message.
getpid;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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