当前位置: 技术问答>linux和unix
为什么会segmentation fault
来源: 互联网 发布时间:2016-04-18
本文导语: 以下是一段测试fork和vfork差别的代码。 #include #include #include int main() { int i = -1; int j = -1; i = vfork(); j = fork(); printf("nThe PID is [%d],[%d]n", i,j); return 0; } ...
以下是一段测试fork和vfork差别的代码。
#include
#include
#include
int main()
{
int i = -1;
int j = -1;
i = vfork();
j = fork();
printf("nThe PID is [%d],[%d]n", i,j);
return 0;
}
[back@Test back]$ gcc test.c -o test
[back@Test back]$ ./test
The PID is [0],[29304] //29303打印
The PID is [29303],[29305] //29302打印
Segmentation fault
[back@Test back]$
The PID is [29303],[0] //29305打印
The PID is [0],[0] //29304打印
初始PID为29302,vfork后其阻塞,其子进程(29303)执行fork()产生29304 ,而29302结束阻塞
后则fork()产生29305如下图.
vfork() fork()
29302------------->29303-------------->29304
|
|
|fork()
|
|
29305
pid的打印结果符合预期,但是为什么会有Segmentation fault呢。
#include
#include
#include
int main()
{
int i = -1;
int j = -1;
i = vfork();
j = fork();
printf("nThe PID is [%d],[%d]n", i,j);
return 0;
}
[back@Test back]$ gcc test.c -o test
[back@Test back]$ ./test
The PID is [0],[29304] //29303打印
The PID is [29303],[29305] //29302打印
Segmentation fault
[back@Test back]$
The PID is [29303],[0] //29305打印
The PID is [0],[0] //29304打印
初始PID为29302,vfork后其阻塞,其子进程(29303)执行fork()产生29304 ,而29302结束阻塞
后则fork()产生29305如下图.
vfork() fork()
29302------------->29303-------------->29304
|
|
|fork()
|
|
29305
pid的打印结果符合预期,但是为什么会有Segmentation fault呢。
|
把return 0 改为exit(0),试试
|
STANDARD DESCRIPTION
(From SUSv2 / POSIX draft.) The vfork() function has the same effect
as fork(), except that the behaviour is undefined if the process cre-
ated by vfork() either modifies any data other than a variable of type
pid_t used to store the return value from vfork(), or returns from the
function in which vfork() was called, or calls any other function
before successfully calling _exit() or one of the exec() family of
functions.
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().
vfork() differs from fork() in that the parent is suspended until the
child makes a call to execve(2) or _exit(2). The child shares all mem-
ory with its parent, including the stack, until execve() is issued by
the child. The child must not return from the current function or call
exit(), but may call _exit().
Signal handlers are inherited, but not shared. Signals to the parent
arrive after the child releases the parent's memory.
(From SUSv2 / POSIX draft.) The vfork() function has the same effect
as fork(), except that the behaviour is undefined if the process cre-
ated by vfork() either modifies any data other than a variable of type
pid_t used to store the return value from vfork(), or returns from the
function in which vfork() was called, or calls any other function
before successfully calling _exit() or one of the exec() family of
functions.
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().
vfork() differs from fork() in that the parent is suspended until the
child makes a call to execve(2) or _exit(2). The child shares all mem-
ory with its parent, including the stack, until execve() is issued by
the child. The child must not return from the current function or call
exit(), but may call _exit().
Signal handlers are inherited, but not shared. Signals to the parent
arrive after the child releases the parent's memory.
|
man vfork,里面已经说得很清除了,看看吧,主要如下一段话:
vfork() differs from fork(2) in that the parent is suspended until the child makes a call to
execve(2) or _exit(2). The child shares all memory with its parent, including the stack, until
execve(2) is issued by the child. The child must not return from the current function or call
exit(3), but may call _exit(2).
|
gdb ....