Linux下C程序调用库函数实现重启

在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员如山石

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值