Interprocess communication using pipe in Linux

一笑奈何 提交于 2019-12-20 07:58:29

问题


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



来源:https://stackoverflow.com/questions/18159051/interprocess-communication-using-pipe-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!