[NSWI004] [Assignment 4] Problems with sem/prodcons

Vojtech Horky horky at d3s.mff.cuni.cz
Wed Dec 11 11:41:34 CET 2019


Hello.

Dne 10. 12. 19 v 19:46 Péťa Emmerová napsal(a):
> Hello, the deadline is approaching, and we are still struggling with 
> test sem/prodcons.
> After a while, the yielding cycle reaches a state where it only yields 
> one thread, altought the test states that there should be plenty of 
> active threads left.
> We would be glad to have a hint how to solve this.

First, looking at your repository, the following fragment is wrong:

interrupts_disable();
do_something();
interrupts_restore(true);

You ignore the return value from interrupts_disable which tells you the 
current state of interrupts. You shall then pass this value back to 
interrupts_restore to restore the previous level. Instead, you 
forcefully enable interrupts even if this code would have been wrapped 
by another function that wanted them disabled.

The proper use for correct nesting is thus

bool ipl = interrupts_disable();
do_something();
interrupts_restore(ipl);



Another issue I see is the following fragment that contains a race 
condition.

1 interrupts_disable();
2 if (mutex->locked) {
3   ...
4   interrupts_restore();
5   thread_suspend();
6 }

@all-teams: I have seen this in several other solutions, please, check 
your code too.

Questions that might help you spot the problem:

  - Is state of interrupts property of the operating system, any thread, 
active thread or a CPU?
  - What happens when interrupt is triggered between lines 2 and 3? What 
about lines 4 and 5?

Hope this helps,
- VH


More information about the NSWI004 mailing list