C程序fork进程导致PHP执行不退出

/*********************************************************************
 *                C程序fork进程导致PHP执行不退出
 * 说明:
 *     由于测试的GPIO程序需要持续运行,而主进程需要处理其他事务但退出时
 * 由于子线程未结束导致PHP系统调用函数不退出,解决办法是双重fork(第一次
 * fork产生子进程用于kill掉让第二次fork出的子进程变成孤儿进程),并将最终
 * 的子进程转换为守护进程,从而不影响PHP获取主进程数据。
 *
 *                                   2017-8-16 深圳 龙华樟坑村 曾剑锋
 ********************************************************************/

一、参考文档: 
    1. Linux 守护进程的实现
        http://alfred-sun.github.io/blog/2015/06/18/daemon-implementation/

二、测试daemon Demo:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <unistd.h>
    #include <sys/param.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    // 守护进程初始化函数
    void init_daemon()
    {
        pid_t pid;
        int i = 0;
    
        if ((pid = fork()) == -1) {
            printf("Fork error !\n");
            exit(1);
        }
        if (pid != 0) {
            exit(0);        // 父进程退出
        }
    
        setsid();           // 子进程开启新会话,并成为会话首进程和组长进程
        if ((pid = fork()) == -1) {
            printf("Fork error !\n");
            exit(-1);
        }
        if (pid != 0) {
            exit(0);        // 结束第一子进程,第二子进程不再是会话首进程
        }
        chdir("/tmp");      // 改变工作目录
        umask(0);           // 重设文件掩码
        for (; i < getdtablesize(); ++i) {
           close(i);        // 关闭打开的文件描述符
        }
    
        return;
    }
    
    int main(int argc, char *argv[])
    {
        int fp;
        time_t t;
        char buf[] = {"This is a daemon:  "};
        char *datetime;
        int len = 0;
        //printf("The NOFILE is: %d\n", NOFILE);
        //printf("The tablesize is: %d\n", getdtablesize());
        //printf("The pid is: %d\n", getpid());
    
        // 初始化 Daemon 进程
        init_daemon();
    
        // 每隔一分钟记录运行状态
        while (1) {
            if (-1 == (fp = open("/tmp/daemon.log", O_CREAT|O_WRONLY|O_APPEND, 0600))) {
              printf("Open file error !\n");
              exit(1);
            }
            len = strlen(buf);
            write(fp, buf, len);
            t = time(0);
            datetime = asctime(localtime(&t));
            len = strlen(datetime);
            write(fp, datetime, len);
            close(fp);
            sleep(60);
        }
    
        return 0;
    }

 

posted on 2017-08-16 17:28  zengjf  阅读(371)  评论(0编辑  收藏  举报

导航