开发者

An infinite loop executing in a single processor system

A process P1 is executing in infinite loop in a system which has only a single CPU. There are also other processes like P2, P3 which are waiting to gain the CPU, but are in wait queue as P1 is already executing.

The program is, something like:

int main( )
{
  while(1);
}

So, what will be the end result? Will the system crash?

Probable answer is, the CPU won't crash and other processes can execute in the CPU as because every process has a specific time slice, so after the time slice of P1 expires, other waiting processes can gain the CPU.

But again, how will the kernel (O/S) check that the time slice has expired, as because there is only one CPU and the process is running in infinite loop? Because if checking has to happe开发者_如何学Cn, it needs CPU to do that, and the CPU is already occupied by process P1 which is executing in infinite loop.

So what happens in this case?


It really depends on what operating system and hardware you are using. Interrupts can transfer the execution of code to another location (interrupt handler). These interrupts can be software (code in the program can call these interrupt handlers) or hardware (the cpu receives a signal on one of its pins). On a motherboard you have something called a Programmable Interrupt Controller (PIC) which can generate a steady stream of interrupts (timer interrupts). The OS can use the timer interrupt handler to stop a running process and continue another one.

Again, it really depends on the OS, the hardware you are working with,... and without more specifics it's too general a question to give a definite answer.


Processor has something called interutps. OS (like windows) tell processor: - Use this process for X time and then tell me So procesor starts timer and works on process. When time pass procesor sends interupt and tell OS that time passed. OS now will decide wwich process will work next.

Hope this answers your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