As the title says, it messes things up. My program is supposed to read in char by char, which works fine without nanosleep, when I add it in, messes things up. Yes I MUST use nanosleep which is why Im using it. Why is it not working in its spot? And where does it belong
Code:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#define TEN_MILLIS_IN_NANOS 10000000
typedef struct {
char data;
off_t offset;
char state;
} BufferItem ;
//nanosleep(&t, NULL);
BufferItem buffer[10000];
pthread_mutex_t mutex;
void *read_file(void *arg)
{
pthread_mutex_init(&mutex, NULL);
struct timespec t;
t.tv_sec = 0;
t.tv_nsec = rand()%(TEN_MILLIS_IN_NANOS+1);
while(1){
// pthread_mutex_lock(&mutex);
buffer->data = fgetc(arg);
buffer->offset = ftell(arg);
buffer->state = 1;
// pthread_mutex_unlock(&mutex);
if (buffer->data == EOF)
{
break;
}
printf("%c", buffer->data);
nanosleep(&t, NULL);
}
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
int KEY = *argv[1];
int nIN = *argv[2];
int nWORK = *argv[3];
int nOUT = *argv[4];
pthread_t IN[nIN];
pthread_t OUT[nOUT];
pthread_t WORK[nWORK];
struct timespec t;
t.tv_sec = 0;
t.tv_nsec = rand()%(TEN_MILLIS_IN_NANOS+1);
char c;
FILE *file_in;
file_in = fopen(argv[5], "r");
int i;
for(i=0;i<nIN;i++)
{
pthread_create(&IN[i], NULL, read_file, file_in);
nanosleep(&t, NULL);
}
}