完成了第 11 章 进程间通信

This commit is contained in:
riba2534
2019-01-22 11:37:58 +08:00
parent b5e701e29a
commit 47d7e9bd6c
6 changed files with 639 additions and 0 deletions

24
ch11/pipe1.c Normal file
View File

@@ -0,0 +1,24 @@
#include <stdio.h>
#include <unistd.h>
#define BUF_SIZE 30
int main(int argc, char *argv[])
{
int fds[2];
char str[] = "Who are you?";
char buf[BUF_SIZE];
pid_t pid;
// 调用 pipe 函数创建管道fds 数组中保存用于 I/O 的文件描述符
pipe(fds);
pid = fork(); //子进程将同时拥有创建管道获取的2个文件描述符复制的并非管道而是文件描述符
if (pid == 0)
{
write(fds[1], str, sizeof(str));
}
else
{
read(fds[0], buf, BUF_SIZE);
puts(buf);
}
return 0;
}