Execute the command stops when os is idle

What is an idle CPU process doing?


Looking at the source of I found the use of the clone flag, which is described there as:

On closer inspection, I found that, while the kernel doesn't handle this flag, it actually uses it during startup to create inactive processes (which should all have PID 0) for each CPU on the computer. ie a machine with 8 CPUs has at least 7 (see question below) such processes "running" (pay attention to the quotation marks).

Now that leads me to a couple of questions about what this "idle" process actually does. I'm assuming the NOP operation will run continuously until the time frame expires and the kernel assigns a real process to run or reassign the idle process (when the CPU is not in use). However, that's a complete guess. In order to:

  1. Are such idle processes generated on a machine with, for example, 8 CPUs 7? (and a CPU is held by the kernel itself while no userspace work is done?)

  2. Is the idle process really just an infinite stream of NOP operations? (or a loop that does the same).

  3. Is CPU usage simply calculated from how long the idle process has been on the CPU and how long it has been absent for a given period of time?


PS Chances are a lot of this question stems from the fact that I don't fully understand how a CPU works. That is, I understand the assembly, the timeframes and the interrupts, but I don't know how a CPU, for example, uses more or less power depending on how it is executed. I would be grateful if someone could enlighten me too.




Reply:


The idle task is used for process billing and to reduce energy consumption. Under Linux, an inactive task is created for each processor and bound to this processor. If no other process can run on this CPU, the inactive task is scheduled. The time spent on inactive tasks is recorded in tools such as (The operating time is calculated differently.)

Unix always seems to have had an idle loop (but not necessarily an idle task itself, see Gilles' answer), and even in V1 it used an instruction that halted the processor until an interrupt occurred (it stood for "wait for") "). Some other operating systems used busy loops, DOS, OS / 2, and earlier versions of Windows in particular. CPUs have been using this type of" wait "statement for some time to reduce their energy consumption and heat generation. They can have several implementations of idle tasks, for example in the Linux kernel: the basic one just calls The other implementations handle various bugs or inefficiencies ( z. B. on some CPUs instead of).

All of this is completely independent of idle states in processes when they are waiting for an event (I / O, etc.).







If there is no process to be planned in a process planner's textbook design (that is, if all processes are blocked and waiting for input), the planner waits for a processor interruption. The interrupt can indicate input from a peripheral (user action, network packet, read from disk completed, etc.) or it can be a timer interrupt that sets a timer in a process.

The Linux scheduler has no special code for when there is nothing to be done. Instead, it encrypts the undone case as a special process, the idle process. The idle process is only scheduled if no other process can be scheduled (it actually has an infinitely low priority). The inactive process is in fact part of the kernel: it is a kernel thread, which is a thread that executes code in the kernel rather than code in a process. (More specifically, there is such a thread for each CPU.) When the idle process is running, it performs the wait-for-interrupt operation.

How the interruption wait works depends on the capabilities of the processor. The most basic processor design is just a busy loop.

The processor will forever execute a branch instruction that does nothing. Most modern operating systems only do this when they're running on a processor that doesn't have anything better on them, and most processors have something better. Ideally, the processor should be turned off rather than just using energy to heat the room. So the kernel is executing code that tells the processor to shut down itself, or at least most of the processor. At least one small part must remain switched on, the interrupt controller. When a peripheral device triggers an interrupt, the interrupt controller sends a wake-up signal to the main processor (part of it).

In practice, modern CPUs like Intel / AMD and ARM have many complex settings for power management. The operating system can estimate how long the processor will remain in idle mode and select different energy-saving modes based on this. The modes offer different tradeoffs between idle power consumption and the time it takes to enter and exit idle mode. On some processors, the operating system can also slow the processor clock speed if it determines that processes are not consuming much CPU time.







No, an inactive task does not waste CPU cycles. The scheduler simply does not select an inactive process to run. An idle process is waiting for an event to occur so that it can continue. For example, it can wait for input in a system call.

The kernel is not a separate process, by the way. Kernel code is always executed in the context of a process (well, with the exception of the special case of a kernel thread), so it is incorrect to say that "and a CPU is held by the kernel itself while no userspace work is done" .




We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.