Unverified Commit 477f0c40 authored by JyothsnaNallani's avatar JyothsnaNallani Committed by GitHub
Browse files

Add files via upload

parents
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "ConcurrencyControl.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "AEbtoGpG2Ucz"
},
"source": [
"file1=open('sample_data/inputs/database.txt', 'r')\n",
"Lines1=file1.readlines()\n",
"nums=[]\n",
"for index,line in enumerate(Lines1):\n",
" line=line.strip()\n",
" if index!=0:\n",
" data=line.split(',') \n",
" x=[] \n",
" for ind,i in enumerate(data): \n",
" if ind!=0:\n",
" x.append(int(i))\n",
" nums.append(x)"
],
"execution_count": 23,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "gjAGh1HL64zT"
},
"source": [
"def serialTransactions():\n",
" vars={\n",
" 'x':0,\n",
" 'y':0,\n",
" 'z':0,\n",
" } \n",
" for transactionId in range(1,7): #looping through each transaction\n",
" filename='sample_data/inputs/t'+str(transactionId)+'.txt'\n",
" file=open(filename, 'r')\n",
" Lines=file.readlines()\n",
"\n",
" schedulerFile=open('sample_data/outputs/schedule1.txt','a')\n",
"\n",
"\n",
" from copy import copy, deepcopy\n",
" dummyDb = deepcopy(nums) \n",
" commands=[]\n",
"\n",
" for ind,line in enumerate(Lines):\n",
" #line in transaction\n",
" c=line.strip()\n",
" operands=c.split(',')\n",
" \n",
" if c.startswith('START',0,5):\n",
" log=operands[0]+' '+operands[1]+'\\n'\n",
" schedulerFile.write(log)\n",
"\n",
" if c.startswith('READ',0,5): #for READ operation\n",
" numsInd=operands[2]\n",
" if operands[2][0]=='A':\n",
" vars[operands[1]]=dummyDb[0][int(numsInd[1:])-1]\n",
" elif operands[2][0]=='B':\n",
" vars[operands[1]]=dummyDb[1][int(numsInd[1:])-1]\n",
" elif operands[2][0]=='C':\n",
" vars[operands[1]]=dummyDb[2][int(numsInd[1:])-1]\n",
" log=operands[0]+' '+operands[2]+' value into '+operands[1]+' i.e, '+str(vars.get(operands[1]))+'\\n'\n",
" schedulerFile.write(log)\n",
" \n",
"\n",
" elif(c.startswith('WRITE',0,5)): #for WRITE operation\n",
" numsInd=operands[2]\n",
" if operands[2][0]=='A':\n",
" dummyDb[0][int(operands[2][1])-1]=vars.get(operands[1])\n",
" elif operands[2][0]=='B':\n",
" dummyDb[1][int(operands[2][1])-1]=vars.get(operands[1])\n",
" elif operands[2][0]=='C':\n",
" dummyDb[2][int(operands[2][1])-1]=vars.get(operands[1])\n",
" log=operands[0]+' '+operands[1]+' value into '+operands[2]+' i.e, '+str(vars.get(operands[1]))+'\\n'\n",
" schedulerFile.write(log)\n",
"\n",
" elif(c.startswith('EXECUTE',0,7)): #for EXECUTE operation\n",
" if '=' in operands[1]:\n",
" vars[operands[1][0]]=vars.get(operands[1][2])+vars.get(operands[1][4])\n",
" else:\n",
" vars[operands[1][0]]=vars.get(operands[1][0])+int(operands[1][2])\n",
" log=operands[0]+' operation '+operands[1]+' i.e, '+str(vars.get(operands[1][0]))+'\\n'\n",
" schedulerFile.write(log)\n",
"\n",
" elif(c.startswith('COMMIT',0,6)): #for COMMIT operation\n",
" log=operands[0]+' transaction T'+str(transactionId)+'\\n'\n",
" schedulerFile.write(log)\n",
"\n",
"\n",
" elif(c.startswith('END',0,6)): #for END operation\n",
" log=operands[0]+' transaction T'+str(transactionId)+'\\n'\n",
" schedulerFile.write(log)\n",
" file.close()\n",
" schedulerFile.close()\n",
"\n",
" f = open(\"sample_data/outputs/database1.txt\", \"w\")\n",
" f.write(Lines1[0])\n",
" for ind,i in enumerate(dummyDb):\n",
" ln=''+str((ind+1))+','\n",
" f.write(ln)\n",
" for j in i:\n",
" ln=''+str(j)+','\n",
" f.write(ln)\n",
" f.write('\\n')\n",
" f.close()\n",
"\n",
"\n",
"\n",
"serialTransactions()"
],
"execution_count": 24,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "UkGU2dZrkoEg"
},
"source": [
"def interleavingTransactions():\n",
" vars={\n",
" 'x':0,\n",
" 'y':0,\n",
" 'z':0,\n",
" }\n",
" from copy import copy, deepcopy\n",
" dummyDb = deepcopy(nums) \n",
" schedulerFile=open('sample_data/outputs/schedule2.txt','a')\n",
"\n",
" def executeCommand(command):\n",
" transId=0\n",
" operands=command.split(',')\n",
" if command.startswith('START',0,5):\n",
" transId=int(operands[1][1])\n",
" log=operands[0]+' '+operands[1]+'\\n'\n",
" schedulerFile.write(log)\n",
" \n",
" elif command.startswith('END',0,6):\n",
" log=operands[0]+' transaction T'+str(transId)+'\\n'\n",
" schedulerFile.write(log)\n",
"\n",
" if command.startswith('READ',0,5):\n",
" #READ\n",
" numsInd=operands[2]\n",
" if operands[2][0]=='A':\n",
" vars[operands[1]]=dummyDb[0][int(numsInd[1:])-1]\n",
" elif operands[2][0]=='B':\n",
" vars[operands[1]]=dummyDb[1][int(numsInd[1:])-1]\n",
" elif operands[2][0]=='C':\n",
" vars[operands[1]]=dummyDb[2][int(numsInd[1:])-1]\n",
" log=operands[0]+' '+operands[2]+' value into '+operands[1]+' i.e, '+str(vars.get(operands[1]))+'\\n'\n",
" schedulerFile.write(log)\n",
"\n",
"\n",
" elif command.startswith('WRITE',0,5): \n",
" #WRITE \n",
" numsInd=operands[2]\n",
" if operands[2][0]=='A':\n",
" dummyDb[0][int(operands[2][1])-1]=vars.get(operands[1])\n",
" elif operands[2][0]=='B':\n",
" dummyDb[1][int(operands[2][1])-1]=vars.get(operands[1])\n",
" elif operands[2][0]=='C':\n",
" dummyDb[2][int(operands[2][1])-1]=vars.get(operands[1])\n",
" log=operands[0]+' '+operands[1]+' value into '+operands[2]+' i.e, '+str(vars.get(operands[1]))+'\\n'\n",
" schedulerFile.write(log)\n",
"\n",
" elif command.startswith('EXECUTE',0,7):\n",
" #EXECUTE\n",
" if '=' in operands[1]:\n",
" vars[operands[1][0]]=vars.get(operands[1][2])+vars.get(operands[1][4])\n",
" else:\n",
" vars[operands[1][0]]=vars.get(operands[1][0])+int(operands[1][2])\n",
" log=operands[0]+' operation '+operands[1]+' i.e, '+str(vars.get(operands[1][0]))+'\\n'\n",
" schedulerFile.write(log) \n",
"\n",
"\n",
" tQueue=[]\n",
" for transId in range(1,7):# each transaction\n",
" tQueue.append('sample_data/inputs/t'+str(transId)+'.txt')\n",
" pipeline1=[]\n",
" pipeline2=[]\n",
"\n",
" while len(tQueue)!=0:\n",
" if len(pipeline1)==0:\n",
" f=open(tQueue[0])\n",
" Lines=f.readlines()\n",
" for l in Lines:\n",
" pipeline1.append(l.strip())\n",
" tQueue.pop(0)\n",
" if len(pipeline2)==0:\n",
" f=open(tQueue[0])\n",
" Lines=f.readlines()\n",
" for l in Lines:\n",
" pipeline2.append(l.strip())\n",
" tQueue.pop(0)\n",
" while len(pipeline1)!=0 and len(pipeline2)!=0:\n",
" executeCommand(pipeline1[0])\n",
" pipeline1.pop(0)\n",
" executeCommand(pipeline2[0])\n",
" pipeline2.pop(0)\n",
" f1 = open(\"sample_data/outputs/database2.txt\", \"w\")\n",
" f1.write(Lines1[0])\n",
" for ind,i in enumerate(dummyDb):\n",
" ln=''+str((ind+1))+','\n",
" f1.write(ln)\n",
" for j in i:\n",
" ln=''+str(j)+','\n",
" f1.write(ln)\n",
" f1.write('\\n')\n",
" f1.close()\n",
" \n",
" \n",
"interleavingTransactions()"
],
"execution_count": 25,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "V3tUyFWU49ae"
},
"source": [
"def strictTwoPhaseLocking():\n",
" vars={\n",
" 'x':0,\n",
" 'y':0,\n",
" 'z':0,\n",
" }\n",
"\n",
" locks = dict()\n",
"\n",
" from copy import copy, deepcopy\n",
" dummyDb = deepcopy(nums) \n",
" schedulerFile=open('sample_data/outputs/schedule3.txt','a')\n",
" transId=0\n",
"\n",
" def executeCommand(command, transId):\n",
" operands=command.split(',')\n",
"\n",
" if len(operands) > 2 and operands[2] not in locks:\n",
" l = dict()\n",
" l['read'] = 0 \n",
" l['write'] = 0\n",
" locks[operands[2]] = l\n",
" \n",
" if command.startswith('START',0,5):\n",
" log=operands[0]+' '+operands[1]+'\\n'\n",
" schedulerFile.write(log)\n",
" \n",
" elif command.startswith('END',0,6):\n",
" log=str(transId)+' '+operands[0]+' transaction T'+str(transId)+'\\n'\n",
" schedulerFile.write(log)\n",
"\n",
" elif command.startswith('COMMIT', 0, 6):\n",
" log=operands[0]+' transaction T'+str(transId)+'\\n'\n",
" schedulerFile.write(log)\n",
" for k, v in locks.items():\n",
" if locks[k]['write'] == transId:\n",
" locks[k]['write'] = 0;\n",
"\n",
" if command.startswith('READ',0,5):\n",
" #READ\n",
"\n",
" if locks[operands[2]]['write'] !=0 and locks[operands[2]]['write'] != transId:\n",
" return False\n",
" locks[operands[2]]['read'] = transId \n",
" \n",
" numsInd=operands[2]\n",
" if operands[2][0]=='A':\n",
" vars[operands[1]]=dummyDb[0][int(numsInd[1:])-1]\n",
" elif operands[2][0]=='B':\n",
" vars[operands[1]]=dummyDb[1][int(numsInd[1:])-1]\n",
" elif operands[2][0]=='C':\n",
" vars[operands[1]]=dummyDb[2][int(numsInd[1:])-1]\n",
" \n",
" log=str(transId)+' '+operands[0]+' '+operands[2]+' value into '+operands[1]+' i.e, '+str(vars.get(operands[1]))+'\\n'\n",
" schedulerFile.write(log)\n",
"\n",
" locks[operands[2]]['read'] = 0\n",
"\n",
" elif command.startswith('WRITE',0,5): \n",
" #WRITE \n",
" if locks[operands[2]]['write'] != 0 and locks[operands[2]]['write'] != transId:\n",
" return False\n",
"\n",
" locks[operands[2]]['write'] = transId\n",
"\n",
" numsInd=operands[2]\n",
" if operands[2][0]=='A':\n",
" dummyDb[0][int(operands[2][1])-1]=vars.get(operands[1])\n",
" elif operands[2][0]=='B':\n",
" dummyDb[1][int(operands[2][1])-1]=vars.get(operands[1])\n",
" elif operands[2][0]=='C':\n",
" dummyDb[2][int(operands[2][1])-1]=vars.get(operands[1])\n",
" log=str(transId)+' '+operands[0]+' '+operands[1]+' value into '+operands[2]+' i.e, '+str(vars.get(operands[1]))+'\\n'\n",
" schedulerFile.write(log)\n",
"\n",
"\n",
" elif command.startswith('EXECUTE',0,7):\n",
" #EXECUTE\n",
" if '=' in operands[1]:\n",
" vars[operands[1][0]]=vars.get(operands[1][2])+vars.get(operands[1][4])\n",
" else:\n",
" vars[operands[1][0]]=vars.get(operands[1][0])+int(operands[1][2])\n",
" log=str(transId)+' '+operands[0]+' operation '+operands[1]+' i.e, '+str(vars.get(operands[1][0]))+'\\n'\n",
" schedulerFile.write(log)\n",
"\n",
" return True\n",
"\n",
"\n",
" tQueue=[]\n",
" for tId in range(1,7):# each transaction\n",
" tQueue.append('sample_data/inputs/t'+str(tId)+'.txt')\n",
" pipeline1=[]\n",
" pipeline2=[]\n",
"\n",
" p1Id = 0\n",
" p2Id = 0\n",
"\n",
" while len(tQueue)!=0:\n",
" if len(pipeline1)==0:\n",
" f=open(tQueue[0])\n",
" Lines=f.readlines()\n",
" for l in Lines:\n",
" pipeline1.append(l.strip())\n",
" p1Id = 7 - len(tQueue)\n",
" tQueue.pop(0)\n",
" if len(pipeline2)==0:\n",
" f=open(tQueue[0])\n",
" Lines=f.readlines()\n",
" for l in Lines:\n",
" pipeline2.append(l.strip())\n",
" p2Id = 7 - len(tQueue)\n",
" tQueue.pop(0)\n",
"\n",
" breaker = 0;\n",
" \n",
" while len(pipeline1)!=0 and len(pipeline2)!=0:\n",
" breaker+= 1\n",
" if executeCommand(pipeline1[0], p1Id):\n",
" pipeline1.pop(0)\n",
" breaker = 0\n",
" if executeCommand(pipeline2[0], p2Id):\n",
" pipeline2.pop(0)\n",
"\n",
" if breaker > 1000:\n",
" break\n",
" if breaker > 1000:\n",
" schedulerFile.write(\"Deadlocked!! with transactions \"+str(p1Id)+\" and \"+str(p2Id))\n",
" break\n",
" f1 = open(\"sample_data/outputs/database3.txt\", \"w\")\n",
" f1.write(Lines1[0])\n",
" for ind,i in enumerate(dummyDb):\n",
" ln=''+str((ind+1))+','\n",
" f1.write(ln)\n",
" for j in i:\n",
" ln=''+str(j)+','\n",
" f1.write(ln)\n",
" f1.write('\\n')\n",
" f1.close()\n",
"\n",
"\n",
"strictTwoPhaseLocking()"
],
"execution_count": 28,
"outputs": []
}
]
}
\ No newline at end of file
%% Cell type:code id: tags:
```
file1=open('sample_data/inputs/database.txt', 'r')
Lines1=file1.readlines()
nums=[]
for index,line in enumerate(Lines1):
line=line.strip()
if index!=0:
data=line.split(',')
x=[]
for ind,i in enumerate(data):
if ind!=0:
x.append(int(i))
nums.append(x)
```
%% Cell type:code id: tags:
```
def serialTransactions():
vars={
'x':0,
'y':0,
'z':0,
}
for transactionId in range(1,7): #looping through each transaction
filename='sample_data/inputs/t'+str(transactionId)+'.txt'
file=open(filename, 'r')
Lines=file.readlines()
schedulerFile=open('sample_data/outputs/schedule1.txt','a')
from copy import copy, deepcopy
dummyDb = deepcopy(nums)
commands=[]
for ind,line in enumerate(Lines):
#line in transaction
c=line.strip()
operands=c.split(',')
if c.startswith('START',0,5):
log=operands[0]+' '+operands[1]+'\n'
schedulerFile.write(log)
if c.startswith('READ',0,5): #for READ operation
numsInd=operands[2]
if operands[2][0]=='A':
vars[operands[1]]=dummyDb[0][int(numsInd[1:])-1]
elif operands[2][0]=='B':
vars[operands[1]]=dummyDb[1][int(numsInd[1:])-1]
elif operands[2][0]=='C':
vars[operands[1]]=dummyDb[2][int(numsInd[1:])-1]
log=operands[0]+' '+operands[2]+' value into '+operands[1]+' i.e, '+str(vars.get(operands[1]))+'\n'
schedulerFile.write(log)
elif(c.startswith('WRITE',0,5)): #for WRITE operation
numsInd=operands[2]
if operands[2][0]=='A':
dummyDb[0][int(operands[2][1])-1]=vars.get(operands[1])
elif operands[2][0]=='B':
dummyDb[1][int(operands[2][1])-1]=vars.get(operands[1])
elif operands[2][0]=='C':
dummyDb[2][int(operands[2][1])-1]=vars.get(operands[1])
log=operands[0]+' '+operands[1]+' value into '+operands[2]+' i.e, '+str(vars.get(operands[1]))+'\n'
schedulerFile.write(log)
elif(c.startswith('EXECUTE',0,7)): #for EXECUTE operation
if '=' in operands[1]:
vars[operands[1][0]]=vars.get(operands[1][2])+vars.get(operands[1][4])
else:
vars[operands[1][0]]=vars.get(operands[1][0])+int(operands[1][2])
log=operands[0]+' operation '+operands[1]+' i.e, '+str(vars.get(operands[1][0]))+'\n'
schedulerFile.write(log)
elif(c.startswith('COMMIT',0,6)): #for COMMIT operation
log=operands[0]+' transaction T'+str(transactionId)+'\n'
schedulerFile.write(log)
elif(c.startswith('END',0,6)): #for END operation
log=operands[0]+' transaction T'+str(transactionId)+'\n'
schedulerFile.write(log)
file.close()
schedulerFile.close()
f = open("sample_data/outputs/database1.txt", "w")
f.write(Lines1[0])
for ind,i in enumerate(dummyDb):
ln=''+str((ind+1))+','
f.write(ln)
for j in i:
ln=''+str(j)+','
f.write(ln)
f.write('\n')
f.close()
serialTransactions()
```
%% Cell type:code id: tags:
```
def interleavingTransactions():
vars={
'x':0,
'y':0,
'z':0,
}
from copy import copy, deepcopy
dummyDb = deepcopy(nums)
schedulerFile=open('sample_data/outputs/schedule2.txt','a')
def executeCommand(command):
transId=0
operands=command.split(',')
if command.startswith('START',0,5):
transId=int(operands[1][1])
log=operands[0]+' '+operands[1]+'\n'
schedulerFile.write(log)
elif command.startswith('END',0,6):
log=operands[0]+' transaction T'+str(transId)+'\n'
schedulerFile.write(log)
if command.startswith('READ',0,5):
#READ
numsInd=operands[2]
if operands[2][0]=='A':
vars[operands[1]]=dummyDb[0][int(numsInd[1:])-1]
elif operands[2][0]=='B':
vars[operands[1]]=dummyDb[1][int(numsInd[1:])-1]
elif operands[2][0]=='C':
vars[operands[1]]=dummyDb[2][int(numsInd[1:])-1]
log=operands[0]+' '+operands[2]+' value into '+operands[1]+' i.e, '+str(vars.get(operands[1]))+'\n'
schedulerFile.write(log)
elif command.startswith('WRITE',0,5):
#WRITE
numsInd=operands[2]
if operands[2][0]=='A':
dummyDb[0][int(operands[2][1])-1]=vars.get(operands[1])
elif operands[2][0]=='B':
dummyDb[1][int(operands[2][1])-1]=vars.get(operands[1])
elif operands[2][0]=='C':
dummyDb[2][int(operands[2][1])-1]=vars.get(operands[1])
log=operands[0]+' '+operands[1]+' value into '+operands[2]+' i.e, '+str(vars.get(operands[1]))+'\n'
schedulerFile.write(log)
elif command.startswith('EXECUTE',0,7):
#EXECUTE
if '=' in operands[1]:
vars[operands[1][0]]=vars.get(operands[1][2])+vars.get(operands[1][4])
else:
vars[operands[1][0]]=vars.get(operands[1][0])+int(operands[1][2])
log=operands[0]+' operation '+operands[1]+' i.e, '+str(vars.get(operands[1][0]))+'\n'
schedulerFile.write(log)
tQueue=[]
for transId in range(1,7):# each transaction
tQueue.append('sample_data/inputs/t'+str(transId)+'.txt')
pipeline1=[]
pipeline2=[]
while len(tQueue)!=0:
if len(pipeline1)==0:
f=open(tQueue[0])
Lines=f.readlines()
for l in Lines:
pipeline1.append(l.strip())
tQueue.pop(0)
if len(pipeline2)==0:
f=open(tQueue[0])
Lines=f.readlines()
for l in Lines:
pipeline2.append(l.strip())
tQueue.pop(0)
while len(pipeline1)!=0 and len(pipeline2)!=0:
executeCommand(pipeline1[0])
pipeline1.pop(0)
executeCommand(pipeline2[0])
pipeline2.pop(0)
f1 = open("sample_data/outputs/database2.txt", "w")
f1.write(Lines1[0])
for ind,i in enumerate(dummyDb):
ln=''+str((ind+1))+','
f1.write(ln)
for j in i:
ln=''+str(j)+','
f1.write(ln)
f1.write('\n')
f1.close()
interleavingTransactions()
```
%% Cell type:code id: tags:
```
def strictTwoPhaseLocking():
vars={
'x':0,
'y':0,
'z':0,
}
locks = dict()
from copy import copy, deepcopy
dummyDb = deepcopy(nums)
schedulerFile=open('sample_data/outputs/schedule3.txt','a')
transId=0
def executeCommand(command, transId):
operands=command.split(',')
if len(operands) > 2 and operands[2] not in locks:
l = dict()
l['read'] = 0
l['write'] = 0
locks[operands[2]] = l
if command.startswith('START',0,5):
log=operands[0]+' '+operands[1]+'\n'
schedulerFile.write(log)
elif command.startswith('END',0,6):
log=str(transId)+' '+operands[0]+' transaction T'+str(transId)+'\n'
schedulerFile.write(log)
elif command.startswith('COMMIT', 0, 6):
log=operands[0]+' transaction T'+str(transId)+'\n'
schedulerFile.write(log)
for k, v in locks.items():
if locks[k]['write'] == transId:
locks[k]['write'] = 0;
if command.startswith('READ',0,5):
#READ
if locks[operands[2]]['write'] !=0 and locks[operands[2]]['write'] != transId:
return False
locks[operands[2]]['read'] = transId
numsInd=operands[2]
if operands[2][0]=='A':
vars[operands[1]]=dummyDb[0][int(numsInd[1:])-1]
elif operands[2][0]=='B':
vars[operands[1]]=dummyDb[1][int(numsInd[1:])-1]
elif operands[2][0]=='C':
vars[operands[1]]=dummyDb[2][int(numsInd[1:])-1]
log=str(transId)+' '+operands[0]+' '+operands[2]+' value into '+operands[1]+' i.e, '+str(vars.get(operands[1]))+'\n'
schedulerFile.write(log)
locks[operands[2]]['read'] = 0
elif command.startswith('WRITE',0,5):
#WRITE
if locks[operands[2]]['write'] != 0 and locks[operands[2]]['write'] != transId:
return False