进程通信(管道 单向通信)
2016-04-14 22:43
218 查看
1 #include<stdio.h> 2#include<sys/wait.h> 3#include<unistd.h> 4#include<string.h> 5#include<stdlib.h> 6int main() 7 { 8 int pipefd[2]; 9 if(pipe(pipefd)==-1) 10 { 11 perror("pipe"); 12 return -1; 13 } 14 pid_t pid=fork(); 15 if(pid<0) 16 { 17 perror("pid"); 18 return -1; 19 } 20 else if(pid==0) 21 { 22 close(pipefd[0]); 23 char buf[20]="we are young"; 24 int i=0; 25 while(i<10) 26 { 27 write(pipefd[1],buf,strlen(buf)); 28 sleep(1); 29 i++; 30 } 31 exit(1); 32 } 33 else 34 { 35 close(pipefd[1]); 36 char buf[200]; 37 int j=0; 38 while(j<10) 39 { 40 memset(buf,'\0',sizeof(buf)); 41 ssize_t size= read(pipefd[0],buf,sizeof(buf)); 42 if(size>0) 43 printf("%s\n",buf); 44 j++; 45 } 46 if(waitpid(pid,NULL,0)==pid) 47 { 48 printf("wait success\n"); 49 } 50 } 51 return 0; 52 } Makefile: 1 Pipe:pipe.c 2 gcc -o $@ $^ 3.PHONY:clean 4clean: 5 rm -f pipe
本文出自 “liveyoung” 博客,转载请与作者联系!
相关文章推荐
- 六 、常量、文件上传
- Aerospike系列:8:集群宕机演练
- C++中static 用法总结
- APIO2007-2015题解大集合(2008年篇)
- 深度缓冲
- iOS 图形图像动画 Core Animation
- Javascript高级篇-面向对象的特性
- Html5 表单的LocalStorage
- Aerospike系列:7:数据分布详解
- 库函数模拟(二),可变参数列表
- 0414结队--软件再升级(冯铭杰 梁毅乾)
- php设计模式之工厂模式
- iOS彩票项目--第七天,初次读取json数据、KVC转模型技巧、运行时字典转模型以及初步对显示网页的操作并且跟踪标签
- JS return false 与 return true(解决表单重复提交的问题)
- 如何利用云计算技术来实现基于Web的Client-Sever系统的non-stop Sever(灾难恢复)
- Aerospike系列:6:AerospikeTools & Utilities
- 神经网络学习笔记
- 1.3.1 Linux的特色
- 很幽默的讲解六种Socket IO模型 Delphi版本(自己Select查看,WM_SOCKET消息通知,WSAEventSelect自动收取,Overlapped I/O 事件通知模型,Overlapped I/O 完成例程模型,IOCP模型机器人)
- 4.14复利计算再升级总结