-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDekkerAlgorithm(18blc1131).sce
More file actions
100 lines (92 loc) · 2.55 KB
/
DekkerAlgorithm(18blc1131).sce
File metadata and controls
100 lines (92 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
////DEKKER'S ALGORITHM////
clear;
clc;
funcprot(0)
disp("Dekkers Algorithm by Nikita Rath (18BLC1131)");
//INSTRUCTIONS TO PROCEED :
printf(" 1) Process 1 enters \n");
printf(" 2) Process 2 enters \n");
printf(" 3) Both process enters \n");
printf(" 4) Exit \n");
printf("\n Enter time in CS for Process 1, 2 and limit of CS :")
//INITIALIZING THE VALUES
c1=1,c2=1,turn=1;
count0=0,count1=0,count=0;
i=0;
//Deafult values
limit=10; //Limit of CS
a=1,b=1; //Time taken in CS by both process
//FUNCTION TO IDENTIFY WHICH PROCESS ENTERS NOW
function [] = P(a,b,limit)
while (i<limit)
printf("\n")
x = input("SELECT THE OPTION : ")
//PROCESS 1 ENTERS
if x==1 then
c1=0;
while c2==0
if turn==2 then
c1=1;
while turn==2 //do nothing
end
c1=0;
end
end
//critical section
count0=count0+a;
i=i+a;
// yield
c1=1;
turn=2;
//remainder section
if (count0>limit) | (i>limit) then
printf("END\n");
return
else
printf("Process P1 Enters the Critical section");
printf("\nTime of P1 in Critical Section :%d\n",count0);
printf("\nIt is the turn process P2\n");
end
end
//PROCESS 2 ENTERS
if x==2 then
c2=0;
while c1==0
if turn==1
c2=1;
while turn==1 //do nothing
end
c2=0;
end
end
//critical section
count1=count1+b;
i=i+b;
// yield
c2=1;
turn=1;
//remainder section
if (count1>limit) | (i>limit) then
printf("END\n");
return
else
printf("Process P2 Enters the Critical section");
printf("\nTime of P2 in Critical Section :%d\n",count1);
printf("\nIt is the turn process P1\n");
end
end
//BOTH PROCESS ENTER AT SAME TIME
if x==3 then
printf("\nBoth process cant enter at same time in Critical Section\n");
if i>limit then
printf("END\n");
return
end
end
//OTHER CONDITIONS
if x==4 then
printf("\nEND\n");
return
end
end
endfunction