Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:00:32+00:00 2026-06-01T17:00:32+00:00

This is a mock bank database problem involving deadlock. I already have what I

  • 0

This is a mock bank database problem involving deadlock. I already have what I think is the answer, but I’m curious if this is a good solution. The question posed is the following:

How do you prevent deadlock in the following code?:

void transaction(Account from, Account to, double amount)
{
    Semaphore lock1, lock2;
    lock1 = getLock(from);
    lock2 = getLock(to);

    wait(lock1);
       wait(lock2);

          withdraw(from, amount);
          deposit(to, amount);

       signal(lock2);
    signal(lock1);
 }

The way this can become deadlocked is via two threads (or processes?) calling the transaction() method at the same time with opposing accounts, i.e.:

transaction(savings, checking, 1); in thread 1 and

transaction(checking, savings, 2); in thread 2.

From my poor understanding, I think what is going on and why it is getting deadlocked is because the lock ordering is not obeyed since both threads are trying to get a lock (from each other?).

My quick and dirty solution would be to move the locks outside of the function transaction() to where it would look like this when called:

 //somewhere in main
    Semaphore lock1, lock2;
    lock1 = getLock(from);
    lock2 = getLock(to);

    wait(lock1);
       wait(lock2);

       transaction(checking, savings, 2);

       signal(lock2);
    signal(lock1);
    //.....

with transaction looking like this:

void transaction(Account from, Account to, double amount)
{
   withdraw(from, amount);
   deposit(to, amount);
}

That way they can never be executed at the same time, since transaction is technically the critical section. If this were a java program, could you also use monitors by putting the word synchronized after void in the function declaration? Would that work? That seems like a smarter way to do it.

I also may not be understanding this at all, so feel free to school me, especially if my explanations are not accurate. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:00:34+00:00Added an answer on June 1, 2026 at 5:00 pm

    I think what is going on and why it is getting deadlocked is because the lock ordering is not obeyed since both threads are trying to get a lock

    The problem here is as you mention that one thread like do:

    wait(fromLock);
       wait(toLock);
    

    while another might do:

    wait(toLock);
       wait(fromLock);
    

    which might cause deadlock.

    What you need to do is insure that they always lock in the same order. You could use the id of the account somehow to figure out the order of the locks:

    if (from.id < to.id) {
       wait(fromLock)
         wait(toLock)
           transaction(checking, savings, 2);
         signal(toLock);
       signal(fromLock);
    } else {
       wait(toLock)
         wait(FromLock)
           transaction(checking, savings, 2);
         signal(FromLock);
       signal(toLock);
    }
    

    I believe that will resolve any deadlocks. You might also want to put a check for the from and to being the same entity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this mock database: users: userid companies: company_id user_id franchises: franchise_id company_id stores:
I've followed this answer to mock HttpContext, Request, Response and User and set Controller.ControllerContext
I have this mock up HTML/CSS website here that is working perfectly fine on
I have a problem with one of my googletest unittests that uses a mock
I have a mock being created like this: var mock = new Mock<IPacket>(MockBehavior.Strict); mock.Setup(p
How do I mock the method GetValues() in System.Data.IDataReader? This method changes the array
Can I do this without Javascript? Here is the mock markup.. <div id=parent> <div
I was wondering how I can mock the ClaimseReponse class in DotNetOpenAuth? This is
I am creating a general general mock-client for testing HTTP-interactions. For this, I would
I'm using Microsoft Moles to mock a method. This method call another method that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.