3:异步输入二
异步输入的第二种方法是通过aio_read来实现的,使用aio_read更加灵活,但是设置起来也比较复杂。
设置步骤如下:
1.设置信号处理函数,接受用户输入
2.设置aiocb结构体中的变量指明等待什么类型的输入,当输入的时候产生什么信号。
3.将aiocb结构体传递给aio_read来递交读入请求。
aiocb结构体定义如下:
struct aiocb {
/* The order of these fields is implementation-dependent */
int aio_fildes; /* File descriptor */
off_t aio_offset; /* File offset */
volatile void *aio_buf; /* Location of buffer */
size_t aio_nbytes; /* Length of transfer */
int aio_reqprio; /* Request priority */
struct sigevent aio_sigevent; /* Notification method */
int aio_lio_opcode; /* Operation to be performed;
lio_listio() only */
/* Various implementation-internal fields not shown */
};
struct sigevent {
int sigev_notify; /* Notification method */
int sigev_signo; /* Notification signal */
union sigval sigev_value; /* Data passed with
notification */
void (*sigev_notify_function) (union sigval);
/* Function used for thread
notification (SIGEV_THREAD) */
void *sigev_notify_attributes;
/* Attributes for notification thread
(SIGEV_THREAD) */
pid_t sigev_notify_thread_id;
/* ID of thread to signal (SIGEV_THREAD_ID) */
};
下面是一个简单的例子:
#include<stdio.h>
#include<stdlib.h>
#include<curses.h>
#include<signal.h>
#include<fcntl.h>
#include<aio.h>
void init_setup ( );
void init_end ( );
void on_input ( );
void do_main ( );
void setup_aio_buffer();
struct aiocb kbcbuf;
int main ( int argc, char *argv[] )
{
init_setup();
signal(SIGIO,on_input);
setup_aio_buffer();
aio_read(&kbcbuf);
do_main();
init_end();
return EXIT_SUCCESS;
}
/* ---------- end of function main ---------- */
void init_setup ( )
{
initscr();
crmode();
noecho();
clear();
} /* ----- end of function init_setup ----- */
void init_end ( )
{
endwin();
} /* ----- end of function init_end ----- */
void on_input ( )
{
char c;
char *cp = (char *)kbcbuf.aio_buf;
if(aio_error(&kbcbuf) != 0)
perror("reading faild");
else
if(aio_return(&kbcbuf) ==1){
c = *cp;
if(c == 'w')
mvaddch(20,20,'!');
else if(c == 'e')
mvaddch(20,20,'0');
else if(c == 'r')
mvaddch(20,20,'t');
}
aio_read(&kbcbuf);
} /* ----- end of function on_input ----- */
void do_main ( )
{
while(1){
sleep(1);
move(50,50);
addstr("do other thing");
refresh();
}
} /* ----- end of function do_main ----- */
void setup_aio_buffer()
{
static char input[1];
kbcbuf.aio_fildes = 0;
#设置接受输入的buf
kbcbuf.aio_buf = input;
#设置接受输入的字节大小
kbcbuf.aio_nbytes = 1;
kbcbuf.aio_offset = 0;
#设置处理输入的方法
#SIGE_SIGNAL 代表通过发送信号来处理
kbcbuf.aio_sigevent.sigev_notify =SIGEV_SIGNAL;
#设置要发送的信号
kbcbuf.aio_sigevent.sigev_signo = SIGIO;