1
votes

Learning about pthreads, but I'm having trouble getting them created. Here is my output and the gdb information:

In main(), creating thread 1 
ERROR: return code from pthread_create() is 22 for thread 1
In main(), creating thread 2
ERROR: return code from pthread_create() is 22 for thread 2
In main(), creating thread 3
ERROR: return code from pthread_create() is 22 for thread 3
In main(), creating thread 4
ERROR: return code from pthread_create() is 22 for thread 4
In main(), creating thread 5
ERROR: return code from pthread_create() is 22 for thread 5

Program received signal SIGSEGV, Segmentation fault. 0xb7fb4d8a in
pthread_join (threadid=76038327, thread_return=0x0)
    at pthread_join.c:46 46 pthread_join.c: No such file or directory.

And here is my code:

#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>

#define SBUFSIZE 1025

char errorstr[SBUFSIZE];
FILE* inputfp[5];

void* f(void* inpFile) {
    fprintf(stderr, "%s\n", (char*)inpFile);
    return NULL;
}

int main (int argc, char* argv[]) {

    int i;

    /* Thread Variables */
    pthread_attr_t attr;
    pthread_t *th[argc-2]; //one thread for each input file

    /* allocate memory for the threads */
    for (i = 0; i < (argc-2); i++) {
        th[i] = (pthread_t *) malloc(sizeof(pthread_t)); 
        inputfp[i] = fopen(argv[i], "r");
        if (!inputfp[i]) {
            sprintf(errorstr, "Error Opening Input File: %s", argv[i]);
            perror(errorstr);
        }
    }

    /* Create one thread for each input file */
    for (i = 1; i < (argc - 1); i++) {
        fprintf (stderr, "In main(), creating thread %1d\n", i);
        int rc = pthread_create (th[i], &attr, f, inputfp[i-1]);
        if (rc) {
            printf("ERROR: return code from pthread_create() is %d for thread %d\n",
                rc, i);
        }
    }

    /* wait for the threads to finish */
    for (i = 1; i < (argc - 1); i++) {
        pthread_join(*th[i], 0);
    }

    return EXIT_SUCCESS;
}

I'm missing something, but I don't know what. Can anyone help? Thank you!

EDIT: Here is how I changed the code per the suggestion from Joachim Pileborg. I'm still getting error 22 returning from pthread_create(), but the SIGSEGV error on pthread_join is no longer occurring.

Anyone have any suggestions about how I can get the pthread_create() to return 0 (indicating a successful thread creation)? Thanks again!

int main (int argc, char* argv[]) {

    int i;

    /* Thread Variables */
    pthread_attr_t attr;
    pthread_t *th[argc-2]; //one thread for each input file

    /* allocate memory for the threads */
    for (i = 0; i < (argc-2); i++) {
        th[i] = (pthread_t *) malloc(sizeof(pthread_t)); 
        printf("%s\n", argv[i+1]);
        inputfp[i] = fopen(argv[i+1], "r");
        if (!inputfp[i]) {
            sprintf(errorstr, "Error Opening Input File: %s", argv[i]);
            perror(errorstr);
        }
    }

    /* Create one thread for each input file */
    for (i = 0; i < (argc - 2); i++) {
        fprintf (stderr, "In main(), creating thread %1d\n", i);
        int rc = pthread_create (th[i], &attr, f, inputfp[i]);
        if (rc) {
            printf("ERROR: return code from pthread_create() is %d for thread %d\n",
                rc, i);
        }
    }

    /* wait for the threads to finish */
    for (i = 0; i < (argc - 2); i++) {
        pthread_join(*th[i], 0);
    }

    return EXIT_SUCCESS;
}
1
It's good that you use GDB to help you debug, but when you get the segmenation fault you can use the bt command to show the function call stack, and use up to go up the call stack. After using up to reach your code, you can print variables to see if they look okay.Some programmer dude

1 Answers

1
votes

You have one loop where you loop from zero to argc - 3, and uses the correct indexes (zero to "size of array minus one".

Then you have two loops where you loop from one to argc - 2, and use indexes from one to "size of array".

You should use the same loop as the first one in all three places.