Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preemptive threads Vs Non Preemptive threads

Can someone please explain the difference between preemptive Threading model and Non Preemptive threading model?

As per my understanding:

  • Non Preemptive threading model: Once a thread is started it cannot be stopped or the control cannot be transferred to other threads until the thread has completed its task.
  • Preemptive Threading Model: The runtime is allowed to step in and hand control from one thread to another at any time. Higher priority threads are given precedence over Lower priority threads.

Can someone please:

  1. Explain if the understanding is correct.
  2. Explain the advantages and disadvantages of both models.
  3. An example of when to use what will be really helpful.
  4. If i create a thread in Linux (system v or Pthread) without mentioning any options(are there any??) by default the threading model used is preemptive threading model?
like image 744
Alok Save Avatar asked Nov 10 '10 17:11

Alok Save


People also ask

What is the difference between preemptive and non-preemptive?

Key Differences Between Preemptive and Non-Preemptive Scheduling: In preemptive scheduling, the CPU is allocated to the processes for a limited time whereas, in Non-preemptive scheduling, the CPU is allocated to the process till it terminates or switches to the waiting state.

What is preemptive thread scheduling?

The Java runtime system's thread scheduling algorithm is also preemptive. If at any time a thread with a higher priority than all other Runnable threads becomes Runnable , the runtime system chooses the new higher-priority thread for execution. The new thread is said to preempt the other threads.

What are preemptive and non-preemptive scheduling policies?

There are two types of scheduling: preemptive scheduling and non-preemptive scheduling. Preemptive scheduling allows a running process to be interrupted by a high priority process, whereas in non-preemptive scheduling, any new process has to wait until the running process finishes its CPU cycle.

What does preemptive mean in OS?

Preemption as used with respect to operating systems means the ability of the operating system to preempt (that is, stop or pause) a currently scheduled task in favour of a higher priority task. The resource being scheduled may be the processor or I/O, among others.


3 Answers

  1. No, your understanding isn't entirely correct. Non-preemptive (aka cooperative) threads typically manually yield control to let other threads run before they finish (though it is up to that thread to call yield() (or whatever) to make that happen.
  2. Preempting threading is simpler. Cooperative threads have less overhead.
  3. Normally use preemptive. If you find your design has a lot of thread-switching overhead, cooperative threads would be a possible optimization. In many (most?) situations, this will be a fairly large investment with minimal payoff though.
  4. Yes, by default you'd get preemptive threading, though if you look around for the CThreads package, it supports cooperative threading. Few enough people (now) want cooperative threads that I'm not sure it's been updated within the last decade though...
like image 194
Jerry Coffin Avatar answered Oct 08 '22 23:10

Jerry Coffin


Non-preemptive threads are also called cooperative threads. An example of these is POE (Perl). Another example is classic Mac OS (before OS X). Cooperative threads have exclusive use of the CPU until they give it up. The scheduler then picks another thread to run.

Preemptive threads can voluntarily give up the CPU just like cooperative ones, but when they don't, it will be taken from them, and the scheduler will start another thread. POSIX & SysV threads fall in this category.

Big advantages of cooperative threads are greater efficiency (on single-core machines, at least) and easier handling of concurrency: it only exists when you yield control, so locking isn't required.

Big advantages of preemptive threads are better fault tolerance: a single thread failing to yield doesn't stop all other threads from executing. Also normally works better on multi-core machines, since multiple threads execute at once. Finally, you don't have to worry about making sure you're constantly yielding. That can be really annoying inside, e.g., a heavy number crunching loop.

You can mix them, of course. A single preemptive thread can have many cooperative threads running inside it.

like image 36
derobert Avatar answered Oct 08 '22 23:10

derobert


If you use non-preemptive it does not mean that process doesn't perform context switching when the process is waiting for I/O. The dispatcher will choose another process according to the scheduling model. We have to trust the process.

non-preemptive:

  1. less context switching, less overhead that can be sensible in non-preemptive model

  2. Easier to handle since it can be handled using a single-core processor

preemptive:

Advantage:

  1. In this model, we have a priority that helps us to have more control over the running process

  2. Better concurrency is a bonus

  3. Handling system calls without blocking the entire system

Disadvantage:

  1. Requires more complex algorithms for synchronization and critical section handling is inevitable.

  2. The overhead that comes with it

like image 9
pooria Avatar answered Oct 08 '22 23:10

pooria