Purpose of while(1); statement in C

前端 未结 12 1647
终归单人心
终归单人心 2020-12-08 00:53

What purpose does while(1); serve ? I am aware while(1) (no semicolon) loops infinitely and is similar to a spinlock situation. However I do not

12条回答
  •  感动是毒
    2020-12-08 01:11

    I assume that the while(1); is not associated with a do loop...

    The only semi-useful implementation of while(1); I have seen is a do-nothing loop waiting for an interrupt; such as a parent process waiting for a SIGCHLD, indicating a child process has terminated. The parent's SIGCHLD handler, after all child processes have terminated, can terminate the parent thread.

    It does the trick, but wastes a lot of CPU-time. Such a usage should perhaps perform some sort of sleep to relinquish the processor periodically.

提交回复
热议问题