Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Preemptive multitasking

Status
Not open for further replies.

Kittu20

Member level 2
Member level 2
Joined
Oct 18, 2022
Messages
52
Helped
1
Reputation
2
Reaction score
0
Trophy points
6
Activity points
427
I'm trying to understand the basics fundamental of preemptive multitasking.
I found multitasking is a method that allows multiple Tasks to share (CPU time).


Let's say we have a Tasks like this

Task 1 - Middle Priority
Task 2 - Lower Priority
Task 3 - High Priority

A single core CPU can execute only one task at a time. Hence CPU will always execute Task 3 as it is higher priority task.

I do not understand that if Task 3 will always be executed then when will the rest of the Task get CPU time.
 

if Task 3 will always be executed then when will the rest of the Task get CPU time.
It's an indication that the current time slicing allocation for the higher priority task is not appropriate to your application, you should increase it accordingly in the scheduler.
 

Some basic RTOS background :





Regards, Dana.
 

The high-priority task doesn’t get ALL the time available, just MORE of the time.
your answer is too short to understand the concept.
Some basic RTOS background :





Regards, Dana.
Can you help me with the example I have provided? I just want to understand that when the higher priority task is executing, when it will give the control of CPU to the rest of the tasks.
 

If you are running an RTOS that is a setting in the RTOS for each task.


Regards, Dana.
 

Hi,

A real time task should have a beginning and an end. It not should be an endless loop.

Also there are just time consuming functions, like: delay_ms(5).
An rtos compiler could recognize this and relace it with: let's give 5ms of processing power to other tasks.

Usually real time taks are fast. Often just take a couple of microseconds.
So it's very likely that tasks 1 and 2 are processed durung such delay times of task3.

*****
Also you need to learn the concept of real time tasks. For example a key is connected to a port pin. And you need to wait for the key to get pressed.
You don't do a function with an infinite loop polling the port state. (100% processing power)
* Either you do it interrupt driven (0 processing power, just a couple of microseconds for every edge)
* Or you consider a how often per second you need the port to be checked. For a key press a check every 50ms is sufficient.
Checking the port state (including software overhead) maybe takes 2 microseconds.
Thus it consumes just 2us/50,000 us = 0.004% of processing power (leaving almost 100% for other tasks)

Klaus
 

Hi,

A real time task should have a beginning and an end. It not should be an endless loop.

Klaus
correct me if i'm wrong

Suppose we have two Tasks, Task 1 is a high priority task and Task 2 is a low priority task. Task 1 should be performed every 100 milliseconds. It's real time task.

I think in preemptive multitasking task 1 will have control of CPU as it is higher priority task. It will finish its work and hand over control of the CPU to Task 2 for 100 milliseconds. Task two will take control of the CPU for 100 milliseconds and then it will give control of the CPU to Task 1
 

If your two tasks take up 100% of your time, you’ve got a problem. Preemptive multitasking means that a higher priority task can PREEMPT, i.e. interrupt, a lower priority one. For instance, a high-priority video routine may interrupt a routine that’s doing some data processing.

I think you are confusing Cooperative multitasking, where every task is allotted a slice of time.
 
Hi,

Task1 should be performed every 100ms. But how long does it take to perform the task?
If it takes 20ms, then you have 80ms left for other tasks.

If you want to give 100ms to other tasks, then task1 must not consume any time (processing power)

We don't know the timings. It should be no big deal to take a sheet of paper and a pencil and draw a timing diagram on your own.
This is how I do it. Maybe use some colors.

Klaus
 
Hi,

Task1 should be performed every 100ms. But how long does it take to perform the task?
If it takes 20ms, then you have 80ms left for other tasks.

Klaus



I am reading the information given in the link many times but i'm having a hard time understanding the concept.

I am looking for examples to clarify the definition.

I am explaining example for state. Let me correct if my example are not good to understand concept and if I am wrong in my explanation.

Suppose we have a Tasks like Turn ON Light, Read data from Serial Port, Write data to serial Port.. etc

In ready state they are ready to run but are not currently running on the CPU.

In running state if reading data from serial port is high priority task then system will choose high priority task and it will execute on CPU

Blocking state we have read the data from the serial port. The next data will not come for the next 2 ms , that's why we block this task for 2 ms.
 

Where is your confusion ?

If you take the communication example, what you would do for its priority
would depend on several things, like protocol, does the protocol include a
confirmation all data is read and acknowledged......safety, or size of dataset......

Ready state, has the task done all its housekeeping so it can execute. and
implement its design goal without impediment.....


Regards, Dana.
 

Where is your confusion ?

Regards, Dana.
I am looking examples to clarify the definition.

I was trying to relate each state with an generic example so that it would easy to understand. I could not find such examples
 

ok, so use your example.Assume read from serial port has highest priority, write to serial port is next highest and turning on the light is lowest priority. Now, say you’re in the middle of the light-on task and there‘s data available in the serial port. The light ON task will be suspended (PREEMPTED) and the read data task will take over. When the read data task is done, the light on task will continue.
 

