如何在不调用"system()"的情况下使用C ++或Qt关闭Linux? [英] How to shutdown Linux using C++ or Qt without call to "system()"?

查看:284
本文介绍了如何在不调用"system()"的情况下使用C ++或Qt关闭Linux?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在UI上按下关闭按钮时关闭嵌入式Linux.我知道可以通过调用system来做到这一点:

I want to shutdown an Embedded Linux when a close button is pushed on the UI. I know I can do it with a call to system:

system("shutdown -P now");

参考:链接

但是不建议使用system,我想知道C ++中是否还有另一种方法可以做到这一点(如果还有使用Qt的特定方法,我也想知道这一点,尽管一般的C ++方法更重要).

But knowing that using system is not advised, I'ld like to know if there is another way in C++ to do this (if there is also a specific way to do this using Qt, I'ld also like to know it although a general C++ method is more important).

推荐答案

在Linux上,您可以调用重新启动系统调用来关闭电源,停止或重新启动. 以下代码段显示了如何关闭计算机电源,但是请注意,它当然仅适用于Linux:

On Linux you can call the reboot system call to poweroff, halt, or reboot. The following snippet shows how to poweroff a machine, but note that it will of course only work on Linux :

#include <unistd.h>
#include <linux/reboot.h>

int main() {
    reboot(LINUX_REBOOT_MAGIC1, 
           LINUX_REBOOT_MAGIC2, 
           LINUX_REBOOT_CMD_POWER_OFF, 0);
}

当然,您将需要足够的特权才能使用此syscall.

Of course, you will need sufficient privileges to use this syscall.

这篇关于如何在不调用"system()"的情况下使用C ++或Qt关闭Linux?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