Reputation: 2884
I had exam and I faced this question in the exam:
For each of the statements below, indicate in one sentence whether or not the statement is true or false, and why. • In a concurrent programming environment that provides non-preemptively scheduled threads (i.e., no involuntary context switches), mutual exclusion synchronization is not required.
After exam I got the answer which is :
[False. Mutex synchronization is still required (1) on multiprocessors, and (2) on uniprocessors for critical sections that contain blocking operations.]
But I can not under stand it. Can anyone explain it clearly?
Upvotes: 0
Views: 91
Reputation: 598
In case of a multiprocessor system, you have for example 2 tasks running concurrently and share a variable s:
s = 1;
T1 reads s1 = 1;
T2 reads s2 = 1;
T1 increments s1: s1 = 2;
T2 increments s2: s2 = 2;
T1 writes s: s = s1 = 2;
T2 writes s: s = s2 = 2;
But s should be 3 by now, as two tasks incremented it by one. To prevent this, you have to ensure the variable is read, manipulated and written before another task is reading it.
On a uniprocessor system, the same can happen:
T1 reads s;
T1 blocks on an operation;
T2 runs and reads s;
T2 blocks on an operation;
T1 manipulates and writes;
T2 manipulates and writes;
s was manipulated twice, but the result only shows the manipulation of T2.
If you just change a single variable you can make use of atomic operations. But if your critical section is larger you have to protect it by using a mutex.
Upvotes: 2