Article Title: Linux automatically shuts down after 30 minutes of no operation. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Now let's try to write a small program that automatically shuts down. This program can run as a daemon. It will automatically shut down after a certain period of time (such as 30 minutes) without mouse or keyboard operations.
This program uses the daemonize function implemented in the previous article to create the runtime environment required by the daemonize program.
Because you need to listen to mouse and keyboard operations at the same time, you need to use multiple threads. The two threads monitor the mouse and the keyboard respectively. Once the corresponding action (such as mouse clicking and moving and hitting) is detected, the global timestamp (time_t) is set to the current time. The main thread checks the stamp at a certain time (such as 1 second). If the current time value (time (NULL) is 30*60 greater than the stamp, then run the stop operation (use the system function to execute the init 0 command, or use the reboot function ).
# Include
# Include
# Include
# Include
# Include //~ O_RDWR, S_IRWXU etc.
# Include
# Include
# Include
# Include
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 );
}
It must be noted that the shared variable stamp needs mutual access. In addition, the mouse event is monitored by reading (blocking) the device file/dev/input/mice ), keyboard listening relies on non-blocking reading of/dev/input/event3, but I guess it may be other files such as event0 and event5 on different machines.
The disadvantage is that you cannot judge the full screen mode. That is to say, if you watch a long movie in full screen, it may be shut down ......