Skip to content

Instantly share code, notes, and snippets.

@nlrahimi
Created October 15, 2020 23:19
Show Gist options
  • Save nlrahimi/364ec8cbfa6c8d935e4902f99fa9040b to your computer and use it in GitHub Desktop.
Save nlrahimi/364ec8cbfa6c8d935e4902f99fa9040b to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Simulate content switching conducted by OS Process Manager. Context switching saves the state of the currently running process from CPU to PCB (unless this process has completed its execution) and restores the state of the newly scheduled process from PCB to CPU. Test your Process Manager on my file “ece565hw02.txt”."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#--Importing Libraries --#\n",
"import time\n",
"from timeit import default_timer as timer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dic = []\n",
"d = {}\n",
"i = 0\n",
"with open(\"ece565hw02.txt\") as file:\n",
" while i<4:\n",
" counter = 0\n",
" for line in file: \n",
" line = line.strip(\"\\n\")\n",
" (key, val) = line.split(\",\")\n",
" d[key]=val\n",
" d_copy = d.copy() # a ref to a copy of dictionary instead of dictionary\n",
" counter +=1\n",
" if counter > 10:\n",
" break\n",
" dic.append(d_copy)\n",
" i+=1\n",
"for di in dic:\n",
" print(di)\n",
" for key in di:\n",
" print(key, di[key])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#This scheduler goes by priority\n",
"def scheduler(running_id, running_pr, PCB):\n",
" best_id = int(running_id)\n",
" best_pr = int(running_pr)\n",
" for i in PCB:\n",
" if int(i['Pr']) < best_pr: #(the less the higher pr) if the pr of running process is more than the 'i'th process,\n",
" #but if the 'i'th process has a higher priority then scheduler should return its ID\n",
" best_id = i['PID']\n",
" best_pr = int(i['Pr'])\n",
" return best_id\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# add the running p to the CPU\n",
"for di in dic:\n",
" if di['State'] == 'Running':\n",
" CPU = di\n",
"print(\"CPU\",CPU)\n",
"# add all other p to the PCB\n",
"PCB1 = []\n",
"for di in dic:\n",
" if di['State'] != 'Running':\n",
" PCB1.append(di)\n",
"print(\"PCB\",PCB1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def dispatcher(CPU, PCB):\n",
" \n",
" #if burst time is not 0\n",
" if CPU['CPU'] != 0: \n",
" CPU['State'] = 'Ready'\n",
" PCB.append(CPU)\n",
" pid = scheduler(CPU['PID'], CPU['Pr'], PCB)\n",
" for p in PCB:\n",
" if p['PID'] == pid:\n",
" p['State'] = 'Running'\n",
" CPU = p \n",
" PCB.remove(p)\n",
" # p in CPU done running\n",
" else:\n",
" CPU['State'] = 'Finished'\n",
" # running p is done, so we don't need to pass it to scheduler\n",
" pid = scheduler(-111,10000, PCB)\n",
" #print('dispatcher scheduler pid', pid)\n",
" #print('dispatcher scheduler PCB', PCB)\n",
" if len(PCB) == 0:\n",
" return 0\n",
" for p in PCB:\n",
" if p['PID'] == pid:\n",
" p['State'] = 'Running'\n",
" CPU = p\n",
" PCB.remove(p)\n",
" \n",
" return [CPU, PCB]\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def Process_Manager(CPU, PCB):\n",
" # cpu start\n",
" start = timer()\n",
" # every second past, the burst of cpu-1\n",
" while True:\n",
" end = timer()\n",
" if end >= start+1:\n",
" start = start + 1\n",
" pid = scheduler(CPU['PID'], CPU['Pr'], PCB)\n",
" print('CPU', CPU)\n",
" if int(pid) == int(CPU['PID']): \n",
" CPU['CPU'] = int(CPU['CPU']) -1\n",
" if CPU['CPU'] == 0:\n",
" print('Proces', CPU['PID'], 'is done.')\n",
" print(\"======================================\")\n",
" prc = dispatcher(CPU, PCB)\n",
" if prc == 0:\n",
" break\n",
" else:\n",
" [CPU, PCB] = prc\n",
" else:\n",
"\n",
" [CPU,PCB] = dispatcher(CPU, PCB)\n",
"\n",
"Process_Manager(CPU, PCB1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.11"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment