Thread: Problem with reboot(), takes a few seconds before rebooting

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    Problem with reboot(), takes a few seconds before rebooting

    Hi,

    I'm trying to use a call to reboot() to reboot the system after my program completes, however it seems that it takes a few seconds before the reboot actually happens. Why is this so?

    My sample code is as follows:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <memory.h>
    #include <time.h>
    #include <ctype.h>
    #include <unistd.h>
    #include <linux/reboot.h>
    
    // Function prototypes
    int restart(int delay);
    int main(void);
    
    int restart(int delay)
    {
    	sleep(delay);
    	return reboot(LINUX_REBOOT_CMD_RESTART);
    }
    
    int main(void)
    {
    	printf("Rebooting...");
    	fflush(stdout);
    	restart(0);
    	return 0;
    }
    After the "Rebooting..." is printed out, it takes a few seconds before the system reboots itself, even though I've specified 0 second sleep. Why is this so?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is because "reboot" is not just rebooting the machine, but shutting down processes so that you will have a (more) clean boot when the system starts up again. For example, you may want any disk-caches to be flushed to disk, so that files are complete on disk - that's just one example.

    --
    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
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    the kernel has to shut down every running process in the system, then it has to shut down the daemons, which will entail flushing any virtual memory and disk caches, then it will actually shut down. Not every program will instantly halt execution when told to terminate, some have their own cleanup to do, which mostly involves disk I/O. Just be happy that it takes fewer than 30 seconds like my windows machine does, mostly because I have a lot of background applications running.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by abachler View Post
    the kernel has to shut down every running process in the system, then it has to shut down the daemons, which will entail flushing any virtual memory and disk caches, then it will actually shut down. Not every program will instantly halt execution when told to terminate, some have their own cleanup to do, which mostly involves disk I/O. Just be happy that it takes fewer than 30 seconds like my windows machine does, mostly because I have a lot of background applications running.
    Actually, none of that should happen. The kernel doesn't do teardown, init does that as it shifts to the reboot runlevel. A call to reboot() should actually cause an immediate reboot without the normal shutdown.

    init itself calls reboot() to do the actual reboot once everything is torn down.

    It does seem odd that it takes a few seconds. LINUX_REBOOT_CMD_RESTART should immediately restart, without even syncing the disks. (Which is an important point in itself -- if the disks don't get synced you'll probably lose data and possibly corrupt the filesystem). At the very least call sync() before calling reboot()
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    4
    Hmm, so which is it? Is it supposed to be an immediate reboot with no concern of running processes/daemons/etc? Or is there some cleaning up to do before an actual reboot?

    My program resides in a very simple Linux environment that does not even have init, in fact the init is simply a script that among other things calls the program itself.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by galapogos View Post
    Hmm, so which is it? Is it supposed to be an immediate reboot with no concern of running processes/daemons/etc? Or is there some cleaning up to do before an actual reboot?
    It is supposed to be an immediate reboot. In this case "immediate" seems to involve some kinds of cleanup but not the normal kind of teardown you'd see on a desktop system.

    You might try building a debug kernel and see what all is happening during the reboot.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple case statements
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 05-08-2006, 08:39 AM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. simple frontend program problem
    By gandalf_bar in forum Linux Programming
    Replies: 16
    Last Post: 04-22-2004, 06:33 AM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM