openstack 云桌面在学校中使用,因为不同的课程教学需要,会生成大量的实例,这会带来几个问题,其中一个是超配,紧跟着的是过量的虚拟机在运行而资源不足,或影响速度造成用户体验差,可能问题在于有的实例启动后,课上完了,实例不使用了但没有及时关闭,一直在运行占用资源,想到的办法是检测鼠标和键盘动作,当一个实例超过一定时间长度,如30分钟或一小时,没有鼠标和键盘动作,就自动关闭掉实例。
一、linux规定时间没有鼠标键盘操作自动关机
对于linux系统,找到如下程序,测试可用:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
#include <time.h>
#include <limits.h>
#include <signal.h>
void daemonize();
void *listen_ms( void * );
void *listen_kb( void * );
volatile time_t stamp;
pthread_mutex_t stamp_mutex;
int main()
{
daemonize();
pthread_mutex_init( &stamp_mutex, NULL );
stamp = time( NULL );
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 )
{
system( "init 0" );
}
pthread_mutex_unlock( &stamp_mutex );
}
pthread_join( ms_tid, NULL );
pthread_join( kb_tid, NULL );
return(0);
}
void * listen_ms( void * arg )
{
int fd = open(