Question

Create a program in C where pipes are used. The program's execution line should indicate the...

Create a program in C where pipes are used. The program's execution line should indicate the number of children and the elements of an array that will compute their sums. The number of children can only be 1, 2, or 3.

./myprogram 3 1 2 3 4 6 12 (3 indicates the number of children in the array)

Example for executing in linux terminal:Assuming parent has pid 5, child one has pid 6, child two has pid 7, and child three has pid 8. We would want to see the following output:

>>I am the parent with pid: 5 sending the array: 1,2,3,4,6,12.

>> I am the child with pid: 6 adding the array 1, 2 and sending the partial sum 3

>> I am the child with pid: 7 adding the array 3, 4 and sending the partial sum 7

>> I am the child with pid: 8adding the array 6, 12 and sending the partial sum 18

>> I am the parent with pid 5 receiving from child with pid 6 the partial sum of 3, for child with pid 7 the partial sum of 7, and for child with pid 8 the partial sum of 18, with a total sum of 28

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include<unistd.h>
#include<stdlib.h>

int main(int argc, char *argv[])
{
int fd1,fd2,fd3;
fd1=fork();
fd2=fork();
fd3=fork();
int f[2],f1[2],f2[2],f3[2],f4[2],f5[2],val=0,val1=0,val2=0,val3=0,val4=0,val5=0;
pipe(f),pipe(f1),pipe(f2),pipe(f3),pipe(f4),pipe(f5);

if(atoi(argv[1])>3){
printf("Child processes exceeded!! Enter less than 3 childs");
exit(0);
}
else{
if(fd1>0 && fd2>0 && fd3>0){
printf("I am the parent with pId: %d",getpid());
printf(" sending the array: ");
for(int i=2;i<argc;i++){
printf("%d ",atoi(argv[i]));
}
close(f[1]);
close(f1[1]);
close(f2[1]);
close(f3[1]);
close(f4[1]);
close(f5[1]);
read(f[0],&val,sizeof(val));
read(f1[0],&val1,sizeof(val1));
read(f2[0],&val2,sizeof(val2));
read(f3[0],&val3,sizeof(val3));
read(f4[0],&val4,sizeof(val4));
read(f5[0],&val5,sizeof(val5));
printf("\nI am the parent with pid %d receiving from child with pid %d the partial sum of %d, for child with pid %d the partial sum of %d, and for child with pid %d the partial sum of: %d, with a total sum of: %d\n",getpid(),val,val1,val2,val3,val4,val5,val1+val3+val5);
close(f[0]);
close(f1[0]);
close(f2[0]);
close(f3[0]);
close(f4[0]);
close(f5[0]);
}
if(fd1==0 && fd2>0 && fd3>0){
close(f[0]);
close(f1[0]);
val=getpid();
printf("\nI am the child with pId: %d",getpid());
printf(" adding the array: %d, %d",atoi(argv[2]),atoi(argv[3]));
val1=atoi(argv[2])+atoi(argv[3]);
printf(" sending the partial sum: %d",atoi(argv[2])+atoi(argv[3]));
write(f[1],&val,sizeof(val));
write(f1[1],&val1,sizeof(val1));
close(f[1]);
close(f1[1]);
}
if(fd1>0 && fd2==0 && fd3>0){
close(f2[0]);
close(f3[0]);
val2=getpid();
printf("\nI am the child with pId: %d",getpid());
printf(" adding the array: %d, %d",atoi(argv[4]),atoi(argv[5]));
val3=atoi(argv[4])+atoi(argv[5]);
printf(" sending the partial sum: %d",atoi(argv[4])+atoi(argv[5]));
write(f2[1],&val2,sizeof(val2));
write(f3[1],&val3,sizeof(val3));
close(f2[1]);
close(f3[1]);
}
if(fd1>0 && fd2>0 && fd3==0){
close(f4[0]);
close(f5[0]);
val4=getpid();
printf("\nI am the child with pId: %d",getpid());
printf(" adding the array: %d, %d",atoi(argv[6]),atoi(argv[7]));
val5=atoi(argv[6])+atoi(argv[7]);
printf(" sending the partial sum: %d",atoi(argv[6])+atoi(argv[7]));
write(f4[1],&val4,sizeof(val4));
write(f5[1],&val5,sizeof(val5));
close(f4[1]);
close(f5[1]);
}
}
return(0);
}

Add a comment
Know the answer?
Add Answer to:
Create a program in C where pipes are used. The program's execution line should indicate the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a se...

    *Write a parallel program pie.c in C or C++ (pie.cc) for Linux that computes an approximation of the number π using a series with N+1 terms.* --The series sum is partitioned in T non-overlapping partial sums, each computed by T separate child processes created with the fork() library function.* --This program demonstrates data parallelism and interprocess communication using pipes. Each child process could perform a (potentially) long computation on a separate CPU (or core). Depending on the computer architecture, the...

  • I am using C++ Write a program to determine if a gird follows the rules to...

    I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...

  • Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of...

    Write a C function  f such that … function  f accepts, as input, … a two-dimensional array of integers in which each row has three columns the number of rows in the array function  f returns the sum of the odd integers in the given array of integers and returns zero if there are no odd integers in the array COMPILER STACK TRACE None PROGRAM EXECUTION STACK TRACE None COMPILER COMMAND LINE ARGUMENTS -lm INPUT OF THE TEST CASE 1 #define NUM_ROWS 5...

  • Language is C programming Student ID should be 8 numbers long. create a program to read...

    Language is C programming Student ID should be 8 numbers long. create a program to read your student ID number and store it in an array. Input can not be hard wired, I have to be able to input my number Then change the first number to a letter. If the same number repeats in other parts of your student ID, you must change that number to a letter as well. print original array and modified array as your output....

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

  • This is a C++ assignment that I'm trying to create and would like some help understanding...

    This is a C++ assignment that I'm trying to create and would like some help understanding while loops, with possible integration of for loops and if statements. This program only uses while, for, and if. I have some code that I have started, but where to go from there is what's giving me some trouble. This is involves a sentinel controlled while loop, and there are a lot of specifications below that I must have in the program. The program...

  • Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

    Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; }; - INPUT i k : Insert the node with the key value of k in...

  • Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

    Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; };    - INPUT i k : Insert the node with the key value of k in...

  • Java 8 9m left Jav 27 28 ALL 29 0 Given an integer array, separate the...

    Java 8 9m left Jav 27 28 ALL 29 0 Given an integer array, separate the values of the array into two subsets, A and B, whose intersection is null and where the addition of the two subsets equals the entire array. The sum of values in set A must be strictly greater than the sum of values in set B, and the number of elements in set A must be minimal. Return the values in set A in increasing...

  • This is a C++ assignment that I'm trying to create and would like some help understanding...

    This is a C++ assignment that I'm trying to create and would like some help understanding while loops, with possible integration of for loops and if statements. This program only uses while, for, and if. I have some code that I have started, but where to go from there is what's giving me some trouble. This is involves a sentinel controlled while loop, and there are a lot of specifications below that I must have in the program. The program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT