What is the best way to implement the busy loop ? correct me if i am wrong ?
while (1); // obviously eats CPU.
while (1) { sleep(100); } // Not sure if it is the correct way ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To do an infinite wait, for a signal (what else) there is the
pause()system call. You’ll have to put it in a loop, as it returns (always with -1 and errno set to EINTR) every time a signal is delivered:As a curious note, this is, AFAIK the only POSIX function documented to always fail.
UPDATE: Thanks to dmckee, in the comments below,
sigsuspend()also fails always. It does just the same aspause(), but it is more signal-friendly. The difference is that it has parameters, so it can fail withEFAULTin addition toEINTR.