Re: vfork causes coredump
- From: Rainer Weikusat <rweikusat@xxxxxxxxxxx>
- Date: Tue, 03 Jul 2007 12:17:00 +0200
Cai Qian <caiqian@xxxxxxxxx> writes:
Why the following code coredump and How can I debug it? GDB only show
information like this,
Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0 0x0000000000000000 in ?? ()
#1 0x00002b222cb3e8e4 in __libc_start_main () from /lib/libc.so.6
#2 0x00000000004004e9 in _start ()
== Code ==
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
void
hello (void)
{
pid_t pid;
if ((pid = vfork ()) == -1)
perror ("vfork");
else if (pid == 0)
return;
else
sleep (5);
}
int
main (void)
{
hello ();
_exit (0);
}
The child process created by vfork uses the address space of its
parent which is suspended until the child either exits or execs
another program. Returning from the hello function in the child causes
the activation record ('stack frame') of that routine to be
destroyed. Because of this, the parent segfaults when trying to return
from the destroyed activation record for a second time.
NB: This is actually (I assume you are using Linux) documented:
The child shares all memory 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().
[vfork(2)]
.
- References:
- vfork causes coredump
- From: Cai Qian
- vfork causes coredump
- Prev by Date: vfork causes coredump
- Next by Date: Re: Why #pragma pack() not take effect?
- Previous by thread: vfork causes coredump
- Next by thread: Re: vfork causes coredump
- Index(es):
Relevant Pages
|