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++) ;
Printable View
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
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