fundamentals of preemptive multitasking

MrChips

Joined Oct 2, 2009
30,824
FFT time depends on speed of the processor, efficiency of the algorithm, fixed point or floating point arithmetic, size of the CPU register and data, and length of the array.

We can pick any number, for example 20ms to process a 1024-word array.
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
FFT time depends on speed of the processor, efficiency of the algorithm, fixed point or floating point arithmetic, size of the CPU register and data, and length of the array.

We can pick any number, for example 20ms to process a 1024-word array.
Does my description match with our problem ?

  • Task 1 runs once every 10us. Highest priority
  • Task 2 runs once every 20ms. Middle priority
  • Task 3 runs once every 17ms for 1ms. lowest priority
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
No.

Task 2 runs as often as possible. It takes 20ms. It has the lowest priority.
Thanks for the correction.

I saw an example on google somewhere for preemptive multitasking in which they took LCM for all tasks.

Do we have to get LCM ?

I still don't understand how all the tasks will be scheduled.

Task 1 is a high priority Task so it should run 10us after this the next higher priority is Task 3 which should be run but i don't understand how long it should run ?will it run only for 10us because Task 1 is ready to run
 

michael8

Joined Jan 11, 2015
415
It all sounds confusing. How about each task has a priority and the dispatcher has a ready to run list of tasks (possibly empty).
Each task has it's own stack, register save area (on the stack?) and program counter (also on the stack?).

At each interrupt the current task's status is saved and the interrupt is processed, possibly changing which is the highest priorty task. Then all the dispatcher does is run the highest ready to run task.

Interrupt processing can add a task to the ready list as needed, it will run when it becomes the highest priority.

No timers..
 

MrChips

Joined Oct 2, 2009
30,824
Servicing the ADC every 10μs is a priority task. However this is interrupt driven and not considered as multitasking.

Hardware tasks such as servicing DAC, ADC, timers, UART, SPI, SCI, I2C, USB, etc. are generally all interrupt driven events.
As long as the interrupt handlers can service the interrupt and return control as quickly as possible, these do not need to be scheduled by an RTOS scheduler.

When multiple tasks require considerable CPU processing then one needs to consider scheduling or round-robin processing.
For example, one task needs to read the contents of a disk file, another task tries to write to another file, data is being processed, the screen needs updating, etc.

The bottom line is, on a single core CPU, you have only so much processing time available. If the total time required by all tasks exceeds the amount of available CPU time then there will be a backup in the queue and delays are inevitable. That's life on a computer.
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
Servicing the ADC every 10μs is a priority task. However this is interrupt driven and not considered as multitasking.

Hardware tasks such as servicing DAC, ADC, timers, UART, SPI, SCI, I2C, USB, etc. are generally all interrupt driven events.
As long as the interrupt handlers can service the interrupt and return control as quickly as possible, these do not need to be scheduled by an RTOS scheduler.

When multiple tasks require considerable CPU processing then one needs to consider scheduling or round-robin processing.
For example, one task needs to read the contents of a disk file, another task tries to write to another file, data is being processed, the screen needs updating, etc.

The bottom line is, on a single core CPU, you have only so much processing time available. If the total time required by all tasks exceeds the amount of available CPU time then there will be a backup in the queue and delays are inevitable. That's life on a computer.
I wanted to see how you will schedule all three tasks. I was trying this from post #1 on how to schedule tasks in the preemptive multitasking. I don't understand even after trying a lot
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
@MrChips I'm confused and don't know what to do ? For all the tasks we have considered time, now i do not understand how to move forward.
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
I wanted to see how you will schedule all three tasks. I was trying this from post #1 on how to schedule tasks in the preemptive multitasking. I don't understand even after trying a lot
FreeRTOS has lots of tutorial information.
https://www.freertos.org/implementation/a00005.html

Better yet, why don’t you download it and try to write a few tasks? Pick a supported target system that has a free IDE and compiler, PIC32, ARM etc and a simulator and write something. Use the simulator to see how the tasks work and get scheduled. It is very difficult to get in depth knowledge of a topic this broad without actually working with it hands-on.

Just my opinion.
 

MrChips

Joined Oct 2, 2009
30,824
Understanding Mulitasking

Let us assume that Task-A takes 3 seconds to complete and Task-B takes 2 seconds.
Multitasking or no multitasking, it still takes 5 seconds for both tasks to be completed.

Multitasking gives the illusion of multiple tasks being performed simultaneously.
For example, if Task-A is outputting a 3-second sound bite while Task-B is printing a page, both tasks might be completed in 3 seconds only if there is available processing time for the processor to be time-shared.

If Task-A requires 3 seconds of 100% CPU time and Task-B needs 2 seconds, then round-robin scheduling will still take 5 seconds for both to be completed.

If Task-A takes 3 seconds but only uses 33% of CPU time, this leaves 2 seconds for Task-B to be processed.
In this case cooperative multitasking means than when Task-A is not doing anything useful, Task-B can proceed.

