#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>
#include<errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//演示父子进程共享文件描述符
//相当于2个fd指向同一块内存空间.
//因为2个进程共享了文件指针偏移量,所以都能向文件中有序写数据
int main(void)
{
pid_t pid;
int fd;
fd = open("test.txt", O_WRONLY);
if (-1 == fd)
{
perror("open err");
exit(0);
}
pid = fork();
if (-1 == pid)
{
perror("fork err");
return 0;
}
if (pid > 0)
{
write(fd, "parent", 6);
close(fd);
}
if (0 == pid)
{
write(fd, "child", 6);
close(fd);
}
return 0;
}