H
H
HamsterGamer2022-01-20 11:36:14
Operating Systems
HamsterGamer, 2022-01-20 11:36:14

What does "time quantum" mean for a context switch?

Hello everyone, I can’t understand something, everywhere about the description of how context switching works, it is said about the so-called time quantum. Here's a quote from the wiki: "The kernel may take control of a running process/thread when the time quantum expires." Well, here the question arises, and the quantum of time is how much? What if I don't have enough time? And can theoretically a situation arise that, due to the load, some process will only have time to restore the registers, and will not do anything useful, that is, there will be constantly zero work during this period of time? Or are there guarantees that at least for some one final operation, but this process will "move"?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2022-01-20
@HamsterGamer

A time quantum is the time during which the OS allows a thread (process) to run continuously. After the time has elapsed, the flow is replaced by another. When it is the turn of this thread (process) to be executed again, it will again be given 1 time quantum. So don't worry about running out of time - time will always be given while the process is running.
The quantum of time in different operating systems is different, usually it is something in the range of 1-10 ms. In Linux, the quantum is dynamic, calculated based on the priority of a process - the higher the priority of a process, the more the kernel lets it work.
Do not forget that processes often themselves fall into the waiting mode and thereby prematurely complete their time quantum. This often happens, for example, when waiting for I/O operations to complete, or if a thread tries to lock a mutex, but it is already occupied, etc.

can theoretically a situation arise that, due to the load, some process will only have time to restore the registers, and will not do anything useful, that is, there will be constantly zero work during this period of time?

Due to the high load in the system, there may be many processes that have the same priority for execution, and among them it will be necessary to share all available cores and time. Naturally, if there are a lot of such processes, then it may take a long time until the queue reaches the process again after the preemption. This situation means a lack of resources. But, nevertheless, the processes will be executed (as long as there is enough virtual memory).
If a process has a low priority, but there are always higher priority processes running on the system, then there is a chance that the low priority process will never get a processor to run.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question