Hey guys, I have a bit of a head scratcher as far as this program goes right now.
The program is a simulation of a movie rental using pthreads. It starts by reading in a file, which sets all of the variables (worker threads, client threads, how many movies and of what, etc). I have the movies that are found in the init file put into a char * array, and printing them right after I store them yields the correct movie title found at the correct index, code snippet below from my init() function
Code:
int nMovies = 0;
...
while ( fgets ( line, sizeof line, file ) != NULL )
{
...(if movie found)
movies[nMovies] = line;
cMovies[nMovies] = line;
printf("Title:%s",cMovies[nMovies]); //Prints out Correct Title
nMovies++;
...
}
movies[] are the movies found on the server
cMovies[] are the movies that the client will try to rent (which is the same array with some extra dummie titles to simulate clients trying to rent an invalid movie).
This part works fine, However, this is where the weird part comes in: after reading the init file, I call to create the client threads:
Code:
void startClients(int clients ){
clients = nClients;
numClients[nClients]; //Global variable of type pthread_t
int rc;
long t;
for(t=0; t<nClients; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&numClients[t], NULL, clientThread, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
}
which calls this function after threads are created:
Code:
void *clientThread(void *threadid){
int tid;
tid = (int)threadid;
//Number of steps (actions of clients) to simulate
int steps = nSteps;
int i;
printf("ThreadID:%d\n",tid);
int rv;
for(i = 0; i < steps; i++){
coin = 0;
//wait until the lock is not taken
while(true){
rv = pthread_mutex_trylock( &clientMutex );
if(rv == 0){
printf("Working???");
break;
}
};
printf("Working???");
if (coin == 0){
randomCheckout(tid);
pthread_exit(NULL);
break;
}else{
//randomReturn();
}
sleep(150);
}
pthread_mutex_unlock( &clientMutex );
printf("\nInside of checkoutVideo \n");
pthread_exit(NULL);
}
This is the first problem: When I run the program, it creates all of the threads perfectly fine, but only the first thread to get the lock will get into the bolded section (even though coin ALWAYS equals 0 right now), and the whole program will only go through 1 step. Am I not releasing the mutex lock correctly or in the wrong place? I tried moving it to after randomCheckout(tid); in the bolded section (instead of break), and ended up with segmentation faults after the first thread went through a step.
without pthread_exit(NULL); in the bolded section (just break), it gets about half way through the # of client threads and seg faults.
The next problem comes next, in the randomcheckout() method:
Code:
void randomCheckout(int tid){
//Number of entries in the client's movie array
int j = (sizeof cMovies)/4;
printf("\n%d\n", j);
//Randomly pick a number from the array
int selection = rand() % j;
printf("%d\n", selection);
int a;
for(a = 0; a < j; a++){
printf("\n%s",cMovies[a]);
}
printf("%s\n", cMovies[selection]);
checkoutVideo(numClients[tid], cMovies[selection]);
}
In the bolded section (which is just debugging code cycling through the array), the output is just the number of entries in the array, but all the same movie title (the last one in the init file/stored into the array)...I'm not accessing the array anywhere in between populating it and here, so why are all of the values changing to the last entry??
Sorry for the bombardment of questions, I've been spending hours upon hours trying to fix these bugs and I just want to pull my hair out
If you need anything other parts of my code/more description of what's going on, just let me know.
Thanks for any input!!
Also for any Mods: I have an old account that I haven't accessed in a while and I guess it was deactivated to posting due to inactivity? Is there anyway to reactivate it?