2.1 KiB
Topic 3 – More forking
Task 1
The main program creates one child process. The offspring is supposed to write numbers from 1 to 10 (every second) to the screen. The parent process should wait for the child process to finish and then output the message "END OF WORK"
man 3 sleep man 2 wait
Task 2
The main program generates two random integers: a - from the range 0...10 b - from the range 20...30
Then it creates a child process.
The child process calculates the sum of a+b values and returns it as its exit status.
The parent process waits for the child process to finish calculating, receives its execution status, and displays returned sum.
man 2 exit man 2 wait
Please read the description of WEXITSTATUS macro carefully.
Task 3
The main program allocates a 1000000-element integer array on the heap (free-store) Then it fills it with random values from the range 0....100
After preparing the array it creates 10 child processes. The task of each of the child processes is to determine the average value of the array fragment (rounded to the nearest integer): descendant 0 - from index 0 to 99999 descendant 1 - from index 100000 to 199999 descendant 2 - from index 200000 to 299999 ...
The parent process waits for the descendant processes to complete their calculations and then determines the average value of the array elements based on the obtained partial averages.
Task 4
The main program allocates a 1000000-element array of floating point numbers on the heap. Then it fills it with random values from the range -1....1
After preparing the array it creates 10 child processes. The task of each of the child processes is to determine the sum of the elements of the array fragment: descendant 0 - from index 0 to 99999 descendant 1 - from index 100000 to 199999 descendant 2 - from index 200000 to 299999 ... After determining the sum the child processes write it to the file named "sum.txt" and finish their work.
The parent process waits for the completion of calculations by the child processes and then determines the average value of the array elements based on the contents of files with partial results.