#include<stdio.h>
#include<pthread.h>
void *histogram(void *);
pthread_cond_t cond1;
pthread_mutex_t lock1;
int *arr,*lower,*upper,*freq;
int nthread,i,min,max,size,counter=0;
int no_bin,binsize,ok;
main()
{
pthread_t id;
pthread_cond_init(&cond1,NULL);
pthread_mutex_init(&lock1,NULL);
printf("Enter The Array Size : ");
scanf("%d",&size);
arr=(int *)malloc(sizeof(int)*size);
for(i=0;i<size;i++)
{
printf("Enter The Element[%d] : ",i+1);
scanf("%d",&arr[i]);
}
printf("Enter The No. Of Bins :");
scanf("%d",&no_bin);
lower=(int *)malloc(sizeof(int)*no_bin);
upper=(int *)malloc(sizeof(int)*no_bin);
freq=(int *)malloc(sizeof(int)*no_bin);
for(i=0;i<no_bin;i++)
{
freq[i]=0;
}
min=arr[0];
max=arr[0];
printf("Enter The No. Of Thread : ");
scanf("%d",&nthread);
// ok=0;for(i=0;i<nthread;i++)
{
if(0==pthread_create(&id,NULL,histogram,(void *)&i))
{
continue;
}
else
{
printf("Error In Thread Creation\n");
exit(0);
}
}
// ok=1;while(counter != nthread)
{
sleep(1);
}
pthread_mutex_lock(&lock1);
counter=0;
pthread_cond_broadcast(&cond1);
pthread_mutex_unlock(&lock1);
printf("The Maximum Is : %d\n",max);
printf("The Minimum Is : %d\n",min);
binsize=(max-min)/no_bin;
printf("The Bin Size Is %d\n",binsize);
lower[0]=min;
upper[0]=lower[0]+binsize;
for(i=1;i<no_bin;i++)
{
if(i==1)
lower[i]=lower[i-1]+binsize+1;
else
lower[i]=lower[i-1]+binsize;
upper[i]=upper[i-1]+binsize;
}
upper[no_bin-1]=max;
pthread_join(id,NULL);
printf("\nThe Frequency Distribution Is :\n");
for(i=0;i<no_bin;i++)
{
printf("\nThe class[%d - %d] : %d\n ",lower[i],upper[i],freq[i]);
}
exit(0);
}
void *histogram(void *i)
{
int *tid,j,k,p;
tid=(int *)i;
p=*tid;
// while(ok==0);
printf("TID:%d\n",*tid);
for(j=*tid;j<size;j+=nthread)
{
if(arr[j] < min)
{
min=arr[j];
}
if(arr[j] > max)
{
max=arr[j];
}
}
pthread_mutex_lock(&lock1);
counter++;
if(counter >0)
{
pthread_cond_wait(&cond1,&lock1);
}
pthread_mutex_unlock(&lock1);
printf(" \n\ntid = %d",p);
for(j=p;j<size;j+=nthread)
{
// printf("Value : %d\n",arr[j]);// k=0; // printf("binsize:%d\n",binsize);
k=(int)((arr[j]-min)/(binsize+1));
// printf("K::%d",k);if(k > no_bin)
{
k=no_bin;
}
pthread_mutex_lock(&lock1);
freq[k]=freq[k]+1;
pthread_mutex_unlock(&lock1);
}
pthread_exit(NULL);
}