I was trying to complete an assignment for my uni, but ran into an issue – the pipes seem to not be working on my computer, or maybe I’m missing some small detail.
Here’s how I configure the pipe:
int pip[2];
if (pipe(pip) < 0) {
logn(ERR_PIPE);
return ERR; //ERR is just -1 and ERR_PIPE is some text
}
And here’s how I try to put some text to the pipe and read it from the other end:
char* buffer;
asprintf(&buffer, "%d, Hi\n", 11);
write(pip[0], buffer, 8);
free(buffer);
char buffer2[8];
read(pip[1], buffer2, 8);
log(buffer2); //log(x) is defined as write(1, x, strlen(x))
And there’s no output. I tried doing it with a fork and closing one side of the pipe in parent process and the other side in the child process, but it doesn’t work either.
You always need to check what
write
andread
return.As a hint, your
log
function writes to descriptor1
. Your code write to the pipe descriptor0
and read from the pipe descriptor1
. Those array elements correspond to the standard input and output descriptor.You need to include more code and I suggest you go back to the
fork
model.write
on pipe is blocking by default if I remember, so your code would have no chance of working if that’s the case.pipe are working on your machine. If pipe weren’t working, nothing would work, and your machine wouldn’t have never reached the point were you can even stat to do something with it.
@Someprogrammerdude both read and write return -1
Show 3 more comments