Logo 
Search:

C Programming Articles

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

Program of histogram using self-scheduling and locking the whole bin.

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

Write a program of histogram using self-scheduling and locking the whole bin.

Code for Program of histogram using self-scheduling and locking the whole bin. 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 *index;                // Shared Counterint NoOfBins=5;                // Number of binsint binsize;                // Size of each binint *histogram;
    int *lock1,*lock2;            // Lock1 for Index// Lock2 For Binint shmidindex,shmidlock1,shmidlock2,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;

    index=(int*) sshared(sizeof(int),&shmidindex);
    lock1=(int*)sshared(sizeof(int),&shmidlock1);
    lock2=(int*)sshared(sizeof(int),&shmidlock2);
    histogram=(int*) sshared(sizeof(int)*NoOfBins,&shmidhist);
    
    printf("\n Bin Size : %d",binsize);
    printf("\n No. Of Bins : %d\n",NoOfBins);
    spin_lock_init(lock1);
    spin_lock_init(lock2);
    *index=0;
    for(iCount=0;iCount<NoOfBins;iCount++)
    {
        histogram[iCount]=0;
    }
    id=process_fork(NoOfBins);
        
    while(1)
    {
        spin_lock(lock1);
            i=*index;
            *index=*index+1;
        spin_unlock(lock1);
        
        if(i>=arrSize)
        {
            break;
        }

        bin=abs((arr[i] - minarr)/binsize);
        if(bin>=NoOfBins)
        {
            bin=NoOfBins-1;
        }
        printf("Number %d is  : %d\t Bin : %d\n",i,arr[i],bin);
        
        /* Locking Whole Bin */
        spin_lock(lock2);
            histogram[bin]++;
        spin_unlock(lock2);
    }
    
    printf("No of Items in Bin(%d) : %d\n",id,histogram[id]);
    process_join(NoOfBins,id);

    cleanup_memory(&shmidindex);
    cleanup_memory(&shmidlock1);
    cleanup_memory(&shmidlock2);
    cleanup_memory(&shmidhist);
    
    return 0;
}
  
Share: 



Easy Tutor
Easy Tutor author of Program of histogram using self-scheduling and locking the whole bin. 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!