您的位置:首页 > 其它

使得守护进程一次执行一个

2017-02-02 23:47 399 查看
转载自:http://www.cnblogs.com/Alarm/p/6212973.html

转载理由,本打算自己写一篇,看着大家写的都不错故转之

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 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: