上一篇简要介绍了守护进程,现在就来实践一下,写一个自动关机的小程序。该程序可以守护进程的方式运行,当用户在一定时间(比如30分钟)没有鼠标和键盘操作后就会自动关机。
  这个程序利用了上篇文章中实现的daemonize函数,为程序创建了守护进程所需要的运行环境。
  由于需要同时监听鼠标和键盘操作,所以需要采用多线程的方式来实现。其中两个线程分别监视鼠标和键盘,一旦检测到相应动作(鼠标点击和移动、击键等),全局时间戳stamp(time_t)就会被设成当前时间。主线程每隔一定时间(比如1秒)检查stamp,若当前时间值(time(NULL))比stamp大30*60,则执行停机操作(使用system函数执行init 0命令,或者使用reboot函数)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h> //~ O_RDWR, S_IRWXU etc.
#include <pthread.h>
#include <time.h>
#include <limits.h>
#include <signal.h>
 
void daemonize();
 
//~ thread functions
void *listen_ms(void *);
void *listen_kb(void *);
 
//~ time stamp, keeping the time
//~ when the last KB or Mouse event happened.
volatile time_t stamp;
 
//~ mutex keeping stamp consistent.
pthread_mutex_t stamp_mutex;
 
int
main()
{
    daemonize();
    //~ initialize the mutex, stamp
    pthread_mutex_init(&stamp_mutex, NULL);
    //time(&stamp);
    stamp = time(NULL);
 
    //~ create two threads monitoring the Mouse and Keyboard.
    pthread_t ms_tid, kb_tid;
    if(pthread_create(&ms_tid, NULL, listen_ms, NULL) != 0)
    {
        perror("pthread_create");
        exit(1);
    }
    if(pthread_create(&kb_tid, NULL, listen_kb, NULL) != 0)
    {
        perror("pthread_create");
        exit(1);
    }
 
    unsigned int interval = 60 * 30;
    while(1)
    {
        sleep(1);
        pthread_mutex_lock(&stamp_mutex);
        if( time(NULL) - stamp > interval )
        {
            /*printf("shutdown\n");*/
            /*fflush(stdin);*/
            system("init 0");
        }
        pthread_mutex_unlock(&stamp_mutex);
    }
    //~ join the threads, though it'll never be excuted.
    pthread_join(ms_tid, NULL);
    pthread_join(kb_tid, NULL);
 
    return 0;
}
 
void *
listen_ms(void * arg)
{
    int fd = open("/dev/input/mice", O_RDONLY);
    if(fd < 0)
    {
        perror("open mice");
        exit(1);
    }
    char buf[256];
    while( read(fd, buf, sizeof(buf)) > 0 )
    {
        /*printf("Moused Moved.\n");*/
        pthread_mutex_lock(&stamp_mutex);
        //time(&stamp);
        stamp = time(NULL);
        pthread_mutex_unlock(&stamp_mutex);
    }
    close(fd);
}
 
void *
listen_kb(void * arg)
{
    int fd = open("/dev/input/event3", O_RDONLY);
    if(fd < 0)
    {
        perror("open event3");
        exit(1);
    }
    char buf[256];
    while( read(fd, buf, sizeof(buf)) > 0 )
    {
        /*printf("Key Hit.\n");*/
        pthread_mutex_lock(&stamp_mutex);
        //time(&stamp);
        stamp = time(NULL);
        pthread_mutex_unlock(&stamp_mutex);
    }
    close(fd);
}
 
void
daemonize()
{
    if( fork() > 0)
        exit(0);
    setsid();
    close(0);
    close(1);
    close(2);
    int fd = open("/dev/null", O_RDWR);
    //int fd = open("log.txt", O_RDWR);
    dup2(fd, 1);
    dup2(fd, 2);
    chdir("/");
    umask(0);
    signal(SIGCHLD, SIG_IGN);
}

  需要说明的是,共享变量stamp需要互斥地访问。另外,对鼠标事件的监听是借助于对设备文件/dev/input/mice的读取(阻塞方式),键盘的监听借助于对/dev/input/event3的阻塞读取,但我猜想在不同机器上可能会是其它诸如event0,event5之类的文件。
  不足之处在于,无法对全屏模式进行判断,即是说,如果你全屏看一部较长的电影,可能会被关机……
  如果你有好的方法来实现本文的功能,还请不吝赐教!

Tags: ,.
你好!除了代码,此处没有多少原创之物,皆为本人搜集、整理、总结之记录与心得,欢迎转载分享!转载时请尽量注明出处,将不胜感激。祝你健康、快乐!
Home

RFC: Request For Comments. Orz..

Name(required)
Mail (required),(will not be published)

RFC: Request For Comments. Orz..

Website(recommended)