Hi,

Let me correct if my example are not good to understand concept and if I am wrong in my explanation.
The problem with your examples is that you just give half of the information.
You need to consider
* how often the task is called
* how long each task takes

Reading data from a 115200 baud UART, full speed
* will be called about every 100us
* but may take less than 1 us
--> thus in total just 1% of processing power

Writing data to serial port:
--> about the same. 1% of processing power

Turn ON/OFF light (port)
* if updated every 20ms
* takes less time than the UART task
--> 0.02 % of processing power

Again: What about pencil and paper? Visualizing will show us your thinking and will help you to understand the problem.

Klaus
 
Hi,

Again: What about pencil and paper? Visualizing will show us your thinking and will help you to understand the problem.

Klaus

I'd like to share ideas for addressing definition before we get to work with paper and pencil. let me know if it makes any sense to you


I am talking about a check gate on highway where the vehicle is stopped to collect the toll charge. We see a red light indicator and the boom barrier is closed, it means you have to stop your vehicle. there is a tag which is fitted on the windscreen of your vehicle.

RFID reader scans the tag on the window screen. and automatically deduct the toll charge from your linked bank account. After which the boom barrier gate opens and screen display message "paid' so you can go ahead from there. If the RFID reader is not working, a message appears on the screen that the reader is not working, so you can deposit the tax manually to the operator and take the receipt there.

Technically I'm assuming we have a device like the Raspberry Pi that turns on the red light and turns off the boom barrier and display "Welcome "Message" on the screen when the system is powered ON. RFID reader scans the tag and sends data to Raspberry Pi over UART. Raspberry Pi send data to getaway server. Server validate the data and deducts the charge from the linked bank account. And send message "Paid " to raspberry PI and Pi send message " paid" to LCD display.
 

Hi,

I thought you need help in understanding processing power... which means "time for a task".
But no information about timing, schedule, your ideas.... in your last post.
You don't refer to anything I wrote.

I guess I have a different way to solve problems. Thus I'm not much of help anymore.

Klaus
 

I'd like to share ideas for addressing definition before we get to work with paper and pencil. let me know if it makes any sense to you


I am talking about a check gate on highway where the vehicle is stopped to collect the toll charge. We see a red light indicator and the boom barrier is closed, it means you have to stop your vehicle. there is a tag which is fitted on the windscreen of your vehicle.

RFID reader scans the tag on the window screen. and automatically deduct the toll charge from your linked bank account. After which the boom barrier gate opens and screen display message "paid' so you can go ahead from there. If the RFID reader is not working, a message appears on the screen that the reader is not working, so you can deposit the tax manually to the operator and take the receipt there.

Technically I'm assuming we have a device like the Raspberry Pi that turns on the red light and turns off the boom barrier and display "Welcome "Message" on the screen when the system is powered ON. RFID reader scans the tag and sends data to Raspberry Pi over UART. Raspberry Pi send data to getaway server. Server validate the data and deducts the charge from the linked bank account. And send message "Paid " to raspberry PI and Pi send message " paid" to LCD display.

So one can see in this high priority tasks and tasks that dont have real time implications.

Example The message handling that RFID reader out of working order, is low priority, if it takes 100 uS
or 1 sec to get message to vehicle, low priority. On the other hand sensor that detects vehicle movement
as gate is moving, or an emergency vehicle approaching that needs gate open so it passes thru without
delay is a real time task.

So in design one evaluates the time it takes to do tasks, their importance, and assigns priority's
and CPU cycles accordingly.

Just like your decision process if your loved one is in danger. The decision how to act if they
are cold versus bleeding, one problem (task) has very high priority, the other not so much.

So start by making a list of various tasks you need to perform, a guess (better yet from some
measurements) of the cpu cycles needed to do the task, and its priority. Then test and see if
gate ever contacts car, emergency vehicle is stopped ratehr than passed, and change the
RTOS or whatever underlying system manager priority and resource parameters to modify the
behaviour.

You are building skyscraper, what do you do first and what needs the most time to
optimize speed its built........put the roof on first then put up the pilings, or vice versa.


Regards, Dana.
 
Last edited:
Hi,

I thought you need help in understanding processing power... which means "time for a task".
But no information about timing, schedule, your ideas.... in your last post.
You don't refer to anything I wrote.
I am sorry, I felt that you are asking me to give a specific example, so I provided an example.

I understand now what you want me to do, I will make an example and provide it very soon.
--- Updated ---

Hi,

I thought you need help in understanding processing power... which means "time for a task".
But no information about timing, schedule, your ideas.... in your last post.
You don't refer to anything I wrote.

Klaus
I am providing this example

Task 1
It takes 1μs of CPU time.
It should perform every 10μs
high priority task
It takes 10% of the CPU time
90% of CPU time is available for other Task

Task 2
It takes 1ms of CPU time.
It should perform every 10ms
Low priority task
It takes 10% of the CPU time
90% of CPU time is available for other Task
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top