Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
me will write a multithreaded program that implements the
banker’s algorithm . Several customers request and
release resources fromthe bank. The bankerwill grant a request only if it leaves
the system in a safe state. A request that leaves the system in an unsafe state
will be denied. This programming assignment combines three separate topics:
(1)multithreading, (2) preventing race conditions, and (3) deadlock avoidance
How do you write a program؟Confused | :confused: Confused | :confused: Confused | :confused: Confused | :confused:
Posted
Comments
saephoed 8-Jan-14 19:28pm    
did you read http://en.wikipedia.org/wiki/Banker%27s_algorithm and try out that sample program there?

When I write a program, I typically start by trying to understand what my requirements are. Now, you have a decent set of requirements there. What you need to do now is break these down into individual tasks, and as you are intending this to be multithreaded, you need to consider what effect changing values will have. An example, suppose that we have a bank that has $200 in the vaults. Now, two customers come through the door - one wants to deposit $100 and the other wants to withdraw $250 - both go to a different teller and perform their transaction, we have a race condition here... If the depositing customer is served first, then the bank will have the necessary funds to give to the second customer. If, however, the second customer is served first their request would be denied because their are insufficient funds.

Now, add in a third customer who also wants to withdraw $250. The first customer deposits the $100, but we have two customers who are trying to withdraw the funds - if both tellers try to take the money at the same time, we have a deadlock.

What you need to do is keep this type of behaviour in your mind while you write your program. It's not a simple task, but it is your task so I'll leave it to you to actually write the code.
 
Share this answer
 
Comments
OriginalGriff 8-Jan-14 14:03pm    
Perfect! :thumbsup:
Ron Beyer 8-Jan-14 14:04pm    
Got my vote.
CPallini 8-Jan-14 15:05pm    
5.
Member 13190926 10-May-17 4:12am    
do you have the code? I still don't understand
All three of those are deeply intertwined topics... in order to use multithreading effectively, you must understand the importance of preventing deadlocks and race conditions. In order for multiple threads to coexist nicely, any communications between them MUST be synchronized and shared data must be access controlled, hence the assignment.

You cannot rely on operations happening in another thread to occur when you logically expect them to, this is because different processors are going to split up processing time between threads and processes slightly differently. That's the reason you need to use synchronization mechanisms such as "events" (google "using events for synchronization" if you don't know what it is).

In addition to synchronization, you also need to make sure data isn't changed/accessed simultaneously (or almost simultaneously) across multiple threads. This is usually what leads to race conditions. To deal with these situations, there are objects that "lock" the data from access while a particular thread is accessing shared data. There are various options available from critical sections to mutexes to semaphores.
 
Share this answer
 
Comments
CPallini 8-Jan-14 15:05pm    
5.
Albert Holguin 8-Jan-14 19:18pm    
Thanks
saephoed 8-Jan-14 17:39pm    
did you read http://en.wikipedia.org/wiki/Banker%27s_algorithm and try out that sample program there?
Albert Holguin 8-Jan-14 19:17pm    
Not sure why this comment is on here for me... I don't follow.
saephoed 8-Jan-14 19:28pm    
sry :( pushed the wrong reply btn

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900