無名管道是父子進程通信的手段,沒有關系的進程是不能使用無名管道的
int fd[2];
if (pipe(fd) == -1) {
printf("打開無名管道出錯,%s\n", strerror(errno));
return -1;
}
char buf[1024];
pid_t pid = fork();
if (pid == -1) {
printf("fork出錯\n");
return -1;
}
if (pid > 0) { //父進程
memset(buf, 0, sizeof(buf));
read(fd[0], buf, sizeof(buf));
printf("%s\n", buf);
} else { //子進程
memset(buf,0,sizeof(buf));
strcpy(buf,"hello world");
write(fd[1],buf,strlen(buf));
}
return 0;