无名管道的用法

无名管道的用法
父进程 pid>0

close(fd[0]);
write(fd[1],"hello from father\n",128);

子进程 pid=0

close(fd[1]);
read(fd[0],buf,128);
#include<stdio.h>
#include<unistd.h>
int main(){
    
    
	int fd[2];
	int pid;
	char buf[1280];
	if(pipe(fd) == -1){
    
    
		printf("pipe failed\n");
	}
	pid = fork();
	if(pid<0){
    
    
		printf("fork failed\n");
	}else if(pid>0){
    
    
		printf("father\n");
		close(fd[0]);
		write(fd[1],"hello from father\n",128);
	}else{
    
    
		printf("child\n");
		close(fd[1]);
		read(fd[0],buf,128);
		printf("come from father %s\n",buf);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_41679960/article/details/114993450
今日推荐