标签:style blog http color os 使用 io strong 文件
int main(){
	int fds[2];
	if(pipe(fds)==-1){
		perror("pipe:");
		return 0;
	}
	char buf[1024]="";
	if(fork()==0){
		close(fds[1]);
		while(memset(buf,0,sizeof(buf))){
			if(read(fds[0],buf,1024)==0){//当管道里没有数据的时候就退出
				break;
			}
			printf("child:read:");
			puts(buf);
		}
		exit(1);
	}
	else{
		close(fds[0]);
//		char p[1024];
//		char *p="Hello world!";
	while(memset(buf,0,1024),fgets(buf,1024,stdin)!=NULL)
			write(fds[1],buf,1024);
		close(fds[1]);
		printf("parents,finish\n");
		wait(NULL);//wait一定要放在close的后面,因为只有先将父进程的fds[1]关掉,当子进程没有数据读的时候才会退出,否则会导致死锁
	}
}#include<iostream>
using namespace std;
int main(int argc,char *argv[]){
	if(mkfifo(argv[1],0666)==-1){
		perror("ew");
		return 0;
	}
	unlink(argv[1]);
}
/*************************************************************************
	> File Name: pork_send.c
	> Author: yang
	> Mail:826123027@qq.com 
	> Created Time: 2014年08月22日 星期五 19:54:08
 ************************************************************************/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<memory.h>
int main(int argc,char *argv[]){
	if(mkfifo(argv[1],0666)==-1){
		perror("mkfifo1");
		exit(1);
	}
	int fd_send,fd_recv;
	fd_send=open(argv[1],O_WRONLY);
	fd_recv=open(argv[2],O_RDONLY);
	char buf_recv[1024]="";
	char buf_send[1024]="";
	printf("opening!\n");
	if(fork()==0){//开皮一个进程,用来接受信息
		close(fd_send);
		while(memset(buf_recv,0,sizeof(buf_recv)),read(fd_recv,buf_recv,1024)>0){
			write(1,buf_recv,strlen(buf_recv));
		}
		close(fd_recv);
		exit(1);
	}
	close(fd_recv);
	while(memset(buf_send,0,sizeof(buf_send)),fgets(buf_send,1024,stdin)!=NULL){
		write(fd_send,buf_send,strlen(buf_send));//用来发送信息
	}
	close(fd_send);
	wait(NULL);
}/*************************************************************************
	> File Name: pork_recv.c
	> Author: yang
	> Mail:826123027@qq.com 
	> Created Time: 2014年08月22日 星期五 20:12:32
 ************************************************************************/
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<memory.h>
#include<stdio.h>
int main(int argc,char *argv[]){
	if(mkfifo(argv[2],0666)==-1){
		perror("mkfifo2");
		exit(1);
	}
	int fd_send,fd_recv;
	fd_recv=open(argv[1],O_RDONLY);//注意这里与上面程序的这个位置有所不一样
	fd_send=open(argv[2],O_WRONLY);
	char buf_recv[1024]="";
	char buf_send[1024]="";
	printf("opening!\n");
	if(fork()==0){//接受信息
		close(fd_send);
		while(memset(buf_recv,0,sizeof(buf_recv)),read(fd_recv,buf_recv,1024)>0){
			write(1,buf_recv,strlen(buf_recv));
		}
		close(fd_recv);
		exit(1);
	}
	close(fd_recv);//发送信息
	while(memset(buf_send,0,sizeof(buf_send)),fgets(buf_send,1024,stdin)!=NULL){
		write(fd_send,buf_send,strlen(buf_send));
	}
	close(fd_send);
	wait(NULL);
}
Linux IPC(Inter-Process Communication,进程间通信)之管道学习
标签:style blog http color os 使用 io strong 文件
原文地址:http://blog.csdn.net/youngyangyang04/article/details/38745563