Implementation of Dekker’s Algorithm


//Dekker’s Algorithm

# include <stdio.h>

# include<pthread.h>

# include<cursors.h>

int turn=0, flag[2]={0,0},bal=0;

void longdelay()

{

long int t1,t2;

for(t1=0;t1<64000;t1++);

for(t2=0;t2<64000;t2++);

}

void shortdelay()

{

int t3,t4;

for(t3=0;t3<32000;t3++)

for(t4=0;t4<32000;t4++)

}

void test(int i)

{

int j,k,m,p,q,r,c;

j=1-i;

for(k=0;k<3;k++)

{

flag[i]=1;

while(flag[j])

{

if (turn==j)

{

flag[i]=0;

printf(“Iam waiting:%d”,i);

while(turn==j);

flag[i]=1;

}

}

longdelay();

printf(“Iam entering %d”,i);

c=bal;

/*critical section*/

printf(“\n process: %d”,l);

printf(“flag[%d]=%d, flag[%d]=%d”,i,flag[i],j,flag[j]);

printf(“turn=%d\n”,turn);

bal=c+1000;

printf(“the balance is %d”,bal);

turn=j;

flag[i]=0;

printf(“Iam exiting: %d”,i);

shortdelay();

}

}

int main()

{

pthread_t t1,t2;

pthread_creat(&t,NULL,(void*)&test,(void*)0);

printf(“After creation of first thread…..”);

pthread_creat(&t2,NULL,(void*)&test,(void*)1);

pthread_join(t1,NULL);

pthread_join(t2,NULL);

sleep(s);

printf(“Parent terminated:%d”,bal);

return(0);

}

Advertisements