为什么我的程序 fork 函数没有像预期的那样 运行
why my program fork function didn't run as expected
#include <stdio.h>
#include <unistd.h>
int main(void)
{
FILE *fp;
int pid;
char msg1[] = "Test 1 2 3 ..\n";
char msg2[] = "Hello, hello\n";
if ((fp = fopen("testfile2", "w")) == NULL) // create the file
return 0;
fprintf(fp, "%d: %s", getpid(), msg1); // parent print the message1
if ((pid = fork()) == -1) // I fork a child process
return 0;
fprintf(fp, "%d: %s", getpid(), msg2); // both parent and child print
fclose(fp); // and close
return 1;
}
这是"testfile2"的内容:
6969: Test 1 2 3 ..
6969: Hello, hello
6969: Test 1 2 3 ..
6970: Hello, hello
让我猜猜:你会期望输出是
6969: Test 1 2 3 ..
6969: Hello, hello
6970: Hello, hello
对吧?那为什么不是这样呢?嗯,我猜
的输出
fprintf(fp, "%d: %s", getpid(), msg1);
在执行fork()
的时候还没有刷新到文件中。所以输出缓冲区也被复制并且包含 still
6969: Test 1 2 3 ..
所以最后你得到了什么 ;) 我认为这可能会有所不同,如果你在第一个 printff(...)
之后调用 fflush(fp);
。
也检查这个不错 SO post ...
您没有正确使用 fork()
并且您的 if 标签没有被包含。
例子
#include <sys/types.h> /* pid_t */#include <sys/wait.h> /* waitpid */#include <stdio.h> /* printf, perror */#include <stdlib.h> /* exit */#include <unistd.h> /* _exit, fork */
int main(void)
{
pid_t pid = fork();
if (pid == -1) {
// When fork() returns -1, an error happened.
perror("fork failed");
exit(EXIT_FAILURE);
}
else if (pid == 0) {
// When fork() returns 0, we are in the child process.
printf("Hello from the child process!\n");
_exit(EXIT_SUCCESS); // exit() is unreliable here, so _exit must be used
}
else {
// When fork() returns a positive number, we are in the parent process
// and the return value is the PID of the newly created child process.
int status;
(void)waitpid(pid, &status, 0);
}
return EXIT_SUCCESS;
}
示例来自:http://en.m.wikipedia.org/wiki/Fork_(system_call)#Example_in_C
#include <stdio.h>
#include <unistd.h>
int main(void)
{
FILE *fp;
int pid;
char msg1[] = "Test 1 2 3 ..\n";
char msg2[] = "Hello, hello\n";
if ((fp = fopen("testfile2", "w")) == NULL) // create the file
return 0;
fprintf(fp, "%d: %s", getpid(), msg1); // parent print the message1
if ((pid = fork()) == -1) // I fork a child process
return 0;
fprintf(fp, "%d: %s", getpid(), msg2); // both parent and child print
fclose(fp); // and close
return 1;
}
这是"testfile2"的内容:
6969: Test 1 2 3 ..
6969: Hello, hello
6969: Test 1 2 3 ..
6970: Hello, hello
让我猜猜:你会期望输出是
6969: Test 1 2 3 ..
6969: Hello, hello
6970: Hello, hello
对吧?那为什么不是这样呢?嗯,我猜
的输出fprintf(fp, "%d: %s", getpid(), msg1);
在执行fork()
的时候还没有刷新到文件中。所以输出缓冲区也被复制并且包含 still
6969: Test 1 2 3 ..
所以最后你得到了什么 ;) 我认为这可能会有所不同,如果你在第一个 printff(...)
之后调用 fflush(fp);
。
也检查这个不错 SO post ...
您没有正确使用 fork()
并且您的 if 标签没有被包含。
例子
#include <sys/types.h> /* pid_t */#include <sys/wait.h> /* waitpid */#include <stdio.h> /* printf, perror */#include <stdlib.h> /* exit */#include <unistd.h> /* _exit, fork */
int main(void)
{
pid_t pid = fork();
if (pid == -1) {
// When fork() returns -1, an error happened.
perror("fork failed");
exit(EXIT_FAILURE);
}
else if (pid == 0) {
// When fork() returns 0, we are in the child process.
printf("Hello from the child process!\n");
_exit(EXIT_SUCCESS); // exit() is unreliable here, so _exit must be used
}
else {
// When fork() returns a positive number, we are in the parent process
// and the return value is the PID of the newly created child process.
int status;
(void)waitpid(pid, &status, 0);
}
return EXIT_SUCCESS;
}
示例来自:http://en.m.wikipedia.org/wiki/Fork_(system_call)#Example_in_C