Page 1 of 1
Writeing A Sleep Function
Posted: Sat Dec 27, 2008 10:06 am
by eenceo2
I've been looking to write a sleep function for a while. Right now all I want is one for the OS itself not for processes. One major issue seams to be the difference between C compilers. I am using Microsoft's Visual C++ compiler. Often I see commands like asm volatile which don't work like they do in other compilers. Does anyone have any suggestions on how to write it or any source code?
Re: Writeing A Sleep Function
Posted: Sat Dec 27, 2008 10:18 am
by Love4Boobies
eenceo2 wrote:Right now all I want is one for the OS itself not for processes.
I'm not sure I get what you're after. Stopping everything that's going on? That's simple: just turn off the scheduler for a while. Or do you have a multi-threaded kernel that you need to implement sleep functions for? Cause that's pretty much the same as sleep functions for user applications' threads: put the current thread on the sleep queue and do a context switch.
One major issue seams to be the difference between C compilers. I am using Microsoft's Visual C++ compiler. Often I see commands like asm volatile which don't work like they do in other compilers.
As far as I can see it, it has nothing to do with the compiler you're using. Again, maybe I didn't understand what you want in the first place. Volatile functions are not "optimized" in any way by the compiler, it just dumps your assembly in the binary.
Re: Writeing A Sleep Function
Posted: Sat Dec 27, 2008 10:20 am
by yemista
why would you want the OS to go to sleep? As far as I know, sleep is called on a process that needs to wait for something, and it sets its status to sleep, and swaps it out with a runnable process.
Re: Writeing A Sleep Function
Posted: Sat Dec 27, 2008 10:24 am
by Love4Boobies
yemista wrote:why would you want the OS to go to sleep? As far as I know, sleep is called on a process that needs to wait for something, and it sets its status to sleep, and swaps it out with a runnable process.
If the kernel is multithreaded, it makes sense in some situations. Also, for singlethreaded kernels: maybe a process makes a system call to get some data that needs to come over the network, but it didn't yet, so the thing is put to sleep. After the thread pool does its job, the thread that gathered the data and put it in memory would wake up the first thread. It's just an example, though - I'm not saying that in this case things couldn't be handled in a better way.
Re: Writeing A Sleep Function
Posted: Sat Dec 27, 2008 10:32 am
by eenceo2
My operating system is a single process system. I need to implement sleep so I can write a floppy driver.
Basically you can think of my operating system as a single program. calling sleep pauses execution for a certain amount of time. This is also used if I want to slowly write out to the screen so people can read it before it is covered up by the next "page" of text. Sleep in my OS will be like sleep in C++ or Pascal, when writing a regular program.
Re: Writeing A Sleep Function
Posted: Sat Dec 27, 2008 10:46 am
by yemista
I dont know much about floppys, but if thats what you are doing, couldnt you just have the driver be a function, and when you call it, it will execute the floppy code, and once that is done, it will just return into the OS? Since its single process system, cant you implement it as a function, so that the OS will effectivley be "sleeping" while waiting for the floppy function to return?
Re: Writeing A Sleep Function
Posted: Sat Dec 27, 2008 10:51 am
by eenceo2
The sleep function is meant to be used in the floppy driver so there are no problems with overheating.
Re: Writeing A Sleep Function
Posted: Sat Dec 27, 2008 10:52 am
by yemista
what do you mean? i dont know how to program floppies so im probably not qualified to answer this
Re: Writeing A Sleep Function
Posted: Sat Dec 27, 2008 11:04 am
by eenceo2
All I need is the sleep function right now.
Here is another example:
printf("Welcome to the slide show");
sleep(2000); // wait for 2 seconds
cls(); //clears the screen
printf("slide1...");
sleep(4000); // wait 4 seconds
...
void sleep(int milli);
All I need to know for this topic is how to wait milli milliseconds before the cpu executes the next command.
Re: Writeing A Sleep Function
Posted: Sat Dec 27, 2008 11:05 am
by System123
Ok this is easy.
First make sure you have set the PIT up and have enabled hardware interrupts. Then you create a function like this.
EG: Assuming PIT is set at 100Hz
- Get current clock ticks ie(221)
- Add on the time to sleep for ie 4sec = 400 ticks
- while loop until current ticks = then variable.
Here is my code in pascal:
Code: Select all
procedure Sleep(Ticks : LongWord);
var
ETicks : LongWord = 0;
begin
ETicks := Ticks + TimerTicks;
While ETicks > TimerTicks do ;
end;
I can try convert that to C for you if you want. I have never used C but I have read a lot of it.
Re: Writeing A Sleep Function
Posted: Sat Dec 27, 2008 11:22 am
by eenceo2
I know pascal, so that's fine
Code: Select all
void Sleep(int Ticks) // maybe some other data type like long?
{
int ETicks = 0;
ETicks = Ticks + get_tick_count();
do
{
// wait
}while(ETicks > get_tick_count());
}
I'll try to see if my pit works. I've been having trouble with it before.
Re: Writeing A Sleep Function
Posted: Sat Dec 27, 2008 12:18 pm
by eenceo2
Thanks! Finally I got my PIT working!

Re: Writeing A Sleep Function
Posted: Sat Dec 27, 2008 12:27 pm
by Firestryke31
eenceo2 wrote:I know pascal, so that's fine
Code: Select all
void Sleep(int Ticks) // maybe some other data type like long?
{
int ETicks = 0;
ETicks = Ticks + get_tick_count();
do
{
// wait
}while(ETicks > get_tick_count());
}
I'll try to see if my pit works. I've been having trouble with it before.
I personally would throw an asm 'hlt' in that do{}while() loop, just because. I'm used to the GBA and TI-83+ where a 'hlt' (or equivalent) would cause the CPU to suck up less power, extending battery life, but I'm sure there's some reason to do it nowadays on a PC.
Re: Writeing A Sleep Function
Posted: Sun Dec 28, 2008 12:21 am
by Brendan
Hi,
Firestryke31 wrote:I personally would throw an asm 'hlt' in that do{}while() loop, just because. I'm used to the GBA and TI-83+ where a 'hlt' (or equivalent) would cause the CPU to suck up less power, extending battery life, but I'm sure there's some reason to do it nowadays on a PC.
On 80x86 the HLT instruction does exactly the same thing (e.g. less power, less heat, less fan noise, better battery life). There's also other low power states that can reduce power usage more, but they're typically a pain in the neck to use.
Also, you can set the timer to generate an IRQ when it's needed (e.g. maintain a sorted list of times when the timer IRQ is needed, and program the timer dynamically in "one shot" mode); rather than setting the timer to a fixed frequency (where the timer takes the CPU out of it's HLT state regularly for no particular reason). This can give better precision (more accurate timing) and better for power usage...
Cheers,
Brendan
Re: Writeing A Sleep Function
Posted: Mon Oct 05, 2015 2:07 am
by pANZERNOOb
Regardless if you're writing a multi-threaded kernel or a monotasking kernel I would suggest coding an asynchronous kernel mode specific sleep function(ksleep) to deal with situations where the kernel and/or drivers need a delay to function properly(Floppy and ATA drivers) as well as a second sleep system call that works synchronously with usermode programs that puts a usermode program on standby for an approximate amount of time while it switches context/processes. In layman's terms write a ksleep function for the kernel and create a system call sleep function inside of the process scheduler for usermode delays.