Program to create histogram using threading in C language

#include
#include
#include

int a[100], size, N;
int min, max;
int grno;
float grsize;
int histo[50];

int start=0;
pthread_mutex_t lock;
pthread_cond_t cond;

void * work()
{
int i, temp;

pthread_mutex_lock(&lock);
if(start==0)
pthread_cond_wait(&cond, &lock);
pthread_mutex_unlock(&lock);

for(;;)
{
pthread_mutex_lock(&lock);
i=size-1;
size--;
pthread_mutex_unlock(&lock);

if(size<0) { pthread_mutex_lock(&lock); pthread_cond_wait(&cond, &lock); pthread_mutex_unlock(&lock); } if(i<0) break; if(min>a[i])
min=a[i];
if(max<0) break; temp=(float)((a[i]-min))/grsize; if(temp >(grno-1))
temp= grno-1;
histo[temp]++;
}
}

int main()
{
pthread_t id;
void * status;
int i, nthread;
int temp1, temp2;

pthread_mutex_init(&lock,0);
pthread_cond_init(&cond,0);

printf("Enter the size of the array :: ");
scanf("%d",&size);
N=size;

printf("\nEnter the %d elements of array\n",N);
for(i=0;i=0)
sleep(1);

grsize=(float)(max-min)/(float)grno;
size=N;

pthread_mutex_lock(&lock);
pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&lock);


while(size>=0)
sleep(1);

pthread_join(id,status);

printf("\nThe histogram is as follows::\n");
temp1=min;
temp2=ceil(grsize);

for(i=0;i %d = %d\n",temp1, temp2,histo[i]);
temp1=temp2+1;
temp2+=ceil(grsize);
}
else
printf("For %d and up values = %d ",temp1, histo[i]);
}

pthread_mutex_destroy(&lock);

printf("\n");
return 0;
}







OUTPUT:

--------
Enter the size of the array :: 4

Enter the 4 elements of array
1
2
3
4

Enter the total number of groups required in histogram :: 4

Enter the total number of threads :: 3

The histogram isas follows::
For 1 -> 1 = 1
For 2 -> 2 = 1
For 3 -> 3 = 1
For 4 and up values = 1

Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!