Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

mutex generic question

A and B are mutexes, and are initialized
Whats the proper way to handle mutex A And B

void Task1(void)
{
   while (1)
   {
      mutex_acquire(A);
      do_task1_A_stuff();

      mutex_acquire(B);
      do_task1_AB_stuff();

      mutex_release(A);
      do_task1_B_stuff();

      mutex_release(B);
   }
}

void Task2(void)
{
   while (1)
   {
      mutex_acquire(B);
      do_task2_B_stuff();

      mutex_acquire(A);
      do_task2_AB_stuff();

      mutex_release(B);
      do_task2_A_stuff();

      mutex_release(A);
   }
}
0
brianpowell
Asked:
brianpowell
  • 2
1 Solution
 
sunnycoderCommented:
There are several ways but the above is not one ... it may lead to deadlock when task 1 acquires A and waits for B & task2 acquires B and waits for A. Googling around for bakery algorithm and deadlock avoidance algorihtms would help. I would also recommend operating system book by silberschatz & peter galvin.
0
 
sunnycoderCommented:
One possible way would be

void Task1(void)
{
   while (1)
   {
      mutex_acquire(A);
      do_task1_A_stuff();

      mutex_acquire(B);
      do_task1_AB_stuff();

      do_task1_B_stuff();

      mutex_release(B);
      mutex_release(A);
   }
}

void Task2(void)
{
   while (1)
   {
      mutex_acquire(A);
      mutex_acquire(B);
      do_task2_B_stuff();

      do_task2_AB_stuff();

      mutex_release(B);
      do_task2_A_stuff();

      mutex_release(A);
   }
}

Make sure that resources/mutexes are acquired in same fixed order and released in the reverse order. It is similar to prioritizing the resources and making sure that no lower priority resource is requested if you have a larger priority resource already acquired.
0
 
balderCommented:
how about

void Task1(void)
{
   while (1)
   {
      mutex_acquire(A);
      do_task1_A_stuff();
      mutex_release(A); /* can be removed, but might interleave better */

      mutex_acquire(A); /* can be removed ... */
      mutex_acquire(B);
      do_task1_AB_stuff();
      mutex_release(B); /* avoid deadlock with task 2 */
      mutex_release(A); /* release task 2 if blocked */

      mutex_acquire(B);
      do_task1_B_stuff();
      mutex_release(B);
   }
}

void Task2(void)
{
   while (1)
   {
      mutex_acquire(B);
      do_task2_B_stuff();
      mutex_release(B); /* release task 1 if blocked */

      mutex_acquire(A); /* wait for task 1 */
      mutex_acquire(B);
      do_task2_AB_stuff();
      mutex_release(B);
      mutex_release(A); /* can be removed ... */

      mutex_acquire(A); /* can be removed ... */
      do_task2_A_stuff();
      mutex_release(A);
   }
}
0
 
Kelvin_KingCommented:
Are you allowed to reorder the P() and V() calls ? or is this some kind of exercise you need to solve in that specific order ?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now