网络编程 笔记(七) 进程间通信
2015-10-22 16:09
591 查看
通过管道(PIPE)实现进程间通信
#include <stdio.h> #include <unistd.h> #define BUF_SIZE 30 int main(int argc, char *argv[]) { int fds[2]; char str[] = "Who are you?"; char buf[BUF_SIZE]; pid_t pid; pipe(fds); pid = fork(); if (pid == 0) { write(fds[1], str, sizeof(str)); } else{ read(fds[0], buf, BUF_SIZE); puts(buf); } return 0; }
通过管道(PIPE)实现进程间双向通信
#include <stdio.h> #include <unistd.h> #define BUF_SIZE 30 int main(int argc, char *argv[]) { int fds[2]; char str1[] = "Who are you?"; char str2[] = "Thank you for your message"; char buf[BUF_SIZE]; pid_t pid; pipe(fds); pid = fork(); if (pid == 0) { write(fds[1], str1, sizeof(str1)); sleep(2); read(fds[0], buf, BUF_SIZE); printf("Child proc output : %s\n", buf); } else{ read(fds[0], buf, BUF_SIZE); printf("Parent proc output : %s\n", buf); write(fds[1], str2, sizeof(str2)); sleep(3); } return 0; }
向管道传递数据时,先读到进程会把数据取走
双管道双向进程间通信
#include <stdio.h> #include <unistd.h> #define BUF_SIZE 30 int main(int argc, char *argv[]) { int fds1[2], fds2[2]; char str1[] = "Who are you?"; char str2[] = "Thank you for your message"; char buf[BUF_SIZE]; pid_t pid; pipe(fds1), pipe(fds2); pid = fork(); if (pid == 0) { write(fds1[1], str1, sizeof(str1)); read(fds2[0], buf, BUF_SIZE); printf("Child proc output : %s\n", buf); } else{ read(fds1[0], buf, BUF_SIZE); printf("Parent proc output : %s\n", buf); write(fds2[1], str2, sizeof(str2)); sleep(3); } return 0; }
相关文章推荐
- linux0.99网络模块-数据链路层(发送)
- RX7640 reset MP卡后的网络设置过程
- asiHttpRequest 取消访问的时候跳exc_bad_access
- 【软考】网络—OSI七层模型
- [IOS] ASIHttpRequest库使用过程中发现的几个小问题
- kernel: TCP: time wait bucket table overflow 的解决
- Winpcap网络开发库入门,分类: C/C++/VC++
- HttpClient的用法
- 一次完整的HTTP事务是怎样一个过程?
- HTTP content-type对照表
- 记录一下centos安装httpd+php+mysql的过程
- DHCP 流程
- 分类算法之贝叶斯网络(Bayesian networks)
- Tomcat处理HTTP请求源码分析(下)
- Tomcat处理HTTP请求源码分析(上)(转)
- 游戏服务器:到底使用UDP还是TCP
- [转]HTTP协议详解
- 网络编程1-----获取本机IP地址
- 内核启动流程
- 使用ss命令代替netstat 统计TCP网络连接信息