How to implement software task switching? - operating-system

I have read in intel manuals, AMD manuals and in other OS development forums how to implement task switching, but i still have some doubts on the use of TSS (Task State Segment).
I have read that TSS are used in hardware task switching, but (if I'm not wrong) are also used in the implementation of user space.
So, there are the questions that i have:
The TSS are implemented and used in hardware task switching? Or also in software task switching?
If TSS are not used in software task switching, can I program my own struct that stores all the useful data ?
I want to know the best strategies and the best practises about task switching.
Thank you.

Related

How do real-time game engines work on Windows?

I come from an embedded background, where hard real-time is taken quite seriously. For me (fast) real time is tricky on Linux, and ludicrous on Windows, jet video games seem to be doing just fine.
Obviously, since it's only soft real time, and the frequency is (relatively) low with 60+ Hz, the simple solution is to render as fast as possible and hope for the best. But are there any advanced techniques in use that further enhance the responsiveness on Windows?
Some concrete questions:
Is there a way to handle Windows pre-empting at unfavourable moments? How do you share work on multiple cores when some of them are doing something else?
Is IO-latency in Windows deterministic? Can (should) you pressure the scheduler to context switch ASAP?
How common are scheduling techniques like Earliest Deadline First / Rate Monotonic Scheduling (or others)? (build on top of the existing one of course)
Any insight would be welcome!
Edit:
I've looked through a few resources and have added my conclusions:
1.
Q: What happens when a time critical calculation gets pre-empted?
A: The thread should have higher priority so pre-emption is rare, and when it happens, the interruption should be brief (short interrupts or IO-operations).
Windows has the capability to move threads to other cores, thou it is recommended to keep them locked ( -> Thread Affinity).
2.
Q: Should IO-operations have a higher priority than the render loop?
A: Yes, Naughty Dog's engine gives IO-operation higher priority.
Q: What kind of scheduling is used?
A: In the presentation, a job system is used (similar to reactive programming).
It uses three priorities that are non-preemptive.
I recommend you read Modern Operating System from Ph.D Andrew Tanenbaum if you haven't read it. His book also have Unix and Windows architecture.
One most major thing: Game programming is different than Embedded programming
I had worked with Windows and Android (Unix-like). Not only Windows but also Android very hard rendering too meet hard real-time. One of our tricks to reduce CPU, GPU latency is using lower feature driver support for the visual effect (which mean if hardware not support good this new graphic API, we will use older API so this will make object less shape and less beauty) or if device is not support the feature and effect is not important, just remove this effect for that device. It will push FPS (frames per second) better.
Game engine is also difference between many graphic APIs, use Vulkan or DX12 (Windows) or Metal (Apple devices) is make more FPS but require newer hardware, because they are low-level APIs. It will give you better performance, more beautiful than OpenGL or DX11, but old APIs is support on older devices.
And 3 your questions, I will answer with my knowledge (may be there is a better answer from other):
I also have a small question: Why don't you use thread, atomic, mutex C++ 11 APIs for share tasks with multi-core CPU? It easier for thread programming, cross-platform and no need to schedule anything.
Windows has classic Win32 thread APIs SetThreadPriority for set the relative priority of a thread. This API isn't recommended for Windows Runtime (Modern Windows concept from Microsoft for Windows 8 and later) which recommended using thread-pool concept instead (use modern API will automation parallel tasks with multi-core, which is better than manually config use classic API).
In Game engine, there is a presentation of Christian Gyrling from Naughty Dog talking concept Parallel and Sergey Makeev from Roblox implement open source TaskScheduler base this concept in Win32 APIs and Pthread APIs. Jeff Preshing from Ubisoft also have a presentation using C++ 11.
What did you mean about IO-latency? Keyboard, mouse or Disk IO? I will edit answer if you edit your question more clear
I can't find a document or anything similar Earliest Deadline First / Rate Monotonic Scheduling on Windows. May be this is limited by prevent kernel mode scheduling from Microsoft.

What is porting in RTOS [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I recently started learning about RTOS’s and came across the subject “porting”. I always tought that I could copy the RTOS files from github, paste it in my project and use it without any problems on any device. So I am a little confused about porting. I have two questions:
What is porting in terms of RTOS?
Why do I need porting of RTOS’s?
Thanks in advance,
Arjon
Porting is the act of taking software written to run on one system and modifying it so it runs on another system. From Wikipedia,
In software engineering, porting is the process of adapting software for the purpose of achieving some form of execution in a computing environment that is different from the one that a given program (meant for such execution) was originally designed for
And,
When code is not compatible with a particular operating system or architecture, the code must be [ported] to the new system.
And,
Porting is also the term used when a video game designed to run on one platform, be it an arcade, video game console, or personal computer, is converted to run on a different platform
In other words, you have some software you want to run on a real-time operating system (RTOS). That software was not written to run on RTOS, and will fail unless it is modified accordingly. It must be ported to RTOS before it can run. If you don't port the software, it will not run on the new system.
The nature and scope of those modifications depends on the software and the specific RTOS.
An RTOS interacts directly with processor and platform hardware. When an RTOS is designed to run of different hardware, you necessarily have target specific code to adapt the generic code to the specific hardware and processor architecture.
As a minimum an RTOS requires a system timer generating an interrupt, and to perform context switch it requires direct access to the processor registers in order to restore the context, set the stack pointer for the task to switch to and effect a jump by setting the program counter - that cannot be done with generic code since it requires intimate hardware knowledge.
The "port" of an RTOS requires code written specifically for the target platform including code to handle the timer interrupt, select which timer will be used as the system timer, and effect a context switch. Often much of this code must be written in assembler for direct register access which is necessarily architecture specific.
In addition to the kernel scheduler, depending on the scope of the RTOS in terms of the services it provides, there may be code required to deal with target specific MMU/MPU support and I/O drivers for filesystems, networking etc.

real-time operating system scheduler

I have to create scheduler for RTOS. How should I start?
What OS is good for writing scheduler? What operating systems should I chose?
How can I test scheduler, debug code?
If you are willing to learn something by writing a real time scheduler, then you should start with reading this. Through this, you can learn different kinds of scheduler design and their applications. You can start with writing a small co-operative scheduler.
I will recommend you to use freeRTOS(as it is free and simple) first before jumping to write your own scheduler. There are user manual available online for free. Download them and go through them. Then, you can develop a application using freeRTOS APIs. Through this, you will understand the necessity of features provided by a RTOS (like process synchronization, priority of tasks, inter-process communication) and it's scheduler.
You may need to buy a development board support by freeRTOS or there is also windows port available online. Then, you can start writing your own scheduler implementation. The freeRTOS source code which is available online for free can be used to aid your development. FreeRTOS is designed to be small and simple. The kernel itself consists of only three C files. To make the code readable, easy to port, and maintainable, it is written mostly in C, but there are a few assembly functions included where needed (mostly in architecture-specific scheduler routines).
Also, POSIX C library can also be used to understand RTOS and features required by any real time system. You can develop application using POSIX library to understand RTOS. Later, you can switch to any other RTOS.

PIC Microcontroller Operating System [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I heard it is possible to write an Operating System, using the built in bootloader and a kernel that you write, for the PIC microcontroller. I also heard it has to be a RTOS.
Is this true? Can you actually make an operating system kernel (using C/C++) for PIC?
If yes to 1, are there any examples of this?
If yes to 1, would you need any type of software to create the kernel?
Is Microchip the only company that makes PIC microcontrollers?
Can the PIC microcontroller be programmed on a mac?
Thanks!
Yes, you can write your own kernel (I have written 2 of my own). Yes you can write it in C for PIC. If you want pre-emptive scheduling, then you're going to have a real tough time avoiding assembly completely when writing the context switch. On the other hand, you can easily write a cooperative kernel purely in C (I have done this myself). (Note that creating an operating system is not a simple task... I'd get your feet wet in pure C first, then USE an OS or two, then try creating one.)
An excellent example of this is FreeRTOS. It has preexisting ports (i.e. MPLAB projects that run without any modification on the Explorer16 demo board) for PIC24F, PIC33F, and PIC32MX (as well as 20-some odd other official ports for other vendors' devices). PIC18F is supported but it's not pretty...
You only need MPLAB to create the kernel (free from Microchip). It can work interchangably with C and assembly. Depending on the processor, there are free versions of their C30 and C32 compilers to go with MPLAB.
PIC is a type of microcontroller, and is a trademark of Microchip. Many other companies make microcontrollers and call them something else (e.g. AVR, LPC, STM32).
Yes, the new version of MPLAB X is supported on Mac, Linux and Windows.
I suggest you check out FreeRTOS.
I second the vote for FreeRTOS; we use this all the time on PIC24 designs. The port works well and doesn't use a ton of memory.
Microchip supports many third party RTOSes.
Most have free demo projects that you can download, build in MPLAB, and program onto an Explorer16 board very easily. You can then experiment to your heart's content.
PIC is not a single architecture. PIC10 differs considerably from PIC24, though they and every PIC in between share some commonality. The MIPS based PIC32 on the other hand is an entirely different architecture. So you have to be clear about what PIC you are referring to.
An OS on a PIC does not have to be and RTOS, but that would be ideally suited to the application domain the devices are used in, so anything that were not real-time capable would be somewhat less useful.
There are many RTOS ports already for PIC.
There is nothing special about about a kernel scheduler in terms of development method, C and in most cases a little assembler are all that are necessary - no special tools. You could use 100% assembler if you wished, and this might be necessary to get the smallest/fastest code, but only if your assembler knowledge is better than the compiler's.
PIC is specific to Microchip, though Parallax SX is more or less a clone. Unlike ARM for example, Microchip do not licence the architecture to third-party chip manufacturers or IP providers. No one would want it in any case IMO; there are far better architectures. ARM Cortex-M is particularly suited to RTOS kernel implementation, and AVR's instruction is designed for efficient translation from C source code. Even the venerable 8051 is well suited to RTOS implementation; its eight register banks make context switches very fast (for up to eight threads), and like ARM, 8051 architecture devices are available from multiple manufacturers.
The hardware stack of PIC 18F CPU is only 31 bytes long. Other RAM memory cannot be used as stack. Even 8051 IRAM memory has 128 byte of stack. I have done RTOS for 8051, ARM and PIC 18F, and feels not good at PIC 18F. If the RAM(16K to 64K) of PIC32 can be used as stack, if the stack pointer is 16 bit long, it will be much better than PIC18F types. Does any one knows that?

Real time system concept proof project

I'm taking an introductory course (3 months) about real time systems design, but any implementation.
I would like to build something that let me understand better what I'll learn in theory, but since I have never done any real time system I can't estimate how long will take any project. It would be a concept proof project, or something like that, given my available time and knowledge.
Please, could you give me some idea? Thank you in advance.
I programm in TSQL, Delphi and C#, but I'll not have any problem in learning another language.
Suggest you consider exploring the Real-Time Specification for Java (RTSJ). While it is not a traditional environment for constructing real-time software, it is an up-and-coming technology with a lot of interest. Even better, you can witness some of the ongoing debate about what matters and what doesn't in real-time systems.
Sun's JavaRTS is freely available for download, and has some interesting demonstrations available to show deterministic behavior, and show off their RT garbage collector.
In terms of a specific project, I suggest you start simple: 1) Build a work-generator that you can tune to consume a given amount of CPU time; 2) Put this into a framework that can produce a distribution of work-generator tasks (as threads, or as chunks of work executed in a thread) and a mechanism for logging the work produced; 3) Produce charts of the execution time, sojourn time, deadline, slack/overrun of these tasks versus their priority; 4) demonstrate that tasks running in the context of real-time threads (vice timesharing) behave differently.
Bonus points if you can measure the overhead in the scheduler by determining at what supplied load (total CPU time produced by your work generator tasks divided by wall-clock time) your tasks begin missing deadlines.
Try to think of real-time tasks that are time-critical, for instance video-playing, which fails if tasks are not finished (e.g. calculating the next frame) in time.
You can also think of some industrial solutions, but they are probably more difficult to study in your local environment.
You should definitely consider building your system using a hardware development board equipped with a small processor (ARM, PIC, AVR, any one will do). This really helped remove my fear of the low-level when I started developing. You'll have to use C or C++ though.
You will then have two alternatives : either go bare-metal, or use a real-time OS.
Going bare-metal, you can learn :
How to initalize your processor from scratch and most importantly how to use interrupts, which are the fastest way you have to respond to an externel event
How to implement lightweight threads with fast context switching, something every real-time OS implements
In order to ease this a bit, look for a dev kit which comes with lots of documentation and source code. I used Embedded Artists ARM boards and they give you a lot of material.
Going with the RT OS :
You'll fast-track your project, and will be able to learn how to fine-tune a RT OS
You may try your hand at an open-source OS, such as Linux or the BSDs, and learn a lot from the source code
Either choice is good, you will get a really cool hands-on project to show off and hopefully better understand your course material. Good luck!
As most realtime systems are still implemented in C or C++ it may be good to brush up your knowledge of these programming languages. Many realtime systems are also embedded systems, so you might want to play around with a cheap open source one like BeagleBoard (http://beagleboard.org/). This will also give you a chance to learn about cross compiling etc.