Suitable example for Preemptive multitasking

Go To Last Post
39 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

I have read a some pages that have great information about Preemptive multitasking but the problem is that there is no reference example given in links in explanation

 

https://en.m.wikipedia.org/wiki/...(computing)

 

https://en.m.wikipedia.org/wiki/...

 

https://www.wikiwand.com/en/Pree...(computing)

 

I did not get any simple and suitable example so I have created an example for learning purpose

 

idea is that there is one motor in industrial machine that goes on/off for every 20 milliseconds.

 

The motor can stop only when there is a fire detected in the room or when the temperature of the room is higher than the set temperature of machine or when the fault has been generated in machine.

 

Task list & priorities

1)Start motor , wait, stop motor - Priority 1
2) Monitor for fire- Priority 2
3) Monitor for fault- Priority 3
4) Monitor for room temperature range- Priority 4

 

Repeat all process

 

1. Is this not the right example for preemptive multitasking, if not, why not?

 

2. We can write programs in different ways, so what to do if I want to write a program for my example in a pre-emptive multitasking fashion?

Last Edited: Thu. Aug 27, 2020 - 06:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It describes the action of the operating system's scheduler, when multiple tasks (programs, processes, etc) need to share the processor fairly.

 

Without preemption, a task could hog the processor, and lock out the other tasks, for as long as it wanted to. This might be deliberate or accidental.

 

With preemption, the scheduler decides which task(s) to run, when, and for how long. It can pause a task, save its execution context, and allow another task to run. How much time each task receives might be driven by some system of priorities.

 

https://en.wikipedia.org/wiki/Pr...(computing)

In computing, preemption is the act of temporarily interrupting a task being carried out by a computer system, without requiring its cooperation, and with the intention of resuming the task at a later time. Such changes of the executed task are known as context switches. They are normally carried out by a privileged task or part of the system known as a preemptive scheduler, which has the power to preempt, or interrupt, and later resume, other tasks in the system.

Clearly, the discussion is only relevant on a system that can run multiple tasks concurrently (or give the impression of this).

 

In an embedded context, you might argue that interrupts, and ISRs, are a form of preemption.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Djsarkar wrote:

Task list & priorities

1)Start motor , wait, stop motor - Priority 1
2) Monitor for fire- Priority 2
3) Monitor for fault- Priority 3
4) Monitor for room temperature range- Priority 4

 

 

I've seen and worked with RT systems where priority was 1(or 0) was low priority, and higher numbers mean higher priority, I'm assuming that is the case here.

However, I have also seen systems (linux) where priority 1 (or 0) was high priority and higher numbers mean lower priority.  Must have been the same guys who came up with little endien!

 

In any case, in your example, 1) should be low priority, scheduling of the motor on/off (really an industrial motor is switched on/off every 20 ms?) anyway, it's just an example

I would think that 2) fire detection would be highest priority, followed by 3) fault detection, followed by 4) temp sensing, and then the background motor task. 

so rearranged as:

1) timed motor cycling

2) temp sensing

3) fault detection

4) fire detection (highest)

You may want to add a motor start/stop switch above fire detection, you never want the keep the naked ape waiting when they are trying to stop the motor!

5) start/stop switch activation (new highest)

 

Jim

 

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

Last Edited: Thu. Aug 27, 2020 - 06:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


I think the preemptive multitasking and free rtos are two two different stuff.

 

I repeat my question because I want to understand correct reasons

 

1. Is that not the right example for preemptive multitasking, if not, why not?

 

2. We can write programs in different ways, so what to do if I want to write a program

Last Edited: Thu. Aug 27, 2020 - 06:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One use of this in hard real-time systems running several periodic jobs ("job" is the term used in real-time literature).

 

Say you have several periodic control jobs: one runs once / second, another once / 100 ms, and another once / 10ms, where all required to complete by the start of the next cycle.

The 10 ms job requires 5 ms to execute, the 100 ms job requires 50 ms to execute, the 1 s job requires 1/2 s to execute.

Typically the 10 ms job is highest priority, then the 100 ms job, then the 1 s job.

