Logo 
Search:

C Programming Articles

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

PROGRAM OF CREATE HEADER FILE FOR BARRIER

Posted By: Harley Evans     Category: C Programming     Views: 2019

PROGRAM OF CREATE HEADER FILE FOR BARRIER.

Code for PROGRAM OF CREATE HEADER FILE FOR BARRIER in C Programming

#include<stdio.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/shm.h>
#include "headsem.h"int * bar_init(int np)
{
        int id,*bar;

        id=shmget(IPC_PRIVATE,10,0666|IPC_CREAT);
        bar=(int *)shmat(id,0,0);

        lock_init(&(bar[3]));

        bar[0]=np;
        bar[1]=0;
        bar[2]=0;
        bar[4]=id;

        return bar;
}

void barrier(int *bar)
{
        int flag=0;
        while(1)
        {
                locksem(&(bar[3]));
                if((flag==0) && (bar[2]>0))
                {
                        unlock(bar+3);
                        continue;
                }
                if(flag==0)
                {
                        bar[1]++;
                        flag=1;
                }
                if((bar[1]<bar[0]) && (bar[2]==0))
                {
                        unlock(bar+3);
                        continue;
                }
                        else
                {
                        if(bar[2]==0)
                        {
                                bar[1]=0;
                                bar[2]=bar[0]-1;
                                unlock(bar+3);
                                return;
                        }
                        else
                        {
                                bar[2]--;
                                unlock(bar+3);
                                return;
                        }
                }
                unlock(bar+3);
                exit(-1);
        }
}

/*void clean_bar(int *bar)
{
free_sem(bar+3);
free_shm(bar[4]);
}*/
  
Share: 


Didn't find what you were looking for? Find more on PROGRAM OF CREATE HEADER FILE FOR BARRIER Or get search suggestion and latest updates.

Harley Evans
Harley Evans author of PROGRAM OF CREATE HEADER FILE FOR BARRIER 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!