I'm experimenting with concurrent programming in C using the Codeblocks IDE with gcc. When I run my program, I receive no output. Interestingly, though, when I put a breakpoint at a certain point in the program, the program will execute all instructions (including outputting values to the console) up to that point. However, afterwards, if I try to execute the instruction srand(time(NULL))
, all of the variables that I'm watching immediately show "Error reading variable, cannot access memory at address X" and the debugging process halts.
Here is my main() function
/*main function*/
int main()
{
int k = 3;
int m = 100;
int n = 10;
int t = 10;
/*First, let's create the threads*/
pthread_t thread[numOfThreads];
/*Second, create the data struct*/
struct programData *data = malloc(sizeof(struct programData));
/*Initialize data struct*/
data->kPumps=k;
data->mVisitors=m;
data->nCars=n;
data->tTime=t; /*They'll drive visitor around for 10 units of time.*/
/*Now let's create the different threads*/
pthread_create(&thread[0], NULL, visitorThread, (void*)data);
pthread_create(&thread[1], NULL, carThread, (void*)data);
pthread_create(&thread[2], NULL, pumpThread, (void*)data);
pthread_create(&thread[3], NULL, gasTruck, (void*)data);
return 0;
}
And my visitor thread
void *visitorThread(void *arg){
int arrayIndex;
int i;
/*Let's create mVisitors*/
struct programData *data;
data = (struct programData*)arg;
int numOfVisitors;
numOfVisitors = data->mVisitors;
/*create an array of visitors*/
struct visitor v[numOfVisitors];
/*Initialize values*/
for(i = 0; i < numOfVisitors; i++){
v[i].id = i+1;
v[i].isInCar = false;
v[i].isInQueue = false;
}
printf("There are %d visitors at the San Diego Zoo \n", numOfVisitors);
printf("At first the visitors wait at the snake exhibit \n");
//sleep(5);
printf("Now some of them are getting bored, and want to get into a car to be shown the rest of the zoo \n");
/*After a random amount of time, some people line up to take cars*/
/*create queue*/
struct visitor* queue[numOfVisitors];
/*Initialize the array*/
for(i = 0; i < numOfVisitors; i++){
queue[i] = NULL;
}
arrayIndex = 0;
/*While there are people in the snake exhibit*/
while(numOfVisitors >=1){
/*After a random period of time, no more than 5 seconds...*/
//srand
srand(time(NULL));
int timeBeforePersonLeaves = rand()%5+1;
//fflush(stdout);
//sleep(timeBeforePersonLeaves);
/*...a random person will get bored and enter the array line to be picked up by a car*/
srand(time(NULL));
int personIndex = rand() % numOfVisitors;
queue[arrayIndex] = &v[personIndex];
v[personIndex].isInQueue = true;
printf("Visitor %d is now in queue spot %d \n", personIndex, arrayIndex);
arrayIndex++;
}
return;
}
The problem seems to exist within the while loop. If I put a breakpoint at the bracket at the end of the while loop, it will output every value. However, if put the breakpoint anywhere within the while loop, as soon as it reaches the srand call, it leads to the same issue. Any help regarding this would be appreciated, and thanks in advance.