When the 10 ms job is ready, it will preempt anything running.  And the 100 ms will preempt the 1 s if it is ready.

So you have a situation where the 1 sec periodic job may be preempted many times before it completes.

 

There is a theory of scheduling, "rate monotonic scheduling", that says if the the sum of the job utilizations is less than ~67% of full capacity then your system will not starve.  (The 67% number is what I remember, I may be off.  Under other assumptions about the schedule, the number may be 80% or even 100%.)

Last Edited: Thu. Aug 27, 2020 - 07:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've always thought that in order to understand preemptive MT, one should also know and understand cooperative MT.

An example of that for the AVR is Salvo, you can find it here: http://www.pumpkininc.com/conten...

 

In the manual is this example:

Let's look at an interesting example application, the controller for a remote soda-can vending machine.

It must indicate (via LEDs on the  buttons)  if  any  selections  are  empty,  handle  the  insertion  of  coins  and  bills,  properly  interpret  the  customer's  selection, 

release  the right item to the customer, and make change properly.

A modern,  microprocessor-controlled  vending  machine  might  also  regulate  internal  temperatures  (e.g.  for  soda  cans),  be  connected  to  a  network to relay out-of-stock information to a remote location, and be tied into a security system to deter vandalism. And of course all of this has to be done without error regardless of how many unpredictable things the customer does in the quest to quench his or her hunger or thirst.

 

it goes on from there to lay out the tasks and priorities needed.

Salvo is a commercial product, but the manual is free to download.

Hope it helps you, and this example could also be written using a PE MT system as well.

 

Jim

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

Last Edited: Thu. Aug 27, 2020 - 07:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

 

I've seen and worked with RT systems where priority was 1(or 0) was low priority, and higher numbers mean higher priority, I'm assuming that is the case here.

However, I have also seen systems (linux) where priority 1 (or 0) was high priority and higher numbers mean lower priority.  Must have been the same guys who came up with little endien!

 

In any case, in your example, 1) should be low priority, scheduling of the motor on/off (really an industrial motor is switched on/off every 20 ms?) anyway, it's just

I would think that 2) fire detection would be highest priority, followed by 3) fault detection, followed by 4) temp sensing, and then the background motor task. 

so rearranged as:

1) timed motor cycling

2) temp sensing

3) fault detection

4) fire detection (highest)

You may want to add a motor start/stop switch above fire detection, you never want the keep the naked ape waiting when they are trying to stop the motor!

5) start/stop switch activation (new highest)

 

Jim

 

Thanks for the clear answer, I see that timer interrupt plays an important role in Preemptive multitasking. It gives required time 

 

As you suggested I assume we have five tasks to complete in the system through preemptive multitasking. Do we  haveto keep a time out of each taske. how much timeout should I set for each task in my example

 

I am sorry if I am going into too much details

Last Edited: Thu. Aug 27, 2020 - 07:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Djsarkar wrote:

I think the preemptive multitasking and free rtos are two two different

then clearly you neither understand preemptive nor FreeRTOS.  The latter is a classic example of a preemptive OS and has the major advantage that not only is there an AVR port  but the website description of how the preemption works specifically focuses on avr-gcc

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Djsarkar wrote:

I think the preemptive multitasking and free rtos are two two different stuff.

 

I repeat my question because I want to understand correct reasons

 

1. Is that not the right example for preemptive multitasking, if not, why not?

 

2. We can write programs in different ways, so what to do if I want to write a program

 

Preemption is a function of the operating system. Do you want to write an operating system ?

 

FreeRTOS is an example of an operating system that supports multiple concurrent tasks. Its scheduler is preemptive, in that it jumps in once every millisecond and determines which task should have control of the processor, based on the tasks' relative priorities.

 

You only need concern yourself with this if you have a system that supports multiple tasks. That excludes 99% of simple embedded programs. 

 

In a system that doesn't support preemption, tasks must cooperate in order to share the processor. A task can be said to be co-operative if, periodically, it yields control of the processor to other tasks.

 

 

Last Edited: Thu. Aug 27, 2020 - 07:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Djsarkar wrote:

I think the preemptive multitasking and free rtos are two two different

