fork1
int main(void)
{
int fd,pid;
char msg1[] = "hello world!\n";
char msg2[] = "i am parent!\n";
char msg3[] = "i am child!\n";
if ( (fd = creat("testfile", "0777")) == -1 ) {
printf("creat error!\n");
return -1;
}
if (write(fd, msg1, strlen(msg1)) == -1) {
printf("write error!\n");
return -1;
}
if ( (pid = fork()) == -1 ) {
printf("fork error!\n");
return -1;
}
if (pid == 0) {
//child
if (write(fd, msg3, strlen(msg3)) == -1) {
printf("write child error!\n");
return -1;
}
} else {
//parent
if (write(fd, msg2, strlen(msg2)) == -1) {
printf("write parent error!\n");
return -1;
}
}
}
//return
hello world!
i am child!
i am parent!
fork2
int main(void)
{
FILE *fd;
int pid;
char msg1[] = "hello world!\n";
char msg2[] = "i am parent!\n";
char msg3[] = "i am child!\n";
if ( (fd = fopen("testfile2", "w")) == NULL ) {
printf("creat error!\n");
return -1;
}
fprintf(fd, "%s", msg1);
if ( (pid = fork()) == -1 ) {
printf("fork error!\n");
return -1;
}
if (pid == 0) {
//child
sleep(3);
fprintf(fd, "%s", msg3);
} else {
//parent
fprintf(fd, "%s", msg2);
}
fclose(fd);
return 0;
}
return
hello world!
i am parent!
hello world!
i am child!
why?
系统调用写文件:用户空间无缓存管理,调用write后逻辑上可以认为msg1已经写入文件(虽然还有page cache和disk cache),fork后,父子进程只各输出msg2
标准IO库函数写文件:默认为全缓存,调用fprintf后msg1只是输出到FILE*管理的用户缓存区里,fork后,子进程copy了父进程的很多数据包括刚才的缓存区中的msg1,所以最终父子进程各输出一个msg1
总之,问题的关键在于是否缓存输出数据
只要在fork2的时候加上fflush就好了
int main(void)
{
int fd,pid;
char msg1[] = "hello world!\n";
char msg2[] = "i am parent!\n";
char msg3[] = "i am child!\n";
if ( (fd = creat("testfile", "0777")) == -1 ) {
printf("creat error!\n");
return -1;
}
if (write(fd, msg1, strlen(msg1)) == -1) {
printf("write error!\n");
return -1;
}
if ( (pid = fork()) == -1 ) {
printf("fork error!\n");
return -1;
}
if (pid == 0) {
//child
if (write(fd, msg3, strlen(msg3)) == -1) {
printf("write child error!\n");
return -1;
}
} else {
//parent
if (write(fd, msg2, strlen(msg2)) == -1) {
printf("write parent error!\n");
return -1;
}
}
}
//return
hello world!
i am child!
i am parent!
fork2
int main(void)
{
FILE *fd;
int pid;
char msg1[] = "hello world!\n";
char msg2[] = "i am parent!\n";
char msg3[] = "i am child!\n";
if ( (fd = fopen("testfile2", "w")) == NULL ) {
printf("creat error!\n");
return -1;
}
fprintf(fd, "%s", msg1);
if ( (pid = fork()) == -1 ) {
printf("fork error!\n");
return -1;
}
if (pid == 0) {
//child
sleep(3);
fprintf(fd, "%s", msg3);
} else {
//parent
fprintf(fd, "%s", msg2);
}
fclose(fd);
return 0;
}
return
hello world!
i am parent!
hello world!
i am child!
why?
系统调用写文件:用户空间无缓存管理,调用write后逻辑上可以认为msg1已经写入文件(虽然还有page cache和disk cache),fork后,父子进程只各输出msg2
标准IO库函数写文件:默认为全缓存,调用fprintf后msg1只是输出到FILE*管理的用户缓存区里,fork后,子进程copy了父进程的很多数据包括刚才的缓存区中的msg1,所以最终父子进程各输出一个msg1
总之,问题的关键在于是否缓存输出数据
只要在fork2的时候加上fflush就好了