Q163: Concurrent Threads Contd.

This question is in continuation of Question 44.

Answer the question below based on the following code:


Global Variable:

int count = 0;

Thread1:

for(int i = 1; i < 50000; i++)

count++;

Thread2:

for(int i = 1; i < 20000; i++)

count++;

 

Question #163: What can be said about the value of count?

Option:

  1. Value of count will always be less than or equal to 70000.
  2. Value of count will always be equal to 70000.
  3. Value of count will always be greater than or equal to 70000.
  4. Nothing can be said about value of count.

Solution: The correct answer is the first one. When there is no interleaving, value might be equal to 70000. When there is interleaving, value can only be less than 70000, and not greater.

Q58: Another attempt to solve critical section problem?

Question #58:

Consider the following solution for critical section problem for two processes, where “process” is the current process ID and “other” is the process ID of the second process:

while (turn != process);

//Critical section

turn = other;

//Non-critical region

Which of the following conditions are not held for the above solution of critical section problem:

  1. No two processes may be simultaneously inside their critical regions.
  2. No assumptions made about speeds or the number of CPUs.
  3. No process running outside its critical region may block other processes.
  4. No process should have to wait forever to enter its critical region.

 

Solution:

Let’s say first process (PID 0) has executed the critical region and has set the turn to 1. Now, till the time other process executes its critical region, first process can’t execute its critical region again! Therefore, a process is blocking the other process even while running outside critical region. Therefore, the correct answer is option 3.

Q52: Producer Consumer Problem

Question #52: Following is a code for Producer Consumer problem.

producer_consumer

Which of the following statements is true?

  1. There is no race condition in the above code.
  2. It is possible that producer sleeps, but consumer never wakes it up.
  3. There is a race condition because variable count is being read/written in both the consumer & producer without mutual exclusion.
  4. Both B & C.

Solution: Clearly, count is a shared variable and is being in both producer and consumer, hence there is a race condition because of violation of mutual exclusion. Now, take the situation when producer sees that buffer is full and before going to sleep, a context switch occurs. Then, consumer consumes an item and tries to wake up producer, but producer is not yet sleeping. Then, again context switch occurs and now producer goes to sleep! Now, there is no one to wake up the producer. This is called lost wakeup problem. Hence, the correct answer is option 4.

Q44: Concurrent Threads

Answer the question below based on the following code:


Global Variable:

int count = 0;

Thread1:

for(int i = 1; i < 50000; i++)

count++;

Thread2:

for(int i = 1; i < 20000; i++)

count++;

 

Question #44: Given that the two threads run concurrently, what will be the value of count at the end of execution of the two threads?

Options:

  • Value of count will be 70000.
  • Value of count can’t be predicted.
  • There is a race condition in the above program.
  • Both B and C.

Solution: count++ is not an atomic statement and is broken down into many instructions while executing. Following is an illustration of how things can go wrong:

Thread 1 Thread 2   Integer value
0
read value 0
read value 0
increase value 0
increase value 0
write back 1
write back 1

Hence, 4th option is the correct solution.

Q40: Problem with software lock based solution to critical section problem

Question 40: Consider the following solution for critical section problem:

while (lock == 1);
lock = 1;
//Critical section
lock = 0;

Which of the following conditions are not held for the above solution of critical section problem:

A) No two processes may be simultaneously inside their critical regions.

B) No assumptions should be made about speeds or the number of CPUs.

C) No process running outside its critical region may block other processes.

D) No process should have to wait forever to enter its critical region.

 

Solution:

There is a race condition in the above solution. It is possible that the first process checks the while condition and finds that lock is 0. Then, context switch happens and the other process also finds that lock is 0, now both of the processes are in the critical region. Hence, the correct answer is A.