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.

  • 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...

  • 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...

  • 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....

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • 1. Suppose you wrote a program that reads data from cin. You are now required to...

    1. Suppose you wrote a program that reads data from cin. You are now required to reimplement it so that you can read data from a file. You are considering the following changes. I. Declare an ifstream variable in_file II. Replace all occurrences of cin with in_file III. Replace all occurrences of > > and get_line with the appropriate operations for ifstream objects What changes do you need to make? I, II, and III II and III I and III...

  • C programming Question1 (a) Write a C program that will print out all command line arguments,...

    C programming Question1 (a) Write a C program that will print out all command line arguments, in reverse order, one per line. Prefix each line with its index. 6 marks] (b) Consider this C code snippet int a- 100 int b- 42; inte p- &a; int q-b; p qi printf ("%d %d\n" ,a,*p); When this code is executed, what numbers will it print? [2 marks] (c) Consider this C program int main(int argc,char argv) char* target- "Apple" char vord[100] printf...

  • need this in c programming and you can edit the code below. also give me screenshot...

    need this in c programming and you can edit the code below. also give me screenshot of the output #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define LINE_SIZE 1024 void my_error(char *s) { fprintf(stderr, "Error: %s\n", s); perror("errno"); exit(-1); } // This funciton prints lines (in a file) that contain string s. // assume all lines has at most (LINE_SIZE - 2) ASCII characters. // // Functions that may be called in this function: // fopen(), fclose(), fgets(), fputs(),...

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
ADVERTISEMENT