Thread: Is sleep system call ?

  1. #1
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154

    Is sleep system call ?

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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    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.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ch4 View Post
    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
    sleep is not a standard C library function, so yes, it is system dependent. If this is Linux or Unix, then it is as I described about. By the way, none of Linux or Windows system calls are part of the C library standard functions.

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

  5. #5
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Quote Originally Posted by ch4 View Post
    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 proof that sleep() isn't this . . .

    Run a program that calls sleep(60) and watch your processor usage. If it was looping furiously, then it would be taking all your processing time as it stepped through each iteration of the loop.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interrupted System Call
    By Seiki in forum C Programming
    Replies: 2
    Last Post: 01-28-2009, 11:55 PM
  2. nanosleep() -system call does some confusing things
    By jtk in forum Linux Programming
    Replies: 5
    Last Post: 08-30-2007, 04:15 AM
  3. What system call use the CreateThread Function of borland?
    By amfm0777 in forum Windows Programming
    Replies: 2
    Last Post: 06-20-2007, 05:41 AM
  4. Multiple types in lists, vectors or arrays.
    By megatron09 in forum C++ Programming
    Replies: 20
    Last Post: 08-31-2006, 01:54 PM
  5. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 08:21 AM