Interprocess communication using pipe in Linux

2020-05-09 18:51发布

问题:

I have written my code for writing a number to pipe in linux. it is as under,but it is showing errors,can anyone help me on this.

Basically the problem statement for the program is as below:- One program will open a pipe, write a number to pipe. - Other program will open the same pipe, will read the number and print them. - Close both the pipes

int main()
{

int number;




FILE *fout;

fout = popen("  ","w");



pclose(fout);

return 0;

}

Now my question is what command should i give in the popen command option (as shown blank above) so as i can proceed further and write a number to pipe.

回答1:

First, create a named pipe using the mknod command. mknod pipe p. Then read and write using functions as if they are files. A node can be created using code also, using the mknod function. Look for error code EEXIST. More information here. http://linux.die.net/man/2/mknod



回答2:

You don't understand well how IPC and pipes work; please read a good book: Advanced Linux Programming has several chapters on these issues.

We don't have hours to explain difficult concepts covered by such good books. Take several hours to read them!

The library function popen(3) runs a command. Very probably, you don't have a p command on your system. I guess fp is NULL and errno is set.

popen is using pipe(2), fork(2), dup2(2), execve(2) and /bin/sh -c etc