Question

Write code that forks into two processes: a parent process, and a child process. Your code...

Write code that forks into two processes: a parent process, and a child process.
Your code will be called with command-line arguments consisting of negative
integers. Do not worry about bad command-line arguments such as "xyz". Your code
will not be tested in this way.

The parent process will take the arguments to main(), convert them into ints by
calling atoi(), and send those ints one at a time to the child process through
a pipe (one call to write() for each int).

Do not send anything from argv[0] through the pipe. Start with argv[1] and
continue through the rest of the arguments.

The child process will read the ints sent by the parent process one at a
time, and add them up. The child process should not use the arguments to main(),
argc and argv, in any way whatsoever. The child process will communicate the sum
of the numbers to the parent as the return value from main().

The parent process will need to reap the child process to find out that sum.

It may be of use to know that read() will return immediately with a zero once
the other end of the pipe is closed by the parent.

your code must be able to handle negative integers input from the command-line.

If I call your code this way:

a03 -3 5 -7

The parent process should print out:

dl200 - Assignment - I.
sum = -5

The sums produced from the test input I use will be in the range [-128 .. 127].

the code c file

// Numbers from command line arguments are sent to child process
// from parent process one at a time through pipe.
//
// Child process adds up numbers sent through pipe.
//
// Child process returns sum of numbers to parent process.
//
// Parent process prints sum of numbers.

#include <stdio.h>

int main(int argc, char **argv)
{
// set up pipe

// call fork()

printf("dl200 - Assignment - I ");

if (0 /* replace 0 with test for parent vs child, delete this comment */) {
// -- running in child process --
int sum = 0;

// Receive numbers from parent process via pipe
// one at a time, and count them.

// Return sum of numbers.
return sum;
}
else {
// -- running in parent process --
int sum = 0;

// Send numbers (datatype: int, 4 bytes) from command line arguments
// starting with argv[1] one at a time through pipe to child process.

// Wait for child process to return. Reap child process.
// Receive sum of numbers via the value returned when
// the child process is reaped.
printf("sum = %d\n", sum);
return 0;
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Do like and comment if you have any queries.

code to copy:

   #include <stdio.h>
   #include <stdlib.h>
   #include <unistd.h>
   #include <sys/wait.h>

   int main(int argc, char *argv[])
   {
       int pipe_Descriptor[2]; //pipe desciptors
       pid_t pID; //pID
       // create pipe descriptors
       pipe(pipe_Descriptor);
       pID=fork() ; //fork the process
       if(pID==0)
       {
           //child process
           int n,args[100],args_Sum=0;
           //reading from pipe ,its a blocking process
           read(pipe_Descriptor[0],&n,sizeof(n)); //reading n from pipe
           int i;
           for(i=0;i<n;i++) //read n elements from pipe
           {
               int y;
               read(pipe_Descriptor[0],&y,sizeof(y)); //read
               args[i]=y; //store in array
           }
           for (i = 0; i < (n); ++i)
           {
               args_Sum+= args[i]; //calculate the Sum
           }
           write(pipe_Descriptor[1],&args_Sum,sizeof(args_Sum)); //send the Sum to parent process
           exit(0); //exit
       }
       else
       {
           //parent process
           int args[100],args_Sum; //array to hold command line arguments
           int i;
           //int n=*(argv[1])-'0';
           if(argc<2)
           {
               printf("Enter number of elements and values: \n");
               return 0;
           }
           int n = atoi(argv[1]); //read no of elements
           //argv[0] stands for program name
           for(i=0;i<(n);i++)
           {
               args[i] = atoi(argv[i+2]); //read each element and store in array
           }
           write(pipe_Descriptor[1],&n, sizeof(n)); //write n
           for(i=0;i<n;i++)
           {
               int x = args[i];
               write(pipe_Descriptor[1],&x, sizeof(x)); //write n values
           }
           //wait for child process to return
           wait(NULL);
           //read Sum
           read(pipe_Descriptor[0],&args_Sum,sizeof(args_Sum));
           //print Sum
           printf("Sum = %d \n",args_Sum );
       }
   }

Output Screenshot:

Code Screenshot:

Add a comment
Know the answer?
Add Answer to:
Write code that forks into two processes: a parent process, and a child process. Your code...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write code that forks into two processes: a parent process, and a child process. Same as...

    Write code that forks into two processes: a parent process, and a child process. Same as the Regular version, except that your code must also be able to handle negative integers input from the command-line. If I call your code this way:     a03 -3 5 -7 The parent process should print out: Assignment 3     sum = -5 The sums produced from the test input I use will be in the range [-128 .. 127] -------------------------------------------------------------------------------------------------------------------- // Numbers from...

  • *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

    *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.* --The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.* --This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the...

  • Give code to set up a pipe or a FIFO for a parent process and it child process. have the child pr...

    give code to set up a pipe or a FIFO for a parent process and it child process. have the child process send a message("i'm ok") to the parent process. the parent process should read the message into a buffer.

  • Command line input In C++ it is possible to accept command line arguments. Command-line arguments are...

    Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in the program, it must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line...

  • Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process...

    Write a program in C using the fork() system call to do the following. The parent process (main program) forks a process (CHILD 1) to compute and print the sum of first n integers where n is a variable shared between the parent and CHILD 1. It also forks another process (CHILD 2) that finds the sum of squares of the first n numbers where n is a variable it shares with the parent. Let CHILD 1 print “The sum...

  • C LANGUAGE PROGRAM: You have the program that creates a parent and child. They communicate via...

    C LANGUAGE PROGRAM: You have the program that creates a parent and child. They communicate via the pipe. The parent writes into the pipe and child reads the data from the pipe. All you got to is replace FILL_IN_THE_BLANK with appropriate values or function names and just type the values of each TASKS, I don't need the c program. TASK_1 = TASK_2 = TASK_3 = TASK_4 = 4 of them [ 5 points each ] -------------------------------------------------------------------------------------------- When you run the...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • In C Programming Adding to your program in part A, go through the command line arguments...

    In C Programming Adding to your program in part A, go through the command line arguments and find the largest and smallest arguments by alphabetical order. Note that you should not need to sort your arguments, but instead compare them and save the smallest and largest strings as you go through. For example, if called with ./reverse one two three: It would display the output for part A:             Three two one And then it would display   The smallest string was:...

  • I have the following code in C: void sighandler(int sig) {      printf("exiting child..."); } int...

    I have the following code in C: void sighandler(int sig) {      printf("exiting child..."); } int main(int argc,char* argv[]) {      if(argc > 1)      {            char* commandName;            for(int i=2;i            {                   forkChild = fork();                   signal(SIGINT,sighandler);                   if(forkChild == 0)                   {                          execlp(commandName,commandName,argv[i],NULL);                          exit(0);                   }                   else                   {                          wait(NULL);                   }       } My problem is that I would like to kill the child with ^C but leave the parent running....

  • I need help programming this question using C language. This program uses input data entered in...

    I need help programming this question using C language. This program uses input data entered in command line at the same time when the command or .exe file is entered. They are called command-line arguments. Because everything entered in command line is taken as a string, these arguments including the command itself or the .exe file name are stored in an array of char pointers, each pointer points to the first character of a string (i.e., argument). The inputs can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT