I got the right header in. rand_r is a function in stdlib.h. It should be written as
Code:
int rand_r(unsigned int * seedptr);
by what I've found in man and about 10 other websites.
I have even tried switching the . to -> but that just gives me an extra error allong with the original. I even tried switching to a State pointer and using that like in my other call to rand_r to no avail. No idea what is causing it.
Posting my whole thing but be warned, I know for a fact there are more errors things I haven't finished yet. I know I still need to fix my calls to nanosleep too. I'll fix those later, right now I just want to figure this thing out.
Code:
#include <sys/types.h>
#include <sys/time.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream.h>
enum { NUMTHREAD = 8, TRACKS = 256, TRANSFER = 3 };
typedef enum { UP =1, DOWN = 0 } Direction;
typedef struct {
pthread_t myTid;
int myId;
unsigned int seed;
pthread_cond_t myCv;
} State;
State states[NUMTHREAD];
typedef struct {
pthread_t myTid;
int curTrack;
pthread_cond_t myCv;
} diskDrive;
diskDrive diskDriver;
typedef struct {
long long threadTimes[NUMTHREAD];
int count;
} Queue;
Queue threadQueue;
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
long long totalTime;
long long startTime;
void queueinit( Queue * pq ) {
pq->count = 0;
}
void enqueue( int id, Queue * pq ) {
pq->threadTimes[id] = gethrtime();
pq->count++;
}
int dequeue( Queue * pq ) {
int check = 0;
for( int i=1; i <= NUMTHREAD; i++ ) {
if( check == 0 ) {
check = i;
} else if( pq->threadTimes[i] < pq->threadTimes[check]) {
check = i;
}
}
startTime = pq->threadTimes[check];
return check;
}
void recordTime( long long aTime ) {
pthread_mutex_lock( &mtx );
totalTime = totalTime + aTime;
pthread_mutex_unlock( &mtx );
}
void * DDR( void * arg) {
int slot = 0, randNum, curTrack = 0;
long long sleepTime, endTime, transTime;
while ( true ) {
pthread_cond_wait( &cond, &mtx );
slot = dequeue( &threadQueue );
randNum = rand_r( &states[slot].seed )%256;
if( curTrack >= randNum ) {
sleepTime = (( curTrack - randNum ) * 50000 + 3000000 );
} else {
sleepTime = (( randNum - curTrack ) * 50000 + 3000000 );
}
nanosleep( &sleepTime );
endTime = gethrtime();
transTime = endTime - startTime;
recordTime( transTime );
pthread_cond_signal( &states[slot].myCv );
}
}
void * user( State * statePtr ) {
long long thinkTime;
for( int q=0; q < 125; q++) {
thinkTime = (rand_r( &statePtr->seed)%120) / 2;
thinkTime = thinkTime * 1000000;
nanosleep( thinkTime );
enqueue( statePtr->myId, &threadQueue);
pthread_cond_signal( &diskDriver.myCv);
pthread_cond_wait ( &cond, &mtx );
}
}
int main() {
queueinit( &threadQueue );
for(int y=0; y < NUMTHREAD; y++ ) {
states[y].seed = getpid() + y;
states[y].myId = y + 1;
pthread_cond_init( &states[y].myCv, NULL);
pthread_create( &states[y].myTid, NULL, user, &states[y] );
}
pthread_create( &diskDriver.myTid, NULL, DDR, NULL);
for(int j=0; j < NUMTHREAD; j++ ) {
pthread_join( states[j].myTid, NULL);
}
cout << totalTime << endl;
return 0;
}