Could anyone explain to me how process states are saved and later restored in a real time os for a micro controller?
I figured out the local variables etc are stored in predefined memory space and the is stored for later use, or something like that. But I have no idea how the data is being restored.
But to begin with, how are processes stored in memory? And how can I have any influence on this?
I like to do make a simple program that executes a process and a certain point in time it suspends this process and starts another process and later it resumes the first process. I just want to know how this works.
A preemptive OS like Windows does context switching simply by saving the state of the running thread. I believe this is basically the processor register values. All variables and such are left untouched in the virtual address space of the process.
Can you meddle with this? I would say NO.
So how to accomplish what you want? Ignoring the fact that this is probably a terrible, terrible design, I'd suggest that you investigate to see if you can use SuspendThread() for Windows.
I don't have suggestions for other OS' as I only program for Windows.
Just about every common embedded OS (pre-emptive or not) will behave the same way. When execution switches from one process to another process, all of the process-specific registers for the old process are saved in a location dedicated to that process (typically called a TCB (task control block) or PCB (process control block)), and then the process-specific registers for the new process are loaded from the new process' TCB/PCB.
One of the key registers saved/restored is a stack pointer, as most of the the process/thread-specific data is stored on the stack for that thread/process.
The differences between a "real-time" OS and something like Windows is in the consistency/predictability of responsiveness. Windows goes through the same register save/restore steps, but it may also have to reload portions of the new process into memory, update process priorities, etc.
Ok, so just save all local data of the function.
But you do you do this? How is the current state of the function stored in memory? And where do you need to resume code execution, how is this done?