Question

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

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

   // call fork()

   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

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/wait.h>

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

{

int fd[2]; //pipe desciptors

pid_t pid; //pid

// create pipe descriptors

pipe(fd);

pid=fork() ; //fork the process

if(pid==0)

{

//child process

int n,a[100],sum=0;

//reading from pipe ,its a blocking process

read(fd[0],&n,sizeof(n)); //reading n from pipe

int i;

for(i=0;i<n;i++) //read n elements from pipe

{

int y;

read(fd[0],&y,sizeof(y)); //read

a[i]=y; //store in array

}

for (i = 0; i < (n); ++i)

{

sum+=a[i]; //calculate the sum

}

write(fd[1],&sum,sizeof(sum)); //send the sum to parent process

exit(0); //exit

}else

{

//parent process

int a[100],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++)

{

a[i]=atoi(argv[i+2]); //read each element and store in array

}

write(fd[1],&n, sizeof(n)); //write n

for(i=0;i<n;i++)

{

int x=a[i];

write(fd[1],&x, sizeof(x)); //write n values

}

//wait for child process to return

wait(NULL);

//read sum

read(fd[0],&sum,sizeof(sum));

//print sum

printf("sum = %d \n",sum );

}

}/HomeworkLib/c/pipec Sublime Text (UNREGISTERED) t E, D(1:00, 47%) 00:58 relation.cppxthread AcxVthread B.c x thread C.c x V array./HomeworkLib/c/pipec Sublime Text (UNREGISTERED) thread_A.c x V thread-B.c x V thread-C.c x V array.cpp ※ rjavaEven Test java x tur

Add a comment
Know the answer?
Add Answer to:
Write code that forks into two processes: a parent process, and a child process. Same as...
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. 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()...

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

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

  • Linux & C code help with creating a child process and kills itself with SIGNAL. I...

    Linux & C code help with creating a child process and kills itself with SIGNAL. I provided most of the code, I need some help to finish. Please provide output screen shot as well. Thank you! A Process creates a child process. Child process will print out "hello 0" "hello 1" ..... 1 line per second. After a certain number of seconds(user input), it kills itself WITH signal mechanism ONLY, don't use exit() on the child. You can use alarm()...

  • Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork...

    Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork a child process*/ pid = fork(); if (pid<0){ /*error occured*/ fprintf(stderr,"Fork failed\n"); return 1; } else if (pid == 0) { /*child process */ printf("I am the child %d\n",pid); execlp("/bin/ls","ls",NULL); } else { /*parent process */ /*parent will wait for the child to complete*/ printf("I am the parent %d\n",pid); wait(NULL); printf("Child Complete\n"); } return 0; } 1. Write program codes for 3.21 a and...

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

  • Complete the following C code by filling all “???”sto write a C program to create a...

    Complete the following C code by filling all “???”sto write a C program to create a child process. Before the child process is created, an integer variable A is defined and assigned value 10. Then in the child process, A is reduced by 5. After the child process completes, the parent process increases A by 5. Then what will A’s value be just before the parent process completes? Why? Write the code and run it to verify your answer. #include...

  • Write a “C” program that creates a child process that prints out its pid and new...

    Write a “C” program that creates a child process that prints out its pid and new line and then calls pause() system call. Once it returns from the pause system call the child program must exit with code 5. The parent program must wait for the child process and print out the pid of the child process and the exit status with the following format: “childpid=%d,exitstatus=%d\n”.

  • 1. Solve synchronization problems inherent in inter-process communication and multi-threaded applications.

    1. Process control system calls: The demonstration of fork, execve and wait system calls along with zombie and orphan states.a. Implement the C program in which main program accepts the integers to besorted. Main program uses the fork system call to create a new process called a child process. Parent process sorts the integers using merge sort and waits for child process using wait system call to sort the integers using quick sort. Also demonstrate zombie and orphan states.b. Implement...

  • Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also,...

    Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also, include a screen shot of the output and terminal window of each command you used. Read carefully to do this task please. shell.c code given below. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <string.h> int main() { int PID; char lineGot[256]; char *cmd; while (1){ printf("cmd: "); fgets(lineGot, 256, stdin); // Get a string from user (includes \n) cmd = strtok(lineGot, "\n");...

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