mirror of
https://github.com/riba2534/TCP-IP-NetworkNote.git
synced 2026-02-03 01:53:19 +08:00
完成了第 11 章 进程间通信
This commit is contained in:
24
ch11/pipe1.c
Normal file
24
ch11/pipe1.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user