Search code examples
operating-systemrtoscontext-switch

In an RTOS can a process be prempted when running in kernel space


I understand that a process can't be preempted when running in kernel space. If that's true, in an RTOS how is responsiveness guaranteed (If a system call takes more time to execute) ? Can a do a context switch for a process even when it is running in kernel space ?


Solution

  • Whether a process can be preempted or not depends on the design of process states. On linux you have UNITERUPTIBLE_SLEEP where the process sleep still a certain service it had requested for is completed. (Ex read) . It may so happen that the service never got completed and the process never got a signal and was infinitely sleeping (holding on to system resources).

    In case of an RTOS, this approach is not justified as the resources(Ex memory) is scarce. So, a process can never be in UNITERUPTILBE STATE. Hence the OS can send a signal to the process sleeping/waiting for some service, prematurely. So the OS has a greater control on process allowing it control system resources as well.

    As pointed by @Levente Kurusa, the OS can send a signal to the process after an interval, making it either yeild control to the OS,( time sliced execution which ensure good response time) or kill it if it was waiting for a service for long time.