-2

I am looking online for peterson's algorithm, and I cant seem to distinguish whether or not it is preemptive.

Preemptive - Non voluntary scheduling, and OS decides when to stop the process

Non-preemptive - Voluntarily giving up CPU once process is complete

enter image description here

I feel that peterson's algorithm seems to be non preemptive since there is no mention about priority of processes.

Is that correct? Would somebody be able to shed some light on this?

RandomMath
  • 675
  • 15
  • 33
  • Where did you get these definitions? They look - incomplete - at best. – too honest for this site Nov 04 '15 at 22:24
  • I found this on an online example - Just looking to see if my answer for the above code is correct? – RandomMath Nov 04 '15 at 22:25
  • 1
    "somewhere" is really a very good reference fo an important definition. You should continue to use such references (disclaimer: comment may include sarcasm). And an algorithm is in general neither. That is a matter of implementation. Please do more research with reliable references. You seem to confuse some phrases and concepts. – too honest for this site Nov 04 '15 at 22:25
  • And: **Don't post images unless required for the question!** – too honest for this site Nov 04 '15 at 22:26
  • Off topic: Peterson's algorithm was developed in 1981 when processors were still quite primitive, i.e. simple. You should not attempt to use Peterson's algorithm on modern processors. See [lockless multithreading](https://msdn.microsoft.com/en-us/library/windows/desktop/ee418650(v=vs.85).aspx). – user3386109 Nov 04 '15 at 23:12
  • By any chance is this a coursework question you had last year? Because if so, same! Just a year out. – Jamie Nov 02 '16 at 15:33

2 Answers2

1

Peterson's Algorithm is neither preemptive or non-preemptive.

Preemption is a notion of the underlying Operating System, more precisely the scheduler. The task of the scheduler is to assign timeslots to processes in a timely (pun intended) manner.

The scheduler can interrupt a process regarding of wait-state.

However, it's "prudent" to signal the scheduler that now is a good time to let another process run by utilizing wait-states.

For Peterson's Algorithm this signal should be inside the while-loop. A simple sleep should suffice.

Wikipedia also mentions:

The while condition works even with preemption

So to conclude as we began:
Peterson's Algorithm is neither preemptive or non-preemptive.

Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
0

Peterson's solution works for a preemptive process. It might fail for a non-preemptive process.

Suppose both process wish to enter the critical section.

do {
   flag[0] = 1; 
   turn = 1;
   while(flag[1] && turn == 1);
   CS
   flag[0] = 0;
   RS
   } while(1);

do {
   flag[1] = 1; 
   turn = 0;
   while(flag[0] && turn == 0);
   CS
   flag[1] = 0;
   RS
   } while(1);

If turn = 1 process 0 will be in a spinlock. It is wasting CPU cycles. It is preempted so that the other process (i.e. process 1) gets chance.

Abhishek Dutta
  • 75
  • 4
  • 12