Writeing A Sleep Function

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
eenceo2
Posts: 6
Joined: Sat Dec 27, 2008 9:56 am

Writeing A Sleep Function

Post 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?
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Writeing A Sleep Function

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
yemista
Member
Member
Posts: 300
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: Writeing A Sleep Function

Post 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.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Writeing A Sleep Function

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
eenceo2
Posts: 6
Joined: Sat Dec 27, 2008 9:56 am

Re: Writeing A Sleep Function

Post 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.
User avatar
yemista
Member
Member
Posts: 300
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: Writeing A Sleep Function

Post 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?
eenceo2
Posts: 6
Joined: Sat Dec 27, 2008 9:56 am

Re: Writeing A Sleep Function

Post by eenceo2 »

The sleep function is meant to be used in the floppy driver so there are no problems with overheating.
User avatar
yemista
Member
Member
Posts: 300
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: Writeing A Sleep Function

Post by yemista »

what do you mean? i dont know how to program floppies so im probably not qualified to answer this
eenceo2
Posts: 6
Joined: Sat Dec 27, 2008 9:56 am

Re: Writeing A Sleep Function

Post 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.
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: Writeing A Sleep Function

Post 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.
Gizmic OS
Currently - Busy with FAT12 driver and VFS
eenceo2
Posts: 6
Joined: Sat Dec 27, 2008 9:56 am

Re: Writeing A Sleep Function

Post 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.
eenceo2
Posts: 6
Joined: Sat Dec 27, 2008 9:56 am

Re: Writeing A Sleep Function

Post by eenceo2 »

Thanks! Finally I got my PIT working! =D>
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Writeing A Sleep Function

Post 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.
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?
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Writeing A Sleep Function

Post 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
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

Post 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.
Post Reply