【Linux】c++创建新线程执行sh脚本

【Linux】c++创建新线程执行sh脚本,第1张

【Linux】c++创建新线程执行sh脚本
  • 前情提要 【Linux】profile.d加入循环shell脚本,重启登录黑屏
  • system函数可以运行.sh脚本, 需要包含stdlib.h
  • 因为我的.sh脚本里有循环, 不能放在主程序, 否则会阻塞, 所以要开一个线程去执行
  • pthread不是linux标准库, 编译时需要链接库lpthread
// g++ test.cpp -lpthread   // 可能需要链接这个库
#include
#include    //  新建线程需要
#include  //  运行.sh文件
#include  //  sleep函数
using namespace std;

void ntpdate_thread()
{
    system("./start.sh");
}

int main(int argc, char const *argv[])
{
    thread t(ntpdate_thread);
    cout << "exit\n" << endl;
    while (1)
    {
        cout << "main thread" << endl;
        sleep(1);
    }
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/3002921.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-27
下一篇 2022-09-27

发表评论

登录后才能评论

评论列表(0条)

保存