Answered step by step
Verified Expert Solution
Link Copied!

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 factorial(int n){
if (n ==0|| n ==1)
return 1;
else
return n * factorial(n -1);
}
// Function to check if a number is prime
int isPrime(int num){
if (num =1)
return 0;
for (int i =2; i * i = num; i++){
if (num % i ==0)
return 0;
}
return 1;
}
int main(){
pid_t child_pid;
int n;
// Prompt user for the number of child processes to create
printf("Enter the number of child processes to create (less than 5): ");
scanf("%d", &n);
// Validate input
if (n 1|| n >4){
printf("Invalid input. Please enter a number between 1 and 4.
");
return 1;
}
// Display initial message
printf("Parent process (PID: %d) is creating %d child processes.
", getpid(), n);
// Create child processes
for (int i =0; i n; i++){
child_pid = fork();
if (child_pid 0){
perror("Fork failed");
exit(EXIT_FAILURE);
} else if (child_pid ==0){
// This code will be executed by the child process
// Print child's identifier (PID) and the task it is performing
printf("Child %d (PID: %d) is performing a custom task.
", i +1, getpid());
// Perform different tasks based on child number
switch (i){
case 0:
printf("Child %d (PID: %d) is computing the factorial of 5.
", i +1, getpid());
printf("Child %d (PID: %d) completed its task. Result: %d
", i +1, getpid(), factorial(5));
break;
case 1:
printf("Child %d (PID: %d) is finding prime numbers up to 20.
", i +1, getpid());
printf("Child %d (PID: %d) completed its task. Result: ", i +1, getpid());
for (int j =2; j =20; j++){
if (isPrime(j))
printf("%d ", j);
}
printf("
");
break;
case 2:
printf("Child %d (PID: %d) is performing a custom task.
", i +1, getpid());
printf("Child %d (PID: %d) completed its task. Result: Custom task completed.
", i +1, getpid());
break;
// Add more cases for additional child processes if needed
}
exit(EXIT_SUCCESS);
}
}
// This code will be executed by the parent process
// Wait for all child processes to finish
for (int i =0; i n; i++){
wait(NULL);
}
printf("All child processes have completed. Parent (PID: %d) is displaying the final message.
", getpid());
return 0;
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

Is financial support available for travel to conferences?

Answered: 1 week ago