Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey Guys! I have a C program where i have to list some primes. The assignment is : Write a single C program (NOT A

Hey Guys!

I have a C program where i have to list some primes. The assignment is :

Write a single C program (NOT A 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 10 processes that will determine the remaining prime numbers from 1,001 to 1,000,000 inclusive.

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. The program must be written in a way that all 10 child processes are executing concurrently. Thus, it is NOT acceptable for the parent process to spawn a child, read its primes, and then spawn another child process. Instead, the parent process must spawn all 10 child processes before reading the values written to the pipe by any of the child processes.

The program i have so far is this but some how its not working right:

#include #include #include #include #include #include int DoWorkInChild(int,int); int main(int argc, char **argv) { pid_t pids[10]; int i,n = 10, j = 2,k=1000, count, c;

/* Start children. */ for (i = 0; i < n; ++i) { if ((pids[i] = fork()) < 0) { perror("fork"); abort(); } else if (pids[i] == 0) { if(i==0) { if ( k <= 1 ) { printf("First %d prime numbers are : ",k); printf("2 "); return 0; } for ( count = 2 ; count <= k ; j++ ) { for ( c = 2 ; c <= j - 1 ; c++ ) { if ( j%c == 0 ) break; } if ( c == j ) { printf(" %d",j); count++; } } } j=DoWorkInChild(j,100000); exit(0); } }

/* Wait for children to exit. */ int status; pid_t pid; while (n > 0) { pid = wait(&status); printf(" Child with PID %ld exited with status 0x%x. ", (long)pid, status); --n; // TODO(pts): Remove pid from the pids array. } }

int DoWorkInChild(int j,int k) { int count,c; if ( k <= 1 ) { printf("First %d prime numbers are : ",k); printf("2 "); return 0; } for ( count = j+1 ; count <= k ; j++ ) { for ( c = 2 ; c <= j - 1 ; c++ ) { if ( j%c == 0 ) break; } if ( c == j ) { printf(" %d",j); count++; } } return j-1; }

Thank you!

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_2

Step: 3

blur-text-image_3

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

Building Database Driven Catalogs

Authors: Sherif Danish

1st Edition

0070153078, 978-0070153073

More Books

Students also viewed these Databases questions

Question

What will ongoing support to teachers look like?

Answered: 1 week ago

Question

=+Describe how banks create money.

Answered: 1 week ago

Question

Q.1. Taxonomic classification of peafowl, Tiger and cow ?

Answered: 1 week ago

Question

Q .1. Different ways of testing the present adulterants ?

Answered: 1 week ago

Question

Q.1. Health issues caused by adulteration data ?

Answered: 1 week ago