can some one tell me where im going wrong
thanks
Code:
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#define BUFFER_SIZE 100;
#define SIGMA 100.8
#define MU 6.6
double randno();
double normal();
double exponential();
void *producer();
void *consumer();
time_t T;
//buffer and related
double buffer[BUFFER_SIZE];
int in=0;
int out=0;
double mean;
int i=0;
//random no b/w 0~1
double randno(){
return (rand()/(double)RAND_MAX);
}
//normal distribution
double normal(){
double x1, x2, w;
x1 = randno();
x2 = randno();
w = sqrt(x1*x1+x2*x2);
return (SIGMA * x1 * sqrt( abs(2 * log(w))/w ) + MU);
}
//exponential distribution
double exponential(){
double x;
x = randno();
return ( -log(1-x)/MU );
}
//producer function
void *producer(){
while(1){
while((in+1)% BUFFER_SIZE == out)
//do nothing as buffer is full
{;}
buffer[in++] = normal();
in %= BUFFER_SIZE;
usleep(exponential());
}
pthread_exit(0);
}
//consumer function
void *consumer(){
while(1){
double sum=0;
while( in == out ){//isempty
usleep(exponential());
}
for(i=0; i<10; i++){
sum+=buffer[out];
out++;
out %= BUFFER_SIZE;
}
mean = sum/10;
printf("%d\t",in);
T = time(NULL);
printf(ctime(&T));
printf("\n");
}
pthread_exit(0);
}
int main(){
pthread_t prod,cons;
pthread_create(&prod, NULL, producer, NULL);
pthread_create(&cons, NULL, consumer, NULL);
pthread_join(prod,NULL);
pthread_join(cons,NULL);
return 0;
}