Uninterruptable process in Windows(or Linux)?

前端 未结 3 702
时光说笑
时光说笑 2020-12-05 12:12

Is there any way to make a program that cannot be interrupted (an uninterrupted program)? By that, I mean a process that can\'t be terminated by any signal, kill comma

相关标签:
3条回答
  • 2020-12-05 12:25

    You can catch pretty-much any signal or input and stay alive through it, the main exception being SIGKILL. It is possible to prevent that from killing you, but you'd have to replace init (and reboot to become the new init). PID 0 is special on most Unixes, in that it's the only thing that can't be KILL'd.

    0 讨论(0)
  • 2020-12-05 12:35

    On Linux, it is possible to avoid being killed by one of two ways:

    1. Become init (PID 1). init ignores all signals that it does not catch, even normally unblockable ones like SIGSTOP and SIGKILL.
    2. Trigger a kernel bug, and get your program stuck in D (uninterruptible wait) state.

    For 2., one common way to end up in D state is to attempt to access some hardware that is not responding. Particularly on older versions of Linux, the process would become stuck in kernel mode, and not respond to any signals until the kernel gave up on the hardware (which can take quite some time!). Of course, your program can't do anything else while it's stuck like this, so it's more annoying than useful, and newer versions of Linux are starting to rectify this problem by dividing D state into a killable state (where SIGKILL works) and an unkillable state (where all signals are blocked).

    Or, of course, you could simply load your code as a kernel module. Kernel modules can't be 'killed', only unloaded - and only if they allow themselves to be unloaded.

    0 讨论(0)
  • 2020-12-05 12:37

    Well, possibly one can write a program which doesn't respond for most signals like SIGQUIT, SIGHUP etc. - each kind of "kill" is actually a kind of signal sent to program by kernel, some signals means for the kernel that program is stuck and should be killed. Actually the only unkillable program is kernel itself, even init ( PID 1 ) can be "killed" with HUP ( which means reload ).

    Learn more about signal handling, starting with kill -l ( list signals ) command.

    Regarding Windows ( basing on "antivirus" tag ) - which actually applies to linux too - if you just need to run some antivirus user is unable to skip/close, it's permission problem, I mean program started by system, and non-administrative user without permission to kill it, won't be able to close/exit it anyway. I guess lameusers on Windows all over the world would start "solving" any problems they have by trying to close antivirus first, just if it would be possible :)

    0 讨论(0)
提交回复
热议问题