- Joined
- Jun 8, 2007
- Messages
- 1,985
- Reaction score
- 490
Operating systems have good ways of making the computer sleep without running up the CPU.
The only problem is, in the world of today, we need to make programs that work on multiple operating systems, and many programs use sleep() functions- which is system-dependent (on the low-level).
Programming languages like Java do this for you, but for languages such as C, it's a bit more difficult to make it work for *nix and windows. (Not really, though- if you think about it, we're lucky there's only 2 main branches of Operating Systems- which makes this quite simple indeed!)
You can make your own Sleep function using a few system-independent libraries and it might work, but more often then not you'll find yourself running up the CPU without a sleeper.
The cure to this problem is using a macro to determine if the operating system is (or is not) windows-based. The macro will determine for which system the program should target during compilation.
Here it is:
The function msleep (millisecond-sleep) depends on one of two functions (and one of two system-dependent libraries):
windows.h -> Sleep( int milliseconds )
unistd.g -> usleep( int microseconds )
If the macro finds that one of the windows-dependent APIs exists, the OS must have windows.h, and thus must have Sleep().
If not, the OS is not windows, so the only other thing we need to worry about is Unix, unix-based, or unix-like- all have the unix library unistd.h.
Hm.. The world would be a better place if DOS were unix-based, wouldn't it?
Enjoy!
The only problem is, in the world of today, we need to make programs that work on multiple operating systems, and many programs use sleep() functions- which is system-dependent (on the low-level).
Programming languages like Java do this for you, but for languages such as C, it's a bit more difficult to make it work for *nix and windows. (Not really, though- if you think about it, we're lucky there's only 2 main branches of Operating Systems- which makes this quite simple indeed!)
You can make your own Sleep function using a few system-independent libraries and it might work, but more often then not you'll find yourself running up the CPU without a sleeper.
The cure to this problem is using a macro to determine if the operating system is (or is not) windows-based. The macro will determine for which system the program should target during compilation.
Here it is:
PHP:
#include <stdio.h>
// Macro to Figure Operating System (Windows/*nix)
#if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
#include <windows.h>
#define IS_WIN
#else
#include <unistd.h>
#endif
// Multi-compatible microsecond Sleep Function.
void msleep( int s )
{
#if defined(IS_WIN)
Sleep(s); // If Windows, use Sleep() from windows.h
#else
usleep( s * 1000 ); // If *nix, use usleep() from unistd.h
#endif
}
int main()
{
printf("Hey bud, just stopping by for a second!\n");
msleep( 1000 ); // Sleep for one second
printf("C-ya later!\n");
return 0;
}
windows.h -> Sleep( int milliseconds )
unistd.g -> usleep( int microseconds )
If the macro finds that one of the windows-dependent APIs exists, the OS must have windows.h, and thus must have Sleep().
If not, the OS is not windows, so the only other thing we need to worry about is Unix, unix-based, or unix-like- all have the unix library unistd.h.
Hm.. The world would be a better place if DOS were unix-based, wouldn't it?
Enjoy!