
Complete the attached template program “server.c” with the following functionalities:
- Parent process creates 5 children processes.
- Each child process has a pipe set up for sending a string to the parent.
- Parent is monitoring data availability in any of the pipes. If it finds available pipes, it will read strings from the pipes into memory locations pointed by buf[] array elements. Parent needs to store the bytes from pipe, pip[i], into a memory pointed by buf[i].
- Once the parent finds out that all its children processes are terminated, it will convert each string (stored in memory pointed by buf[i]) into an integer and add them up to find the total sum into a variable, sum.
- Variable sum will be returned at the end of the parent().
- Parent also monitors any keyboard stroke. If a user types in ‘q’ character in the keyboard, parent needs to print a message “Quit!” and return 0 immediately. Other characters will be simply ignored by the parent, and it will continue its execution.
- You may refer to the example program in the textbook p. 166-167.
The output from your program should be:
180690
* server -- creates five children and then add numbers sent by them and print the sum */
#include <sys/time.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#define MSGSIZE 6 char msg[5][MSGSIZE] = {"12345", "45689", "15689", "34592", "72375"};
int parent(int [5][2]);
int child(int [2], int);
int main() { int pip[5][2];
int i; /* create five communication pipes, and spawn five children */ for(i = 0; i < 5; i++) {
if(pipe(pip[i]) == -1) perror("pipe call");
switch (fork() ) { case -1: /* error */ perror("fork call");
case 0: /* child */ child(pip[i], i);
}
}
int sum = parent(pip);
printf("%d", sum);
exit(0); } /* parent sits listening on all five pipes, once it hears from all 5 pipes it will convert each string, add them up, and return its sum as an integer */
int parent(int p[5][2]) /* code for parent */ { //// buf[5][] is used to 5 strings to be received from 5 children processes!
char buf[5][MSGSIZE], ch;
fd_set set, master;
int i; int sum=0; /* close all unwanted write file descriptors */
//// Fill up this part! /* set the bit masks for the select system call */
//// Fill up this part! ////
//// /* Select is called with no timeout, it will block until an event occurs */
while(set = master, select( /* Fill up this part! */ ) > 0) {
/* we mustn't forget information,Information on standard input, * i.e. fd=0 */ /*** when a user presses 'q' character, server needs to print a message "Quit!" and return 0 immediately if a user presses other character, server needs to ignore it! ***
/ //// //// Fill up this part! ////
for(i = 0; i < 5; i++) {
//// check which pipes have data bytes available for reading //// then, read the bytes into proper target buf[] array //// bytes sent by a child (connected through pip[i]) should //// be stored into buf[i][] //// If read() fails, then use perror to print an error message and return -1 ////
//// Fill up this part! ////
} /* The server will return the sum of the numbers it reaceived from children to the main program if all its children have died */
if (waitpid (-1, NULL, WNOHANG) == -1) {
int i=0; for(i=0; i<5; i++) sum += atoi(buf[i]); return sum; } } } /* id - child identifier - from 0 through 4 */
int child(int p[2], int id) {
int count;
close(p[0]); write(p[1], msg[id], MSGSIZE); sleep(getpid() % 4); exit(0);
}

Step by stepSolved in 2 steps

- Please answer the below question for operating system using shell script. write a program in bash that has a user_defined function array_sum that takes each element of array as argument andcalculates their sum. At each step the sum will printed and then the final sum will be printed. For example:Input Array:[2 3 7 9]Output: 2 5 12 21final sum: 21arrow_forwardProblem: Implement a part of functionality for the Netflix DVD queue. It's a service that allows a user to create a list of desired movies and then sends DVDs with movies on top (what we called head in our lectures) of this list to the subscriber one at a time. A subscriber should be able to create a list of desired movies and manipulate the order in a movie queue in their account. Your program will implement some of the desired functionality by storing the list of movie titles in a linked list. You are provided with the following files available in the "Downloadable files" section: MovieList.h contains a class declaration for the class that represents a list of movies. top refers to the head position of a list and bottom refers to the tail or end position of a list. Driver.cpp contains the main function you can use to test your implementation. *** NOTE: Please do not change any of the function names since our automated test script calls those functions. Changing function names or…arrow_forwardin C++ answer the following question in the image :arrow_forward
- Create a computer programme to arrange objects in a stack so that the smallest ones are on top. Although you are allowed to utilise an extra temporary stack, you are not allowed to transfer the components into another data structure (such an array). The following operations are supported by the stack: push, pop, peek, and is Empty.arrow_forwardYou will create two programs. The first one will use the data structure Stack and the other program will use the data structure Queue. Keep in mind that you should already know from your video and free textbook that Java uses a LinkedList integration for Queue. Stack Program Create a deck of cards using an array (Array size 15). Each card is an object. So you will have to create a Card class that has a value (1 - 10, Jack, Queen, King, Ace) and suit (clubs, diamonds, heart, spade). You will create a stack and randomly pick a card from the deck to put be pushed onto the stack. You will repeat this 5 times. Then you will take cards off the top of the stack (pop) and reveal the values of the cards in the output. As a challenge, you may have the user guess the value and suit of the card at the bottom of the stack. Queue Program There is a new concert coming to town. This concert is popular and has a long line. The line uses the data structure Queue. The people in the line are objects…arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





