编写程序模拟2-3个事务并发执行,简单实现调度器功能:加锁(含更新锁),根据锁表判断事务是否可获得锁,解锁
生活随笔
收集整理的這篇文章主要介紹了
编写程序模拟2-3个事务并发执行,简单实现调度器功能:加锁(含更新锁),根据锁表判断事务是否可获得锁,解锁
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Disk = {'A': 2,'B': 2,
}Memory = {}T = 10flag = Falsedef r1(read_data):print('讀取完成:' + read_data + '={}'.format(Disk[read_data]))def r2(read_data):print('讀取完成:' + read_data + '={}'.format(Disk[read_data]))def w1(write_data):global TDisk[write_data] = Tprint('寫入完成:' + write_data + '={}'.format(Disk[write_data]))def print_now_lock_status(list):print(list)lock_list = {"A": {'model': '默認','Wait': False,'wait_list': {'Tran': '', 'Mode': '', 'wait': False, 'Tnext': None,'Next': {'Tran': '', 'Mode': '', 'wait': False, 'Tnext': None, 'Next': None},},},"B": {'model': '默認','Wait': False,'wait_list': {'Tran': '', 'Mode': '', 'wait': False, 'Tnext': None,'Next': {'Tran': '', 'Mode': '', 'wait': False, 'Tnext': None, 'Next': None},},},
}def print_now_status():print('鎖表信息:')print(lock_list)def sl1(element):print('Transaction1對元素A進行操作')print('鎖表中不存在A的表項,可以加鎖')lock_list[element]['model'] = 'S'lock_list[element]['wait_list']['Tran'] = 'T1'lock_list[element]['wait_list']['Mode'] = 'S'lock_list[element]['wait_list']['wait'] = Falselock_list[element]['wait_list']['Tnext'] = Nonelock_list[element]['wait_list']['Next'] = {'Tran': '', 'Mode': '', 'wait': False, 'Tnext': None, 'Next': None}def sl2(element):if lock_list[element]['model'] == 'U':print('Transaction2對元素{}進行操作'.format(element))print('鎖表中元素{}的組模式為U,不可授予新的共享鎖,請等待釋放后再加鎖!'.format(element))lock_list[element]['Wait'] = Truelock_list[element]['wait_list']['Tran'] = 'T2'lock_list[element]['wait_list']['Mode'] = 'S'lock_list[element]['wait_list']['wait'] = Truelock_list[element]['wait_list']['Tnext'] = Nonelock_list[element]['wait_list']['Next'] = None# lock_list[element]['wait_list'] = {{'Tran': 'T2', 'Mode': 'S', 'wait': True, 'Tnext': None, 'Next': None}, }elif lock_list[element]['model'] == 'X':print('Transaction2對元素{}進行操作'.format(element))print('鎖表中元素{}的組模式為X,不可授予新的共享鎖,請等待釋放后再加鎖!'.format(element))lock_list[element]['Wait'] = Truelock_list[element]['wait_list']['Tran'] = 'T2'lock_list[element]['wait_list']['Mode'] = 'S'lock_list[element]['wait_list']['wait'] = Truelock_list[element]['wait_list']['Tnext'] = Nonelock_list[element]['wait_list']['Next'] = None# lock_list[element]['wait_list'] = {{'Tran': 'T2', 'Mode': 'S', 'wait': True, 'Tnext': None, 'Next': None}, }else:if lock_list[element]['wait_list']['Tran'] == '':print('Transaction2對元素B進行操作')print('鎖表中不存在B的表項,可以加鎖')lock_list[element]['model'] = 'S'lock_list[element]['wait_list']['Tran'] = 'T2'lock_list[element]['wait_list']['Mode'] = 'S'lock_list[element]['wait_list']['wait'] = Falselock_list[element]['wait_list']['Tnext'] = Nonelock_list[element]['wait_list']['Next'] = {'Tran': '', 'Mode': '', 'wait': False, 'Tnext': None,'Next': None}else:print('Transaction2對元素{}進行操作'.format(element))print('鎖表中元素{}的組模式為S,可以加共享鎖'.format(element))lock_list[element]['model'] = 'S'lock_list[element]['wait_list']['Next']['Tran'] = 'T2'lock_list[element]['wait_list']['Next']['Mode'] = 'S'lock_list[element]['wait_list']['Next']['wait'] = Falselock_list[element]['wait_list']['Next']['Tnext'] = None# lock_list['A']['wait_list']['Next'] = {'Tran': 'T2', 'Mode': 'S', 'wait': True, 'Tnext': None, 'Next': None}lock_list[element]['wait_list']['Next']['Next'] = Nonedef ul1(element):if lock_list[element]['model'] == 'S':print('可以加鎖,組模式變為U')lock_list[element]['model'] = 'U'lock_list[element]['Wait'] = Falselock_list[element]['wait_list']['Next']['Tran'] = 'T1'lock_list[element]['wait_list']['Next']['Mode'] = 'U'lock_list[element]['wait_list']['Next']['wait'] = Falselock_list[element]['wait_list']['Next']['Tnext'] = Nonelock_list[element]['wait_list']['Next']['Next'] = {'Tran': '', 'Mode': '', 'wait': False, 'Tnext': None,'Next': None}elif lock_list[element]['model'] == 'X':print('不可授予新的鎖,請等待其他事務該元素后再加鎖!')lock_list[element]['Wait'] = Truelock_list[element]['wait_list']['Tran'] = 'T1'lock_list[element]['wait_list']['Mode'] = 'U'lock_list[element]['wait_list']['wait'] = Truelock_list[element]['wait_list']['Tnext'] = Nonelock_list[element]['wait_list']['Next'] = {'Tran': '', 'Mode': '', 'wait': False, 'Tnext': None, 'Next': None}else:while True:if flag == True:if lock_list[element]['wait_list']['Next']['wait'] == False:print('Transaction2已經釋放該元素加了sl鎖,所以事務1的ul鎖可以升級為xl鎖')lock_list[element]['model'] = 'X'lock_list[element]['Wait'] = Falselock_list[element]['wait_list']['Next']['Next']['Tran'] = 'T1'lock_list[element]['wait_list']['Next']['Next']['Mode'] = 'X'lock_list[element]['wait_list']['Next']['Next']['wait'] = Falselock_list[element]['wait_list']['Next']['Next']['Tnext'] = Nonelock_list[element]['wait_list']['Next']['Next']['Next'] = Noneglobal TDisk[element] = T# print(Disk[element])breakbreak# 定義事務類
class Transaction:# 初始化def __init__(self, df, cmd):self.define = dfself.command = cmd# self.flag = Falsedef pause(self):print('執行一步 : 按n ')c = input()print(c)while (c != 'n'):print('輸入錯誤')c = input()print(c)return cdef excute(self):# self.print_cmd()print('初始鎖表信息:')print_now_status()can_pause = Truefor cmd in self.command:if can_pause:c = self.pause()print('執行動作 {}'.format(cmd))exc_cmd = cmdexec(exc_cmd)print_now_status()def xl1(element):if lock_list[element]['wait_list']['Tran'] != '':print('Transaction2已經對元素{}加了sl鎖,所以事務1的ul鎖不可以升級為xl鎖'.format(element))lock_list[element]['Wait'] = Truelock_list[element]['wait_list']['Next']['Next']['Tran'] = 'T1'lock_list[element]['wait_list']['Next']['Next']['Mode'] = 'U'lock_list[element]['wait_list']['Next']['Next']['wait'] = Truelock_list[element]['wait_list']['Next']['Next']['Tnext'] = Nonelock_list[element]['wait_list']['Next']['Next']['Next'] = Nonedef u2(element):if element == 'A':lock_list['A']['wait_list']['Next'] = Noneelif element == 'B':lock_list['B']['wait_list']['Next']['wait'] = Falselock_list['B']['wait_list']['Tran'] = ''lock_list['B']['wait_list']['Mode'] = ''global flagflag = Trueul1('B')def u1(element):if element == 'A':lock_list['A']['model'] = ''lock_list['A']['Wait'] = Falselock_list['A']['wait_list'] = Noneelif element == 'B':lock_list['B']['model'] = ''lock_list['B']['Wait'] = Falselock_list['B']['wait_list'] = Noneif __name__ == "__main__":df3 = 'A = 2,B = 2'commend3 = ['sl1(\'A\')', 'r1(\'A\')', 'sl2(\'A\')', 'r2(\'A\')', 'sl2(\'B\')', 'r2(\'B\')', 'ul1(\'B\')','r1(\'B\')', 'xl1(\'B\')', 'u2(\'A\')', 'u2(\'B\')', 'w1(\'B\')', 'u1(\'A\')', 'u1(\'B\')']Transaction3 = Transaction(df3, commend3)print('事務執行順序:')print_now_lock_status(commend3)Transaction3.excute()
總結
以上是生活随笔為你收集整理的编写程序模拟2-3个事务并发执行,简单实现调度器功能:加锁(含更新锁),根据锁表判断事务是否可获得锁,解锁的全部內容,希望文章能夠幫你解決所遇到的問題。