Real-Time Operating System in 96 Lines of C | RIOS

  Рет қаралды 90,056

Cheesed Up

Cheesed Up

Күн бұрын

Пікірлер: 128
@UliTroyo
@UliTroyo 6 ай бұрын
3:24 "The next thing... is a typo" cracked me up.
@LuminosityUK
@LuminosityUK 6 ай бұрын
I wrote one for an 8080 in 1981 for a custom industrial SBC. It is still in use in a few places in the world today.
@MrJegerjeg
@MrJegerjeg 5 ай бұрын
Well, now I am impressed!
@blackcathardware6238
@blackcathardware6238 5 ай бұрын
Impressed,too. Especially as the first Computer I ever saw was a CBM 3032 in 1984/85.
@1jnk
@1jnk 6 ай бұрын
We got Suckless OS before GTA6
@ZE_TRVTH_NVKE
@ZE_TRVTH_NVKE 6 ай бұрын
Saving this OS to a disquette with a note "Linux is depricated." and mailing it to Linus.
@Tenraiden
@Tenraiden 6 ай бұрын
GTA sucks
@blackice3403
@blackice3403 5 ай бұрын
​@@Tenraidentrue
@alexsteve647
@alexsteve647 5 ай бұрын
​@@blackice3403false
@st20332
@st20332 5 ай бұрын
online does, story doesn't ​@@Tenraiden
@優さん-n7m
@優さん-n7m 6 ай бұрын
I waited for something like this since 2006
@Jupiter__001_
@Jupiter__001_ 6 ай бұрын
You could install an RTOS from a USB stick if you wanted. Interestingly BlackBerryOS was real-time despite being a user-facing OS.
@eekee6034
@eekee6034 6 ай бұрын
Linux developed real-time features in the early 00s. Windows may have had some degree of real-time support earlier as it's desirable for games and media. Both OSs certainly have real-time support now. Only critical applications really need stripped down OSs.
@trejohnson7677
@trejohnson7677 6 ай бұрын
i find it hilarious we call them real time operating systems, when they should be called defined time.
@marcombo01
@marcombo01 6 ай бұрын
I think most cars infotainment systems run on a RTOS. And maybe that's reason why most car infotainment are still really bad even today
@adversemiller
@adversemiller 6 ай бұрын
@@marcombo01 most cars run android (linux), so you would be right about that
@anonymousarmadillo6589
@anonymousarmadillo6589 6 ай бұрын
Android OS isn't real time by any stretch of the imagination ​@@adversemiller
@Gersberms
@Gersberms 6 ай бұрын
Very impressive! I've wondered for like 20 years how a multi tasking operating system would switch mid-task to the next process. Now I know a lot more about this process.
@maksymiliank5135
@maksymiliank5135 6 ай бұрын
This process on a OS like linux, windows, macos is a lot more complicated but the same ideas apply. There is a process called scheduler which schedules when each task should be executed. There are some mechanism to adjust priority so that some tasks won't "starve" (they will be executed at some point even if their priority is very low)
@zach430
@zach430 6 ай бұрын
Yes nowadays they mash all the basic scheduling principles into one scheduler
@Gersberms
@Gersberms 6 ай бұрын
@@maksymiliank5135 I understand. Memory management and all that, process isolation. With this scheduler here any function can access whatever it wants.
@JacobsKrąnųg
@JacobsKrąnųg 5 ай бұрын
if you were really curious, you would know it by now. its not that hard to google it
@RegisMichelLeclerc
@RegisMichelLeclerc 6 ай бұрын
The microcontroller in Arduino was originally an atMega8 at 16MHz, replaced by an atMega328P (at 16MHz too, but upgradable to 20MHz), which is the argument you passed to AVRDude. Now, to be fair, using interrupts (especially the CLK one) is catastrophic in terms of timewaste and stack management. You're better putting counters and have a switch/case in your "main" loop that calls functions by pointer. From there, you can craft your functions so they perform an atomic part of the entire task every time they're called (such as updating the screen, turning one LED on or off in a field of LEDs, etc.). Interrupts are cool, but that's admitting the defeat of intelligence by paying incompetece with clock cycles.
@Freshbott2
@Freshbott2 6 ай бұрын
Thanks for sharing this, I’d never even considered that it could even be any different to any normal operating system.
@eekee6034
@eekee6034 6 ай бұрын
If you don't use interrupts, your user code had better not go into a long loop without calling the OS. It can be done, but you have to carefully craft everything in little chunks as you implied, with, for safety, no longer-running background processes. If you have background processes, they themselves will have to waste time and stack space yielding to the kernel just in case, and woe betide you if a bug causes such yielding to be skipped.
@simontillson482
@simontillson482 6 ай бұрын
Not every task can be done in small steps. Also, crafting functions to operate in this way is often very inefficient in practise. There are many, many use cases where preemptive multitasking is not just preferable but an absolute requirement.
@superxpro12
@superxpro12 6 ай бұрын
Using an isr allows for preemptive task interruption, not cooperative. On ultra low power micros you don't get a dedicated CPU isr operation like pendsv for this purpose, so you have to use a timer.
@RegisMichelLeclerc
@RegisMichelLeclerc 6 ай бұрын
@@simontillson482 That's why I'm mentioning the stack: you better be very reentrant if you use preemptive multitasking, and ve even more cautious about the timings of what interrupts what. I prefer keeping my interrupts for low-level signals and use a "token-loop" (by analogy to "token-ring") for time-based stuff. In that way, yes, everything can be decomposed in micro-tasks, it just takes a different way of thinking them with a lot of pointers and global variables.
@mecitpamuk5623
@mecitpamuk5623 5 ай бұрын
Thats good for only periodic tasks. We also have inter task communucation. Meaning that task b can send an event to task a in that case an asyncronous preemption can be needed!
@jwd.t
@jwd.t 6 ай бұрын
Man, where have you been all these days?
@dzm_mlc
@dzm_mlc 6 ай бұрын
I just cant describe amount of problems you'll get when using such approach, it's completely wrong. Just never use interrupts like this, that's not what they meant for. Never call user code bigger that assigning value to a register/shared memory from ISR. Never enable interrupts from ISR unless you want another high-priority interrupt to be called. And never use nested interrupts like this - its a way to stack overflow, only use it when it's necessary to handle event in time. If you want just run some code "simultaniousely" just add all your "tasks" into for() loop and in every task add check for timeout and do something only when timeouted (see how Runnables in AUTOSAR works)
@un2mensch
@un2mensch 5 ай бұрын
Every single thing you've listed as problematic is, in reality, a perfectly relevant and solid way to implement things if they result in the desired behavior / functionality. Just because you have in mind an extremely specific and limited design does not make other approaches wrong.
@whiteingale
@whiteingale 5 ай бұрын
OKAY, "magic man" but I didn't hear you say "Big McThankie from McSpankie" to the customers.
@dzm_mlc
@dzm_mlc 4 ай бұрын
@@un2mensch There is many ways and shortcuts to satisfy customers requirements, however, smart engineers tried them all for us and concidered some of them as problematic and hard to maintain, unreliable and unsafe. You can always go your own way in development, however one day when reached enough expertise you'll find that these smart engineers was right. This is what engineereng education for - to not waste your time for dead-end approaches
@ivanmaglica264
@ivanmaglica264 5 ай бұрын
I don't think I would consider this as an OS, it still fits more into runtime category. Having a loader for external binary executables is I think an essential part to qualify as OS, otherwise it's just statically compiled task scheduler. Good stuff thou, especially for learning, and still very useful. Registers not being pushed to and popped from stack between task switching is a bit concerning. Guess this has to be done by task itself.
@donaldwright2426
@donaldwright2426 20 күн бұрын
Thanks for the explanations. I'm writing a tiny RTOS for the MEGA 2560 (that's because it's what I have) and what is propose by RIOS is close to what I'm looking but lack of small features. PORTB = 0xff is to enable all 8 ports of port B as outputs. 🙂
@josealejandrovaroncarreno1692
@josealejandrovaroncarreno1692 6 ай бұрын
excelent video, please continue
@lets_lvl_up
@lets_lvl_up 5 ай бұрын
avrdude..... gr8 naming right there
@trejohnson7677
@trejohnson7677 6 ай бұрын
*laughs in solar flare on unhardened chip*. really though, this a nice exposition, its got the bones.
@eekee6034
@eekee6034 6 ай бұрын
A char in a struct full of ints won't save any memory unless your compiler packs structs. Packed structs mean unaligned access which is slow on most architectures. Even some models of x86 (which has unaligned opcodes) have slow unaligned access to data. I don't know the native word size of the CPU you're using. Any larger than a byte and you don't want to pack.
@jojoposter
@jojoposter 6 ай бұрын
The avr is an 8bit cpu. So aligned means 8bit or byte-aligned on this architecture.
@eekee6034
@eekee6034 6 ай бұрын
@@jojoposter Ah, thanks! I should have looked it up before writing all that. :)
@anon_y_mousse
@anon_y_mousse 5 ай бұрын
@@eekee6034 While it is technically true that misaligned memory accesses will be slower, unless you're living in the early aughts or earlier you might as well just do it anyway. Back a couple of decades ago when I was on Win98 using a 533mhz Celery, I ran all kinds of tests and found that if you can align before a bunch of looped reads/writes, then do so, but if you couldn't it was only a 15% hit on performance, and that was back then. Now the performance hit is negligible and only long-running server applications ever really have to pay that cost.
@eekee6034
@eekee6034 5 ай бұрын
@@anon_y_mousse I'm sorry, but progress isn't always linear like that. Listening to my OS-developing friends in the 2010s, I learned some Intel and other CPUs were not good with unaligned access. I don't know how bad they were, but these weren't performance nuts. (They were simplicity nuts.) Anyway, 10% is my upper limit for thinking of something as minor.
@anon_y_mousse
@anon_y_mousse 5 ай бұрын
@@eekee6034 Negligible for most applications is less than 5%, but unless you're doing tons of copying all over the place, you're never going to hit that high. What's more is that you literally can't always align every pointer when dealing with large buffers. This is part of the reason why most allocators waste a little extra space when allocating to ensure that pointers are aligned on specific boundaries, but then it's up to you to align the data when dealing with structs. All of this is of course a moot point when it comes to 8-bit processors, but it's essentially irrelevant for most people anyway on modern 64-bit processors.
@jon9103
@jon9103 5 ай бұрын
RTOS doesn't mean a basic OS, though they typically are. It means that they are designed to handle real-time constraints which is difficult to do on bloated OSes, hence why they tend to be stripped down, bare bones implementations.
@cheesed_up
@cheesed_up 5 ай бұрын
If I could pin a comment, this would probably be it :D
@cryptic_daemon_
@cryptic_daemon_ 6 ай бұрын
"...it gonna run minecraft..." me: and I took that personally
@esra_erimez
@esra_erimez 5 ай бұрын
This was so informative and interesting
@GroovThe
@GroovThe 6 ай бұрын
Right, that makes a lot of sense. RTOSs allow this structure of overlapping periodic tasks. This seems like a very basic and common requirement. The existence of RIOS bags the question then, why use something bigger like freeRTOS, or even pay for one? What's the thing that makes RIOS non-viable for "real" projects?
@kriszSTNX0
@kriszSTNX0 6 ай бұрын
How complex, time consuming can the tick function be? If none of the tasks finishes before the next interrupt, is the avr gonna crash bc of stack overflow?
@Thecommet
@Thecommet 6 ай бұрын
Yep, from the looks of it. He also starts the timer and enables global interrupts before initializing the task structures
@caspianmaclean8122
@caspianmaclean8122 3 ай бұрын
It looks like once the highest priority task is running, the next interrupt should quickly see there's no higher priority tasks left to start, and exit. So possibly it won't overflow, at least I think this approach could be made to avoid overflowing if all the tasks can fit on the stack at once.
@arduous222
@arduous222 5 ай бұрын
0:18 Bold of you to assume that my OS can run Minecraft.
@johnschmidt874
@johnschmidt874 6 ай бұрын
Nice video! I've just subscribed to your channel.
@blackmagicprod7039
@blackmagicprod7039 6 ай бұрын
Any chance you’d be willing to give high-precision, high-accuracy timing tasks a try with a tiny RTOS like this? I’m VERY interested in various applications of high-precision timing in small consumer devices. I’m trying to figure out how accurate AND precise you could get, for example: with an Arduino, a GPS module, and an oven-controlled oscillator (and ideally without breaking the bank). Imagine a system such as that with an antenna connected (even just a long wire on a GPIO pin) that’s listening for the low frequency bangs of lightning flashes. Like, with 3 of them, what would be the minimum separation necessary between the units to localize a lightning strike to within 10m? The timing requirements for an application like lightning localization become very extreme very quickly, and I am a complete novice. Uncertainty about precisely how the processor will schedule execution is fatal to an application like that where microseconds means literal miles of difference. Anyway, just an idea to consider. Lots of very interesting applications for RTOS… Liked the video and subbed
@cheesed_up
@cheesed_up 6 ай бұрын
Hey, very interesting ideas you have there! I'm gonna be frank - I don't have the expertise to dive this deep into this topic. That said, I would be very interested to see if you ever come up with something like this! I'm glad you liked the video! Good luck on your journey!
@blackmagicprod7039
@blackmagicprod7039 6 ай бұрын
@@cheesed_up Thanks for your consideration and for your honest reply! I’ve been kicking around several ideas along these lines for many months, and seriously investigating this specific application for a few weeks. I don’t think I’ll stop until it’s done, but I also probably won’t document it on KZbin. Do you have a discord or something? Otherwise I guess I’ll just come back to this comment and update you 😅
@mindasb
@mindasb 6 ай бұрын
​@@blackmagicprod7039very interesting idea. I would be interested in hearing more ideas and also the updates if you decide to implemwnt them
@elias-ou2sp
@elias-ou2sp 6 ай бұрын
okay, now create a lisp machine.
@ItsCOMMANDer_
@ItsCOMMANDer_ 5 ай бұрын
Looks cool, maybe i'll make my own using a round robin sheduler, i'll see
@rescyy2235
@rescyy2235 6 ай бұрын
8:14 "we need to enable our GPIO pins", sure buddy, pins
@pecfexfextus
@pecfexfextus 6 ай бұрын
wondering the same thing
@cheesed_up
@cheesed_up 4 ай бұрын
I thought I enabled interrupts and initialized GPIO pins. Must have heard it wrong :D
@rescyy2235
@rescyy2235 4 ай бұрын
​@@cheesed_up ah no, that's not what I meant, I have nothing against what you did in the video. I was referring to the way you said PINS.
@viewerguy10
@viewerguy10 3 ай бұрын
Lol the auto-generated subtitles was way off.
@gerooq
@gerooq 5 ай бұрын
Very cool but how is this actually different from just regular old embedded programming?
@cheesed_up
@cheesed_up 5 ай бұрын
I don't think it's different. This is just an example of how a basic preemptive scheduler might look like.
@Prescott2400-my6di
@Prescott2400-my6di 6 ай бұрын
I want a queue! Where are my queue! I want a queue on my desk until the end of the world! 😡
@thegermantomoeser
@thegermantomoeser 5 ай бұрын
Ahh, that was great.
@RasitEvduzen
@RasitEvduzen 5 ай бұрын
Actually this is very similar to time driven non-cooperative multitasking system.
@ferroalloys594
@ferroalloys594 5 ай бұрын
Why is the ancient "pseudo-metric" of 'lines of code' used? Indeed, why use the misread term "code" at all, what conventional use of the term conventionally associated with ways of encryption/decryption was involved, none? How many actual TERMINAL SYMBOLS in the SOURCE DESCRIPTION (that are often mistakenly termed "code") were involved? Now, maybe there's an actual textual metric with some meaning? Simples ):-)
@cheesed_up
@cheesed_up 4 ай бұрын
clickbait!! In all seriousness - I just didn't have a better idea how to name the series of videos. I'm sorry if it's a bit annoying!
@ferroalloys594
@ferroalloys594 4 ай бұрын
No problem! Thanks indeed for your reply, much appreciated... ):-)
@DwAboutItManFr
@DwAboutItManFr 6 ай бұрын
No assembly?
@anon_y_mousse
@anon_y_mousse 5 ай бұрын
I suspect the goal was portability. Different microcontrollers will require different instruction mnemonics. It seems stupid when you think about it, but it's generally the processor manufacturer that supplies the assembler and thus requires whatever mnemonics they want even if they mean basically the same thing between 100 different chips. Granted, it doesn't make it any easier when different chips will require a different amount of operands to an `add` instruction, think ARM versus x86, but that's easily solved by requiring two operand chips to have the mnemonic of repeating the destination or three operand chips of allowing for a mnemonic of not repeating the destination when it's the same as the next operand.
@hugopontes4989
@hugopontes4989 5 ай бұрын
How come you are running the task functions from inside the timer ISR? For this the timer interrupt must be set to the lowest priority (so that other interrupts still seem like interrupts)? And the timer interrupt can interrupt itself recursively? Is that a good idea?
@cheesed_up
@cheesed_up 4 ай бұрын
Not sure if it's a good idea if you are the one maintaining the code. But if I remember correctly, freeRTOS does something like this as well.
@fishsayhelo9872
@fishsayhelo9872 5 ай бұрын
very gud 👍
@swgman
@swgman 6 ай бұрын
How did you know I was playing minecraft when I was watching 0:23
@Kalumbatsch
@Kalumbatsch 5 ай бұрын
Not sure if a minimalist scheduler running on a microcontroller qualifies as an operating system, but hey. It's kind of cute.
@cheesed_up
@cheesed_up 5 ай бұрын
To add to this: I'm not sure if an RTOS qualifies as an operating system :D
@poparab
@poparab 5 ай бұрын
real time means you have a time on a computer, and only this time is valid for all on the system. that has nothing to do with it was 7.00 in the morning. Robotic time is with special time cards.
@cheesed_up
@cheesed_up 4 ай бұрын
Not sure if that's 100% correct - real time in this context means that ceratain operations need to execute WITHIN a certain timeline or EXACTLY at a certain times (hard real time). Thanks for the comment!
@hatsuneadc
@hatsuneadc 6 ай бұрын
Very nice
@cvabds
@cvabds 6 ай бұрын
Now do templeOS
@maiconloure
@maiconloure 6 ай бұрын
Don't depend on one resource
@thomasluk4319
@thomasluk4319 6 ай бұрын
what theme are you using
@cheesed_up
@cheesed_up 6 ай бұрын
I think it's something like "gleam theme" - found it randomly in vscode extension store.
@tugaric
@tugaric 6 ай бұрын
*hears russian accent* ohh boy I nees to see this one
@friedrichmyers
@friedrichmyers 6 ай бұрын
Fr. Tsoding also has an accent and that's how I knew the mf was real
@cheesed_up
@cheesed_up 6 ай бұрын
Not Russian, but yeah it's slavic :D
@youtube-is-cringe
@youtube-is-cringe 6 ай бұрын
nice
@platin2148
@platin2148 6 ай бұрын
Well its bigger than that as it requires interrupt library.
@emilien.breton
@emilien.breton 6 ай бұрын
Pushing aside my opinion that library line counts should be excluded anyway, `io.h` and `interrupt.h` are basically a bunch of one-liner `#define`s and so hard-coding everything wouldn't even increase client line count.
@Tenraiden
@Tenraiden 6 ай бұрын
Could've made that in fewer lines if you knew about for loops lol
@earth2k66
@earth2k66 6 ай бұрын
This model is used in RP2040 c-sdk's async_context module too. Very useful for doing pseudo async stuff without wasting cpu cycles. The only thing lacking in the model is the same pseudo sleep() that would save the state of a task and jump to next tusks then resumes the sleeping task when timeout is over. Back in the day I first read about this concept of pseudo sleep in python with generators (yield), then they actually came up with the same concept in python asyncio. However there is probably no way to do this in pure C without getting into assembly like RTOS.
@user-sq1oi9qp8w
@user-sq1oi9qp8w 6 ай бұрын
what font in thumbnail?
@cheesed_up
@cheesed_up 6 ай бұрын
Comic Code - I recommend you search youtube if you wanna find out more.
@RichardLofty
@RichardLofty 6 ай бұрын
MsDOS works in real mode.
@typedef_
@typedef_ 5 ай бұрын
I only understood line 26
@imakhlaqXD
@imakhlaqXD 6 ай бұрын
You dont care about hardware writing "javascript". Lool good one
@alaricgorb
@alaricgorb 6 ай бұрын
free retos
@Kyoz
@Kyoz 5 ай бұрын
🤍
@mad_t
@mad_t 5 ай бұрын
96 lines my ass. Try to count lines including headers ;)
@cheesed_up
@cheesed_up 5 ай бұрын
Please don't be mad_t about my clickbait titles :D
@bubach85
@bubach85 4 ай бұрын
FYI: This guy has no idea what an RTOS is. Nothing he said about it is true. 🤣
@cheesed_up
@cheesed_up 4 ай бұрын
Thanks for the feedback! May I ask what makes you say that? I don't want to spread misinformation and am happy to listen to what you have to say!
@idesys3135
@idesys3135 5 ай бұрын
Now do it in rust
@romsthe
@romsthe 6 ай бұрын
Did you not get my comment about not running your task inside an ISR, or do you keep deleting it ? if it's a bug from youtube ok, but if you'd rather hide it rather than learn something then it's bad. Good devs learn from their mistakes and from colleagues, refusing knowledge won't get you far. Also don't hide knowledge to others, I think it's as bad as a crime, sincerely
@joshnjoshgaming
@joshnjoshgaming 6 ай бұрын
Did it contain a link? often links get shadow-banned by youtube
@romsthe
@romsthe 5 ай бұрын
@@joshnjoshgaming well I tried again but it won't show up. If you want look for "context switch" and not running code inside the ISR. You can also check interrupt code for storing and retrieving the context for each arch in free RTOS for examples
@romsthe
@romsthe 5 ай бұрын
@@joshnjoshgaming actually there's an excellent series of videos about this if you look for "element14 introduction to RTOS" on youtube
@cheesed_up
@cheesed_up 5 ай бұрын
Hey @romsthe, sorry that your comment dissapeared. And yeah, I'm pretty sure that @joshnjoshgaming is right - youtube probably hides comments with links. I also can't put links into video description without youtube complaining.
Game of Life in 54 Lines of Python | Conway's Game of Life
5:02
Cheesed Up
Рет қаралды 1,6 М.
OCCUPIED #shortssprintbrasil
0:37
Natan por Aí
Рет қаралды 131 МЛН
JISOO - ‘꽃(FLOWER)’ M/V
3:05
BLACKPINK
Рет қаралды 137 МЛН
Writing a game the hard way - from scratch using C. #1
34:20
NCOT Technology
Рет қаралды 201 М.
Ollama+Py-GPT: Integrate Py-GPT with Ollama Models-Part 01
4:36
How do CPUs read machine code? - 6502 part 2
49:42
Ben Eater
Рет қаралды 2,9 МЛН
rust runs on EVERYTHING (no operating system, just Rust)
14:29
Low Level
Рет қаралды 218 М.
Compiler in 191 Lines of C | tinyc
6:16
Cheesed Up
Рет қаралды 24 М.
Real Time Operating Systems (RTOS) - Nate Graff
35:02
White Hat Cal Poly
Рет қаралды 55 М.
How does an OS boot? //Source Dive// 001
50:22
Low Byte Productions
Рет қаралды 432 М.
How Linux Kernel Prints Text on Screen
12:46
Nir Lichtman
Рет қаралды 78 М.
OCCUPIED #shortssprintbrasil
0:37
Natan por Aí
Рет қаралды 131 МЛН