Write a single C that uses fork and pipe The program should

Write a single C ++ 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 100, 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 fare 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. 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. Along with this assignment, you will find a \"sample. out\" file that contains the expected output from the program.

Solution

#include<iostream>

#include <stdlib>

using namespace std;

int main()

{
int count=0;

int number;

cout<<\"enter an integer:\";

cin>>number;

for(int i=1;i<=number;i++)

{

if( number%i=0)

{

count++;

}

}

if(count++2)

   cout<<number <<\"is prime\"<< end1;

else

   cout<<number <<\"is not prime \" << end1;

system(\"PAUSE\");

return(0);

}

IN C LANGUAGE WE CAN WRITE CODE LIKE THIS :

#include <unistd.h>

#include <sys/types.h>

#include <errno.h>

#include <stdio.h>

#include <sys/wait.h>

#include <stdlib.h>

#include<time.h>

static void Usage(const char* prog)

{

printf(\"not enough arguments\ \");

printf(\"usage:\ \");

printf(\"%5 <number>\ \", prog);

printf(\"\\t where <number> is number of child process to start \ \");

return;

}
int main(int argc,char* argv[])

{

pid_t childpid;

int retval;

int status;

int count;

int child;

srand(time(NULL));

if(argc <2)

{

Usage(argv[0]);

exit(0);

}

childs = (int)srtol(argv[1],NULL,10);

for(count=0; count < childs; count++)

{
childpid=fork();

if (childpid >=0)

{

if (childpid ==0)

{

printf(\"child: i am the child process!\ \");

printf(\"child: here is my pid %d\ \",getpid());

printf(\"child: my parent pid id:%d\ \", getpid());

printf(\"child: the value of my copy of childpid is: %d\ \", childpid);

printf(\"child: sleeping for 1 second.......\ \");

sleep(1);

retval = getpid()%;

exit(retval);

}

else

{

if (count ==0)

{

printf(\"parent: i am the parent process!\ \");

printf(\"parent: here\'s my PID:%d\ \", getpid(0);

}

printf(\"parent the value of my copy of childpid id %d\ \", childpid);

}

}

else

{

perror(\"fork\");

exit(0);

}

}

for(count=0; count < child; count++)

{

printf(\"parent: i will now wait for my child %d to exit \ \", count);

wait(&status);

printf(\"parent: child exit code is: %d\ \", WEXITSTATUS(status));

}

printf(\"parent: goodbye!\ );

exit(0);

}



 Write a single C ++ that uses fork and pipe. The program should first calculate the prime numbers between 2 and 1, 000 inclusive. The program should then use f
 Write a single C ++ that uses fork and pipe. The program should first calculate the prime numbers between 2 and 1, 000 inclusive. The program should then use f
 Write a single C ++ that uses fork and pipe. The program should first calculate the prime numbers between 2 and 1, 000 inclusive. The program should then use f
 Write a single C ++ that uses fork and pipe. The program should first calculate the prime numbers between 2 and 1, 000 inclusive. The program should then use f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site