then clearly you neither understand preemptive nor FreeRTOS.  The latter is a classic example of a preemptive OS and has the major advantage that not only is there an AVR port  but the website description of how the preemption works specifically focuses on avr-gcc

Yes i don't understand both preemptive and RTOS That's why i spent my time on reading preemptive multitasking. When I did not understand even by reading, I created one of my example and tried to understand it. And that's why I have asked two questions so that I can understand my problem where exactly I am lacking in basics

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Djsarkar wrote:
. Do we  have to keep a time out of each task. how much timeout should I set for each task

That is a design parameter that you need to work out during the planning stage of your project.

A task does not have to have a timeout, and if it does, then what happens when it does timeout needs to be planned.

Sorry I can not give specific answer, it all depends on the task.

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Djsarkar wrote:
Yes i don't understand both preemptive and RTOS

Read Chapter 2 RTOS fundamentals in the link I provide to the salvo manual, it may help clear up for you the basics.

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm sitting here at my MacBook. It has two processor cores and, with hyper threading, can run four processes at a time. But there are 358 processes running in the system. The scheduler alternates between them, making it appear as if all 358 are running concurrently.

 

None of those processes have been written to voluntarily yield to one another (i.e. they are not cooperative). The operating system's scheduler gives them small time slices of processor time, and then *preemptively* pauses each of them, so it can give some time to another process. 

 

You only need to concern yourself with preemption if you are writing an operating system. Here's an article on how the Linux scheduler works: https://www.linuxjournal.com/nod...

 

In the context of simple embedded systems, as I said upthread, you could consider interrupts as a form of preemption. The main application runs until an interrupt arrives. The microcontroller is configured to *preempt* the main application code, by saving its execution state and jumping to the interrupt handler code. Thus, the interrupt handler has a higher priority to execute and the main application code doesn't have to concern itself with yielding.

 

As a software designer you have a number of choices for execution model. Preemptive multitasking is one model, but you have to compare the options and decide which is the more appropriate for the project at hand.

 

If you'd like to get some hands-on experience with FreeRTOS, buy a $5 ESP32 board and use the Arduino IDE. That uses FreeRTOS and there is lots of example code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

obdevel wrote:

You only need to concern yourself with preemption if you are writing an operating system. Here's an article on how the Linux scheduler works: https://www.linuxjournal.com/nod...

 

In

 

As a software designer you have a number of choices for execution model. Preemptive multitasking is one model, but you have to compare the options and decide which is the more appropriate for the project at hand..

My first question was that, I do not have much experience in project planning .As designer how do I find out if preemptive multi tasking is suitable for my example or not, I just wanted to know the reason why it is suitable or why it is not suitable?

 

This is not homework assignment so can anyone help me to find reason because I have tried my best and still searching on internet and reading pages to understand reasons 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is a large grey area where "YES, RTOS" and "FORGET RTOS" overlap. In this area you CAN use RTOS but it probably will neither help nor hurt, very much. 

 

Because this grey area exists, it is often hard to tell whether or not RTOS is a good choice. I happen to like finite state machines, and use those extensively, often in places that common wisdom might suggest RTOS. For one thing, I like this certainty of processing responses. On the other hand, you have to have a really good handle on what the application needs and where the potential problems are. I tend to design everything as though time and resources are severely limited, and I make choices on that basis. So far, it has served me well.

 

But, also, because this grey area exists, there is probably no single set of criteria that you can use to make that decision. THAT is one of those decision skills that has to be developed by anyone who tries to do more than "blink the LED". How you make that decision will depend on a number of things. Not  the least "things" are simply "what are your skills" and "what are you comfortable with".

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would recommend reading the MicroC/OS book available on Amazon.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The linux kernel scheduler is basically round robin.  Processes are switched based on a resource not being available or by a clock interrupt generating a time-slice.  I don't see how this would be useful on AVR type systems.  You can get the same behavior with a cooperative scheduler, without the high cost of time for context switches and ram for multiple stacks.  The bulk of embedded systems I've seen which use an RTOS are for real-time operation, where schedulers are (typically) priority driven.

 