Preemptive multitasking means that a task of higher priority can take access to the CPU away from a lower priority task.
The requirement here is that the high priority task should be processed very quickly, i.e. it consumes a very small percentage of CPU resources. You do not use a scheduler for this.

For example, assume Task-A is to play a 3-minute song. The output DAC interrupts the computer every 25μs but it only uses 1μs of CPU time, i.e. the load on the computer is 4%. (This is not how it is done but I am only using this as an example. See below*) 96% of the CPU time is still available for other tasks to use over the 3-minute period.

Now let us make it much more complex. The computer needs to read a 10MB compressed file from the disk, decompress the video data and the audio data, and output real-time audio and video without missing a beat. Assuming visual flicker rate is 25Hz, the screen must be updated every 20ms, for example. Thus you need to do the time analysis and determine how to do all tasks under 20ms. If it takes longer than 20ms, display and/or sound performance will suffer.

Multitasking is effective when high priority tasks can be processed very quickly, e.g. ADC, DAC, timer, keyboard events, and when time consuming tasks have or allow intermittent pauses, e.g. disk file access, screen updates.

*Many hardware services such as sound, video, serial interfaces, USB, internet access, file access, etc. take advantage of interrupts, DMA (direct memory access) and buffering for enhanced performance. For example, instead of the sound card interrupting the CPU every 25μs, it might request 1k of data once every 25ms. Data is transferred to a memory buffer using DMA. Data is also sent out to the DAC using DMA. Thus the total interaction with the CPU could be a handful of instructions once every 25ms.
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
Use the simulator to see how the tasks work and get scheduled. It is very difficult to get in depth knowledge of a topic this broad without actually working with it hands-on.

Just my opinion.
It is very difficult for me to write code without understanding basics so I try to understand concepts how it works. if i understand basic i can try to write my code. Only then can I use the simulator to check the result.
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
Understanding Mulitasking

Let us assume that Task-A takes 3 seconds to complete and Task-B takes 2 seconds.
Multitasking or no multitasking, it still takes 5 seconds for both tasks to be completed.

Multitasking gives the illusion of multiple tasks being performed simultaneously.
For example, if Task-A is outputting a 3-second sound bite while Task-B is printing a page, both tasks might be completed in 3 seconds only if there is available processing time for the processor to be time-shared.

If Task-A requires 3 seconds of 100% CPU time and Task-B needs 2 seconds, then round-robin scheduling will still take 5 seconds for both to be completed.

If Task-A takes 3 seconds but only uses 33% of CPU time, this leaves 2 seconds for Task-B to be processed.
In this case cooperative multitasking means than when Task-A is not doing anything useful, Task-B can proceed.

Preemptive multitasking means that a task of higher priority can take access to the CPU away from a lower priority task.
The requirement here is that the high priority task should be processed very quickly, i.e. it consumes a very small percentage of CPU resources. You do not use a scheduler for this.

For example, assume Task-A is to play a 3-minute song. The output DAC interrupts the computer every 25μs but it only uses 1μs of CPU time, i.e. the load on the computer is 4%. (This is not how it is done but I am only using this as an example. See below*) 96% of the CPU time is still available for other tasks to use over the 3-minute period.

Now let us make it much more complex. The computer needs to read a 10MB compressed file from the disk, decompress the video data and the audio data, and output real-time audio and video without missing a beat. Assuming visual flicker rate is 25Hz, the screen must be updated every 20ms, for example. Thus you need to do the time analysis and determine how to do all tasks under 20ms. If it takes longer than 20ms, display and/or sound performance will suffer.

Multitasking is effective when high priority tasks can be processed very quickly, e.g. ADC, DAC, timer, keyboard events, and when time consuming tasks have or allow intermittent pauses, e.g. disk file access, screen updates.

*Many hardware services such as sound, video, serial interfaces, USB, internet access, file access, etc. take advantage of interrupts, DMA (direct memory access) and buffering for enhanced performance. For example, instead of the sound card interrupting the CPU every 25μs, it might request 1k of data once every 25ms. Data is transferred to a memory buffer using DMA. Data is also sent out to the DAC using DMA. Thus the total interaction with the CPU could be a handful of instructions once every 25ms.
@MrChips

I understand that preemptive multitasking manages time and resources for multiple tasks based on task priority. Multiple tasks share resources such as CPU, memory peripherals.

We considered that the Frequency Spectrum Analyzer is an suitable example of preemptive multitasking. We know the priority and deadline time for each task. I think we have all the necessary things so that we can schedule the all three tasks. The first task must be completed every 10us. The CPU has only 10us to finish Task 2 and Task 3. And both the tasks cannot be finished in such a short time. From this I do not understand how to schedule the all task in their deadline.

I have been trying to figure out since yesterday how to schedule all the tasks so that they all get completed in their deadline but i haven't been able to do it yet can you offer any help or make it easier to understand how the task is scheduled in spectrum analyzer
 
Last edited:

MrChips

Joined Oct 2, 2009
30,824
No. You have this wrong.

