How to shutdown or restart a PC from a pthread-process

This is a discussion on How to shutdown or restart a PC from a pthread-process within the C Programming forums, part of the General Programming Boards category; How to restart or shutdown the whole computer with a command inside a pthreads-process? exec ("reboot"); ?? exec("shutdown now");??...

  1. #1
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251

    How to shutdown or restart a PC from a pthread-process

    How to restart or shutdown the whole computer with a command inside a pthreads-process?

    exec ("reboot"); ??
    exec("shutdown now");??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The Windows API to do stuff like this is ExitWidnowsEx.

    It is not, normally considered polite to restart/shut down the machine programmatically tho'.

    --
    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
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Be sure to have the user's permission first. I would be quite annoyed if a program rebooted my machine without my permission.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by matsp View Post
    The Windows API to do stuff like this is ExitWidnowsEx.

    It is not, normally considered polite to restart/shut down the machine programmatically tho'.

    --
    Mats
    thanks but I forgot to specify I need it for Linux!! I need a "clean" shutdown of course!!
    I am talkinng of an industrial PC in an industrial application, not for the use of several users so SW shutdown and restart are needed!
    The main process (multipthread) is written by me and a colleague and only a few other server daemons will be running (like ssh, ftp etc.)
    Last edited by mynickmynick; 07-31-2008 at 01:21 AM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    system("init 1") will reboot the system. See http://unixhelp.ed.ac.uk/CGI/man-cgi?init+8

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

  6. #6
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    Quote Originally Posted by matsp View Post
    system("init 1") will reboot the system. See http://unixhelp.ed.ac.uk/CGI/man-cgi?init+8

    --
    Mats
    Thank you

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Another (fast+forced+manual) way is (kernel must be compiled with magic sysrq keys):
    Code:
    FILE *f=fopen("/proc/sysrq-trigger", "w");
    if (f)
    {
        // s = sync
        // u = remount all read-only
        // b = reboot
        // o = halt
        fprintf(f, "s\n");
    
        ...
        // sleep between steps
        ...
    
        fclose(f);  // reached ???
    }
    So, basicly, you write 's' (sync), wait some time, write 'u' (remount ro), wait some time, and then write 'b' or 'o'. ('o' didn't test, but docs says it's for shutdown).

    EDIT: assuming, of course, you have all the system under control, and can afford this approach...
    Last edited by rasta_freak; 08-07-2008 at 05:45 AM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    I suggest doing a "man init" to be sure. IIRC, "init 0" halts the system, "init 6" reboots, and "init 1" gets the system back to single-user mode.

  9. #9
    Alessio Stella
    Join Date
    May 2008
    Location
    Italy, Bologna
    Posts
    251
    thank you!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  2. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Shutdown Pc Timer
    By miranda in forum Windows Programming
    Replies: 10
    Last Post: 12-31-2005, 12:36 AM
  5. How to shutdown (or Restart) a pc with C++
    By Unregistered in forum C++ Programming
    Replies: 27
    Last Post: 08-05-2005, 12:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21