fundamentals of preemptive multitasking

BobTPH

Joined Jun 5, 2013
9,003
Good definitions. I would disagree with the definition of preemptive, though. A time slicing system will preempt a task at the end of its slice to run another task of equal priority. In fact, priorities are not always needed.

Bob
 

BobTPH

Joined Jun 5, 2013
9,003
If this specification make sense to you then how would you schedule the all tasks based on preemptive multitasking?

Task 1
  • Task 1 execute once for 500 ms
  • Task 1 has lowest priority
  • Deadline 1000 ms

Task 2
  • Task 2 execute once for 50 ms
  • Task 2 has Middle priority
  • Deadline 100 ms

Task 3
  • Task 3 execute once for 5 ms
  • Task 3 has highest priority
  • Deadline 10 ms

When the Task 3 is ready, it will preempt anything running. And the Task 2 will preempt the Task 1 if it is ready.

If anyone can fill in the table that is great it will give me complete idea.

Code:
0
5ms ------> Task 3 is running because it has highest priority
10ms -----> Task 2 is running because it is next high priority task
15ms -----> Task 3 running, Task 2 is interrupted by Task 2 before it has finished
Please let go of that example. It is only confusing you and is not the way things are actually done.

Bob
 

ApacheKid

Joined Jan 12, 2015
1,619
If this specification make sense to you then how would you schedule the all tasks based on preemptive multitasking?

Task 1
  • Task 1 execute once for 500 ms
  • Task 1 has lowest priority
  • Deadline 1000 ms

Task 2
  • Task 2 execute once for 50 ms
  • Task 2 has Middle priority
  • Deadline 100 ms

Task 3
  • Task 3 execute once for 5 ms
  • Task 3 has highest priority
  • Deadline 10 ms

When the Task 3 is ready, it will preempt anything running. And the Task 2 will preempt the Task 1 if it is ready.

If anyone can fill in the table that is great it will give me complete idea.

Code:
0
5ms ------> Task 3 is running because it has highest priority
10ms -----> Task 2 is running because it is next high priority task
15ms -----> Task 3 running, Task 2 is interrupted by Task 2 before it has finished
The Windows OS scheduler is a very well designed system (designed initially by Dave Cutler) I want to suggest you get a copy of this book as it is very detailed and will explain a lot of intricacies about this subject, it will explain all you need to know about the principles involved in scheduling and multitasking and it covers multiple processors too.
 
Last edited:

ApacheKid

Joined Jan 12, 2015
1,619
Happy New Year to all

ok maybe i got it. Preemptive multitasking allocates CPU time for each task and interrupts the task to schedule CPU time for another task waiting for execution.

There are many algorithms based on preemptive scheduling such as given in this page https://www.google.com/amp/s/www.geeksforgeeks.org/preemptive-and-non-preemptive-scheduling/amp/
The specific way that preemptive scheduling is implemented is quite fascinating. It requires that code be able to execute at some higher privilege ("kernel mode" say) and have a stack that is separate from "user mode" stacks.

Modern CPUs as used in our PCs etc have these capabilities as does the ARM CPU.

Anyway, code running in user mode (some task/thread) just runs, then a scheduler interrupt takes place and the CPU switches to kernel mode and the stack becomes the kernel stack. The scheduling code (running in kernel mode) modifies the (kernel) stack so that the return address (that was just placed there during the interrupt) is now an address in another user task/thread and saves the address that was there in some context area associated with the task/thread that was just interrupted.

Then it simply executes an RTI and hey presto it resorts back to user mode and is running code in the just selected task/thread.

Modifying the kernel stack like this as part of the scheduling is very elegant.
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
Understood we need to select the algorithm that the project requires.

Round-robin algorithm is a pre-emptive algorithm. It is used in many projects

I am planning to write C code for round-robin type pre-emptive scheduler. I will create new thread for this. For now it will take some time as I haven't done much research on it yet.
 

djsfantasi

Joined Apr 11, 2010
9,163
Understood we need to select the algorithm that the project requires.

Round-robin algorithm is a pre-emptive algorithm. It is used in many projects

I am planning to write C code for round-robin type pre-emptive scheduler. I will create new thread for this. For now it will take some time as I haven't done much research on it yet.
I’m re-learning along with this thread, since it’s been almost 50 years since I’ve covered the topic in class.

My co-operative multitasking program uses a variety of methods. Tasks may consist of one or more discrete steps or instructions. Most take significantly longer to complete than others and leave a lot of idle time. Tasks are executed in the order they appear in the task list, but are placed in the list in a non-deterministic manner. Specifically, a new task is placed in the first slot left empty by a completed task. Plus, most steps have an idle time specified, which must elapse before the next instruction in the task is executed.

The program is a run-time for an animatronic. Thus, many motions can easily occur simultaneously.
 
Top