Strange Pthread Behavior On IRIX

Discussion in 'C Programming' started by Keith, Jun 25, 2003.

  1. Keith

    Keith Guest

    Hi. Was hoping somebody could try and compile this on their SGI system and see
    if it cores. If it cores, comment out the strcpy, and it will work. Or change
    256->32 and it will work.

    To compile: (not sure if the -D_POSIX_C_SOURCE=199506L is necessary)
    cc -D_POSIX_C_SOURCE=199506L -o test test.c -lpthread

    /********** test.c *******/
    #include <pthread.h>
    #include <stdio.h>
    #include <string.h>

    void* test_func( void* );

    int main() {
    pthread_t test_thread ;
    pthread_create( &test_thread, NULL, test_func, NULL) ;
    pthread_join( test_thread, (void**)NULL);
    return 0 ;

    void* test_func( void* silly ) {

    typedef struct {
    char str[1000] ;
    } MY_TEST ;
    MY_TEST my_test[256];

    if ( 0 ) {
    strcpy( my_test[0].str, "Mooo" ) ;

    fprintf(stderr, "Meow\n");

    return ((void*)NULL) ;
    /***** end test.c ****/
    Keith, Jun 25, 2003
    1. Advertisements

  2. Keith

    Keith Guest

    I got this answer off of another forum.

    The test_func() function is placing an automatic variable of size
    256*1000 (about 250KB) on the stack. When added together with the
    other values the compiler wants to place on the stack, it comes
    out to a total of 256048 bytes (with MIPSPro C

    The default size of an individual Pthread's stack under IRIX is 128KB.
    This is documented on the pthread_attr_setstacksize(3P) man page.

    On the first reference to the stack (saving the value of the gp
    register at 24 bytes above the stack pointer), the program generates
    a reference to unmapped memory (i.e. there's nothing allocated there),
    and you get a segment violation.

    To fix the program one must use pthread_attr_setstacksize(3P) to
    define a sufficiently large stack size for the thread in question.

    The reason that a value of 32 works (and even 127) should be clear
    from the above statement.
    Keith, Jun 26, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.