I have given you a hypothetical example. The first task, servicing the ADC, occurs every 10μs. It does not take 10μs to complete.
Think duty-cycle. If the first task takes 10μs once every 10μs, that is a 100% duty-cycle. There is no time left for any other tasks.

If the first tasks takes 1μs every 10μs, that is a duty-cycle of 10%. The other tasks have 90% of the CPU time.

Here is another way of looking at it.
The ADC is sampled 100,000 times each second.
Assuming that the screen refresh rate is 10ms. That is 100 times each second.
In every 10ms, the ADC is sampled 1000 times requiring 1ms of CPU time.
Thus, every 10ms, all other tasks have 9ms of CPU time to complete their tasks, i.e. 90% of CPU time.

Multitasking is all about time management, i.e. how you make free time available for other tasks to use.
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
No. You have this wrong.

I have given you a hypothetical example.
Multitasking is all about time management, i.e. how you make free time available for other tasks to use.
as far as I understand, To design preemptive multitasking we need to know the following

  • Task execution time
  • Task deadline time
  • Task priority


Ok i try again, Here is our hypothetical example

Task 1 runs once for 1us every 10us. Highest priority

Task 2 runs once every 20ms. Lowest priority

Task 3 runs once for 1ms every 17ms . Middle priority.

What should be the execution time of Task 2 in the hypothetical situation ?
 

BobTPH

Joined Jun 5, 2013
9,003
as far as I understand, To design preemptive multitasking we need to know the following

  • Task execution time
  • Task deadline time
  • Task priority


Ok i try again, Here is our hypothetical example

Task 1 runs once for 1us every 10us. Highest priority

Task 2 runs once every 20ms. Lowest priority

Task 3 runs once for 1ms every 17ms . Middle priority.

What should be the execution time of Task 2 in the hypothetical situation ?
No, you do not have to know any of those things to design a multitasking system.

Think of your PC. Does it know anything about any of these things about the typical task running on it?

I think you are getting hung up on things people give as examples, which are nothing like the real world.

A multitasking system just needs to facilitate the running of multiple streams of program execution In such a way they all share the same processor and, generally, do not know anything about the other tasks that are also running.

A cooperative system can switch tasks only on certain system calls, like a wait for timer.

A preemptive system can switch tasks anywhere in their execution, between any two instructions.

Bob
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
No, you do not have to know any of those things to design a multitasking system.
i'm completely lost. I mentioned preemptive multitasking. As far as I understand, preemptive multitasking requires knowing task execution time, task priority and task deadline time. I guess you are referring to cooperative multitasking.
 

djsfantasi

Joined Apr 11, 2010
9,163
i'm completely lost. I mentioned preemptive multitasking. As far as I understand, preemptive multitasking requires knowing task execution time, task priority and task deadline time. I guess you are referring to cooperative multitasking.
I’ve kept quiet because I’m not as familiar with preemptive multitasking, but I do know that you don’t need to know all those things you mention.

A preemptive system can switch tasks anywhere in their execution, between any two instructions.
That’s the definition. It preempts a task to let another run. Nothing about length, priority or deadline.

Those parameters can be used in a custom scheduling routine, but that’s not specifically required for preemptive multitasking.
 

MrChips

Joined Oct 2, 2009
30,824
TS is now learning the meaning of the words:

  1. multitasking
  2. interrupts
  3. preemptive
  4. cooperative
  5. priority
  6. scheduling
  7. round-robin

multitasking

A single core CPU can only execute one instruction at a time. Hence it can act only on one task at a time. You can switch between tasks to make it appear as if multiple tasks are being executed.

round-robin
This is where each task is given a slice of CPU time during which time a portion of one task is allowed to proceed. There is no time saved by doing this.

scheduling
This is the process whereby an overlying software controller decides which task gets the next time-slice based on priority and frequency of requests.

interrupts
This is a hardware mechanism for pausing a task in progress and having to deal with another event. Interrupts are by definition preemptive. Interrupts are independent of multitasking. There is such a thing as a software interrupt.

preemptive
This means that a process can be interrupted in order for an event of higher priority to be processed.

priority
Priority determines the pecking order as to which tasks are more important. Another concept of priority is foreground/background processes. A task of low priority where timely completion of execution is not critical could be run in the background, for example, printing a page or scanning for virus on disk files. A task is given a higher priority if time is of the essence or the function of the task is critical to the integrity of the system, for example, delayed response or lost of data.

cooperative
This means that a task will allow another task to use free CPU time while it is waiting for a process to be completed. Software delay loops that are blocking are not cooperative. System delay functions can be designed to be cooperative processing.
As an example, a process tries to open and read a disk file. It sends a request to open the file. It has to wait for the file to be ready for reading. You can have a software loop waiting for the "file ready" signal. This is a blocking process. Instead you can ask the OS to respond when the file is ready. In the meantime the OS can allow another task to proceed while waiting for the disk file to be ready. This is a non-blocking cooperative request.
 

Thread Starter

Sparsh45

Joined Dec 6, 2021
143
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
 
Last edited:
Top