Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 ", sum); return 0; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions