\'m trying to make a program where the parent reads from a file some operations,
ID: 3740639 • Letter: #
Question
'm trying to make a program where the parent reads from a file some operations, passes them to the child with a pipe, and the child makes all the operations with bc. Later on, the child has to pass it back to the parent and parents will print.
I wrote the code. The main problem is though the parent is reading all the lines from the file and pass it to child but when I am getting output of bc being printed by the parent I am not getting output of all the lines. here is my code.
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
main(int argc, char *argv[])
{
int des,bytes;
int op;
char buffer[1000];
int i, rc;
int stdin_pipe_fds[2], stdout_pipe_fds[2];
char buf[100];
pipe(stdin_pipe_fds);
pipe(stdout_pipe_fds);
rc = fork();
if (rc == -1)
{
printf("fork failed ");
exit(-1);
}
if (rc == 0) /* child */
{
close(stdin_pipe_fds[1]);
close(stdout_pipe_fds[0]);
close(0);
dup(stdin_pipe_fds[0]);
close(stdin_pipe_fds[0]);
close(1);
dup(stdout_pipe_fds[1]);
close(stdout_pipe_fds[1]);
execlp("/usr/bin/bc", "usr/bin/bc","-l", NULL);
exit(0);
}
/* parent */
close(stdin_pipe_fds[0]);
close(stdout_pipe_fds[1]);
des=open("myfile.txt",O_RDONLY);
while ((bytes=read(des,buffer,sizeof(buffer)))>0){
op=write(stdin_pipe_fds[1], buffer, bytes);
buffer[op]=0;
printf("in: %s ",buffer);
}
if ((i = read(stdout_pipe_fds[0],buf,100)) > 0)
{
buf[i] = '';
printf("bc: %s ",buf);
}
close(stdin_pipe_fds[1]);
close(stdout_pipe_fds[0]);
}
here is the text of the file:
print "ABC DEF "
a = 765 + 8000
print "a is ", a, " "
44+55
22*5+1
8/3
a = 33
b = 22
c = a * 3 + b
c
Can you tell me in my code which things I have to change to get output of all the lines after bc operations
Explanation / Answer
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
main(int argc, char *argv[])
{
int des,bytes;
int op;
char buffer[1000];
int i, rc;
int stdin_pipe_fds[2], stdout_pipe_fds[2];
char buf[100];
pipe(stdin_pipe_fds);
pipe(stdout_pipe_fds);
rc = fork();
if (rc == -1)
{
printf("fork failed ");
exit(-1);
}
if (rc == 0) /* child */
{
close(stdin_pipe_fds[1]);
close(stdout_pipe_fds[0]);
close(0);
dup(stdin_pipe_fds[0]);
close(stdin_pipe_fds[0]);
close(1);
dup(stdout_pipe_fds[1]);
close(stdout_pipe_fds[1]);
execlp("/usr/bin/bc", "usr/bin/bc","-l", NULL);
exit(0);
}
/* parent */
close(stdin_pipe_fds[0]);
close(stdout_pipe_fds[1]);
des=open("myfile.txt",O_RDONLY);
while ((bytes=read(des,buffer,sizeof(buffer)))>0){
op=write(stdin_pipe_fds[1], buffer, bytes);
buffer[op]=0;
printf("in: %s ",buffer);
}
if ((i = read(stdout_pipe_fds[0],buf,100)) > 0)
{
buf[i] = '';
printf("bc: %s ",buf);
}
close(stdin_pipe_fds[1]);
close(stdout_pipe_fds[0]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.