守候进程main函数模版及linux守护写法
来源:互联网 发布:重庆 青少年编程 编辑:程序博客网 时间:2024/05/17 02:06
本文旨在提供守护程序(包括windows,linux)的main函数的模版。
此模版内同时包含Linux守护进程的编写方法。
代码如下
#include <stdio.h>#include <string.h>#include <stdlib.h>#ifdef WIN32#else#include <signal.h>#include <unistd.h>#include <sys/wait.h>#include <sys/time.h>#include <sys/resource.h>#endif//WIN32#define VERSION_NO "1.0"int RunProgram(){ printf("i am running\n"); // do while loop return 0;}#ifdef WIN32int RunDaemon(){ //1 run windows daemon //2 use CreateMutex to make sure program have only one instance //3 RunProgram RunProgram(); return 0;}#elseint RunDaemon(){ struct sigaction act; struct rlimit rl; // pid_t processID = 0; int status = 0; int pid = 0; // ignore signal act.sa_flags = 0; act.sa_handler = SIG_IGN; (void)::sigaction(SIGPIPE, &act, NULL); (void)::sigaction(SIGHUP, &act, NULL); (void)::sigaction(SIGINT, &act, NULL); (void)::sigaction(SIGTERM, &act, NULL); (void)::sigaction(SIGQUIT, &act, NULL); (void)::sigaction(SIGALRM, &act, NULL); //set max file handle count rl.rlim_cur = 10240; rl.rlim_max = 10240; setrlimit(RLIMIT_NOFILE, &rl); //set core dump size rl.rlim_cur = RLIM_INFINITY; rl.rlim_max = RLIM_INFINITY; setrlimit(RLIMIT_CORE, &rl); //run in background if (daemon(0,0) != 0) { exit(-1); } while(true) { processID = fork(); if(processID > 0) { status = 0; while(status==0) { pid =::wait(&status); int exitStatus = WEXITSTATUS(status); if(WIFEXITED(status) && pid > 0 && status != 0) { if(exitStatus == -1) { printf("child exit\r\n"); exit(EXIT_FAILURE); } printf("[RunDaemon] - WIFEXITED\r\n"); break; } if(WIFSIGNALED(status)) { printf("[RunDaemon] - WIFSIGNALED\r\n"); break; } if(WIFSTOPPED(status)) { printf("[RunDaemon] - WIFSTOPPED\r\n"); break; } if(pid == -1 && status == 0) { continue; } if(pid > 0 && status == 0) { printf("[RunDaemon] - status == 0\r\n"); exit(EXIT_SUCCESS); } printf("[RunDaemon] - EXIT_FAILURE\r\n"); exit(EXIT_FAILURE); } } else if(processID == 0) { break; } else { exit(EXIT_FAILURE); } sleep(1); } if(processID != 0) { exit(EXIT_SUCCESS); } else { RunProgram(); } return 0;}#endif//WIN32int main(int argc, char *argv[]){ bool bExit = false; if ( (argc >= 2) && (strcmp(argv[1], "-v") == 0) ) { printf("MyProgram %s %s %s\r\n", VERSION_NO, __DATE__, __TIME__); bExit = true; } if (!bExit) { RunDaemon(); } return 0;}
0 0
- 守候进程main函数模版及linux守护写法
- linux 守候进程介绍
- Linux守候进程
- 一个Linux守候进程例子
- 守护进程的写法
- Linux进程控制及守护进程
- Linux进程控制及守护进程
- 【Linux】守护进程及守护进程的创建
- linux守护进程原理及创建详解
- Linux守护进程 及编程实现
- linux守护进程原理及创建详解
- linux守护进程原理及创建详解
- Linux守护进程设计规范及python实现
- Linux守护进程设计规范及python实现
- linux守护进程及系统启动项
- Linux守护进程设计规范及python实现
- Linux守护进程设计规范及python实现
- linux守护进程原理及创建详解
- 推荐:《软件开发者路线图:从学徒到高手》 ApprenticeshipPatterns:Guidance for the Aspiring Software Craftsman
- 多线程实验
- windows 守护进程编写模板
- Linux 学习笔记_12_Windows与Linux文件共享服务_1.1_--Samba(上)
- Node.js学习笔记(4、events模块)
- 守候进程main函数模版及linux守护写法
- [LeetCode]24.Swap Nodes in Pairs
- UVA - 1291 Dance Dance Revolution
- 【树状数组】hdu 1541 Stars
- directx 使用环境配置
- ocx编写及注意点
- Linux中select IO多路复用机制
- DLL编写及注意点
- Python模块功能在实际应用方案中的相关功能的介绍