一、子进程继承父进程的普通文件描述符
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
char buf[1024]={0};
pid_t pid;
int ret=0;
int fd = open("./temp.txt", O_CREAT|O_TRUNC|O_RDWR, 0666);
if( fd == -1 ){
perror("open ./temp.txt");
return -1;
}
if ((pid = fork()) < 0) {
perror("fork");
return -1;
}
else if (pid == 0) { //child process
sleep(1);
ret = sprintf(buf, "child process pid:%d,parent pid:%d\n", getpid(), getppid());
write(fd, buf, ret);
close(fd);
exit(0);
}
else { //parent process
ret = sprintf(buf, "parent process pid:%d, child pid:%d\n", getpid(), pid);
write(fd, buf, ret);
close(fd);
}
waitpid(pid, NULL, 0);
return 0;
}#include <stdio.h>
#includ