读书人

字符装置异步通知

发布时间: 2012-12-28 10:29:04 作者: rapoo

字符设备异步通知

driver:

#include<stdio.h>  #include<sys/types.h>  #include<sys/stat.h>  #include<fcntl.h>  #include<sys/select.h>  #include<unistd.h>  #include<signal.h>  #include<string.h>    unsigned int flag = 0;    void sig_handler(int sig)  {    printf("%s\n",__FUNCTION__);    flag++;  }  int main(void)  {      char r_buf[20];      char *w_buf = "hello write!\n";      int r_count = 0;      int fd;      int f_flags;      flag++;      fd=open("/dev/fpga_key",O_RDWR);      if(fd<0)      {          perror("open");          return-1;      }      signal(SIGIO, sig_handler);     fcntl(fd, F_SETOWN, getpid());    f_flags = fcntl(fd, F_GETFL);    fcntl(fd, F_SETFL, FASYNC | f_flags);     while(1)    {    printf("waiting \n");     sleep(2);    if(flag > 3)    break;printf("flag = %d\n", flag);    write(fd, w_buf, strlen(w_buf));      }           close(fd);        return 0;  }  



读书人网 >移动开发

热点推荐