使用管道实现进程间的双向通信
2012-03-29 23:13
459 查看
#include<stdio.h>
#include<unistd.h>
int main(int argc, char * argv[])
{
int f_des1[2];
int f_des2[2];
int pid;
char msg[BUFSIZ];
char p_msg[BUFSIZ];
if(argc != 2)
{
printf("Usage: %s message\n",argv[0]);
return 1;
}
if(pipe(f_des1) == -1)
{
printf("cannot create the IPC pipe");
return 1;
}
pid = fork();
if(pid == -1)
{
printf("cannot create new process");
return 1;
}
else if(pid == 0)
{
close(f_des1[1]);
close(f_des2[0]);
if(read(f_des1[0], msg, BUFSIZ) == -1)
{
printf("child process cannot read data from pipe");
return 1;
}
else
{
printf("in child proccess, receive message: %s\n",msg);
}
if(write(f_des2[1], msg, strlen(msg)) == -1)
{
printf("child process cannot write data to pipe");
return 1;
}
else
{
printf("in child process , send message back: %s\n", argv[1]);
}
exit(0);
}
else
{
close(f_des1[0]);
close(f_des2[1]);
if(write(f_des1[1], argv[1], strlen(argv[1])) == -1)
{
printf("parent process cannot write data to pipe");
return 1;
}
else
{
printf("in parent process , send mwssage: %s\n",argv[1]);
}
if(read(f_des2[0], p_msg, BUFSIZ) == -1)
{
printf("parent process cannot read data from pipe");
return 1;
}
else
{
printf("in parent proccess, receive message: %s\n",p_msg);
}
wait(NULL);
exit(0);
}
return 0;
}
#include<unistd.h>
int main(int argc, char * argv[])
{
int f_des1[2];
int f_des2[2];
int pid;
char msg[BUFSIZ];
char p_msg[BUFSIZ];
if(argc != 2)
{
printf("Usage: %s message\n",argv[0]);
return 1;
}
if(pipe(f_des1) == -1)
{
printf("cannot create the IPC pipe");
return 1;
}
pid = fork();
if(pid == -1)
{
printf("cannot create new process");
return 1;
}
else if(pid == 0)
{
close(f_des1[1]);
close(f_des2[0]);
if(read(f_des1[0], msg, BUFSIZ) == -1)
{
printf("child process cannot read data from pipe");
return 1;
}
else
{
printf("in child proccess, receive message: %s\n",msg);
}
if(write(f_des2[1], msg, strlen(msg)) == -1)
{
printf("child process cannot write data to pipe");
return 1;
}
else
{
printf("in child process , send message back: %s\n", argv[1]);
}
exit(0);
}
else
{
close(f_des1[0]);
close(f_des2[1]);
if(write(f_des1[1], argv[1], strlen(argv[1])) == -1)
{
printf("parent process cannot write data to pipe");
return 1;
}
else
{
printf("in parent process , send mwssage: %s\n",argv[1]);
}
if(read(f_des2[0], p_msg, BUFSIZ) == -1)
{
printf("parent process cannot read data from pipe");
return 1;
}
else
{
printf("in parent proccess, receive message: %s\n",p_msg);
}
wait(NULL);
exit(0);
}
return 0;
}
相关文章推荐
- Android使用Messenger实现进程间双向通信
- 使用pipe()与fork()函数通过管道实现父子进程之间的通信
- Perl 使用管道实现进程间的通信
- 使用管道实现两个进程之间的通信
- Linux----进程间通信-管道与两个命名管道实现进程双向通信
- 使用管道实现进程间的通信
- Linux下的有名管道(05)---使用两个管道实现两个进程之间的通信(对讲机模式)
- 【C语言】【unix c】如何使用管道实现两个进程间的通信
- Linux下的有名管道(06)---使用两个管道实现两个进程之间的通信(手机模式)
- Linux进程间通信-----使用数据报套接字实现两个进程之间的通信
- System.IO系列:局域网内多线程使用命名管道在进程之间通信实例
- 多线程 : 使用 wait 和 notify 实现进程间同步通信
- 在Android Studio中使用AIDL实现进程之间的通信
- 利用匿名管道技术实现本地进程通信
- 基础总结篇之四:Service完全解析——使用AIDL实现进程间的通信
- 采用虚拟命名管道的字符设备和阻塞型I/O实现进程间的通信实现KWIC程序
- 使用AIDL实现进程间的通信之复杂类型传递
- [Linux管道和IPC]使用信号量和共享内存进行父子进程通信
- C#使用命名管道通过网络在进程之间进行通信
- 使用Handler实现Service和Activity之间的双向通信