1.子进程,共享父进程打开的文件;
#include <unistd.h>
#include <iostream>
const int BUFF_SIZE = 11;
int main()
{
int pfd[2];
if (pipe(pfd) == -1)
printf("failed to create a pipe");
switch (fork())
{
case -1:
printf("failed to create a process");
case 0:
if (close(pfd[1]) == -1)
_exit(1);
char read_buff[BUFF_SIZE];
int readn = read(pfd[0], read_buff, BUFF_SIZE);
printf("child process read: %s\n", read_buff);
_exit(0);
}
if (close(pfd[0]) == -1)
exit(1);
char write_buff[] = "hello world";
int writen = write(pfd[1], write_buff, BUFF_SIZE);
exit(0);
}
父进程关闭read,fd[0], can write, children close write, fd[1], can read;