2

That feels a bit counter-intuitive to me. Wasn't preemptive scheduling done so that all processes can get a share of the cpu ? So can there be starvation under this scheduling scheme ? if so, how ?

Also, can someone show a simple pseudocode about how i can code this up, to show the starvation property at work.

Somjit
  • 2,503
  • 5
  • 33
  • 60

3 Answers3

3

Can pre-emptive scheduling cause starvation?

Yes, pre-emptive scheduling may cause starvation.

Let's see Longest remaining time first scheme. It is pre-emptive scheduling. At any point of time, if any process having larger execution time or larger remaining time arrives, processor will switched to that process pausing current ongoing process. Process starvation happen for processes having smaller execution time.

Sometime, pre-emptive scheme does not cause starvation.

Let's take an example of Round Robin Scheduling Algorithm. Round Robin algorithm is a pre-emptive algorithm as the scheduler forces the process out of the CPU once the time quota expires.

Unlike SJF or SRTF, processer will not stuck at any process. Like a process that never ends will not end, but other processes will still run. So it solves starvation problem.

more detail about Round Robin Algorithm: http://en.wikipedia.org/wiki/Round-robin_scheduling

Implementation of RR algo: Implementing round robin scheduling algorithm in Java

If you have any further query do comment.

Thanks,

Bhavik

Community
  • 1
  • 1
Bhavik Patel
  • 193
  • 1
  • 8
2

Pre-emptive scheduling just means that whatever is running now can be stopped for something else to run. Emphasis on can be stopped. It may never be stopped, as well as it may never end up running at all. As you asked, I will try to describe a pseudocode for a preemptive scheduling algorithm that can cause starvation:

P = running processes list q = quantum while P is not empty: t_0 <- infinity for each p in P: t <- p.totalTime if t < t_0: t_0 <- t p <- process with running time t_0 run p for quantum q

Suppose now that processes can be added to the list P at any time. If you keep adding small processes, they would always be executed before any other big processes, resulting on starvation.

This algorithm is a pre-emptive form of SJF (Shortest Job First). You can extrapolate and make a pre-emptive algorithm that simply never runs a given process; instead, if this is the only process left, the algorithm just waits in a loop until something new appears. It's not a good algorithm, but it is pre-emptive and causes starvation.

Actually, if you stop to think, the only way you can get starvation is on pre-emptive schemes. You can't get starvation in non-preemptive schemes, as they will all run until the end, and you can't add any jobs. Preemptive schemes comes actually not to give all processes a share of CPU, but to make your system more responsive. So, a smart preemptive scheme is one that increases responsiveness by not giving I/O bounded processes too much attention, stopping them while they are waiting for some input, for example.

  • you mean non-pre-emptive scheme does not have starvation? – Bhavik Patel Mar 20 '15 at 04:18
  • Well, if you extrapolate the concept you can have a non-preemptive scheme that, again, never runs one job, and that can be considered starvation. That depends on your definition of non-preemptive algorithm. If, to be considered one, it has to run all jobs until the end, then starvation can not occur. All the classic algorithms that I know, like FIFO or SJF, can't have starvation, since they will run all jobs until the end. – Guilherme Caminha Mar 20 '15 at 15:18
  • you are going wrong buddy, as far as I think, SJF has starvation. as Long process need to wait for long time. – Bhavik Patel Mar 20 '15 at 16:43
  • 1
    If a job ever runs, even if it takes a very looong time, then it has not starved. Check: http://en.wikipedia.org/wiki/Resource_starvation "In computer science, starvation is a problem encountered in multitasking where a process is **perpetually** denied necessary resources." – Guilherme Caminha Mar 20 '15 at 22:27
0

Preemptive scheduling means process switch from running state to ready state or waiting state to ready state.

for more details check out: https://www.geeksforgeeks.org/preemptive-and-non-preemptive-scheduling

Let's take an example of priority scheduling(preemptive).In this scheduling when higher priority process arrives then the running process then higher priority process get access of cpu and lower priority process switch from running state to waiting state.In this case it is possible that lower priority process not switch from waiting state to running state because whenever higher priority process arrives it take access of cpu.So it is possible that lower priority process will not execute at any point of time and this is called starvation.

In shortest remaining time next/first,process with lower burst time(cpu access time) executed first with respect to process with higher burst time.Whenever process with lower burst time arrives and if currently running process have higher burst time then currently running process switch from running state to waiting state and process with lower burst time switch to running state.In this case it is possible that process with higher burst time will not execute or not switch from waiting state state to running state.That's called starvation.

starvation means process have to wait for indefinitely time because of another process.And we see that from above cases some process have to wait because of another process,So,that's why preemptive scheduling can cause starvation.

Apurv
  • 71
  • 1
  • 2