4

A process may ask the OS to pause it for 0.5 seconds, but how does that work?

The CPU doesn't simply stop running for that 0.5 second as other programs work well simultaneously, so how do we pause running an application for some time?

yanpas
  • 512
  • 5
  • 14
  • A pause in a programming language results in assembly (`NOOP` or `NOP`) no operation instruction. What problem are you trying to solve? – Ramhound Jan 17 '15 at 18:27
  • @Ramhound I'm interested why when program is paused it doesn't consume resources (Processor time). Proccessor is to count nanoseconds till the pause is finished. Or is there timer mechanism in CPU? – yanpas Jan 17 '15 at 20:49
  • 1
    It doesn't consume resources because like I said. It performs a NOOP operation. You might want to do some research on what that actually means. It also means the operating system simply switches to another task until it gets interrupted to back to the other task. – Ramhound Jan 17 '15 at 21:15
  • related: https://stackoverflow.com/questions/175882/whats-the-algorithm-behind-sleep – Ciro Santilli OurBigBook.com Dec 18 '19 at 15:46

1 Answers1

4

In a multitasking operating system the OS does not have to do anything as extreme as outright stopping the processor from running at all or filling the time that that process was going to be using with other instructions. It can do something infinitely simpler. It can simply not schedule the task to be run at all.

This is the whole point of a multitasking operating system. If multiple tasks all need to run at the same time then the OS swaps one onto the processor, lets it run for a period of time, then swaps the next task in and carries on until all the tasks that need to run have done their jobs.

When a task signals that it wants to sleep for a period of time or to wait for a keypress what it is effectively telling the operating system is don't bother swapping me onto the processor unless time X has passed or don't bother running me unless you have event X waiting for me to action it.

Basically the OS has a long list of tasks that need to run, and against each one would be a "waiting for event" descriptor. Any tasks that are ready to run would be able to be scheduled and any that are not would simply be skipped, simple as that.

If there are literally no tasks at all to run on the processor in a particular time slice then the operating system schedules an "Idle" task which will actually HaLT the CPU and tell it not to execute instructions until it gets woken up, usually by an interrupt of some sort. Using this method of scheduling time to "do no work" fits in well with the paradigm of a multitasking operating system as well.

In order to wake up again the CPU gets interrupt "ticks" via the RTC (real-time clock) and additionally can set up the HPET (High Precision Event Timer) to fire an interrupt at a specific time interval. This allows the OS to halt the CPU for very precise lengths of time and then kick the CPU out of sleep to begin processing tasks again.

Mokubai
  • 89,133
  • 25
  • 207
  • 233
  • When I mention NOOP instruction I mean a literal block of code that pauses the program. But your answer is of course correct – Ramhound Jan 18 '15 at 00:51
  • @Ramhound I hadn't meant for my answer to focus on that point and I was not meaning it to sound like I was trying to correct you. I meant it in the same way you did, that there is no need to do anything so extreme as filling in the time with code or outright halting processor. I saw a NOOP in the comments and it stuck in my mind for some reason. I'll clarify that first paragraph shortly. – Mokubai Jan 18 '15 at 10:18
  • We are saying similar if not identical things. You didn't have to correct anything really. You did something I couldn't answer the question by providing additional information. – Ramhound Jan 18 '15 at 11:20