in Operating System edited by
4,424 views
21 votes
21 votes

A certain processor provides a 'test and set' instruction that is used as follows:

  TSET register, flag

This instruction atomically copies flag to register and sets flag to $1$. Give pseudo-code for implementing the entry and exit code to a critical region using this instruction.

in Operating System edited by
4.4k views

4 Comments

option B shows the disadvantages of using sleep() and wait() calls as wakeups can't be saved for the future use. And thus we go for the semaphores.
0
0
why did you removed part b
0
0

The question is incomplete. Please do complete it...

https://www.geeksforgeeks.org/gate-gate-cs-1999-question-68/

1
1
0
0

6 Answers

18 votes
18 votes
Best answer

 

  1. TSET $R1$, flag
  2. CMP $R1, \#0$
  3. JNZ Step$1$
  4. $[CS]$
  5. Store $M[Flag], \#0$
edited by

4 Comments

https://gateoverflow.in/205817/gate1999-20-b

This link made sense, Ma'am! Thank you! :)

0
0

iarnav The testing part is "if count=0"(or "if count=1") not "if count then sleep".  

0
0
Best thing about this TSL is it is an atomic instruction, no one i repeat no one can stop you once you execute this instruction

It would always give you a feel that you are executing an atomic instruction.
0
0
13 votes
13 votes

Q-2)

for consumer part

if (count==0)

consumer understands that buffer is full,But before consumer going  to sleep It got preempted and went to the Ready Queue

when producer produce an item and make Count=1,it will think consumer is blocked and try to wake up the consumer.

But actually Consumer is not Blocked it is in ready queue

After Some time Consumer come and Go to sleep[as before preemption it had seen that buffer is empty]

producer think that he has woken up consumer  and consumer is busy in consuming its produced item and consumer is waitinng for producer wakeup call

After sometime when buffer is full,producer went to sleep  thinking that when buffer is empty consumer will wake him up .And consumer is still waiting for producer wakeup call

So now Both are sleeping nd deadlock happens.

After some

edited by

4 Comments

Actually in B) code is working like this

-------------------------------------------------------------------------------

At first producer and consumer both are active state

Now, initially count =0

So, Say consumer active this part of code

if count = 0

And stop now

And producer starts and executes this part

place item in buffer.
    count = 1;
    Wakeup(Consumer);

Now producer goes to sleep

Consumer active now execute next part of it's code

 then sleep;
    Remove item from buffer;
    count = 0;
    Wakeup(Producer);
    Consume item;

So, at first it get instruction for sleep and it also goes to sleep

Both are in sleep mode now :)

2
2
how does producer went into sleep? can you explain it a bit?
0
0

@Anshul Shankar because when Producer runs again and it has made count ==1 in previous execution. 

So, Producer runs line P1 okay

then in line P2 if (count=1) then sleep; and yes count = 1 

Hence if (1==1) is True and producer goes to sleep!

0
0
3 votes
3 votes
part b) Initially when buffer is empty consumer reads count value =0  at that instant scheduler stops consumer temporarily and start producer process..Producer after inserting item finds count=1 and goes to sleep...Wakeup call of producer is lost and consumer finds count value =0 and go to sleep...so both are sleeping at same time

2 Comments

In part b in the question, it is said that, "
Also assume that the testing of count and assignment to count are atomic operations". Can you please explain, what does this statement means?    Thank you.......
0
0
0
0
2 votes
2 votes

Q1. ENTRY SECTION (Assuming flag is set to 0 initially before any process entered critical section)

while(register!=0)
    Test(register, flag);

EXIT SECTION

f=0;

Related questions