#include <fcntl.h> //提供open函数
#include <sys/types.h> // 该头文件提供系统调用的标志
#include <sys/stat.h> // 该头文件提供系统状态信息和相关函数
#include <sys/uio.h> // 该头文件提供进程I/O操作的相关函数
#include <unistd.h> // 标准函数库
#include <fcntl.h> // 文件操作相关函数库
#include <string.h> // 字符串操作函数库
#include <sys/wait.h> // wait调用相关函数库
#include <stdio.h> // 标准输入与输出函数库
#include <stdlib.h> // 常用工具函数库
int main() {
int fd[2], len;
char buf[200];
pid_t cld_pid;
if(pipe(fd) == -1) {
puts("创建管道失败");
}
if((cld_pid = fork()) == 0) { //zijincheng
close(fd[1]); //关闭写端口
len = read(fd[0], buf, sizeof(buf));
printf("1\n");
buf[len] = '\0';
printf("子进程从管道中读取的数据是:%s\n", buf);
exit(0);
} else { //fujincheng
close(fd[0]); //关闭读端口
printf("2\n");
sprintf(buf, "zijinchengde pid shi %d", cld_pid);
write(fd[1], buf, strlen(buf));
exit(0);
}
return 0;
}
进程间通信---在父进程跟子进程之间利用管道进行通信。一个简单的例子
最新推荐文章于 2022-04-21 18:28:06 发布