#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
pid_t pid;
printf("before fork\n");
pid = fork();
if(pid < 0)
{
perror("fork error");
}
else if(0 == pid)
{ //child
char ch[] = "Hello Process";
int fd = -1;
fd = open("f.txt",O_WRONLY);
while(fd > 0)
{
write(fd,&ch,1);
}
close(fd);
}
else
{ //parent
char ret[] = "Hello Process";
int fd = -1;
fd = open("f.txt",O_RDONLY);
while(fd > 0)
{
read(fd,output,1);
}
close(fd);
}
printf("pid = %s\n",pid);
return 0;
使用父进程waitpid等待子进程退出
另外,把代码格式改改吧