Question: Write a C program that accepts an argument. In the following discussion, we refer to that argument as the main argument. The main program
Write a C program that accepts an argument. In the following discussion, we refer to that argument as the main argument. The main program must create a child. If the main argument is greater or equal to 5, then the child must call an executable file that finds the sum of numbers 1 to that main argument. Then the child must print the sum. If the argument is 0 to 4 (including 4), then the function must execute a Linux script that asks the user to enter two numbers and add the numbers and display the result. The parent process must wait for the child to finish if the main argument is not negative and then display the statement that the parent waited for the child to finish. Otherwise, the parent must not wait and display the main argument was negative. You must write the above program, the C program that the child may executes its executable and the script that the child may execute.
Step by Step Solution
3.34 Rating (148 Votes )
There are 3 Steps involved in it
Below is a C program that accomplishes the described task Additionally Ill provide the childs C program and the Linux script that it may execute 1 Parent C Program parentc c include include include include int mainint argc char argv if argc 2 printfUsage s argv0 return 1 int mainArg atoiargv1 if mainArg 0 pidt childpid fork if childpid 0 perrorFork failed return 1 if childpid 0 Child process if mainArg 5 execlpsumexecutable sumexecutable argv1 NULL else execlpaddnumbersscriptsh addnumbersscriptsh NULL perrorExec failed exit1 else Parent process if mainArg 0 int status waitstatus printfParent waited for the child ... View full answer
Get step-by-step solutions from verified subject matter experts
