Suppose we have three programs aaa, bbb, ccc which are already developed, compil
ID: 3586541 • Letter: S
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 #include #include #include int main (int argc, char argv[]) int mypipe [2]; int child1=1, child2-1, child3-1; /*parent: create mypipe and children processes (3pt)Explanation / Answer
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(" ");
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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.