当前位置: 技术问答>linux和unix
关于vfork()问题,为什么其对父进程共享的变量的操作结果不是预期的?请高手指点,多谢了
来源: 互联网 发布时间:2016-05-30
本文导语: 1 #include 2 #include 3 #include 4 5 int globVar = 5; 6 7 int main(void) 8 { 9 pid_t pid; 10 int var = 1, i; ...
1 #include
2 #include
3 #include
4
5 int globVar = 5;
6
7 int main(void)
8 {
9 pid_t pid;
10 int var = 1, i;
11
12 printf("fork is diffirent with vfrok n");
13
14
15 pid = vfork();
16 switch(pid) {
17 case 0:
18 i = 3;
19 while(i-- > 0)
20 {
21 printf("Child process is runningn");
22 globVar++;
23 var++;
24 sleep(1);
25 }
26 printf("Child's globVar = %d,var = %dn",globVar,var);
27 break;
28 case -1:
29 perror("Process creation failedn");
30 exit(0);
///////////////运行结果///////////////
fork is diffirent with vfrok
Child process is running
Child process is running
Child process is running
Child's globVar = 8,var = 4
Parent process is running
Parent process is running
Parent process is running
Parent process is running
Parent process is running
Parent's globVar = 13 ,var = 33
/////运行环境:redhat 9。问题:var的最终结果为什么是33,而不是9呢?
//////在26行设置断点,GDB调试,本想监控一下结果,却好像死机了一样,一到断点处就没有反映
////这都是为什么呢,请高手指点
2 #include
3 #include
4
5 int globVar = 5;
6
7 int main(void)
8 {
9 pid_t pid;
10 int var = 1, i;
11
12 printf("fork is diffirent with vfrok n");
13
14
15 pid = vfork();
16 switch(pid) {
17 case 0:
18 i = 3;
19 while(i-- > 0)
20 {
21 printf("Child process is runningn");
22 globVar++;
23 var++;
24 sleep(1);
25 }
26 printf("Child's globVar = %d,var = %dn",globVar,var);
27 break;
28 case -1:
29 perror("Process creation failedn");
30 exit(0);
///////////////运行结果///////////////
fork is diffirent with vfrok
Child process is running
Child process is running
Child process is running
Child's globVar = 8,var = 4
Parent process is running
Parent process is running
Parent process is running
Parent process is running
Parent process is running
Parent's globVar = 13 ,var = 33
/////运行环境:redhat 9。问题:var的最终结果为什么是33,而不是9呢?
//////在26行设置断点,GDB调试,本想监控一下结果,却好像死机了一样,一到断点处就没有反映
////这都是为什么呢,请高手指点
|
fork is diffirent with vfrok
Child process is running
Child process is running
Child process is running
Child's globVar = 8,var = 4
这一段正常啊.parent又没看到.
Child process is running
Child process is running
Child process is running
Child's globVar = 8,var = 4
这一段正常啊.parent又没看到.
|
有可能的话可以看一下 copy on write。 多线程调试要用到 attach pid 这样两个线程都可以调试了。
|
哎,代码不全啊,如何分析...
|
楼主要是肯自己动手man一下,就不至于上来问了...
Linux Description
vfork(), just like fork(2), creates a child process of the calling pro‐
cess. For details and return value and errors, see fork(2).
vfork() is a special case of clone(2). It is used to create new pro‐
cesses without copying the page tables of the parent process. It may
be useful in performance-sensitive applications where a child will be
created which then immediately issues an execve(2).
vfork() differs from fork(2) in that the parent is suspended until the
child terminates (either normally, by calling exit(2), or abnormally,
after delivery of a fatal signal), or it makes a call to execve(2).
Until that point, the child shares all memory with its parent, includ‐
ing the stack. The child must not return from the current function or
call exit(3), but may call _exit(2).
Signal handlers are inherited, but not shared. Signals to the parent
arrive after the child releases the parent’s memory (i.e., after the
child terminates or calls execve(2)).
Linux Description
vfork(), just like fork(2), creates a child process of the calling pro‐
cess. For details and return value and errors, see fork(2).
vfork() is a special case of clone(2). It is used to create new pro‐
cesses without copying the page tables of the parent process. It may
be useful in performance-sensitive applications where a child will be
created which then immediately issues an execve(2).
vfork() differs from fork(2) in that the parent is suspended until the
child terminates (either normally, by calling exit(2), or abnormally,
after delivery of a fatal signal), or it makes a call to execve(2).
Until that point, the child shares all memory with its parent, includ‐
ing the stack. The child must not return from the current function or
call exit(3), but may call _exit(2).
Signal handlers are inherited, but not shared. Signals to the parent
arrive after the child releases the parent’s memory (i.e., after the
child terminates or calls execve(2)).