Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14579
  • 博文数量: 8
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 69
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-15 09:05
文章存档

2013年(8)

我的朋友

分类: C/C++

2013-11-03 16:51:02

有错误,没法写入文档,不知道为什么。代码如下:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <sys/wait.h>
  8. #include <fcntl.h>
  9. #include <termios.h>
  10. #include <term.h>
  11. #include <curses.h>

  12. #define FIFO_NAME "/tmp/my_fifo"

  13. static struct termios initial_settings,new_settings;
  14. static int peek_character=-1;

  15. void init_keyboard();
  16. void close_keyboard();
  17. int kbhit();
  18. int readch();

  19. int main(int argc, char* argv[])
  20. {
  21.   int pipe_fd;
  22.   pid_t pid_listener, pid_writer;
  23.   if(access (FIFO_NAME,F_OK)==-1)
  24.     {
  25.       mkfifo(FIFO_NAME,0766);
  26.     }
  27.   pid_listener = fork();
  28.   if(pid_listener<0)
  29.     {
  30.       perror("fork");
  31.       exit(1);
  32.     }
  33.   else if(pid_listener>0)/*the parent process*/
  34.     {
  35.       pid_writer = fork();
  36.       if(pid_writer<0)
  37.     {
  38.      perror("fork");
  39.      exit(1);
  40.     }
  41.       if(pid_writer == 0) /*the writer process*/
  42.     {
  43.      /*read data from pipe, then store the data to data.out*/
  44.      int fout;
  45.      fout = open("data.out",O_WRONLY|O_CREAT|O_APPEND);
  46.      int ch;
  47.      pipe_fd = open(FIFO_NAME,O_RDONLY);
  48.      if(pipe_fd!=-1)
  49.      {
  50.      while(read(pipe_fd,&ch,1))
  51.         {    
  52.          write(fout ,&ch,1);
  53.         }
  54.      }
  55.      close(pipe_fd);
  56.     }
  57.     }
  58.   else/*the listener process*/
  59.     {
  60.       /*detect the keyboard hit and send the characher to writer through pipe.*/
  61.       init_keyboard();
  62.       pipe_fd = open(FIFO_NAME,O_WRONLY);
  63.       int ch = 0;
  64.       while(
  65.     {

  66.      if(kbhit())
  67.      {
  68.      ch = readch();
  69.      printf("you hit %c\n",ch);
  70.      write(pipe_fd,&ch,1);
  71.      }

  72.     }
  73.       close(pipe_fd);
  74.       close_keyboard();
  75.       exit(0);
  76.     }
  77.   wait(NULL);
  78.   return 0;
  79. }

  80. void init_keyboard()
  81. {
  82.    tcgetattr(0,&initial_settings);
  83.    new_settings=initial_settings;
  84.    new_settings.c_lflag &= ~ICANON;
  85.    new_settings.c_lflag &= ~ECHO;
  86.    new_settings.c_lflag &= ~ISIG;
  87.    new_settings.c_cc[VMIN] = 1;
  88.    new_settings.c_cc[VTIME] = 0;
  89.    tcsetattr(0,TCSANOW,&new_settings);
  90. }

  91. void close_keyboard()
  92. {
  93.   tcsetattr(0,TCSANOW,&initial_settings);
  94. }

  95. int kbhit()
  96. {
  97.   char ch;
  98.   int nread;
  99.   
  100.   if(peek_character!=-1)
  101.      return 1;
  102.   new_settings.c_cc[VMIN]=0;
  103.   tcsetattr(0,TCSANOW,&new_settings);
  104.   nread=read(0,&ch,1);
  105.   new_settings.c_cc[VMIN]=1;
  106.   tcsetattr(0,TCSANOW,&new_settings);

  107.   if(nread==1){
  108.       peek_character=ch;
  109.       return 1;
  110.   }
  111.   return 0;
  112. }

  113. int readch()
  114. {
  115.   char ch;
  116.   if(peek_character!=-1)
  117.     {
  118.       ch=peek_character;
  119.       peek_character=-1;
  120.       return ch;
  121.     }
  122.   read(0,&ch,1);
  123.   return ch;
  124. }

阅读(159) | 评论(0) | 转发(0) |
0

上一篇:Emacs GCC GDB 编译调试方法

下一篇:没有了

给主人留下些什么吧!~~