Write a C-program that does the following: The program first creates a single pi
ID: 663815 • Letter: W
Question
Write a C-program that does the following: The program first creates a single pipe, using the pipe() system call. It then forks a child process using the fork() call. The goal is to share the work of counting numbers that are multiples of 3 and 5 from the set of integers from 1-1 0000. The parent process executes the for-loop (from 1- 10000) and takes the responsibility for counting all multiples of 5. AIl numbers that are not multiple of 5 are passed through the pipe to the child process to be inspected and counted if they are multiples of 3. Note: Only the parent process will implement the for-loop from 1-1 0000. After the processes have completed their work, both, parent and child will print out a corresponding message together with their corresponding process-id. The child then uses the pipe to send its result to the parent. Upon reading the number of multiples of 3 from the pipe, the parent will print out the complete response to the user (i.e., count of multiples of 3 and 5 and wait for the child to exit properly and exit. What happens if the child process is executing significantly slower than the parent process, i.e., what if the parent wants to read from the pipe while the child process has not had a chance to read all multiples of 3 from it? Describe the problem? Describe how you simulated this problem? What do you need to do to avoid this problem? Show your solution!Explanation / Answer
code to copy:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t processid;
int pipes[2];
FILE *stream,*stream2;
int sum,t=0,c=0,i,j;
int s[1000];
int tp[1000];
if(pipe(pipes))
{
fprintf(stderr,"Pipe creation failed ");
return EXIT_FAILURE;
}
close(pipes[0]);
for(i=1,j=0;i<1000;i++)
{
if(i%5==0)
c=c+i;
else
{
s[j]=8;
j=j+1;
}
}
write(pipes[1],s,sizeof(int));
close(pipes[1]);
read(pipes[0],&t,sizeof(int));
printf("%d e ",t);
processid=fork();
if(processid==(pid_t)0)
{
close(pipes[1]);
read(pipes[0],&tp,4*(j+1));
for(i=0;i<j+1;i++)
t=t+tp[i];
close(pipes[0]);
open(pipes[1]);
write(pipes[1],&t,sizeof(int));
return EXIT_SUCCESS;
exit(0);
}
else if(processid<(pid_t)0)
{
fprintf(stderr,"FORK failed");
return EXIT_FAILURE;
}
else
{
close(pipes[1]);
read(pipes[0],&t,sieof(int));
sum=c+t;
printf("The sum is %d",sum);
return EXIT_SUCCESS;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.