node ninja
node ninja

Reputation: 33036

What is the code for the idle process?

When the cpu isn't doing anything, it runs the idle process. I heard that this process looks for programs that are waiting in the queue so that the cpu can run them. Is that all it does? What does the code for it look like? I am also interested in knowing the file name of the system idle process in the various OSes.

Upvotes: 10

Views: 8065

Answers (5)

Umar Farooq
Umar Farooq

Reputation: 57

The idle process is for scheduler to run something when no process is executing. It depends on the OS particularly.

For simple understanding we can say idle process is a infinite loop, which will get scheduled when no process is running

Upvotes: 0

JdeBP
JdeBP

Reputation: 2192

The question contains several erroneous tacit assumptions. Here are some pointers:

  • It's not necessarily an idle process. It's an idle process on non-multithreading operating systems, but not on multithreading ones. Concentrating upon the process in the latter kind of operating system is concentrating upon the wrong thing. Although Microsoft Windows NT calls it an "Idle Process", and displays that in its Task Manager, the important mechanisms are the idle threads. The idle process is simply the process to which those threads belong for the sake of bookkeeping. (All threads must belong to a process.) There's no filename. (Processes don't even have names on many systems.)
  • Not all operating systems even have these idle processes/threads. On several older uniprocessor operating systems, idling the system when there was nothing to do was simply a special case in the dispatcher. This approach is problematic for multiprocessor operating systems (when one CPU is idling in a process/thread that another CPU wants to dispatch to), and is why the idea of a special process/thread that was always ready to run, one per processor in the system, so that the CPU could idle in its own private thread context, became the norm.
  • What an idle process/thread does is CPU-specific. The important quality of an idle process/thread is that it must always be ready to run. It must never block. But it can do whatever it likes in doing so. Usually "whatever it likes" means "as little as possible", however. The canonical idle thread is just an infinite loop: an unconditional branch instruction branching to itself. Several processor architectures provide equivalents to the x86 hlt instruction, the intent of which is in general terms to reduce the idling processor's use of the system bus (so that, of course, non-idle processors can use that bus bandwidth). So on many architectures the infinite loop repeatedly executes those instructions. Some processors can signal their "idle" state on the bus when they execute such instructions, which external hardware can recognize and act upon (such as by slowing bus clocks down and consuming less power, for example). Similarly, the idle instructions can cause the processors themselves to do things like clock-slowing and power-saving.
  • Low-level scheduling is not an "in-thread" thing. After all, it's the low-level scheduler, often called the dispatcher, that determines what thread to even run in the first place. It's medium-level and (sometimes, albeit rarely) high-level scheduling that are run within threads. A medium-level scheduler can be, for example, a thread that wakes up every N seconds and scans the thread table recomputing the thread priorities of dynamic-priority threads. Or it could be a thread that, every N seconds, pushes entire process segments out to disk and pulls them back again, depending from process priority and recent CPU usage. (This latter type is rare on modern paging operating systems, but it existed on segment-swapping operating systems.)

Upvotes: 13

Wooble
Wooble

Reputation: 90027

The Idle process doesn't do anything; the OS itself is responsible for scheduling processes to run. The Idle process itself just loops HLT instructions. (source: wikipedia )

Upvotes: 5

mouviciel
mouviciel

Reputation: 67879

In space critical embedded systems, the idle process is used to scrub memory in order to check whether cosmic rays have introduced bit flips.

Upvotes: 7

Tobu
Tobu

Reputation: 25436

The OS runs the scheduler whenever a process is at the end of its time slice, and whenever a process has performed a blocking operation. The scheduler then picks the next process to run. On the platforms I know, it does not make sense to see the scheduler as a process.

Here is a little more about scheduling.

Upvotes: 2

Related Questions