栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

在linux下实现系统的重启,函数很多,一般常用:

system(“reboot”);

这都是调用shell命令实现的,这样的话,依赖于/sbin/reboot这个外部程序,还有直接利用库函数实现的,利用库函数实现的更为直接,shell肯定也是封装了这些函数实现的。

先上代码:

#include 
#include 
int main()
{
    sync(); // 同步磁盘数据,将缓存数据回写到硬盘,以防数据丢失[luther.gliethttp]
    return reboot(RB_AUTOBOOT);
}

代码很简单,下面是官方文档对库函数reboot的说明:

#include
#include
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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/680815.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号