Question

Suppose we have three programs aaa, bbb, ccc which are already developed, compiled and ready to execute. The first two, namely aaa and bbb, simply do some random amount of computation and write a character (respectively, A and B to STDOUT_FILENO in a loop that is repeated 1000 times. If we run both programs at the same time, we might see different numbers of As and Bs such as AABBBAAABBBBABB. . . on the screen. The third program, namely ccc, reads a sequence of characters from STDIN_FILENO and simply counts the number of the same characters that appear consecutively and writes that information to STDOUT_FILENO. For example, if we give the above possible sequence of As and Bs to ccc, it will print A 2, B 3, A 3, B 4, A 1, B 2, .. . on the screen. Now you are asked to write a program (say xyz.c) which can execute aaa, bbb, and ccc as children and connect them in a way that the output of aaa and bbb will go into a single pipe. Then, ccc will get the mixed sequence of characters from this pipe and do its job. Your program (parent) will wait for all three children to be done, then it will quit! Convince yourself that your program should create the following relation through a pipe! childl (aaa) [1] ---> I---| child2 (bbb) [1] ->I I mypipe -> [0]child3 (ccc) You can ignore most of the error checking to make your code clear, but you need to close all unnecessary file/pipe descriptors and need to check what fork0 returns to detect child/parent proceees and/or what read0/writeO returns to detect end of file etc. To execute a given program simply use execl (aaa, aaa , NULL); etc. /your implementation of xyz.c / #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <sys/stat.h> int main (int argc, char argv[]) int mypipe [2]; int child1=1, child2-1, child3-1; /*parent: create mypipe and children processes (3pt)

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

aaa.c:

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

int main(void)
{
int i=0;

for(i=0;i<100;i++)
{
printf("%c", 'A');
usleep(200); //sleep in microsecs
}

return 0;
}

bbb.c

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

int main(void)
{
int i=0;

for(i=0;i<100;i++)
{
printf("%c", 'B');
usleep(200);
}

return 0;
}

ccc.c

#include<stdio.h>

int main(void)
{

char arr[2001];

scanf("%s",arr);

int i=0;
int size=sizeof(arr)/sizeof(int);

char c=arr[0];
int count=0;
for(i=0;i<size;i++)
{
if(c==arr[i])
{
count++;
}
else
{
printf("%c %d,",c,count);
c=arr[i];
count=1;
}
}

printf("\n");

return 0;
}

xyz.c

#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/wait.h>

int main(int argc, char *argv[])
{

int mypipe[2];
int child1=1,child2=1,child3=1;

pipe(mypipe);

child1=fork();
child2=fork();
child3=fork();

if(child1==0)
{
close(mypipe[0]);
dup2(mypipe[1],1);
close(mypipe[1]);
execl("aaa","aaa",NULL);
perror("cannot start aaa");
return 1;
}

if(child2==0)
{
close(mypipe[0]);
dup2(mypipe[1],1);
close(mypipe[1]);
execl("bbb","bbb",NULL);
perror("cannot start bbb");
return 1;
}

if(child3==0)
{
close(mypipe[1]);
dup2(mypipe[0],0);
close(mypipe[0]);
execl("ccc","ccc",NULL);
perror("cannot start ccc");
return 1;
}
  
return 0;
}

output:

A 200,B 200,

Note: The order of the output may differ based on the architecture of the CPUs.

Add a comment
Know the answer?
Add Answer to:
Suppose we have three programs aaa, bbb, ccc which are already developed, compiled and ready to...
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
  • Suppose that you have three programs that are suppose to print a house diagram in a...

    Suppose that you have three programs that are suppose to print a house diagram in a collaborative manner. (i) prog1.c #include <stdio.h>      /* Input/Output */ #include <stdlib.h>     /* General Utilities */ int main() {     printf(“                      ^^^^^^^^^^^^                       \n”);     printf(“           ^^^^^^^^^^^            ^^^^^^^^^^^            \n”);     printf(“^^^^^^^^^^^                                  ^^^^^^^^^^^ \n”);     printf(“     |                                            |      \n”);     printf(“     |                                            |      \n”);     exit(1); } (i) prog2.c #include <stdio.h>      /* Input/Output */ #include <stdlib.h>     /* General Utilities */ int main() {     printf(“     |                                             |    ...

  • I have the following code....from the previous lab....the above needs to be added to what is...

    I have the following code....from the previous lab....the above needs to be added to what is already existing. ALSO MODIFY SEMAPHORES TO USE pthreads instead of the pipe constructs P() & V() #include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> #include <sys/stat.h> void printStat(char *filename); //Main int main(int argc, char *argv[]) { //Process Id (storing)    pid_t pid;    int j;    //printf("Welcome to Project Three\n”);    // For loop*/    for (j = 1; j...

  • operating system programming i need ans and explen after 20 min 2 When we are implementing...

    operating system programming i need ans and explen after 20 min 2 When we are implementing the following program, then we press "CTRL+C" on the keyboard; that will cause: #include <stdio.h> #include<signal.h> #include <stdlib.h> #include<unistd.h> void handle_sigint (int sig) { } printf("Caught signal $d\n", sig); int main(int argc, char *argv[]) { signal (SIGCONT, handle_sigint); while (1) { printf("the process id is $d \n",getpid()); sleep (1); } return 0; } O print the sentence" Caught signal 2" on the terminal 53,65,67,37,14,98,122,124,183...

  • In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 st...

    In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 standard input (stdin) 1 standard output (stdout) 2 standard error (stderr) By default, the command interpreter (shell) reads keyboard input from file descriptor 0 (stdin) and writes output to file descriptor 1 (stdout), which appears on the screen. As you explored in Lab 2, input/output can be...

  • Update the program in the bottom using C++ to fit the requirements specified in the assignment....

    Update the program in the bottom using C++ to fit the requirements specified in the assignment. Description For this assignment, you will be writing a single program that enters a loop in which each iteration prompts the user for two, single-line inputs. If the text of either one of the inputs is “quit”, the program should immediately exit. If “quit” is not found, each of these lines of input will be treated as a command line to be executed. These...

  • Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also,...

    Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also, include a screen shot of the output and terminal window of each command you used. Read carefully to do this task please. shell.c code given below. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <string.h> int main() { int PID; char lineGot[256]; char *cmd; while (1){ printf("cmd: "); fgets(lineGot, 256, stdin); // Get a string from user (includes \n) cmd = strtok(lineGot, "\n");...

  • *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...

  • It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #inclu...

    It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) {    this->type = type;    this->name = name; } //returns the type. string File::getType() {    return type; } //returns the name of file. string File::getName() {    return name; } File.h #ifndef __FILE_H__ #define...

  • The last 3 cases are tests .cpp files to test the code. The language of this...

    The last 3 cases are tests .cpp files to test the code. The language of this code must be C++ because that is the only I am familiar with. Soreland, a software company, is planning on releasing its first C++ compiler with the hopes of putting MiniSoft's Visual C++ out of business (that reminds me of another story). Consequently, Soreland has contracted with the Fruugle Corporation to design and build part of their compiler. Of course, since Fruugle gets all...

  • Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array...

    Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array of struct student_info ( char abc123171 char name [101 double GPA; To simplify the tasks, the server should create a static array of 10 students with random abc123, name, and GPA in the server program. Then the server waits for clients. When a client...

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