Question
In this project, you will write a program that finds prime numbers between 2 to 1,000,000 inclusive. Remember: Prime numbers are natural numbers greater than
In this project, you will write a program that finds prime numbers between 2 to 1,000,000 inclusive. Remember: Prime numbers are natural numbers greater than 1 that have no divisors other than 1 and itself. The first five prime numbers are 2, 3, 5, 7, and 11. Hints: There are no more than 100,000 primes between 2 and 1,000,000 inclusive. You can check to see if a number is prime using trial division. You divide the number by each integer greater than 1 but less than or equal the square root of the candidate number. If any of these divisions results in an integer (i.e., no remainder), the number is not prime. This check can be made more efficient if you realize you need only divide the number by each prime less than or equal to the square root of the number. Write a single C or C++ program that uses fork and pipe. The program should first calculate the prime numbers between 2 and 1,000 inclusive. The program should then use fork to spawn a total of 10 child processes that will determine the remaining prime numbers from 1,001 to 1,000,000 inclusive. However, no more than 3 child processes should exist at a time. That is, the original process (i.e., the parent) should spawn 3 child processes and wait for one to complete before spawning another. At all times, 3 child processes should be running in parallel until the end of the run as the final 3 processes terminate. Each child process should print a message to STANDARD ERROR when it comes into existence and a message to STANDARD ERROR just before it terminates. One child process should calculate the prime numbers from 1,001 to 100,000 inclusive. Another child process should calculate the prime numbers from 100,001 to 200,000 inclusive. Another child process should calculate the prime numbers from 200,001 to 300,000 inclusive. Another child process should calculate the prime numbers from 300,001 to 400,000 inclusive.
Another child process should calculate the prime numbers from 400,001 to 500,000 inclusive. Another child process should calculate the prime numbers from 500,001 to 600,000 inclusive. Another child process should calculate the prime numbers from 600,001 to 700,000 inclusive. Another child process should calculate the prime numbers from 700,001 to 800,000 inclusive. Another child process should calculate the prime numbers from 800,001 to 900,000 inclusive. Another child process should calculate the prime numbers from 900,001 to 1,000,000 inclusive. Each child process should write its primes into a pipe that will be read by the parent process. Thus, the parent process collects all the primes identified by the child processes. It is NOT acceptable for the parent process to spawn a child, read its primes, and then spawn another child process. There must be multiple simultaneous child processes executing. Each child process should also open a file unique to that process (e.g., each child process has its own file). As each child process identifies a prime, the child process should write that prime (in human readable format) to its file, one prime per line. After reading all the primes from all the child processes, the parent process writes them out to standard output, in ascending order, with one prime per line.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started