-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathos_project.c
More file actions
63 lines (56 loc) · 1.53 KB
/
Copy pathos_project.c
File metadata and controls
63 lines (56 loc) · 1.53 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
/*
A) The first known correct software solution to the critical-section problem
for two processes was developed by Dekker. The two processes, P0 and P1 , share the following
variables:
boolean flag[2]; (initially false)
int turn;
The structure of process Pi is ( I == 0 or 1) ; the other process is Pj (j== 1 or 0). Write a program which satisfies all three requirements for the critical-section problem.
B) Write a program for the Swap () instruction can be used to provide mutual exclusion that satisfies the
bounded-waiting requirement.
CODE
*/
#include<pthread.h>
#include<stdio.h>
void *P0(void *);
void *P1(void *);
int flag[2];
int turn=0;
int global=100;
int main()
{
pthread_t pid1,pid2;
pthread_create(&pid1,NULL,P0,NULL);
pthread_create(&pid2,NULL,P1,NULL);
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
}
void *P0(void *val)
{
int i=0;
while(i<2)
{
flag[0]=1;
turn=1;
while(flag[1]==1 && turn==1)
printf("Critical Section"); // Enters Critical Section
global+=100;
printf("\n Remainder Section %d",global); // Remainder Section
flag[0]=0;
i++;
}
}
void *P1(void *val)
{
int i=0;
while(i<2)
{
flag[1]=1;
turn=0;
while(flag[0]==1 && turn==0)
printf("Critical Section"); // Enters Critical Section
global-=75;
printf("\n Remainder Section %d",global); // Remainder Section
flag[1]=0;
i++;
}
}