C函数关闭linux系统
发布时间:2020-12-16 07:24:11 所属栏目:百科 来源:网络整理
导读:我正在开发C函数以使用以下方式关闭我的嵌入式 Linux系统(Ubuntu). #include stdlib.hint main() { system("shutdown -P now"); return 0;} 这种方法是安全的吗?如果不是,有什么更好更安全的方法可以执行相同的任务吗? 解决方法 man reboot(2) #include un
我正在开发C函数以使用以下方式关闭我的嵌入式
Linux系统(Ubuntu).
#include <stdlib.h> int main() { system("shutdown -P now"); return 0; } 这种方法是安全的吗?如果不是,有什么更好更安全的方法可以执行相同的任务吗? 解决方法
man reboot(2)
#include <unistd.h> #include <sys/reboot.h> int main () { sync(); // If reboot() not preceded by a sync(),data will be lost. setuid(0); // set uid to root,the running uid must already have the // appropriate permissions to do this. reboot(RB_AUTOBOOT); // note,this reboots the system,it's not as return(0); // graceful as asking the init system to reboot. } 预先系统化,你有时也可以逃脱: int main() { sync(); kill(1,SIGTERM); return 0; } 这种方法在程序在单个shell下运行的嵌入式系统中更为普遍,但杀死initd也是有效的.请注意,在使用systemd / upstart的较新GNU / Linux上,systemd会忽略SIGTERM. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |