Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program using the fork() system call that computes the factorial of a gi

ID: 2247732 • Letter: W

Question

Write a program using the fork() system call that computes the factorial of a given integer in the child process. The integer whose factorial is to be computed will be provided in the command line. For example, if 5 is provided, the child will compute 5! and output 120 Because the parent and child processes have their own copies of the data it will be necessary for the child to output the factorial. Have the parent invoke the wait () call to wait for the child process to complete before exiting the program. Perform necessary error checking to ensure that a non-negative number is passed on the command line.

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

int main(int argc,char *argv[])

{

int k, pid,i=0,j=0;

int sum=1;

int num=atoi(argv[1]);

if (argc == 0)

{

printf ("Please enter a number non negative number");

}pid = fork();

if (pid == 0)

{

for(k=1;k<num;k++)

{

i = j;j= sum;

sum = i + j;

printf("The non-negative number seqence is: %d ",sum);

}

}

else

{

wait();

printf ("parent is waiting for child ");

}return 0:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote