使得守护进程一次执行一个
2017-02-02 23:47
399 查看
转载自:http://www.cnblogs.com/Alarm/p/6212973.html
转载理由,本打算自己写一篇,看着大家写的都不错故转之
1.守护进程
守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件。
2.让程序只运行一次
如果让程序只运行一次,有很多方法,此处的一种方法是创建一个名字古怪的文件(保证不跟系统文件或其他文件重名),判断文件存在则让程序不再运行且提示程序正在运行,如果不存在则可以运行。
3.测试代码
此代码额外添加了系统LOG,记录操作的信息。
转载理由,本打算自己写一篇,看着大家写的都不错故转之
1.守护进程
守护进程(Daemon)是一种运行在后台的特殊进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件。
2.让程序只运行一次
如果让程序只运行一次,有很多方法,此处的一种方法是创建一个名字古怪的文件(保证不跟系统文件或其他文件重名),判断文件存在则让程序不再运行且提示程序正在运行,如果不存在则可以运行。
3.测试代码
此代码额外添加了系统LOG,记录操作的信息。
1 #include <stdio.h> 2 #include <unistd.h> 3 #include <stdlib.h> 4 #include <syslog.h> 5 #include <sys/types.h> 6 #include <sys/stat.h> 7 #include <fcntl.h> 8 #include <errno.h> 9 10 #define FILE "/var/cxl_single_file" 11 12 // 函数声明 13 void create_daemon(void); 14 void delete_file(void); 15 16 int main(int argc, char **argv) 17 { 18 int fd = -1; 19 20 // 打开LOG系统 21 openlog(argv[0], LOG_PID | LOG_CONS, LOG_USER); 22 23 // 打开文件,不存在则创建,存在则报错误提示 24 fd = open(FILE, O_RDWR | O_TRUNC | O_CREAT | O_EXCL, 0664); 25 if ( fd < 0 ) 26 { 27 28 if ( errno == EEXIST ) 29 { 30 printf("%s is running...\n", argv[0]); 31 _exit(-1); 32 } 33 } 34 syslog(LOG_INFO, "Create file success!"); 35 36 // 注册进程清理函数 37 atexit(delete_file); // Kill -9 PID 后 此函数没有运行 38 39 close(fd); 40 closelog(); 41 42 // 创建守护进程 43 create_daemon(); 44 while (1); 45 46 return 0; 47 } 48 49 // create_daemon() 创建守护进程 50 void create_daemon(void) 51 { 52 int i = 0, cnt = 0; 53 pid_t pid = -1; 54 55 pid = fork(); 56 if ( -1 == pid) 57 { 58 perror("fork"); 59 _exit(-1); 60 } 61 if ( pid > 0 ) 62 { 63 // 子进程等待父进程退出 64 _exit(0); 65 } 66 67 /* 此处是子进程执行 */ 68 // setsid() 子进程创建新的会话期,脱离控制台 69 pid = setsid(); 70 if ( -1 == pid ) 71 { 72 perror("setsid"); 73 _exit(-1); 74 } 75 76 // chdir() 调用此函数将当前工作目录设置为/ 77 chdir("/"); 78 79 // umask() 设置为0以取消任何文件权限屏蔽 80 umask(0); 81 82 // 关闭所有文件描述符 83 // sysconf() 获取当前系统中文件描述符的最大数目 84 cnt = sysconf(_SC_OPEN_MAX); 85 for ( i = 0; i < cnt; i++ ) 86 { 87 close(i); 88 } 89 90 // 将0、1、2定位到/dev/null 91 open("/dev/null", O_RDWR); 92 open("/dev/null", O_RDWR); 93 open("/dev/null", O_RDWR); 94 } 95 96 // 删除文件 97 void delete_file(void) 98 { 99 remove(FILE); 100 }
相关文章推荐
- php守护进程 加linux命令nohup实现任务每秒执行一次
- php守护进程,加linux命令nohup实现任务每秒执行一次
- 在VC中实现一次执行一个进程,如果同时打开两个,则报该程序已经运行
- php守护进程 加linux命令nohup实现任务每秒执行一次
- php守护进程,加linux命令nohup实现任务每秒执行一次
- UNIX环境高级编程学习之第十三章守护进程 - 初始化一个守护进程
- 让一个程序运行一次,在它运行时,不允许再启动一个新的进程。
- WinXP下如何查看一个进程的详细命令行 - 任务管理器中只能看到进程的可执行文件名字
- 启动GNOME设置守护进程时出错的一个解决方法
- 在.Net中,如何创建一个后台执行的进程?
- 启动一个程序使之成为守护进程
- 一个守护进程的例子
- shell中确保只有一个进程在执行
- 一个简单的linux守护进程
- 用PHP写一个Daemon守护进程
- 如何写一个简单的守护(精灵)进程原型
- linux 一个简单守护进程实例
- 向进程传递消息的一个API[这个例子具有破坏性,执行前请保存未存档文件]
- 防止应用程序多次执行(在进程里只一个)
- python 写的一个Ice服务端在linux下面的守护进程程序