3.Communication using pipes: Use pipes to rewrite part 4 in lab 05 to allow C1 t
ID: 3601870 • Letter: 3
Question
3.Communication using pipes:
Use pipes to rewrite part 4 in lab 05 to allow C1 to send the value for sum to C2 to use it in computing the average. Then C2 sends the value of average to C3 that uses it to compute the value of variance.
Code from part 4 of lab 5:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char ** argv)
{
if (argc < 3)
{
printf("Usage : ./a.out num1 num2 ");
return 0;
}
int i;
int length = argc-1;
float amount = (float)length;
float mean=0;
float average = 0;
float sum = 0;
float variance = 0;
float difference = 0;
float array[length];
for(i = 1; i<=length;i++){ //create array filled with arguments from command line
array[i-1] = atof(argv[i]);
}
if(fork() == 0) // create first child
{
for(i = 0;i<length;i++){ //calculate sum
sum = sum + array[i];
}
printf("C1 sum - %.2f ",sum); //print sum
exit(0); // exit with any value
}
else
{
wait(0);
if(fork() == 0) // create second child
{
for(i = 0;i<length;i++){ //calculate sum
sum = sum + array[i];
}
average = (float)sum/length; //calculate average
printf("C2 average - %.2f ",average); //pring average
exit(0); // exit
}
else
{
wait(0);
if(fork() == 0) //create third child
{
for(i = 0;i<length;i++){ //calculate sum
sum = sum + array[i];
}
average = (float)sum/length; //calculate average
for(i = 0;i<length;i++){ //calculate variance
difference = array[i]-average;
variance = variance + (difference*difference);
}
printf("C3 variance - %.2f ", (float)variance/length); // print variance
exit(0);
}
else
{
wait(0); // finally compilte after this wait
}
}
}
}
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char ** argv)
{
if (argc < 3)
{
printf("Usage : ./a.out num1 num2 ");
return 0;
}
int i;
int length = argc-1;
float amount = (float)length;
float mean=0;
float average = 0;
float sum = 0;
float variance = 0;
float difference = 0;
float array[length];
for(i = 1; i<=length;i++){ //create array filled with arguments from command line
array[i-1] = atof(argv[i]);
}
// create file descriptors
int fd_pipe1[2]; // Used to store two ends of first pipe
int fd_pipe2[2]; // Used to store two ends of second pipe
// check if pipe connections are successful
if ( -1 == pipe(fd_pipe1) )
{
fprintf(stderr, "Pipe creation failed" );
return 1;
}
if ( -1 == pipe(fd_pipe2) )
{
fprintf(stderr, "Pipe creation failed" );
return 1;
}
if(fork() == 0) // create first child
{
for(i = 0;i<length;i++){ //calculate sum
sum = sum + array[i];
}
printf("C1 sum - %.2f ",sum); //print sum
// close the reading end of pipe1 and write the Sum into it for reading by the other child.
close(fd_pipe1[0]); // Close reading end of first pipe
write(fd_pipe1[1], &sum, sizeof(sum)); // write sum into the pipe1
close(fd_pipe1[1]); // close the writing end of pipe after placing the data
exit(0); // exit with any value
}
else
{
wait(0);
if(fork() == 0) // create second child
{
// close the wrintg end of pipe1 and read the Sum from the same pipe.
close(fd_pipe1[1]); // Close wrintg end of first pipe
read(fd_pipe1[0], &sum, sizeof(sum)); // read sum from the pipe1
close(fd_pipe1[0]); // close the reading end of pipe after fetching the data
average = (float)sum/length; //calculate average
printf("C2 average - %.2f ",average); //pring average
// close the reading end of pipe2 and write the average into it for reading by the other child.
close(fd_pipe2[0]); // Close reading end of first pipe
write(fd_pipe2[1], &average, sizeof(average)); // write average into pipe2
close(fd_pipe2[1]); // close the writing end of pipe after placing the data
exit(0); // exit
}
else
{
wait(0);
if(fork() == 0) //create third child
{
// close the wrintg end of pipe2 and read the average from the same pipe.
close(fd_pipe2[1]); // Close wrintg end of second pipe
read(fd_pipe2[0], &average, sizeof(average)); // read average from the pipe2
close(fd_pipe2[0]); // close the reading end of pipe after fetching the data
for(i = 0;i<length;i++){ //calculate variance
difference = array[i]-average;
variance = variance + (difference*difference);
}
printf("C3 variance - %.2f ", (float)variance/length); // print variance
exit(0);
}
else
{
wait(0); // finally compilte after this wait
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.