Logo 
Search:

C Programming Articles

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

Program of histogram by calculating the partial histograms and consolidating in the end to get the final histogram

Posted By: Easy Tutor     Category: C Programming     Views: 4236

Write a program of histogram by calculating the partial histograms and consolidating in the end to get the final histogram.

Code for Program of histogram by calculating the partial histograms and consolidating in the end to get the final histogram in C Programming

# include <stdio.h>
# include </usr/include/sys/types.h>
# include </usr/include/sys/shm.h>
# include </usr/include/sys/sem.h>
# include </usr/include/sys/ipc.h>
# include <stdlib.h>                 // For rand() Function
# include "forkjoin.h"
# include "sharedlib.h"
# include "spinlock.h"
# define arrSize 50
int main()
{    
    int arr[arrSize];
    int iCount;                  // Counter Variableint NoOfBins=5;                // Number of binsint binsize;                // Size of each binint *histogram;
    int parhist[50];            // Partial Histogram for each Processint *lock;                // For Locking entire Binint shmidlock,shmidhist;
    int id;
    int bin; // Subscript of histogram arrayint minarr,maxarr;
    int i;
    // Initialize an Arrayfor(iCount=0;iCount<arrSize;iCount++)
    {
        arr[iCount]=rand()%arrSize;
    }
    
    minarr=arr[0];
    maxarr=arr[0];
    for(iCount=1;iCount<arrSize;iCount++)
    {
        if(minarr > arr[iCount])
        {
            minarr=arr[iCount];
        }
        if(maxarr < arr[iCount])
        {
            maxarr=arr[iCount];
        }
    }

    binsize=(maxarr-minarr) / NoOfBins;

    
    lock=(int*)sshared(sizeof(int)*NoOfBins,&shmidlock);
    histogram=(int*) sshared(sizeof(int)*NoOfBins,&shmidhist);
    
    printf("\n Bin Size : %d",binsize);
    printf("\n No. Of Bins : %d\n",NoOfBins);
    
    for(iCount=0;iCount<NoOfBins;iCount++)
    {
        histogram[iCount]=0;
        parhist[iCount]=0;
    }
    printf("\n");
    id=process_fork(NoOfBins);
    spin_lock_init(&lock[id]);    
    for(iCount=id;iCount<arrSize;iCount=iCount+NoOfBins)
    {
        bin=abs(arr[iCount] - minarr)/binsize;
        if(bin>=NoOfBins)
        {
            bin=NoOfBins - 1;
        }
        printf("Process( %d ):Number [%d] is %d\t Bin:%d\n",id,iCount,arr[iCount],bin);
        
        parhist[bin]++;    
    
    }
    
    for(iCount=0;iCount<NoOfBins;iCount++)
    {
        spin_lock(&lock[id]);
            histogram[iCount] = histogram[iCount] + parhist[iCount];
        spin_unlock(&lock[id]);
    }
    
    printf("\n");    
    process_join(NoOfBins,id);
    
    for(iCount=0;iCount<NoOfBins;iCount++)
    {
        printf("No Of Items in Bin(%d) is %d\n",iCount,histogram[iCount]);
    }
    cleanup_memory(&shmidlock);;
    cleanup_memory(&shmidhist);    
    return 0;
}
  
Share: 



Easy Tutor
Easy Tutor author of Program of histogram by calculating the partial histograms and consolidating in the end to get the final histogram is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
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!