· 5 years ago · Apr 01, 2020, 10:58 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"Process P0 is waiting for Deposit")
63 time.sleep(1)
64 else:
65 # PetersonSolution.balance = PetersonSolution.balance - 10
66 print(f"Process 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"Initial Balance = {PetersonSolution.balance} Rupees")
75 balance = PetersonSolution.balance + 10
76 print(f"Balance after Depositing = {balance} Rupees")
77 c = conn.cursor()
78 c.execute('UPDATE security SET `Account` = ? WHERE `Account` = ?', (balance, PetersonSolution.balance))
79 conn.commit()
80 #c.close()
81
82 else:
83 print(f"Initail Balance = {PetersonSolution.balance} Rupees")
84 balance = PetersonSolution.balance - 10
85 print(f"Balance After Withdrawing = {balance} Rupees")
86 c = conn.cursor()
87 c.execute('UPDATE security SET `Account` = ? WHERE `Account` = ?', (balance, PetersonSolution.balance))
88 conn.commit()
89
90 self.ExitSection(args[0])
91 time.sleep(3000)
92
93
94 def ExitSection(self, process):
95 # process finished executing, make interested for the process as False
96 PetersonSolution.interested[process] = False
97
98 def main(self):
99 p = 50
100 while p > 1:
101 # start process 0, passing process index 0 as args since Thread supports args and kwargs argument only
102 t1 = threading.Thread(target=self.EntrySection, args=(0,))
103 t1.start()
104 # start process 1,passing process index 1 as args
105 t2 = threading.Thread(target=self.EntrySection, args=(1,))
106 t2.start()
107 #time.sleep(0.1)
108 p = p - 1
109 print("iteration = ", p)
110
111
112
113if __name__ == "__main__":
114 p = PetersonSolution()
115 p.main()
116
117 #i = 3000
118 #i = i + 10