1,018 views

1 Answer

Best answer
3 votes
3 votes

Here x= 0 means there is a process running in CS.

And x =1 means CS, no process is there in CS. 

And I think there should be a semi- colon at the end of the while.

// while (compare_and_swap(<x>,1,0));

Since here compare and swap is an atomic operation, hence only one process can get into CS at a time.

So, Option 1- False. 


And if it was not atomic then it may not satisfy mutual exclusion.

Consider a situation, when there is no process in CS ( x=1) and  p1 comes and execute if( <mem-LOC> == <Expected- value >). Here if condition is satisfied, since x=1. now p1 gets pre-empted. 

Now if another process p2 comes and goes into CS because still <mem-LOC> = 1. Now P1 resumes after  if( <mem-LOC> == <Expected- value >). and p1 also goes into CS. Now p1 and p2 both are in CS at the same time. Hence it does not satisfy mutual exclusion. 


Now coming to deadlock.

Suppose a situation where no process is there in CS and a new process wants to get into CS and goes into "Compare-and-Swap" block and after executing <mem-LOC> = <new-value> = 0, it gets terminated. Now if any other process wants to get into CS then it can not get the CS, because it will see <x>= 0, that is some other process is already there in CS(but it is not so) and will not be able to get CS. So there is no progress but the system is not in Deadlock because here there is no circular- wait. And if terminated process again resumes its execution then it will get into CS by returning 0 from ''Compare-and-Swap'' block and make <x> =1 while leaving CS and will allow another process to get CS.

Option 2- True. 

Thanks @Kapilp, @Amit Pal.. for correcting me. :)

edited by

Related questions

0 votes
0 votes
2 answers
3
shivajikobardan asked Jul 22, 2023
727 views
Sorry if this is a stupid question. But it really intrigued me. Same resources at different algorithms are telling different ways to test these stuffs.Here's an algorith...
0 votes
0 votes
1 answer
4