Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在linux中创建守护进程 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 内部主(空){ /*我们的进程ID和会话ID*/ pid_t pid,sid; /*离开父进程*/ pid=fork(); 如果(pid==0){//子进程 CreateSocket();//此函数将从客户端接收数据 } 其他的 { exit(exit_SUCCESS);//退出父进程 } /*更改文件模式掩码*/ 乌马斯克(0); /*在此处打开所有日志*/ /*为子进程创建新SID*/ sid=setsid(); 如果(sid_C_Linux_Process_Timer_Daemon - Fatal编程技术网

在linux中创建守护进程 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 内部主(空){ /*我们的进程ID和会话ID*/ pid_t pid,sid; /*离开父进程*/ pid=fork(); 如果(pid==0){//子进程 CreateSocket();//此函数将从客户端接收数据 } 其他的 { exit(exit_SUCCESS);//退出父进程 } /*更改文件模式掩码*/ 乌马斯克(0); /*在此处打开所有日志*/ /*为子进程创建新SID*/ sid=setsid(); 如果(sid

在linux中创建守护进程 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 内部主(空){ /*我们的进程ID和会话ID*/ pid_t pid,sid; /*离开父进程*/ pid=fork(); 如果(pid==0){//子进程 CreateSocket();//此函数将从客户端接收数据 } 其他的 { exit(exit_SUCCESS);//退出父进程 } /*更改文件模式掩码*/ 乌马斯克(0); /*在此处打开所有日志*/ /*为子进程创建新SID*/ sid=setsid(); 如果(sid,c,linux,process,timer,daemon,C,Linux,Process,Timer,Daemon,我的任务是通过套接字从客户端接收数据,并在后台运行计时器。因此,我创建了一个父进程和子进程,然后创建了后台进程,以便在后台运行。我调用了名为CreateSocket()的函数;在子进程中接收来自子进程中客户端的数据,而我在父进程中没有做任何事情(只是退出)。稍后调用守护进程在后台运行我的计时器任务 我的问题:如果我这样做,那么我的计时器任务将继续在后台运行,创建套接字将继续从客户端接收数据对某些进程进行后台监控的最简单方法是在程序开始附近使用库函数(内部基于fork和setid…)(例如,在创建

我的任务是通过套接字从客户端接收数据,并在后台运行计时器。因此,我创建了一个父进程和子进程,然后创建了后台进程,以便在后台运行。我调用了名为CreateSocket()的函数;在子进程中接收来自子进程中客户端的数据,而我在父进程中没有做任何事情(只是退出)。稍后调用守护进程在后台运行我的计时器任务


我的问题:如果我这样做,那么我的计时器任务将继续在后台运行,创建套接字将继续从客户端接收数据

对某些进程进行后台监控的最简单方法是在程序开始附近使用库函数(内部基于
fork
setid
…)(例如,在创建任何线程之前)。

如果您的应用程序属于以下类型之一:

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>

int main(void) {

        /* Our process ID and Session ID */
        pid_t pid, sid;

        /* Fork off the parent process */
        pid = fork();
        if (pid == 0) {             //child process
                CreateSocket();    //this function will recieve the data from the client
        }
        else
        {
         exit(EXIT_SUCCESS);   //exit the parent process
        }

        /* Change the file mode mask */
        umask(0);

        /* Open any logs here */        

        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
                /* Log the failure */
                exit(EXIT_FAILURE);
        }



        /* Change the current working directory */
        if ((chdir("/")) < 0) {
                /* Log the failure */
                exit(EXIT_FAILURE);
        }

        /* Close out the standard file descriptors */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);

        /* Daemon-specific initialization goes here */

        /* The Big Loop */
        while (1) {
           /* Do some task here ... */

           Timer();   // this will create a timer and call the task for every 2ms, 10ms
        }
   exit(EXIT_SUCCESS);
}
您想要使用npm的pm2包

它将通过运行pm2 start来监控您的应用程序

此外,虽然你的应用程序通常在1个内核上运行,但使用pm2,你可以非常轻松地在所有内核上运行你的应用程序,并且它将在内核之间实现负载平衡。如果这听起来对你来说太奇怪,那么很容易不实现负载平衡

这就是你要做的:

{
  ".sh": "bash",
  ".py": "python",
  ".rb": "ruby",
  ".coffee" : "coffee",
  ".php": "php",
  ".pl" : "perl",
  ".js" : "node"
}
然后:


完成!

如果我像上面那样做,那么我的计时器任务将继续在后台运行,而创建套接字将继续从客户端接收数据。您希望发生什么不同于此的情况?我希望我的计时器在后台运行,这就是您所说的。那么问题出在哪里?我没有客户端发送数据!!所以我是问你们-如果我像上面那样做,它会工作吗?我的套接字程序和计时器工作得很好。如果我只使用daemon并在其中调用我的计时器,那么我的计时器会在后台运行吗?你们能给我一个小例子吗?使用
npm
假设人们愿意使用
node.js
来管理这个任务。这是一个很大的飞跃在一个问题中用C作为唯一的语言。
npm install -g pm2

pm2 start app.js -i 0 --name "api" # load balance app on all cores

pm2 start app.js --name "api" # run on 1 core, like apps typically do
pm2 startup (creates a pm2 systemd or equivalent service)

pm2 save (remembers the apps pm2 is running and starts them on reboot)