Help - can anyone explain this segfault?

Help - can anyone explain this segfault?

Shachar Shemesh shachar at shemesh.biz
Fri Jun 19 17:53:07 IDT 2009


Hi all,

Here is fork.c in its entirety:
> #define _GNU_SOURCE
> #include <sys/types.h>
> #include <unistd.h>
> #include <sched.h>
> #include <sys/syscall.h>
> #include <signal.h>
>
> #include <stdio.h>
>
> int main()
> {
>     pid_t child;
>
>     printf("Started, pid=%d\n", getpid() );
>
>     //child=fork();
>     child=vfork();
>     //child=syscall(__NR_fork);
>     //child=syscall(__NR_vfork);
>     //child=syscall(__NR_clone, CLONE_VFORK|SIGCHLD, 0);
>
>     if( child<0 ) {
>         perror("fork failed");
>
>         return 1;
>     }
>
>     if( child==0 ) {
>         printf("Child, pid=%d\n", getpid() );
>     } else {
>         printf("Parent, child=%d\n", child );
>
>         int status;
>         wait(&status);
>     }
>
>     return 0;
> }
I compile it with gcc 4.3.2 from Debian Lenny with the following command 
line:
gcc -O0 -g -o fork fork.c

When I run it, I get:
> $ ./fork
> Started, pid=13711
> Child, pid=13712
> Parent, child=13712
> Segmentation fault

The core dump is of no use whats o' ever - it shows an rip of 0. If the 
program is run from within strace, there is no segmentation fault. Same 
goes if it is being run by gdb or valgrind. In fact, I'm having a 
somewhat difficult time deciding whether it is the parent or the child 
that dumps core! (adding a printf after the "wait" confirms that the 
child returned with status code 0, so it must be the parent that is seg 
faulting).

help?

Shachar

-- 
Shachar Shemesh
Lingnu Open Source Consulting Ltd.
http://www.lingnu.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.cs.huji.ac.il/pipermail/linux-il/attachments/20090619/0238d953/attachment.html>


More information about the Linux-il mailing list