Re: vfork causes coredump



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)]
.



Relevant Pages

  • Re: Killing a process that takes too long
    ... You may instead use fork and exec; this lets you use the process-ID to ... kill 'INT', $pid; ... and it does not guarantee that the child ... So we need a way to kill several processes of the process group of the parent, ...
    (perl.beginners)
  • Non-random PIDs
    ... new process ID's, in the way that OpenBSD does. ... I'm the child and my pid is 21116. ... I'm the parent and my pid is 21115. ...
    (RedHat)
  • Re: Killing a process that takes too long
    ... and it does not guarantee that the child ... You can test it by placing $$ (process pid) in the output of these two ... So we need a way to kill several processes of the process group of the parent, ...
    (perl.beginners)
  • Re: Creatng 100% separate process from Parent
    ... the "child" process still maintains its ... So another process could start with the same pid as the "parent" - should ... > configuration tool) is typically just run from the start menu. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: optimize log parsing
    ... >> Hey Xho, I tried this: ... >> for the constructor is 0 then, assuming you're in the child process, ... >> called after the successful startup of a child in the parent process. ... >> - pid of the process which has been started ...
    (comp.lang.perl.misc)