Google Ads

Monday, July 27, 2009

DEKKER ‘S ALGORITHM

DEKKER ‘S ALGORITHM

Previous Next

#include”stdio.h”
#include"conio.h"
#include"time.h"
#include"type.h"
int turn;
int f0=0,f1=0;
int count=0;
void p0()
{
f0=1;
while(f1)
{
if(turn==1)
{
f0=0;
printf("\n Waiting time of p0 is starting \n");
while(turn==1)
{
if(count<5)
{
printf("\n p1 is in critical section \n");
printf("\n Do nothing \n");
count=count+1;
}
else
{
count=0;
turn=0;
f1=0;
}
}
f0=1;
}
}
printf("\n p0 is now u r in critical section \n");
turn=1;
f0=0;
}
void p1()
{
f1=1;
while(f0)
{
if(turn==0)
{
f1=0;
while(turn==0)
{
if(count<5)
{
printf("\n p0 is in critical section \n");
printf("\n do nothing \n");
count=count+1;
}
else
{
count=0;
turn=1;
f0=0;
}
}
f1=1;
}
}
printf("\n p1 is now u r in critical section \n");
turn=0;
f1=0;
}

void main()
{
int op;
do
{ clrscr();
printf("\n\t\t\t MUTUAL EXCLUSION PROBLEM \n");
printf(" \t\t\t ~~~~~~~~~~~~~~~~~~~~~~~~\n\n");
printf("\n 1: p0 in p1 wants \n");
printf("\n 2: p1 in p0 wants \n");
printf("\n 3: First p0 next p1 \n");
printf("\n 4: First p1 next p0 \n");
switch(op)
{
case 1:
f0=1;
turn=0;
p1();
break;
case 2:
f1=1;
turn=1;
p0();
break;
case 3:
p0();
p1();
break;
case 4:
p1();
p0();
break;
/* default;
break; */
}
printf("\n Enter u r choice-->");
scanf("%d",&op);
}
while(op<5);
}

No comments:

Post a Comment