I am looking for someone who has already worked on preemptive multitasking. What real-world example comes to your mind when you think of preemptive multitasking?
Hi @Rehan11
Might I ask what the premise of your question is?
I did work on Multics in the late 70s… does it count?
Then all sorts of Unix and Mac OS X and iOS etc.. all « modern » OSes are using preemptive multitasking (Windows was late to the party)
![]()
ÉDIT: apple was even later to the party too. That almost killed them !
For real world examples Google for: "microcontroller preemptive multitasking"
And this about Linux / Unix: https://www.linuxtopia.org/online_books/programming_books/art_of_unix_programming/ch03s01_1.html
Hello
I´ve been started wirh VersaDOS and Assembler ![]()
Simple home appliances such a microwave, oven or washing machine can do without preemptive multitasking. A industrial apparatus that is more complex will probably use preemptive multitasking.
My induction cooker has no preemptive multitasking, because it has a hiccup in the fan speed if I press a button.
that's probably more due to poor coding / hardware than the absence of preemptive multitasking ![]()
I think Windows was early to the party - I mean earlier than Mac OS.
An ESP32 will do multitasking and multi-processing with the OS freeRTOS. Preemptive, probablly not but give it a look, FreeRTOS API categories.
Very true
I think (not an expert in windows stuff) win32 is the first attempt to introduce Some preemptive multitasking - circa 1993 and Apple had to wait after they went almost bankrupt and bought NeXT and release the first version of OS X in 2001!! (Not having this in the OS almost Killed them)
Preemptive multitasking is what you use when you have more processes than cores and you don't want to rely on those processes to be good neighbors and yield in a timely fashion.
The downside is that every process will get a time slice - eventually, but there are no guarantees about when. This can be a serious issue for micro controllers trying to control hardware that has particular timing needs. That is the reason that using Neopixels on a Pi is not recommended by Adafruit because task switching may mess up attempts to send data to a strip.
Yes, that's exactly right. MacOS was good from a UI point of view (considered better than Windows at the time), but it's memory management and multi-tasking were crap. NeXT was an absolute lifesaver for Apple.
Well, a decent pre-emptive real-time multitasking OS does give the designer a powerful toolkit with which to manage their real-time system. You can get guaranteed response times if you use the features properly.
The issue with Neopixels on Pi isn't really about Linux (the OS on the Pi) being multi-tasking, it's about it not being a real-time OS (neither is Windows or MacOS). That is one reason why Pi isn't regarded as a competitor to advanced microcontroller boards - it was designed as a general purpose, low cost computer, which is why it runs a general purpose, free, operating system.
As I say, Linux is multi-user, multi-processing, multi-tasking, but not real-time.
As @Idahowalker mentions FreeRTOS is a real-time, pre-emptive, embedded scheduler has been ported to work on most Arduino Boards, including AVR, ARM and ESP. It even works suprisingly well on microcontrollers with limited memory such as the Atmega328P (Uno/Nano) and Atmega32U4 (Leonardo/Micro).
What's quite nice about FreeRTOS, is that it's really flexible and in addition to prioritised tasks, allows you to set up oneshot or continous software timers. It's also possible to select pre-emption, with or without time slicing, or alternatively just use co-operative scheduling (without time slicing), where tasks are still prioritised, but allowed to run to completion (until they block).
Co-operative scheduling can be handy in certain situations when sharing variables (or other shared resources) between tasks, since it largely removes the need for mutual exclusion or the implementation of gateway tasks to control access.
Furthermore, it's possible to use interrupts both to run code immediately outside of context of the scheduler, but also conversely to quickly bring interrupt handling within the context of the scheduler. FreeRTOS achieves the latter by employing FreeRTOS's thead safe "From ISR" API functions. These functions can be called from within the microcontroller's interrupt service routines themselves, in order to activate high priority interrupt handling tasks. Moving the interrupt handling to a task, keeps the code in the interrupt service routine itself to a minimum.
Inter-task communication is provided by queues, binary/counting semaphores, event groups and task notifications, which can be set to either blocking, non-blocking or timed.
Here's an excellent guide to FreeRTOS:
I think FreeRTOS is great, although I understand the time-slicing is quite coarse in the lower-powered Arduinos - measured in milliseconds?
Yes, as you mention on AVR microcontrollers the scheduler "tick" is provided by the Watchdog's 128kHz oscillator/prescaler with a minimum interval of 15ms, which might limit its use for some applications.
Although, I guess this can be mitigated to a certain extent if the system is interrupt driven, since in addition to the scheduler "tick", interrupts also give the scheduler the opportunity to switch to a higher priority task.
Also, the WDT's oscillator is hardly what you'd call a precision timepiece, but is OK for rough timing measurements.