Help - can anyone explain this segfault?

Help - can anyone explain this segfault?

Baruch Siach baruch at tkos.co.il
Fri Jun 19 18:08:29 IDT 2009


Hi Shachar,

On Fri, Jun 19, 2009 at 05:53:07PM +0300, 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
>
> 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?

Compiling your program with -Wall emits the following warning on Debian 
Squeeze (amd64, gcc 4.3.3):

fork.c: In function ‘main’:
fork.c:34: warning: implicit declaration of function ‘wait’

This may indicate a headers mismatch in Debian.

Running the program gives the same result as yours.

Hope this helps.

baruch

-- 
                                                     ~. .~   Tk Open Systems
=}------------------------------------------------ooO--U--Ooo------------{=
   - baruch at tkos.co.il - tel: +972.2.679.5364, http://www.tkos.co.il -



More information about the Linux-il mailing list