Logo 
Search:

C Programming Articles

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

Program to calculate average of the elements of an array and then the average deviation using race condition

Posted By: Lola Hughes     Category: C Programming     Views: 3108

Program to calculate average of the elements of an array and then the average deviation using race condition.

Code for Program to calculate average of the elements of an array and then the average deviation using race condition in C Programming

# include <stdio.h>
# include <math.h>
# include "/usr/include/sys/types.h"
# include "/usr/include/sys/shm.h"
# include "/usr/include/sys/ipc.h"
# include "/usr/include/sys/sem.h"
# include "forkjoin.h"
# include "sharedlib.h"
# include "spinlock.h"int main()
{
    int arr[100];
    int arrSize;        //Size of arrayint iCount;        //Counter Variableint id;            //Process IDint nProc=3;        //number of Processesfloat sum=0;
    
    float *deviation,*avg;  // Shared Variablesint *lock;        // Shared Variable for Spinlockint shmidavg,shmidlock,shmiddeviation;    // Shmid for Shared Variables
    
    printf("Enter the Size of an Array :");
    scanf("%d",&arrSize);

    for(iCount=0;iCount<arrSize;iCount++)
    {
        printf("Enter arr[%d] :",iCount);
        scanf("%d",&arr[iCount]);
    }

    /* Allocate Shared memory */
avg=(float*)sshared(sizeof(float),&shmidavg); deviation=(float*)sshared(sizeof(float),&shmiddeviation); lock=(int*)sshared(sizeof(int),&shmidlock); spin_lock_init(lock); // Spin Lock Initialization *avg=0; id=process_fork(nProc); // Forking Processes/* Partial Sum using Loop Spliting */
for(iCount=id;iCount<arrSize;iCount=iCount+nProc) { sum = sum + arr[iCount]; } spin_lock(lock); /* Critical Region */
*avg = *avg + ( sum /arrSize); // Calculate Average from Partial Sums spin_unlock(lock); /*
---------------------------------------------------------------------------
Barrier Should be here, So that all Processes have to wait until Final
Average is Calculated.
---------------------------------------------------------------------------
*/
sum=0; /* Calculate Sum(Xi - Mean) Using Loop Spliting */
for(iCount=id;iCount<arrSize;iCount=iCount+nProc) { sum = sum + pow(((float)arr[iCount] - *avg),2); } spin_lock(lock); /* Critical Region */
*deviation = *deviation + (sum / arrSize-1); // Calculate Final Deviation spin_unlock(lock); process_join(nProc,id); // Joining the Process *deviation=sqrt(*deviation); printf("Deviation : %f\n",*deviation); /* Cleaning the Shared Region */
cleanup_memory(&shmidavg); cleanup_memory(&shmidlock); cleanup_memory(&shmiddeviation); return 0; } /* Output

[divyen@localhost pp-tw3]$ cc -o Prog01 -lm Prog01.c
[divyen@localhost pp-tw3]$ ./Prog01
Enter the Size of an Array :5
Enter arr[0] :10
Enter arr[1] :20
Enter arr[2] :30
Enter arr[3] :40
Enter arr[4] :50
Deviation : 19.580603

*/
  
Share: 



Lola Hughes
Lola Hughes author of Program to calculate average of the elements of an array and then the average deviation using race condition is from London, United Kingdom.
 
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!