mirror of
https://github.com/riba2534/TCP-IP-NetworkNote.git
synced 2026-02-02 17:48:55 +08:00
25 lines
585 B
C
25 lines
585 B
C
#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;
|
||
}
|