1

The following is a simple solution to the 'too much milk problem'

    lock mutex;

    while (1){

        lock_acquire(mutex);

        if (no milk)
            go and buy milk;//action-1

        lock_release(mutex);
    }

The problem is that, action-1 can take a lot of time to accomplish, making any of the processes waiting to acquire the mutex to wait for a long time.

One way to avoid this is to have a timer so that the process buying milk will return with or without milk once the timer goes off. As you can see, there are problems with this. (e.g: there is no way to identify whether the process has already bought milk and on its way home)

Is there a better solution to this?

EDIT: The Too Much Milk Problem

Nht_e0
  • 140
  • 4
  • 15
  • I was under the impression that this is a well known problem like the dining philosophers' problem. I included a link that explains it – Nht_e0 Aug 02 '18 at 14:05
  • I think you are looking for condition variable. You need to unlock the mutex and you want other entering threads to wait for the thread that "went for milk". The paper you linked seems to explain everything. –  Aug 02 '18 at 14:14

1 Answers1

0

The solution in real life is to leave a note that you've gone out to buy milk.

Now, in programming, this doesn't quite solve things, only mitigates the risk of a race condition, since both Jack and Jill look at the fridge when it's empty, and both leave the note. But if you made the leave-note-if-no-milk-and-no-note part locked, then you're set. The time it takes to leave the note is very short compared to going out and buying milk.

einpoklum
  • 118,144
  • 57
  • 340
  • 684