Question

Modify the template program below and include the following additional functionalities: - Instead of 3 children...

Modify the template program below and include the following additional functionalities:

- Instead of 3 children processes, create 6 children processes.

- 6 children processes are connected to the parent process via 6 pipes, one for each pair, child1-parent, child2-parent, child3-parent, ....

- Child keeps writing to the pipe with 1 second apart between consecutive writes (as in child() in p. 167).

- Keyboard inputs need to be monitored as in the original code.

- Parent keeps reading from the 6 pipes and keyboard whenever any of them has data ready for reading.

- You have to use the “select” system call with a non-blocking feature as was done in the template program.

#include
#include

#define MSGSIZE 6

char *msg1 = "hello";
char *msg2 = "bye!!";

void parent(int [][]);
int child(int []);

main()
{
int pip[3][2];
int i;

for(i = 0; i < 3; i++)
{
if(pipe(pip[i]) == -1)
fatal("pipe call");

switch(fork()){
case -1:
fatal("fork call");
case 0:
child(pip[i]);
}
}

parent (pip);

exit(0);
}

void parent(int p[3][2])
{
char buf[MSGSIZE], ch;
fd_set set, master;
int i ;

for(i = 0; i < 3; i++)
close(p[i][1]);

FD_ZERO(&master);
FD_SET(0, &master);

for(i = 0; i < 3; i++)
FD_SET(p[i][0], &master);

while(set = master, select(p[2][0]+1, &set, NULL, NULL, NULL) > 0)
{
if(FD_ISSET(0, &set))
{
printf("From standard input...");
read(0, &ch, 1);
printf("%c\n", ch);
}

for(i = 0; i < 3; i++)
{
if(FD_ISSET(p[i][0], buf, &set))
{
if(read(p[i][0], buf, MSGSIZE)>0)
{
printf("Message from child%d\n", i);
printf("MSG=%s\n", buf);
}
}
}

if(waitpid(-1, NULL, WNOHANG) == -1)
return;
}
}

int child(int p[2])
{
int count;

close(p[0]);

for(count = 0; count < 2; count++)
{
write(p[1], msg1, MSGSIZE);
sleep(getpid() % 4);
}

write(p[1], msg2, MSGSIZE);
exit(0);
}

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

// Chegg1_C++.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#define MSGSIZE 6

char *msg1 = "hello";
char *msg2 = "bye!!";

void parent(int [][]);
int child(int []);

main()
{
   int pip[6][2]; // 6 children processes are connected to the parent process via 6 pipes
   int i;

   for(i = 0; i < 6; i++) //"Instead of 3 children processes, create 6 children processes."
   {
       if(pipe(pip[i]) == -1)
       fatal("pipe call");

       switch(fork())
       {
       case -1:
       fatal("fork call");
       case 0:
       child(pip[i]);
       }
   }

parent (pip);
exit(0);
}

void parent(int p[6][2])
{
   char buf[MSGSIZE], ch;
   fd_set set, master;
   int i ;

   for(i = 0; i < 6; i++)
   close(p[i][1]);

   FD_ZERO(&master);
   FD_SET(0, &master);

   for(i = 0; i < 6; i++)
   FD_SET(p[i][0], &master);

   while(set = master, select(p[2][0]+1, &set, NULL, NULL, NULL) > 0)
   {
       if(FD_ISSET(0, &set))
       {
           printf("From standard input...");
           read(0, &ch, 1);
           printf("%c\n", ch);
       }

       for(i = 0; i < 6; i++)
       {
           if(FD_ISSET(p[i][0], buf, &set))
               {
                   if(read(p[i][0], buf, MSGSIZE)>0)
                       {
                       printf("Message from child%d\n", i);
                       printf("MSG=%s\n", buf);
                       }
               }
       }

       if(waitpid(-1, NULL, WNOHANG) == -1)
       return;
   }
}

int child(int p[2])
{
   int count;

   close(p[0]);

   for(count = 0; count < 2; count++)
   {
       write(p[1], msg1, MSGSIZE); //Child keeps writing to the pipe with 1 second apart between consecutive writes
       sleep(1);
   }

   write(p[1], msg2, MSGSIZE);
   exit(0);
}

Add a comment
Know the answer?
Add Answer to:
Modify the template program below and include the following additional functionalities: - Instead of 3 children...
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 code that forks into two processes: a parent process, and a child process. Same as...

    Write code that forks into two processes: a parent process, and a child process. Same as the Regular version, except that your code must also be able to handle negative integers input from the command-line. If I call your code this way:     a03 -3 5 -7 The parent process should print out: Assignment 3     sum = -5 The sums produced from the test input I use will be in the range [-128 .. 127] -------------------------------------------------------------------------------------------------------------------- // Numbers from...

  • Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork...

    Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork a child process*/ pid = fork(); if (pid<0){ /*error occured*/ fprintf(stderr,"Fork failed\n"); return 1; } else if (pid == 0) { /*child process */ printf("I am the child %d\n",pid); execlp("/bin/ls","ls",NULL); } else { /*parent process */ /*parent will wait for the child to complete*/ printf("I am the parent %d\n",pid); wait(NULL); printf("Child Complete\n"); } return 0; } 1. Write program codes for 3.21 a and...

  • Write code that forks into two processes: a parent process, and a child process. Your code...

    Write code that forks into two processes: a parent process, and a child process. Your code will be called with command-line arguments consisting of negative integers. Do not worry about bad command-line arguments such as "xyz". Your code will not be tested in this way. The parent process will take the arguments to main(), convert them into ints by calling atoi(), and send those ints one at a time to the child process through a pipe (one call to write()...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • Modify the program in Figure 8.18 so that the following two conditions are met: 1. Each...

    Modify the program in Figure 8.18 so that the following two conditions are met: 1. Each child terminates abnormally after attempting to write to a location in the read-only text segment. 2. The parent prints output that is identical (except for the PIDs) to the following: child 12255 terminated by signal 11: Segmentation fault child 12254 terminated by signal 11: Segmentation fault Modify your solution to Problem 8.24 so that one (and only one) child installs a Segmentation-fault handler which...

  • Rewrite the program as a set of two programs that utilize a named pipe “ages” in...

    Rewrite the program as a set of two programs that utilize a named pipe “ages” in the current folder for IPC and implement the same function as discussed above. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> int main(void) {         int     fd[2], nbytes;         pid_t   childpid;         int users = 95;         char mystring[80];         char    readbuffer[80];         char digit1,digit2;         digit1 = (char) users%10;         digit2 = (char ) users/10;         mystring[0] = digit1;         mystring[1] = digit2;...

  • Consider the following program: 1int main) 2nt count1; 3 int pid 0,pid2-0 4if ((pid - fork))) cou...

    Consider the following program: 1int main) 2nt count1; 3 int pid 0,pid2-0 4if ((pid - fork))) count-count 2; printf("%d ", count); 8 if (count 1) count++ pid2-fork) printf("%d ", 12 13 14 if(pid2){ 15 16 17 18 19 > count); wa i tpid(pid2, NULL, θ); countcount*2; printf("%d ", count); a. How many processes are created during the execution of this program? Explain b. List all the possible outputs of the program c. If we delete line 15 (the waitpid) show...

  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

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