Write a project in C, 2 programs to pass information from one to the other. Prog
ID: 3819037 • Letter: W
Question
Write a project in C, 2 programs to pass information from one to the other.
Program 1 must be called ‘proj3’. It will open a named pipe (pick a name) and fork; the child will execute Program 2. In the parent, Program 1 will read from standard input pairs of integers and writes the sum and difference of the integers to the pipe. Finally it waits for the child to terminate, prints the child’s status code, and it terminates.
Program 2 (pick a name) will open the named pipe and read pairs of integers. From these it will recover the original pair the Program 1 read and print the original pair to standard output. When it sees a pair of zeros, it will terminate and return the status code of the number of [non-zero] pairs it read. I want the .c and any .h files that you need to create the executables.
I also want a Makefile with a target ‘all’ that makes both of the executables. I do not want you to turn in any .o file or the executables. Nota the only name restrictions are the Makefile must be called either ‘Makefile’ or ‘makefile’, and the parent executable must be called ‘proj3’.
Explanation / Answer
//prog3.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[]){
int result;
int pid;
if (argc != 2) {
fprintf(stderr, "Usage: ./a.out fifoname ");
exit (1);
}
result = mknod ("mypipe", S_IRUSR| S_IWUSR|S_IFIFO, 0);
if (result < 0) {
perror ("mknod");
exit (2);
}
pid = fork();
if(pid == 0)
{
//child prcess exec child program for reading here
}
else
{
//parent process wait for
int a, b, sum, diff;
printf("Enter the two integer: ") ;
scanf("%d%d",&a,&b);
sum = a + b;
diff = a - b;
write(result,(void*)&sum,sizef(int));
write(result,(void*)&sum,sizef(int));
}
}
---------------------------------------------
//child read
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int result;
int sum, diff;
result = mknod ("mypipe", S_IRUSR| S_IWUSR|S_IFIFO, 0);
read(result,(void*)&sum,sizeof(int));
read(result,(void*)&diff,sizeof(int));
printf("Read sum = %d Read diff f two integers = %d ",sum,diff);
return 0;
}
-----------------------------
//Makefile
Makefile
proj3: proj3.o
gcc -g -o proj3 proj3.o
proj3.o: proj3.c
gcc -c -g proj3.c
clean:
rm *.o proj3
--------------------------------------------------------------
//childReadpipe.c
//childReadpipe.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[]){
int result;
int sum,diff;
result = mknod ("mypipe", S_IRUSR| S_IWUSR|S_IFIFO, 0);
if (result < 0) {
perror ("mknod");
exit (2);
}
read(result,(void*)&sum,sizeof(int));
read(result,(void*)&diff,sizeof(int));
printf("Sum of two integers = %d and difference of integers= %d ",sum,diff);
return 0;
}
---------------------
//Makefile for child
childReadpipe: childReadpipe.o
gcc -g -o childReadpipe childReadpipe.o
childReadpipe.o: childReadpipe.c
g++ -c -g childReadpipe.c
clean:
rm *.o childReadpipe
---------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.