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

4. argvll gets inherited by child processes (lastnameTestArgv.c): A newly create

ID: 3590890 • Letter: 4

Question

4. argvll gets inherited by child processes (lastnameTestArgv.c): A newly created process inherits many attributes from the parent process including the argument and environment variables. Test this fact by writing a C program that creates 3 processes C1, C2 and C3: o C1 will compute the sum of the numbers entered by user as arguments in the command line o C2 will compute the average of these numbers. o C3 will computer the variance of these numbers The parent should wait until all children have finished executing before exiting itself. To test your code, use the following calculator: http://www.calculator.net/standard-deviation calculator.html

Explanation / Answer

#include<stdio.h>
#include<unistd.h>
#include<math.h>

int main(int argc, char *argv[])
{
int pid1, pid2,pid3;
int i,sum=0,sum1=0;
float avg,var;

if ( argc < 2 )
   {
     printf("Insufficient agruments .... ");
     printf("Usage: lastnameTstArgv <num1>[, num2, ... ] ");
     return -1;
   }
pid1=fork(); // creates a child process
if(pid1>0) // this if condition, will executes, recently created child process and its else part having wait(-1)
              // wait will be excuted by the parent, wait - it will wait , until the child program execution completes
   {
      printf("The 1st child process id %d ", pid1);
      for (i=1;i<argc;i++)
        {
                 sum += atoi(argv[i]);
        }
      printf("Sum = %d ", sum);
      pid2=fork(); // creates 2nd child process under 1st child process
      if(pid2>0) // this if conditon, will executes only 2nd child process and its else part is executed by the 1st child process
                  // the first child process will waits until the second child process exectuion completes.
       {
          printf("The 2nd Child process id %d ", pid2);
          avg=(float)sum/(argc-1);
          printf("Average = %.2f ", avg);
          pid3=fork(); // creates 3rd child process by the 2nd child process
          if(pid3>0) // this if conditon, will executes only 3rdd child process and its else part is executed by the 2nd child process
                    // the second child process will waits until the second child process exectuion completes.
           {
               printf("The 3rd Child process id %d ", pid3);
               for (i = 1; i < argc; i++)
                 {
                    sum1 = sum1 + pow((atoi(argv[i]) - avg), 2); // atoi is a function which will converts a string to an integer
                  }
               var = sum1 / (float)(argc-1);
               printf("Variance = %.2f ", var);
           }
          else
           {
               wait(-1); // parent will waits until, the third child process execution will completes

            }
        }
       else
        {
             wait(-1); // parent will waits until, the second child process execution will completes
        }
       }
   else
    {
      wait(-1); // parent will waits until, the first child process execution will completes
    }
return 0;
}

/*

lenovo@lenovo-Vbox:~/chegg$ cc lastnameTestArgv.c -lm -o lastnamTestArgv
lenovo@lenovo-Vbox:~/chegg$ ./lastnamTestArgv
Insufficient agruments ....
Usage:
   lastnameTstArgv <num1>[, num2, ... ]
lenovo@lenovo-Vbox:~/chegg$ ./lastnamTestArgv 2 3 4 5 6 7 9
The 1st child process id 9804
Sum = 36
The 2nd Child process id 9805
Average = 5.14
The 3rd Child process id 9806
Variance = 4.43
lenovo@lenovo-Vbox:~/chegg$

lenovo@lenovo-Vbox:~/chegg$ ./lastnamTestArgv 8 2 9 1 3 5 2 8 7 4 6 9
The 1st child process id 9827
Sum = 64
The 2nd Child process id 9828
Average = 5.33
The 3rd Child process id 9829
Variance = 7.33
lenovo@lenovo-Vbox:~/chegg$
*/

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