Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program in C that uses fork ( ) The parent should read the first line of the input file. Once it has read

Write a program in C that uses fork ( )

The parent should read the first line of the input file. Once it has read that line, it should go into a loop based on that number with each iteration of the loop forking off a copy that will then process the next two lines. Once that child has finished its work, it will write some data to an output file and then terminate.

As the parent detects its child has terminated, it should perform another iteration of the loop until done. After all children have terminated, the parent will write the PIDs of all of its children that it launched, as well as its own PID to the output file.

When a child process starts, it should read the next line of the file, which will tell it how many numbers to read afterwards. We see in our example file that the first forked child would read a 6. It should then read that number of integers and put them into a stack. After putting all of the numbers into a stack, the child should write its PID to the output file, followed by those numbers in reverse order. For example:

Input file sample:

3

6

3 6 107 8 1 3 7

4

1 3 50 4

7

5 3 5 2 8 9 1

Output file sample:

130: 7 3 1 8 107 6 3

136: 4 50 3 1

145: 1 9 8 2 5 3 5

All children were: 130 136 145

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions

Question

=+2 Is the decision sustainable in the long run?

Answered: 1 week ago

Question

=+1 Is the decision fair to employees?

Answered: 1 week ago