OK, I have my own programming question. Not a homework. I am also
posting it to comp.unix.programming, but this list can be more
responsive.

I have the following trivial piece of code that I am trying to
compile with g++ (gcc-2.96-98) on RH 7.2 (Linux 2.4.9-13 on i686).
The code looks like C, but the real code I am debugging is C++, and
exhibits the same problem on RH7.2 and on a fully updated RH7.1
(Linux 2.4.9-12 on i686).

///////////////////// pth.cpp ////////////////////////////////

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>

static void* start(void* arg)
{
        int c = *((int*)arg);
        free(arg);
        if (pthread_detach(pthread_self()) != 0)
        {
                fprintf(stderr,"Error: %s\n",strerror(errno));
                exit(EXIT_FAILURE);
        }
        printf("The argument is %d\n",c);
        return NULL;
}

int main(void)
{
        int* arg = NULL;

        if ((arg = (int*)malloc(sizeof(*arg))) == NULL)
        {
                fprintf(stderr,"Error: malloc failed\n");
                return EXIT_FAILURE;
        }

        *arg = 1;
        
        if (pthread_create(NULL, NULL, start, arg) != 0)
        {
                free(arg);
                fprintf(stderr,"Error: %s\n",strerror(errno));
                return EXIT_FAILURE;
        }

        return EXIT_SUCCESS;
}

/////////////// end of pth.cpp //////////////////////////

Compilation looks like this:

$ g++ -o pth -g -D_REENTRANT pth.cpp -lpthread

When I run ./pth it happily prints out what I expect and segfaults:

$ ./pth
The argument is 1
Segmentation fault

>From the debugger I see the following:

(gdb) n
The argument is 1   [ this is from the printf() - OG ]
(gdb) n
(gdb) n             [ this is when start() returns - OG ]
pthread_start_thread (arg=0xbf7ffc00) at manager.c:287
287     manager.c: No such file or directory.
        in manager.c
Current language:  auto; currently c
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 1024 (LWP 9296)]
0x40032e49 in __pthread_create_2_1 (thread=0x0, attr=0x0, 
    start_routine=0x8048800 <start(void *)>, arg=0x8049b90) at pthread.c:662
662     pthread.c: No such file or directory.
        in pthread.c
(gdb) 

I have not tried to debug the guts of the threads library (from
glibc-devel-2.2.4-19 on RH7.2), I am not even crazy about downloading
the glibc sources over 56K. A Google search led me to some postings on
the bug-glibc m.l.  that discuss a similar effect, but the context
there seems to be different (SMP, my machines are uniprocessor,
cf. http://sources.redhat.com/ml/bug-glibc/2001-07/msg00076.html).

The above is reproducible.

Is it a bug in pthreads? I still suspect that I am making a stupid
mistake somewhere. Can anyone enlighten me? Thanks,

-- 
Oleg Goldshmidt | [EMAIL PROTECTED] 
If it aint't broken it hasn't got enough features yet.

=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to