I've only been playing with the AVR architecture for 1.5 years, but my observation is that if you can implement with a cooperative scheduler (where you are always running some function to completion) then do so.  If not, then a RT OS (not Linux) is an option.

 

When to use cooperative:

1) You can chop up long running code into pieces.  This typically means, saving state somewhere, where each "piece" starts at a specific state and runs to completion to a new state.  Sometimes, the piece ends at a point where you are waiting for a resource (e.g., input data from sensor or user input device).

2) You want extremely low interrupt latency.

3) You are short on ram (i.e., stack space).

4) You are short on code space.  How big is FreeRTOS?

 

When to use preemptive MT:

1) when you have mutliple deadlines in your system

2) when you can't chop up code into deterministic pieces

3) when you don't mind delays in interrupt process (How long is the FreeRTOS worst-case interrupt latency?)

 

[I have written a lightweight priority-driven real-time preemptive tasking system for the AVRs.  But after doing so, I think I would always look to the cooperative scheduler first.]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Djsarkar wrote:
As designer how do I find out if preemptive multi tasking is suitable for my example or not, I just wanted to know the reason why it is suitable or why it is not suitable?

 

Build a minimalistic round-robin version and a preemptive multi-tasking version and evaluate them.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

After a spending time on reading, I get the idea of ​​a weather monitoring system in which I think preemptive multitasking can be used.

 

What do you think about weather monitoring systems that keep the record at every 20 ms

 

It will not hurt to assume that small weather monitoring system have only three sensors. wind sensor to measure the speed of wind , temperature sensor to measure temperature and humidity to measure the humidity in environment

 

Micro reads the sensor and send it to the PC, PC send acknowledge when it received data successful. Every value should be transferred at the fixed time and acknowledge must be received at the fixed time. If data will not found at fixed time it would be the failure of system. Critical time for each task is 20ms this is reason I think preemptive multitasking can be used in weather monitoring system

 

Last Edited: Fri. Aug 28, 2020 - 05:32 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For weather, 20ms is MUCH too fast. Most of your sensors will have time constants on the order of seconds.

 

That is a simple enough project that a simple sequential state machine will do an excellent job. I use a state machine to sample a three-axis accelerometer, generate time stamps, process, and write to a disk file up to 25 times per second. 

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Fri. Aug 28, 2020 - 05:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Do you understand German?..

 

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Djsarkar wrote:
After a spending time on reading, I get the idea of ​​a weather monitoring system in which I think preemptive multitasking can be used
sure it can be used but does it have to be used? The fact is that you only employ an RTOS when the design of the system becomes so complex that you have no other choice. For AVR usually a main() loop and a collection of ISR() is all that's required. You will waste resources trying to shoehorn an OS into such a design.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The generic weather monitor is not such a good RTOS example, since there can be a lot of slop and everything will probably still be perfectly fine (who cares if you read the temperature in 10 seconds or 10.07 seconds, or maybe even miss one reading?).   Think of applications where timing is critical, or any out of sequence or missed responses would be a performance disaster.  Or an application where many complex things need to happen "simultaneously". How about a missile guidance, where any late or missing flight control adjustments might spell doom?  Some flight control systems are inherently unstable, without a continual closed loop correction.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This thread looks and reads so very similar to a recent thread. ... Let's see if Google can find it.

...

...

Found it:

https://www.avrfreaks.net/forum/atm-machine-work-rtos

 

Oh dear; I think you guys are wasting your time here.

 

Last Edited: Fri. Aug 28, 2020 - 10:24 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:

This thread looks and reads so very similar to a recent thread. ... Let's see if Google can find it.

...

...

Found it:

https://www.avrfreaks.net/forum/atm-machine-work-rtos

 

Oh dear; I think you guys are wasting your time here.

 


I ask with all due respect , Have I asked question for RTOS? I am not interested in RTOS.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Djsarkar wrote:
N.Winterbottom wrote:

This thread looks and reads so very similar to a recent thread. ... Let's see if Google can find it.

...

...

Found it:

https://www.avrfreaks.net/forum/atm-machine-work-rtos

 

Oh dear; I think you guys are wasting your time here.

 

