Writeing A Sleep Function
Writeing A Sleep Function
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?
- Love4Boobies
- Member

- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: Writeing A Sleep Function
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.eenceo2 wrote:Right now all I want is one for the OS itself not for processes.
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.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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: Writeing A Sleep Function
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.
- Love4Boobies
- Member

- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: Writeing A Sleep Function
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.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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: Writeing A Sleep Function
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.
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
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
The sleep function is meant to be used in the floppy driver so there are no problems with overheating.
Re: Writeing A Sleep Function
what do you mean? i dont know how to program floppies so im probably not qualified to answer this
Re: Writeing A Sleep Function
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.
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
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:
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.
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;
Gizmic OS
Currently - Busy with FAT12 driver and VFS
Currently - Busy with FAT12 driver and VFS
Re: Writeing A Sleep Function
I know pascal, so that's fine
I'll try to see if my pit works. I've been having trouble with it before.
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());
}
Re: Writeing A Sleep Function
Thanks! Finally I got my PIT working! 
- Firestryke31
- Member

- Posts: 550
- Joined: Sat Nov 29, 2008 1:07 pm
- Location: Throw a dart at central Texas
- Contact:
Re: Writeing A Sleep Function
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.eenceo2 wrote:I know pascal, so that's fine
I'll try to see if my pit works. I've been having trouble with it before.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()); }
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
Re: Writeing A Sleep Function
Hi,
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
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.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.
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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
-
pANZERNOOb
- Posts: 2
- Joined: Thu Jul 18, 2013 3:48 am
Re: Writeing A Sleep Function
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.
