【发布时间】:2021-12-02 03:14:34
【问题描述】:
在fork child中,如果我们修改了一个全局变量,它不会在主程序中改变。
有没有办法改变子叉子中的全局变量?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int glob_var;
main (int ac, char **av)
{
int pid;
glob_var = 1;
if ((pid = fork()) == 0) {
/* child */
glob_var = 5;
}
else {
/* Error */
perror ("fork");
exit (1);
}
int status;
while (wait(&status) != pid) {
}
printf("%d\n",glob_var); // this will display 1 and not 5.
}
【问题讨论】: