简体   繁体   English

c程序以进程为背景而永不死?

[英]c program to run process as background and never dies?

int daemon()
{
if (daemon(1, 1) < 0) /* Keep the same working directory and pipes */
{


             makeTimer("First Timer", &firstTimerID, 2, 2);   //2ms
             makeTimer("Second Timer", &secondTimerID, 10, 10);    //10ms
             makeTimer("Third Timer", &thirdTimerID, 100, 100);  //100ms
    return 1;
              }
    }




 int main()
{
daemon();
}


           return 0;

         }

I created a timer and the timer is calling the task for every 2ms, 10ms and 100ms. 我创建了一个计时器,计时器每2ms,10ms和100ms调用一次任务。 I want to run the timer in the background and it should never die. 我想在后台运行计时器,它永远不会死。 Could anyone give me some ideas in c program to run the task in the background for linux operating system. 任何人都可以在c程序中给我一些想法,在linux操作系统的后台运行任务。 I want to make this three calls to run in the back ground : 我想让这三个电话在后台运行:

   makeTimer("First Timer", &firstTimerID, 2, 2);   //2ms
   makeTimer("Second Timer", &secondTimerID, 10, 10);    //10ms
   makeTimer("Third Timer", &thirdTimerID, 100, 100);  //100ms

Try the daemon() function in C from unistd.h . unistd.h尝试C语言中的daemon()函数。 It makes it easy to detach yourself from the terminal and run in the background. 它使您可以轻松地从终端分离并在后台运行。

It's as simple as 这很简单

if (daemon(1, 1) < 0) /* Keep the same working directory and pipes */
{
     perror("daemon");
     return 1;
}

and just put that in your main function 并把它放在你的main功能

You could call your script using the nohup command. 您可以使用nohup命令调用脚本。 Simply run your script on the command line as follows nohup <myscript> 只需在命令行上运行脚本,如下所示nohup <myscript>

It may be what you really want is to create a cron job, however. 但是,您可能真正想要的是创建一个cron作业。 Check docs for your linux distro to find out more. 检查您的Linux发行版的文档以了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM