Help - can anyone explain this segfault?
Shlomi Fish
shlomif at iglu.org.il
Fri Jun 19 18:16:53 IDT 2009
On Friday 19 June 2009 17:53:07 Shachar Shemesh wrote:
> 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
>
Works fine here (Mandriva Linux Cooker with gcc-4.4.0):
{{{{{{{{{{{
Started, pid=29262
Child, pid=29263
Parent, child=29263
Started, pid=29262
Child, pid=29264
Parent, child=29264
Started, pid=29262
Child, pid=29265
Parent, child=29265
Started, pid=29262
Child, pid=29266
Parent, child=29266
Started, pid=29262
Child, pid=29267
Parent, child=29267
Started, pid=29262
Child, pid=29268
Parent, child=29268
Started, pid=29262
Child, pid=29269
Parent, child=29269
Started, pid=29262
Child, pid=29270
Parent, child=29270
Started, pid=29262
Child, pid=29271
Parent, child=29271
Started, pid=29262
Child, pid=29272
Parent, child=29272
Started, pid=29262
Child, pid=29273
Parent, child=29273
Started, pid=29262
Child, pid=29274
Parent, child=29274
Started, pid=29262
Child, pid=29275
Parent, child=29275
Started, pid=29262
Child, pid=29276
Parent, child=29276
}}}}}}}}}}}
And so to infinity.
Perhaps you should try it in different Linux distributions and versions inside
virtual machines.
Regards,
Shlomi Fish
> 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
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Understand what Open Source is - http://xrl.us/bjn82
God gave us two eyes and ten fingers so we will type five times as much as we
read.
More information about the Linux-il
mailing list