In traditional embedded programming, we will give a delay function like so:
for(i=0;i<255;i++) for(j=0;j<255;j++);
In the microprocessor’s view, is this how the sleep() function works?
Is there an alternative for the sleep() function in C?
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.
Alternatives depend in what you are trying to do and what OS you are on.
If you just want to waste time, then these might help:
On most unix-type systems you’ll find a ‘usleep’ function, which is more or less like sleep with greater resolution. Be careful with that one because it usually can not sleep for just one microsecond.
On some unix-type systems, the select system call can be used with all file descriptor sets zero in order to get a fairly accurate sub-second wait.
On windows systems, you have Sleep, which is pretty much the same, but taking a number of milliseconds.
In a multi-tasking operating system, a sleep function can sometimes be given 0 as a parameter. This generally causes the function to give up it’s timeslice, but be re-scheduled immediately if no other task is ready to run.