1

Whenever I run my code I get through 4 iterations of reading the file and creating a pthread until it segfaults with ID 11.

The segfault is caused by my print ln: printf("%s %s\n", "Calling lab_manifes_alignment with package", *package); But why does this cause a segfault?

Halp?

#include <stdio.h> #include <string.h> #include <pthread.h> #include <stdlib.h> pthread_mutex_t mutex; FILE *packageList; void *submitPackage(void * packageReq){ char ** package = packageReq; strcat(packageReq, " | sh lab_manifest_alignment.sh"); printf("%s %s\n", "Calling lab_manifes_alignment with package", *package); system(packageReq); return NULL; } int main(){ int numThreads; pthread_t threads[numThreads]; //Init mutex if(pthread_mutex_init(&mutex, NULL)){ fprintf(stderr, "Error initializing mutex"); } int rc; FILE *packageList = fopen("package_list.txt", "r"); if(packageList == NULL){ fprintf(stderr, "ERROR: cannot open file.\n"); return 1; } int i = 0; char line[128]; while ( fgets ( line, sizeof line, packageList ) != NULL ){ /* read a line spawn as many threads as needeed*/ printf("%s %d, %s\n", "line: ",i, line); rc = pthread_create(&(threads[i]), NULL, submitPackage, line); if(rc){ printf("ERROR: return code from pthread_create() is %d\n", rc); exit(EXIT_FAILURE); } i++; } numThreads = i; for(i = 0; i < numThreads; i++){ pthread_join(threads[i], NULL); } fclose(packageList); return 0; 

}

4
  • Are you intentionally causing concurrency problems? Commented Nov 19, 2015 at 18:21
  • That's kind of rude? Sorry I haven't used pthreads in years......What are the issues if theres so many blatant problems? Commented Nov 19, 2015 at 18:37
  • No it is not rude. Concurrency is one of the problems. As I have asked before, is it intentional? Commented Nov 19, 2015 at 18:42
  • I have not introduced any intentional problems, I'm and amateur at C Commented Nov 19, 2015 at 18:46

2 Answers 2

1

pthread_t threads[numThreads]; numThreads is uninitialized here, you should choose a maximum value of threads or allocate it dynamically.

fgets ( line, sizeof line, packageList ) reads 128 bytes into line (the size of the array), but strcat(packageReq, " | sh lab_manifest_alignment.sh"); adds something behind it. This is the reason for your segfault. You should increase the size of the array and decrease the size parameter in fgets.

The next iteration in your main thread overwrites your line array, while the threads working with it. You should use a 2D array or allocate a buffer in each iteration and free it in the thread after working with it. Each thread must get his own buffer, not everyone the same.

char ** package = packageReq; should be char *package = packageReq; and remove the * at the printf.

Sign up to request clarification or add additional context in comments.

5 Comments

Note that my numThreads does get set to i, which is equal to the number of lines in my file. After changing from using strcat to the first option here: stackoverflow.com/questions/1383649/… I got the seg fault to quit. Thanks, I forgot to think that I would need a buffer for these lines..
I noted that, but during the declaration of the array its value is uninitialized andso it invokes undefined behaviour.
Ah..thanks..also..why the 2D buffer? Couldn't I just make a 1D shared buffer, use a mutex, and have each line get push'd and popp'd from it?
sure, but since only 1 thread (including the main thread) can use the buffer at the same time, it would be like a single threaded program.
So I have gotten the buffer working...however when I changed char ** package = packageReq; to char *package = packageReq; I now get the error: can't set (char *) to type (void **) --- using char ** package = &packageReq; is correct but still raises a warning...any info on how to eliminate this warning?
1

I think problem in here:

char ** package = packageReq; 

Try to change:

char ** package = &packageReq; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.