What programming language you start with really all depends on where you want to go with programming/coding. The great thing about this field is that there are an absolute abundance of smaller fields that you can go into, all using programming in their own unique ways. For web applications, a good start would be with HTML and later moving your way through CSS, JavaScript, JQuery, PHP, SQL, and any of the JavaScript libraries. Ruby is also a popular choice, so I would recommend checking that out too. For more scientific fields or areas with more machine learning and A.I., Python is generally a great place to start as it is widely used in that field of study. C++ is also a very useful language to know for that, but it can be a little more challenging for beginners. For game and application design, languages such as C#, C, Swift, Kotlin, and Java are most often used for that.
Description:
Create a program that will solve the banker’s algorithm. This algorithm stops deadlocks from happening by not allowing processes to start if they don’t have access to the resources necessary to finish. A process is allocated certain resources from the start, and there are other available resources. In order for the process to end, it has to have the maximum resources in each slot.
Ex:
Process Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
Since there is 3, 3, 2 available, P1 or P3 would be able to go first. Let’s pick P1 for the example. Next, P1 will release the resources that it held, so the next available would be 5, 3, 2.
The Challenge:
Create a program that will read a text file with the banker’s algorithm in it, and output the order that the processes should go in. An example of a text file would be like this:
[3 3 2]
[0 1 0 7 5 3]
[2 0 0 3 2 2]
[3 0 2 9 0 2]
[2 1 1 2 2 2]
[0 0 2 4 3 3]
And the program would print out:
P1, P4, P3, P0, P2
Solution
in Python
def add(iter1, iter2):
for a, b in zip(iter1, iter2):
yield a + b
def subtract(iter1, iter2):
for a, b in zip(iter1, iter2):
yield a - b
def greater_equal(iter1, iter2):
for a, b in zip(iter1, iter2):
yield a >= b
with open('bank.txt', 'r') as file:
lines = file.readlines()
lines = [tuple(map(int, line.strip('[] \n\r').split())) for line in lines]
available = lines[0]
processes = [(i, process[:len(available)], process[len(available):]) for i, process in enumerate(lines[1:])]
order = []
while processes:
for process in processes:
if all(greater_equal(available, subtract(process[2], process[1]))):
processes.remove(process)
available = tuple(add(available, process[1]))
order.append(process[0])
break
else:
print("no solution found")
break
else:
print(', '.join(['P' + str(n) for n in order]))
Create a program that will solve the banker’s algorithm. This algorithm stops deadlocks from happening by not allowing processes to start if they don’t have access to the resources necessary to finish. A process is allocated certain resources from the start, and there are other available resources. In order for the process to end, it has to have the maximum resources in each slot.
Ex:
Process Allocation Max Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
Since there is 3, 3, 2 available, P1 or P3 would be able to go first. Let’s pick P1 for the example. Next, P1 will release the resources that it held, so the next available would be 5, 3, 2.
The Challenge:
Create a program that will read a text file with the banker’s algorithm in it, and output the order that the processes should go in. An example of a text file would be like this:
[3 3 2]
[0 1 0 7 5 3]
[2 0 0 3 2 2]
[3 0 2 9 0 2]
[2 1 1 2 2 2]
[0 0 2 4 3 3]
And the program would print out:
P1, P4, P3, P0, P2
Solution
in Python
def add(iter1, iter2):
for a, b in zip(iter1, iter2):
yield a + b
def subtract(iter1, iter2):
for a, b in zip(iter1, iter2):
yield a - b
def greater_equal(iter1, iter2):
for a, b in zip(iter1, iter2):
yield a >= b
with open('bank.txt', 'r') as file:
lines = file.readlines()
lines = [tuple(map(int, line.strip('[] \n\r').split())) for line in lines]
available = lines[0]
processes = [(i, process[:len(available)], process[len(available):]) for i, process in enumerate(lines[1:])]
order = []
while processes:
for process in processes:
if all(greater_equal(available, subtract(process[2], process[1]))):
processes.remove(process)
available = tuple(add(available, process[1]))
order.append(process[0])
break
else:
print("no solution found")
break
else:
print(', '.join(['P' + str(n) for n in order]))
Comments
Post a Comment