vfork()有关问题
vfork()问题
运行结果

请问结果为什么会这样?
------解决思路----------------------
vfork() differs from fork(2) in that the parent is suspended until the child terminates (either normally, by call-
ing _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, including the stack. The child must not return from the current func-
tion or call exit(3), but may call _exit(2).
由于子进程没有调用execve或者_exit,最终会进入return,这跟最后一句child不能return相违背(因为fork下父子共享stack),其结果是破坏了parent的stack(?有待确认?),导致parent的行为异常。
#include "stdio.h"
#include <unistd.h>
int main(int argc, char const *argv[])
{
pid_t result = vfork();
if(result == 0)
{
}
else if(result > 0)
{
printf("123\n");
}
else printf("vfork failed\n");
return 0;
}
运行结果
请问结果为什么会这样?
------解决思路----------------------
vfork() differs from fork(2) in that the parent is suspended until the child terminates (either normally, by call-
ing _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, including the stack. The child must not return from the current func-
tion or call exit(3), but may call _exit(2).
由于子进程没有调用execve或者_exit,最终会进入return,这跟最后一句child不能return相违背(因为fork下父子共享stack),其结果是破坏了parent的stack(?有待确认?),导致parent的行为异常。