· 5 years ago · Apr 01, 2020, 11:10 AM
1import pathlib
2import sqlite3
3import threading
4import random
5import time
6
7
8class PetersonSolution():
9 # Check if database exists or not, if not Create a new database
10 file = pathlib.Path("Peter.db")
11 if file.exists():
12 print("File exist")
13 conn = sqlite3.connect('Peter.db')
14 c = conn.cursor()
15 c.execute("SELECT * FROM `security`")
16 conn.commit()
17 for row in c.fetchall():
18 user = row[1]
19 print("user = ", user)
20 break
21 else:
22 print("Creating DB")
23 conn = sqlite3.connect('Peter.db')
24 c = conn.cursor()
25 c.execute("""CREATE TABLE security(Account INTEGER, User string NOT NULL)""")
26 user = input("Enter Username")
27 abc = input("enter account money")
28 c = conn.cursor()
29 c.execute('INSERT INTO security(Account, User) VALUES(?,?)', (abc, user))
30 conn.commit()
31 sel_user = user
32 c = conn.cursor()
33 c.execute("SELECT `Account` FROM `security` WHERE `User` = ?", (sel_user,))
34 conn.commit()
35 for row in c.fetchall():
36 balance = row[0]
37 print("Balance = ", balance)
38 print()
39 break
40 # since we are starting with process 0, the other process would be 1.
41 other = 1
42 turn = 0
43
44 # at the start none of the process is interested in critical section.
45 interested = [False, False]
46
47
48
49 def EntrySection(self, *args):
50 # if process 0 entered other process would be 1 and if process 1 entered other process would be 0.
51 PetersonSolution.other = 1 - args[0]
52 # entered process is intrested, hence True
53 PetersonSolution.interested[args[0]] = True
54
55 # set turn to the entering process
56 PetersonSolution.turn = args[0]
57
58 # if other process is already in the critical section prevent any other process from entering
59 while PetersonSolution.interested[PetersonSolution.other] == True and PetersonSolution.turn == args[0]:
60 if args[0] == 0:
61 # PetersonSolution.balance = PetersonSolution.balance + 10
62 print(f"\tProcess P0 is waiting for Deposit")
63 #time.sleep(1)
64 else:
65 # PetersonSolution.balance = PetersonSolution.balance - 10
66 print(f"\tProcess P1 is waiting for Withdrawal")
67
68 #time.sleep(1)
69
70
71 # if no process is in critical section then run critical section
72 conn = sqlite3.connect('Peter.db')
73 if args[0] == 0:
74 print(f"\tInitial Balance = {PetersonSolution.balance} Rupees")
75 balance = PetersonSolution.balance + 10
76 print(f"\tBalance after Depositing = {balance} Rupees")
77 print()
78 c = conn.cursor()
79 c.execute('UPDATE security SET `Account` = ? WHERE `Account` = ?', (balance, PetersonSolution.balance))
80 conn.commit()
81 #c.close()
82
83 else:
84 print(f"\tInitail Balance = {PetersonSolution.balance} Rupees")
85 balance = PetersonSolution.balance - 10
86 print(f"\tBalance After Withdrawing = {balance} Rupees")
87 print()
88 c = conn.cursor()
89 c.execute('UPDATE security SET `Account` = ? WHERE `Account` = ?', (balance, PetersonSolution.balance))
90 conn.commit()
91
92 self.ExitSection(args[0])
93 time.sleep(3000)
94
95
96 def ExitSection(self, process):
97 # process finished executing, make interested for the process as False
98 PetersonSolution.interested[process] = False
99
100 def main(self):
101 p = 50
102 while p > 1:
103 p
104 print(f"Iteration {p} :-")
105 # start process 0, passing process index 0 as args since Thread supports args and kwargs argument only
106 t1 = threading.Thread(target=self.EntrySection, args=(0,))
107 t1.start()
108 # start process 1,passing process index 1 as args
109 t2 = threading.Thread(target=self.EntrySection, args=(1,))
110 t2.start()
111 #time.sleep(0.1)
112 p = p - 1
113
114
115
116
117if __name__ == "__main__":
118 p = PetersonSolution()
119 p.main()
120
121 #i = 3000
122 #i = i + 10