Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

As to CPU scheduling or interrupt, will CPU finish executing its current instruction?

As we know, one high-level language instruction like "counter++" in C++ will be translated into several assembly langauge instructions.

My question is what about a single assembly language instruction? Is it true that some assembly language instruction will also be translated into several machine instructions that hardware can directly reads?

If positive, when thread/process scheduling occurs, is it true that context-switch(or other stuff needs to be done during scheduling) takes place as soon as CPU finishes executing its current machine instruction, even if there are several instructions left which form a single assembly language instruction together with the current one?

For example, one assembly language instruction A may be translated into machine instruction a1 ,a2, a3. Can context-switch happene between a2, a3?

corresponding machine instructions of a single assembly language instruction A:

a1 (executed) a2 (executing..) <--- next context switch is about to occur a3 (not executed yet) <--- Am I be able to execute before the next context switch?

My guess is a3 will not be executed because the hardware only knows its machine instructions and have no idea of assembly language. So each instruction is executed separatedly. Am I right?

like image 685
Eric Z Avatar asked Oct 12 '22 05:10

Eric Z


2 Answers

Actually, on quite a few machines, counter++ will translate to a single instruction (e.g., inc counter).

Pretty much the definition of assembly language is that what you're writing is simply mnemonics for the actual machine code, so one assembly instruction converts to one machine instruction.

Some CPUs (x86) are micro-coded, so what you enter as a single instruction is actually executed as a series of micro-ops. The exact mapping from instructions to micro-ops isn't usually documented (e.g., Intel did around the Pentium Pro/Pentium II time frame, but mostly doesn't any more).

Interrupts come in two varieties. Most will only be executed after the current machine instruction finishes execution. A few, however, can interrupt during the execution of a single instruction. A typical example would be attempting to access a page of memory that's not present. If you do this, the machine will interrupt the current instruction, and jump to the exception handler. The exception handler will normally page in the data for that address. When it returns, the data is present for the address. The instruction re-starts, and can successfully execute.

like image 177
Jerry Coffin Avatar answered Oct 15 '22 11:10

Jerry Coffin


Sometimes during assembly, the assembler might sneak in a NOP in order to pad or maintain alignment. Depends on the processors and all sorts of things. But pretty much assembly instructions translate 1:1 to machine instructions.

In general, the CPU will finish its current instruction and then respond to the interrupt. My only qualification there is that in some CPUs there are some instructions that can actually take quite a long time. For example, the Z80 has a block move instruction that can take a "long" time, especially in CPU terms, and x86 has its vector instructions, etc. So I don't know if those instructions are stopped in the middle and restarted later or if the CPU waits for them to finish.

But as for interrupts in general, yea they happen "between" machine instructions.

like image 42
Will Hartung Avatar answered Oct 15 '22 10:10

Will Hartung