-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bank Application using Petersons Algo.c
76 lines (65 loc) · 1.62 KB
/
Bank Application using Petersons Algo.c
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
//Bank application using Peterson’s algorithm.
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct
{
int turn ,flag[2],bal;
}shmpacket;
int main()
{
int shmid, damt,wamt,sleeptime,option,pid;
shmpacket *ptr;
shmid=shmget(200, sizeof (shmpacket), IPC_CREAT|0666);
ptr=(shmpacket *) shmat(shmid,0,0);
ptr->bal=5000;
pid=fork();
if (pid==0)
{
do
{ ptr->flag[1]=1;
ptr->turn=0;
while ((ptr->turn==0) && (ptr->flag[0]==1));
printf("c: entering cs\n");
printf("c:enter the amount to withdraw\n");
wamt=rand()%1000;
printf(" c:withdraw amt= %d\n",wamt);
if (ptr->bal -wamt >=1000)
{ printf("c:withdraw amt= %d\n", wamt);
ptr->bal=ptr->bal-wamt;
printf("C: remaining balance = %d\n",ptr->bal);
}
else
{ printf("c:withdraw not possible- minimum balance problem\n");
printf("C: current balance = %d\n",ptr->bal);
}
ptr->flag[1]=0;
sleeptime=rand()%5;
sleep(sleeptime);
option=rand()%10;
} while(option!=0);
exit(0);
}
if (pid >0)
{
do
{ ptr->flag[0]=1;
ptr->turn=1;
while ((ptr->turn==1) && (ptr->flag[1]==1));
printf("p: entering cs\n");
printf("p:enter the amount to deposit\n");
damt=rand()%1000;
printf(" P:deposit amt= %d\n",damt);
ptr->bal=ptr->bal+damt;
printf("P: balance = %d\n",ptr->bal);
ptr->flag[0]=0;
sleeptime=rand()%5;
sleep(sleeptime);
option=rand()%10;
} while(option!=0);
exit(0);
}
}