Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help! I need help with doing the premium version(actually include the regular one). Thank you in advance!!! Instruction: Write code that forks into two

Please help! I need help with doing the premium version(actually include the regular one). Thank you in advance!!!

Instruction:

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

--------------------------------------------------------------------------------

REGULAR VERSION

Your code will be called with command-line arguments consisting of positive 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.

If I call your code this way:

a03 3 5 7

The parent process should print out:

CS201 - Assignment 3 Regular - I. Forgot sum = 15

The sums produced from the test input I use will be in the range [0 .. 255].

Your code must be able to handle any number of command line arguments.

Important: printing should be done only by the parent process. The child process should not print anything.

Your output should be formatted as shown (small differences in whitespace are OK). Remember to replace "I. Forgot" with your own name.

Start with the provided code file a03.c. Upload your code as a03.c.

--------------------------------------------------------------------------------

PREMIUM VERSION

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:

CS201 - Assignment 3 Premium - I. Forgot sum = -5

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

Start with the provided code file a03p.c. Upload your code as a03p.c.

source:

// 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()

printf("CS201 - Assignment 3 Premium - I. Forgot ");

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