Is sleep a system call or fixed function ?
If sleep isn't system call then how does it work ?
A huge loop doing nothing ?
Code:for(i=0;i<seconds*10000;i++) ;
Is sleep a system call or fixed function ?
If sleep isn't system call then how does it work ?
A huge loop doing nothing ?
Code:for(i=0;i<seconds*10000;i++) ;
The answer to your question depends on where the sleep function you are referring to comes from. If it's the Unix sleep() or Windows Sleep() function you are talking about, then it is a system call [or a wrapper for a system call]. In other systems, it may be as you describe, a big loop [although your loop will certainly take less than one second in a modern system - 10000 loops is about 10000-20000 clock-cycles on a modern processor, which is about 5-10 microseconds, give or take a bit. That is, of course, if the compiler doesn't determine that the loop is not of any use, and just removes the entire loop. ]
The way it works in an OS that provides a system call to sleep, is that it figures out how long it needs to sleep, and puts processes that are sleeping in a list of "not runnable", with a count of how many timer ticks it is from this process until the sleep expires. When a timer interrupt happens in the system (say once every millisecond), the tick count is decremented. If the tick count is zero, the process is moved to the runnable list.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thanks matsp
I'm referring to sleep inside a code. Inside code does it depends on the OS ?
Code://code here printf("Wait a second\n"); sleep(1); printf("Ok"); //code here
Last edited by ch4; 11-28-2008 at 05:54 AM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Do as I say, not as I do . . .
Experimentation is the essence of programming. Just remember to make a backup first.
"I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF
Questions posted by these guidelines are more likely to be answered.
Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.