Logo 
Search:

C Programming Articles

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

PROGRAM FOR HISTOGRAM FOR INDIVIDUAL CELL OF HISTOGRAM THERE ARE SEPARATE LOCK

Posted By: Hilma Miller     Category: C Programming     Views: 2418

PROGRAM FOR HISTOGRAM FOR INDIVIDUAL CELL OF HISTOGRAM THERE ARE SEPARATE LOCK.

Code for PROGRAM FOR HISTOGRAM FOR INDIVIDUAL CELL OF HISTOGRAM THERE ARE SEPARATE LOCK in C Programming

#include<stdio.h>
#include "headsem.h"
#include "headfork.h"
#include "headshr.h"
#include<math.h>

main()
{
    int i,np,bin,nobins,a[10],n;
    int id,sid1,sid2,sid3,sid4;
    float binsz;
    int *min,*max,*list,*lock1;

    void find_min_max(int *,int *,int *,int);

    min=(int *)shared(sizeof(int)*2,&sid1);
          max=(int *)shared(sizeof(int)*2,&sid2);
          lock1=(int *)shared(sizeof(int)*2,&sid3);
          list=(int *)shared(sizeof(int)*2,&sid4);

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

    printf("Enter the elements of array :\n");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);

    printf("Enter the no.of proc: ");
    scanf("%d",&np);

    printf("Enter the no.of bins :");
    scanf("%d",&nobins);
    
    for(i=0;i<nobins;i++)
        list[i]=0;
    for(i=0;i<nobins;i++)
        lock_init(&lock1[i]);
    
    find_min_max(a,min,max,n);

    binsz=(*max-*min)/(float)nobins;
    printf("\nBin size is : %f\n",binsz);
    
    id=p_fork(np);

    for(i=id;i<n;i+=np)
    {
        bin=1+(int)((abs(a[i]-*min))/binsz);
        
        if(bin>nobins)
            bin=nobins;
        
        locksem(&lock1[i]);
        list[bin]=list[bin]+1;
        unlock(&lock1[i]);
    }

    p_join(np,id);

    printf("Hist array :\n");
    for(i=1;i<=nobins;i++)
        printf("%d\t",list[i]);

}

void find_min_max(int *a,int *min,int *max,int n)
{
    int i=0;
    *min=*max=a[0];

    for(i=1;i<n;i++)
    {
        if(a[i] < *min)
            *min=a[i];
        if(a[i] > *max)
            *max=a[i];
    }
}




    
    
 
OUTPUT

knoppix@ttyp0[pp]$ cc histo_lps.c
knoppix@ttyp0[pp]$ ./a.out

Enter the size of array : 7

Enter the elements of array :
2
3
4
5
6
7
8

Enter the no.of proc: 3

Enter the no.of bins :4

Bin size is : 1.500000

Hist array :
2       1       2       2
  
Share: 



Hilma Miller
Hilma Miller author of PROGRAM FOR HISTOGRAM FOR INDIVIDUAL CELL OF HISTOGRAM THERE ARE SEPARATE LOCK is from Frankfurt, Germany.
 
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!