Logo 
Search:

C Programming Articles

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

PROGRAM FOR SUM OF ARRAY USING SPIN LOCK

Posted By: Freya Brown     Category: C Programming     Views: 2457

PROGRAM FOR SUM OF ARRAY USING SPIN LOCK.

Code for PROGRAM FOR SUM OF ARRAY USING SPIN LOCK in C Programming

#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h>
#include<sys/ipc.h>
#include "headsem.h"
#include "headfork.h"

main()
{
    int tempsum=0,np,a[10];
    int *lock1,*finalsum;
    int id,n,i,sid1,sid2;

    sid1=shmget(IPC_PRIVATE,10,0666|IPC_CREAT);
    lock1=(int *)shmat(sid1,0,0);

    sid2=shmget(IPC_PRIVATE,10,0666|IPC_CREAT);
    finalsum=(int *)shmat(sid2,0,0);

    *finalsum=0;

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

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

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

    lock_init(lock1);

    id=p_fork(np);

    for(i=id;i<n;i+=np)
        tempsum+=a[i];

    locksem(lock1);

    *finalsum+=tempsum;

    unlock(lock1);

    p_join(np,id);
    printf("Sum : %d",*finalsum);
}

  
Share: 


Didn't find what you were looking for? Find more on PROGRAM FOR SUM OF ARRAY USING SPIN LOCK Or get search suggestion and latest updates.

Freya Brown
Freya Brown author of PROGRAM FOR SUM OF ARRAY USING SPIN LOCK 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!