Hey all. I have been working on this problem for well over 3 hours and I think its time to ask for help. What the program should do is create a thread within a thread. Now the only way I know how to do this is with a struct. Now whenever I run my code I keep getting seg. faults. Here is the complete code...
Code:#include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <ctype.h> int total_words ; pthread_mutex_t counter_lock = PTHREAD_MUTEX_INITIALIZER; struct command_t{ pthread_t pth; int argc; char *argv; //char *argv2; }; main(int ac, char *av[]) { printf("Main loop!"); void *count_words(void *); int i = 0; pthread_t t1, t2; struct command_t command; command.argc = i; //command.argv[1] = av[1]; //command.argv[2] = av[2]; command.pth = t2; printf("This is av[0] %s, av[1] %s, av[2] %s, av[3] %s",av[0],av[1],av[2],av[3]); //if ( ac != 3 ){ // printf("usage: %s file1 file2\n", av[0]); // exit(1); //} total_words = 0; printf("Right before pthread_create"); //pthread_create(&t1, NULL, count_words, (void *) &command); pthread_join(t1, NULL); pthread_join(t2, NULL); printf("%5d: total words\n", total_words); } //************************************************************************ //Method for counting words //************************************************************************ void *count_words(void *f) { printf("First line in method!"); struct command_t *my_data; my_data = (struct command_t *) f; char *filename = my_data->argv[my_data->argc]; /*if(my_data->argc > 0) { filename = my_data->argv2; } else { filename = my_data->argv; }*/ FILE *fp; int c, prevc = '\0'; printf("i = %i", my_data->argc); //************************************************************************ //Creates a thread within the thread //************************************************************************ if(my_data->argc = 0) { my_data->argc = my_data->argc + 1; pthread_create(&my_data->pth, NULL, count_words, (void *) &my_data); printf("Ohs Nos! We created another thread!!"); } if ( (fp = fopen(filename, "r")) != NULL ){ while( ( c = getc(fp)) != EOF ){ //************************************************************************ //The For loop for counting the number of words! //************************************************************************ if ( !isalnum(c) && isalnum(prevc) ) { pthread_mutex_lock(&counter_lock); total_words++; pthread_mutex_unlock(&counter_lock); } prevc = c; } fclose(fp); } else perror(filename); return NULL; }
I have a few questions as well...
1. Is this legal:
What I want to do is make it so the array value argv gets set with the different files passed in. So then within the method I can use the counter to access the correct one.Code:struct command_t{ pthread_t pth; int argc; char *argv[]; }; ........ command.argc = i; command.argv[1] = av[1]; command.argv[2] = av[2]; command.pth = t2; ......
2.I am not sure if this is correct. But I believe it might be.Code:char *filename = my_data->argv[my_data->argc];
3.Again is saying &my_data->pth correct? Also within the struct can I make the pth an array so then I can pass multiple thread names in?Code:pthread_create(&my_data->pth, NULL, count_words, (void *) &my_data);
Please can anyone help me with this because I am about to start ripping my hair out!![]()



LinkBack URL
About LinkBacks



