Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program to create histogram using threading

Posted By: Adalric Fischer     Category: C Programming     Views: 3918

Program to create histogram using threading.

Code for Program to create histogram using threading in C Programming

#include<stdio.h>
#include<math.h>
#include<pthread.h>

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<a[i])
                        max=a[i];                   
        }

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

                if(i<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<N;i++)
                scanf("%d",&a[i]);
        min=max=a[0];

        printf("\nEnter the total number of groups required in histogram::");
        scanf("%d",&grno);

        for(i=0;i<grno;i++)
                histo[i]=0;

        printf("\nEnter the total number of threads :: ");
        scanf("%d",&nthread);                 
        for(i=0;i<nthread;i++)
        {
                if(0==pthread_create(&id,0,work,0))
                        continue;
                else
                        printf("\nError in creating threads");
        }

        pthread_mutex_lock(&lock);
        start=1;
        if(0!=pthread_cond_broadcast(&cond))
                printf("\nError in broadcasting");
        pthread_mutex_unlock(&lock);

        while(size>=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<grno;i++)
        {
                if(i!=grno-1)
                {
                        printf("For %d -> %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   
  
Share: 


Didn't find what you were looking for? Find more on Program to create histogram using threading Or get search suggestion and latest updates.

Adalric Fischer
Adalric Fischer author of Program to create histogram using threading is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!