C程序fork进程导致PHP执行不退出
2017-08-16 17:28
423 查看
/********************************************************************* * 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; }
相关文章推荐
- java main执行时如果外部资源没有关闭可能导致报错后程序不退出一直等待
- php进程管理--手动创建进程锁,防止重复执行某程序代码
- windows 批处理(bat)中执行程序后不等待直接退出(cmd中新进程执行程序)
- 关于fork&exec之进程的创建和可执行程序的加载过程
- php fork太多进程导致整体性能下降,mysql down掉的解决.
- exit--进程退出;wait--进程等待;execl--执行程序
- 多线程程序中fork容易导致fork出来的子进程中死锁(转载)
- 子进程中调用system命令执行openssl后,程序退出了,原因是SIGCHLD信号的处理
- socket监控进程,并对程序执行有关操作。
- 进程的创建与可执行程序的加载
- WINDOWS 命令行下执行PHP程序
- Android程序通过Apache服务器执行PHP程序连接Mysql数据库(重要!)
- 【Android】【转】Android程序退出彻底关闭进程的方法
- 进程的创建与可执行程序的加载
- 多进程导致Application中onCreate执行多次
- PHP中的异常处理对程序执行效率的影响
- 进程的创建与可执行程序的加载
- Python3 被 PHP 程序调用执行返回乱码问题
- aemon 进程为什么要fork两次,与后台运行程序区别?
- 内核源码阅读(七)新程序的启动与进程的退出