Hi people, I just wondered how sleep function can be implemented...
Prototype:
void sleep(int milliseconds)
Thanx....
sleep
Re:sleep
Well, sleep() is not much use in a single tasking environment as its task is to halt execution of the currently running task/thread and switch to another ready one.
Basically, sleep works as follows:
1. Process calls sleep(time)
2. Kernel changes the state of the task from running to sleeping.
3. It sets a timeout value based on the number of times per second the timer interrupt occurs.
4. It then switches to another ready task and executes away.
5. On the next timer interrupt it decrements the timeout value. If this value is now zero (i.e. the sleep time has passed) it sets the task to ready and will execute again shortly.
So, without a scheduler or multi-tasking it won't do very much!!
If you just wan't to freeze execution, well there are various ways of doing this, including counting timer interrupts and using a counting loop.
Daryl.
Basically, sleep works as follows:
1. Process calls sleep(time)
2. Kernel changes the state of the task from running to sleeping.
3. It sets a timeout value based on the number of times per second the timer interrupt occurs.
4. It then switches to another ready task and executes away.
5. On the next timer interrupt it decrements the timeout value. If this value is now zero (i.e. the sleep time has passed) it sets the task to ready and will execute again shortly.
So, without a scheduler or multi-tasking it won't do very much!!
If you just wan't to freeze execution, well there are various ways of doing this, including counting timer interrupts and using a counting loop.
Daryl.