The following algorithm for the mutual exclusion problem does not satisfied the mutual exclusion property. is it satisfy the deadlock, starvation? And also is it operate correctly in the absence of contention?
int p=1;
int q=1;
process P process Q
while(true){ while(true){
a1 : nonCriticallSection1; a2 : nonCriticallSection1;
b1 : while (q !=1){ do nothing} b2 : while (p !=1){ do nothing}
c1 : p=0; c2 : q=0;
d1 : critical section d2 : critical section
e1 : p=1; e2 : q=1;
} }
end P; end Q;
Your algorithm should be ok for deadlock, starvation and contention.
However, this kind of solution is not scalable and will work only for 2 processes that is probably not what you are looking for.
You can have a look at wikipedia’e entry for deadlock to find some useful algorithm.
EDIT: i cannot starvate as you set a per process flag that says when a process wants to enter critical section. So if process P owns critical section but process Q wants to enter, it will do when P ends critical section because even if the cheduler will choose to re-execute P, P itself will check if q==0 and if so it will wait.
Of course your example it’s a study-case. It will not work in real application as it uses polling and possibly infinite cycles. I strongly suggest not to try to use it.