I ask with all due respect , Have I asked question for RTOS? I am not interested in RTOS.
This is the 2nd time I've looked at poster s IP addresses which make me rather suspect were being played by a multi account troll here!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here's a comparison article from a reliable source: https://www.keil.com/rl-arm/rtx_... (slightly biased towards using an rtos).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Djsarkar wrote:
I ask with all due respect , Have I asked question for RTOS? I am not interested in RTOS.

RTOS has been mentioned liberally throughout this thread already. Your dis-interest in anything RTOS isn't helping you understand a task scheduler for the following reason:

 

Your subject of interest; the Pre-emptive Task Scheduler is rarely written or provided by a software vendor as a standalone component. It is generally a core component of a Kernel. And that Kernel; again part of an operating system. The default embedded operating system is FreeRTOS for which AVR source is available; so that's why it's been mentioned so much already.

 

I had a good rummage around avrfreaks for "Pre-emptive Task Scheduler" threads. The majority degenerate like the one linked #25 and are not worth reading. This mammoth thread https://www.avrfreaks.net/forum/rtos-avr-0 however does not degenerate and is packed full of useful information after #50 or so.

 

BTW: I found only one decent non RTOS example via Google: http://kevincuzner.com/2015/12/31/writing-a-preemptive-task-scheduler-for-avr/.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I vote to pull the plug - enough bandwidth has been wasted.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
I vote to pull the plug - enough bandwidth has been wasted.

Well I though that - but gave him the benefit of the doubt.

 

OP has offered some clarification of exactly what he's looking for so let's see for now.

 

Last Edited: Fri. Aug 28, 2020 - 12:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The MO is the same. The outcome is the same.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
I vote to pull the plug - enough bandwidth has been wasted.

 

Does the OP understand state machines yet?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I seems the OP is having problems finding a "simple" example of PEMT because, as pointed out here many times, PEMT is not justified for simple tasks!   It's as simple as that! smiley

 

I would suggest the OP enroll in a college course in operating systems design. 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

Last Edited: Fri. Aug 28, 2020 - 01:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:

Does the OP understand state machines yet?

It was OP himself asked for that thread to be locked. That's one of the reasons I don't think he's a proper Troll.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have already told very calmly I am not trolling. Isn't it common that two people live in the same state and Both use same  network provider company that is most popular in the country. Is it fair to call someone a troll after seeing this much equality. 

 

Just think how you feel when someone is blaming when you have not made any mistake.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I routinely use simple time sharing without a scheduler.
It will force the task to switch when the time comes, unless it blocks interrupts.
It has some disadvantages, but it is very convenient.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why do you ask a question when you don't really understand what you're asking? Then you argue. Then time and time again, we've given you links to read. You've still not read my tutorial on multi-tasking.

 

You clearly don't understand the problem that state machines and task management are trying to solve, so how can you begin to understand them? It is backwards logic.

 

If you REALLY want to learn, start writing your own programs and solving the problem yourself. Then you can better appreciate the purpose of state machines and task management.

 

It also appears you have some form of learning difficulty - maybe you're dyslexic? In which case you should understand techniques to work around such difficulties. Your current technique is clearly not working and only serving to alienate yourself.

 

What is the motivation for these questions? Are there job advertisements wanting embedded systems people with RTOS experience? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Everyone has to start somewhere, but one can easily overwhelm themselves worrying about too much.  Instead of traveling, you can't go because you haven't figured out exactly when earthquakes happen, no matter how many explanations are read.   

 

What programs HAVE you written for the AVR?  Able to read buttons, turn on some leds, set up the PWM on a timer, read some voltages from the ADC?  Send & rcv information through the uart?   Write values out using the SPI & TWI?  Getting good at those will help understanding many important things & probably make other concepts easier to digest or appreciate what to look out for.  Don't paralyze yourself into not being able to do anything, until finding the perfect answer to various vague theoretical questions (that aren't involved in anything you are presently doing).  Wandering around aimlessly can be depressing, so pick a project, work on it, and get it running.   The difficult & complex stuff will present themselves in their own time. Then you will have specific questions associated with your situation.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!