下面是游念头文件kbhit.h:
QUOTE:
#ifndef KBHITh
#define KBHITh
void init_keyboard(void)
void close_keyboard(void)
int kbhit(void)
int readch(void)
#endif
下面式源程序kbhit.c:
QUOTE:#include "kbhit.h"
#include <stdio.h>
#include <termios.h>
static struct termios initial_settings, new_settings
static int peek_character = -1
void init_keyboard()
{
tcgetattr(0,&initial_settings)
new_settings = initial_settings
new_settings.c_lflag &= ~ICANON
new_settings.c_lflag &= ~ECHO
new_settings.c_lflag &= ~ISIG
new_settings.c_cc[VMIN] = 1
new_settings.c_cc[VTIME] = 0
tcsetattr(0, TCSANOW, &new_settings)
}
void close_keyboard()
{
tcsetattr(0, TCSANOW, &initial_settings)
}
int kbhit()
{
unsigned char ch
int nread
if (peek_character != -1) return 1
new_settings.c_cc[VMIN]=0
tcsetattr(0, TCSANOW, &new_settings)
nread = read(0,&ch,1)
new_settings.c_cc[VMIN]=1
tcsetattr(0, TCSANOW, &new_settings)
if(nread == 1)
{
peek_character = ch
return 1
}
return 0
}
int readch()
{
char ch
if(peek_character != -1)
{
ch = peek_character
peek_character = -1
return ch
}
read(0,&ch,1)
return ch
}
现在睁宴的Linux *** 作系统基本上都用systemd管理服败物务了,这玩意本身就自带服务挂了自动重启功能。只需要配置一个on-failure属性即可。如果你想把察早液你的程序变成服务,建议抽点时间学学systemd,不会占用你太多时间。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)