Sunday, February 6, 2011

Critical section Problem Using Dekker's Algorithm


OBJECTIVE: Write a program for synchronizing of critical section problem using Dekker’s Algorithm.
THEORY: In concurrent programming a critical section is a piece of code that asses a shared resource (data structure or device ) that must not be concurrently accessed by more than one thread of execution. A critical section will usually terminate in fixed time, and a thread, task or process will only have to wait a fixed time to enter it (i.e. bounded waiting .
PROGRAM:
#include<sydio.h>
#include<conio.h>
void main()
{
int choice;
int c1=1,c2=1,turn=1;
clrscr();
do
{
printf(“\n1.Process 1 Enter”);
printf(“\n2.Process 2 Enter”);
printf(“\n3.Both Process Enter”);
printf(“\n4.Exit”);
scanf(“%d”,&choice);
if(choice==1)
{
c1=0;
while(c2==0)
{
                                if(turn==2)
c1=1
while(turn==2);
c1=0;
}
printf(“\nProcess P1 Enters the Critical section”);
c1=1;
turn=2;
else
printf(“\nIt is the turn process P2”);
}
if(choice==2)
{
                c2=0;
                while(c1==0)
{
                if(turn==1)
                c2=1;
while(turn==1);
c2=0;
}
printf(“\nProcess P2 enters in critical section”);
c2=1;
turn=1;
else
printf(“\nIt is turn of Process P1”);
}
}
while(choice!=4);
}
OUTPUT:
Enter  the choice:             1
Process 1 Enters Critical section
Enter choice       3
Both Process i

1 comment:

  1. there is problem with this
    if you enter 1 2
    then it runs separately
    like first it run taking option as 1
    then taking 2 as option
    if we enter 1 2 3 then also it run three times separately
    it should enter only once take account which process to enter first

    ReplyDelete