• 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 !
    ");
                exit(1);
            }
            if (pid != 0) {
                exit(0);        // 父进程退出
            }
        
            setsid();           // 子进程开启新会话,并成为会话首进程和组长进程
            if ((pid = fork()) == -1) {
                printf("Fork error !
    ");
                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
    ", NOFILE);
            //printf("The tablesize is: %d
    ", getdtablesize());
            //printf("The pid is: %d
    ", getpid());
        
            // 初始化 Daemon 进程
            init_daemon();
        
            // 每隔一分钟记录运行状态
            while (1) {
                if (-1 == (fp = open("/tmp/daemon.log", O_CREAT|O_WRONLY|O_APPEND, 0600))) {
                  printf("Open file error !
    ");
                  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;
        }
  • 相关阅读:
    hbase删除标记和注意事项
    马上拥有一台自己的云服务器
    在 Ubuntu 开启 GO 程序编译之旅
    在 Ubuntu 上安装 Protobuf 3
    功能设计中技术人员之惑
    关于数据可视化页面制作
    快速响应请求浅谈
    自定义一个代码耗时计数工具类
    一种极简的异步超时处理机制设计与实现(C#版)
    百度地图API图标、文本、图例与连线
  • 原文地址:https://www.cnblogs.com/zengjfgit/p/7374516.html
Copyright © 2020-2023  润新知