Smalltalk: Can a single object block the entire system by entering an infinite loop?

眉间皱痕 提交于 2021-01-04 07:23:06

问题


Since Smalltalk scheduling is non-preemptive, processes must explicitly yield or wait on a semaphore

Does this mean that one object entering an infinite loop could stall the entire system?

the loop can be interrupted at any time. Even an atomic loop like [true] whileTrue can be interrupted before "executing" the true object

By what can it be interrupted?


回答1:


It is the Virtual Machine who may interrupt the image. Under a normal execution flow, the VM is basically sending messages, one after the other. However, certain events may impact the natural flow of execution by interrupting it, if needed. While concrete examples may change from one dialect to the other, these usually correspond to OS events that need to be communicated to the image for their consideration.

An interruption may also be caused if the VM is running out of memory. In this case it will interrupt the image requesting it to do garbage collection.

Loops are interesting because they have the semantics of regular messages, so what happens is that the block of code inside the loop is evaluated (#value & friends) every time the loop repeats. So, you should think of loops as regular messages. However, this semantics is usually optimized so the re-evaluation is not explicitly requested by a Smalltalk message. In that case the VM will check for interruptions before executing the block. Thus, if you run

[true] whileTrue

before designating the object true as the current receiver (in this case, of no message) the VM will check whether there is any interrupt to pay attention to (in the same way it checks for interruptions before starting to execute any given method).

Most dialects implement some "break" keystroke that would produce a "halt" and open a debugger for the programmer to recover manual control.

Note that, depending on the dialect, an interruption may only consist of the signaling of a semaphore. This will have the effect of moving the waiting process (if any) to the ready queue of the ProcessScheduler. So, the intended "routine" may not run immediately but change to the ready state for the next time there is a process switch (at that level of priority).

The last example that comes to mind is the StackOverflow exception (no pun intended), where the VM realizes that it is running out of stack space and interrupts the image by signaling an exception.

You may also think of the #messageNotUnderstood: as an interruption generated by the VM when it realizes that an object has received a message for which is has no implementation. In this case, the natural flow will change so that the object will receive the message #messageNotUnderstood: with the actual message as the argument.

One more thing. Whether a loop may or may not stall the system depends on the priority of the process it is running. If the loop is running with low priority an interruption that awakes a process of higher priority will take precedence and be run while the loop is sent to sleep. By the same logic, if your endless loop runs in a process at a higher priority no interruption will stop it.




回答2:


Yes, it is super simple to just run

[ true ] whileTrue: [  ]

and you won't be able to do anything else.

Pharo has a "ripcord" when you press comand + . on Mac. For Windows or Linux it's either alt or control. This action should halt the thing that you are running and allow you to intervene.



来源:https://stackoverflow.com/questions/65258157/smalltalk-can-a-single-object-block-the-entire-system-by-entering-an-infinite-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!