在linux下实现系统的重启,函数很多,一般常用:
system(“reboot”);
这都是调用shell命令实现的,这样的话,依赖于/sbin/reboot这个外部程序,还有直接利用库函数实现的,利用库函数实现的更为直接,shell肯定也是封装了这些函数实现的。
先上代码:
#include <unistd.h>
#include <sys/reboot.h>
int main()
{
sync(); // 同步磁盘数据,将缓存数据回写到硬盘,以防数据丢失[luther.gliethttp]
return reboot(RB_AUTOBOOT);
}
代码很简单,下面是官方文档对库函数reboot的说明:
#include <unistd.h>
#include <sys/reboot.h>
int reboot(int cmd);
DESCRIPTION
The reboot() call reboots the system, or enables/disables the reboot keystroke (abbreviated CAD, since the default is Ctrl-Alt-Delete;
The cmd argument can have the following values:
LINUX_REBOOT_CMD_HALT
(RB_HALT_SYSTEM, 0xcdef0123; since Linux 1.1.76). The message "System halted." is printed, and the system is halted. Control is
given to the ROM monitor, if there is one. If not preceded by a sync(2), data will be lost.
LINUX_REBOOT_CMD_KEXEC
(RB_KEXEC, 0x45584543, since Linux 2.6.13). Execute a kernel that has been loaded earlier with kexec_load(2). This option is
available only if the kernel was configured with CONFIG_KEXEC.
LINUX_REBOOT_CMD_POWER_OFF
(RB_POWER_OFF, 0x4321fedc; since Linux 2.1.30). The message "Power down." is printed, the system is stopped, and all power is
removed from the system, if possible. If not preceded by a sync(2), data will be lost.
LINUX_REBOOT_CMD_RESTART
(RB_AUTOBOOT, 0x1234567). The message "Restarting system." is printed, and a default restart is performed immediately. If not pre‐
ceded by a sync(2), data will be lost.
注:这个就是我们平常用的reboot