Question: Create a program that does the following. (C program) - A master process which prompts a user for five numbers and writes the five numbers
Create a program that does the following. (C program)
- A master process which prompts a user for five numbers and writes the five numbers to a file called numbers.txt
- The master process forks and creates a child process and then waits for the child process to terminate
- The child process reads the five numbers from numbers.txt and creates five threads
- Each thread executes a function factorial, which takes the number as an argument and does the following:
Calculates the factorial (e.g. 5! = 5x4x3x2x1 = 120).
Adds the factorial calculated to a global variable total_sum using the += operator
The total_sum must be incremented in a thread-safe manner using semaphores
Prints the current factorial value and the calculated factorial
- The child process has a join on all threads and after all threads have completed writes the total_sum to a file called sum.txt and terminates
- After the child process has terminated the parent process reads the contents of sum.txt and prints the total sum.
- Reading and writing files between processes is one of the simplest methods of IPC.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
