Tuesday, 13 August 2013

How to correctly receive data from pipes?

How to correctly receive data from pipes?

So I wrote a program using pipes and execl to do some arithmetic. I'm not
sure how to debug pipes and different processes, but from what I can see
it has to have something to do with my pipes reading the numbers. So my
setup so far is the children are created, and based on thier given
operation, they execl to different programs that will calculate the
written values in the pipe. I don't know if I should implement a wait or
something in the parent after it has written its values to the children
before it reads the result. I keep getting Z as my output.
So this is one of the files I'm execl to and the other files look exactly
the same it just shows its corresponding operation:
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
using namespace std;
main(int argc, char *argv[])
{
int x, y, z;
while (read(0, (char *)&x, sizeof(int)) &&
read(3, (char *)&y, sizeof(int))) {
z = x * y;
if (argc > 1)
cerr << "multiply: " << x << " * " << y << " = " << z << endl;
write(1, (char *)&z, sizeof(int));
}
}
This is my code, which would actually be doing the calculating, which I
think the pipes are set up correctly. Just a little explanation on the
pipe setup, to save time. Child 0 -- reads from pipe 0 1 and writes to 2.
Child 1 reads from pipe 2 and 3 and writes to 4. The parent writes to 0 1
3 and reads from 4. My code:
for(int i =0; i<(2*n+1); i++)
pipe(pipes[i]);
for (int i=0; i <n; i++)
{
if(fork() == 0)
{
int p = 2*i;
close(0);
dup(pipes[p][0]);
close(3);
dup(pipes[p+1][0]);
close(1);
dup(pipes[p+2][1]);
switch(operations[i])
{
case '+':
execl("add","add", NULL);
case '-':
execl("subtract","multiply", NULL);
case '*':
execl("multiply","multiply", NULL);
case '/':
execl("divide","divide", NULL);
}
cout<< "No match for operation!";
}
}
int x, y, z, result;
while(!datafile.eof())
{
getline(datafile,line);
istringstream ins(line);
ins >> x >> y >> z;
write(0, (char *)&x, sizeof(int));
write(1, (char *)&y, sizeof(int));
write(3, (char *)&z, sizeof(int));
}
close(0);
close(1);
close(3);
while(read(pipes[2*n][1], (char *)&result,sizeof(int)))
cout<<result<<endl;
}
else
cout<<"Error reading file";
datafile.close();
return 0;
}

No comments:

Post a Comment