Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1218820
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: LINUX

2015-12-02 15:45:14

select.c文件

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/select.h>

  6. int main(int argc, char * * argv)
  7. {
  8.         fd_set fdset;
  9.         
  10.         FD_ZERO(&fdset); /*清空集合中所有元素*/
  11.         
  12.         FD_SET(STDIN_FILENO, &fdset); /*将STDIN_FILENO加入集合中*/
  13.         
  14.         int ret;
  15.         
  16.         ret = FD_ISSET(STDIN_FILENO, &fdset); /*测试STDIN_FILENO 是否包含在集合中*/
  17.         if(ret == 1)
  18.         {
  19.                 printf("STDIN_FILENO 已经被设置了!fdset=[%d]\n", fdset);
  20.         }
  21.         else if(ret == 0)
  22.         {
  23.                 printf("STDIN_FILENO 已经未被设置!\n");
  24.         }
  25.         
  26.         FD_CLR(STDIN_FILENO, &fdset); /*将STDIN_FILENO 从集合中清除*/
  27.         ret = FD_ISSET(STDIN_FILENO, &fdset);
  28.         if(ret == 1)
  29.         {
  30.                 printf("STDIN_FILENO 已经被设置了!fdset=[%d]\n", fdset);
  31.         }
  32.         else if(ret == 0)
  33.         {
  34.                 printf("STDIN_FILENO 已经未被设置!\n");
  35.         }
  36.         
  37.         return 0;    
  38. }
select2.c文件

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/select.h>
  7. #include <assert.h>

  8. int isReadReady(int fd);
  9. int isWriteReady(int fd);

  10. int main(int argc, char * * argv)
  11. {
  12.         int ret = -1;
  13.         int fd = STDOUT_FILENO;
  14.         ret = isReadReady(fd);
  15.         if(ret == 1)
  16.         {
  17.                 printf("fd=[%d] 可读!\n", fd);
  18.         }
  19.         else if(ret == 0)
  20.         {
  21.                 printf("fd=[%d] 不可读!\n", fd);
  22.         }
  23.         
  24.         ret = isWriteReady(fd);
  25.         if(ret == 1)
  26.         {
  27.                 printf("fd=[%d] 可读!\n", fd);
  28.         }
  29.         else if(ret == 0)
  30.         {
  31.                 printf("fd=[%d] 不可读!\n", fd);
  32.         }
  33.         
  34.         fd = STDIN_FILENO;
  35.         ret = isReadReady(fd);
  36.         if(ret == 1)
  37.         {
  38.                 printf("fd=[%d] 可读!\n", fd);
  39.         }
  40.         else if(ret == 0)
  41.         {
  42.                 printf("fd=[%d] 不可读!\n", fd);
  43.         }
  44.         
  45.         ret = isWriteReady(fd);
  46.         if(ret == 1)
  47.         {
  48.                 printf("fd=[%d] 可读!\n", fd);
  49.         }
  50.         else if(ret == 0)
  51.         {
  52.                 printf("fd=[%d] 不可读!\n", fd);
  53.         }
  54.         
  55.         printf("**************************************\n");
  56.         int keyboard;
  57.         char ch;
  58.         fd_set readfd;
  59.         struct timeval timeout;
  60.         
  61.         //keyboard = open("/dev/tty", O_RDONLY|O_NONBLOCK); /*/dev/tty就是控制终端*/
  62.         keyboard = 0;
  63.         printf("keyboard=[%d]\n", keyboard);
  64.         //assert(keyboard > 0);
  65.         
  66.         read(keyboard, &ch, 1*sizeof(char));
  67.         printf("输入字符=[%c]\n", ch);

  68.         while(1)
  69.         {
  70.                 timeout.tv_sec = 1;
  71.                 timeout.tv_usec = 0;
  72.                 FD_ZERO(&readfd);
  73.                 FD_SET(keyboard, &readfd);
  74.                 
  75.                 /*标准输入文件中,即屏幕中,
  76.                 如果没有数据,则表示读状态还未准备好:直接去read会阻塞。
  77.                 如果有数据,则表示读状态已经准备好:直接去read,不会阻塞,
  78.                 所以select函数执行时:
  79.                 若标准输入文件中有数据,read不会阻塞,则此描述符是准备好啊,则select立马检测到,select不会阻塞,立即返回1。
  80.                 若标准输入文件中没有数据,read会阻塞,未准备好,则select会阻塞1秒,1秒内还没有数据的话,还未准备好,再返回0*/
  81.                 ret = select(keyboard+10, &readfd, NULL, NULL, &timeout);
  82.                 printf("select ret=[%d]\n", ret);
  83.                 if(ret < 0)
  84.                 {
  85.                         printf("select err!\n");
  86.                         return -1;
  87.                 }
  88.                 
  89.                 ret = FD_ISSET(keyboard, &readfd);
  90.                 printf("FD_ISSET ret=[%d]\n", ret);
  91.                 if(ret == 1) /*可读*/
  92.                 {
  93.                         printf("===========\n");
  94.                         read(keyboard, &ch, 1*sizeof(char));
  95.                         if(ch == '\n')
  96.                         {
  97.                                 continue;
  98.                         }
  99.                         printf("输入字符=[%c]\n", ch);
  100.                         
  101.                         if(ch == 'q')
  102.                         {
  103.                                 break;
  104.                         }
  105.                 }
  106.         }
  107.         
  108.         return 0;
  109. }

  110. /*功能:测试文件fd是否可读*/
  111. int isReadReady(int fd)
  112. {
  113.         int ret = -1;
  114.         fd_set readfdset;
  115.         struct timeval time;
  116.         memset(&time, 0, sizeof(struct timeval));
  117.         
  118.         FD_ZERO(&readfdset);
  119.         FD_SET(fd, &readfdset);
  120.         time.tv_sec = 0;
  121.         time.tv_usec = 0;
  122.         
  123.         ret = select(fd+1, &readfdset, NULL, NULL, &time);
  124.         printf("select ret=[%d]\n", ret);
  125.         if(ret < 0)
  126.         {
  127.                 printf("select err!\n");
  128.                 return -1;
  129.         }
  130.         ret = FD_ISSET(fd, &readfdset);
  131.         if(ret == 1)
  132.         {
  133.                 return 1;
  134.         }
  135.         else if(ret == 0)
  136.         {
  137.                 return 0;
  138.         }
  139. }

  140. /*功能:测试文件fd是否可写*/
  141. int isWriteReady(int fd)
  142. {
  143.         int ret = -1;
  144.         fd_set writefdset;
  145.         struct timeval time;
  146.         memset(&time, 0, sizeof(struct timeval));
  147.         
  148.         FD_ZERO(&writefdset);
  149.         FD_SET(fd, &writefdset);
  150.         time.tv_sec = 0;
  151.         time.tv_usec = 0;
  152.         
  153.         ret = select(fd+1, NULL, &writefdset, NULL, &time);
  154.         printf("select ret=[%d]\n", ret);
  155.         if(ret < 0)
  156.         {
  157.                 printf("select err!\n");
  158.                 return -1;
  159.         }
  160.         ret = FD_ISSET(fd, &writefdset);
  161.         if(ret == 1)
  162.         {
  163.                 return 1;
  164.         }
  165.         else if(ret == 0)
  166.         {
  167.                 return 0;
  168.         }
  169. }
select3.c文件

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/select.h>
  7. #include <assert.h>

  8. int isReadReady(int fd);
  9. int isWriteReady(int fd);

  10. int main(int argc, char * * argv)
  11. {
  12.         int ret = -1;
  13.         int fd = STDOUT_FILENO;
  14.         ret = isReadReady(fd);
  15.         if(ret == 1)
  16.         {
  17.                 printf("fd=[%d] 可读!\n", fd);
  18.         }
  19.         else if(ret == 0)
  20.         {
  21.                 printf("fd=[%d] 不可读!\n", fd);
  22.         }
  23.         
  24.         ret = isWriteReady(fd);
  25.         if(ret == 1)
  26.         {
  27.                 printf("fd=[%d] 可读!\n", fd);
  28.         }
  29.         else if(ret == 0)
  30.         {
  31.                 printf("fd=[%d] 不可读!\n", fd);
  32.         }
  33.         
  34.         fd = STDIN_FILENO;
  35.         ret = isReadReady(fd);
  36.         if(ret == 1)
  37.         {
  38.                 printf("fd=[%d] 可读!\n", fd);
  39.         }
  40.         else if(ret == 0)
  41.         {
  42.                 printf("fd=[%d] 不可读!\n", fd);
  43.         }
  44.         
  45.         ret = isWriteReady(fd);
  46.         if(ret == 1)
  47.         {
  48.                 printf("fd=[%d] 可读!\n", fd);
  49.         }
  50.         else if(ret == 0)
  51.         {
  52.                 printf("fd=[%d] 不可读!\n", fd);
  53.         }
  54.         
  55.         printf("**************************************\n");
  56.         int keyboard;
  57.         char ch;
  58.         char str[128];
  59.         fd_set writefd;
  60.         struct timeval timeout;
  61.         
  62.         //keyboard = open("/dev/tty", O_RDONLY|O_NONBLOCK); /*/dev/tty就是控制终端*/
  63.         keyboard = STDOUT_FILENO;
  64.         printf("keyboard=[%d]\n", keyboard);
  65.         //assert(keyboard > 0);
  66.         
  67.         strcpy(str, "wangxiancai");
  68.         printf("输出字符=\n");
  69.         write(keyboard, str, strlen(str));
  70.         printf("\n");
  71.         
  72.         while(1)
  73.         {
  74.                 sleep(1);
  75.                 timeout.tv_sec = 1;
  76.                 timeout.tv_usec = 0;
  77.                 FD_ZERO(&writefd);
  78.                 FD_SET(keyboard, &writefd);
  79.                 
  80.                 /*标准输出文件:任何时候都是可写的,即任何时候都是准备好的。
  81.                 所以在select一执行,就能立马检测到有描述符准备好,select根本不会有阻塞现象。*/
  82.                 ret = select(keyboard+10, NULL, &writefd, NULL, &timeout);
  83.                 printf("select ret=[%d]\n", ret);
  84.                 if(ret < 0)
  85.                 {
  86.                         printf("select err!\n");
  87.                         return -1;
  88.                 }
  89.                 
  90.                 ret = FD_ISSET(keyboard, &writefd);
  91.                 printf("FD_ISSET ret=[%d]\n", ret);
  92.                 if(ret == 1) /*可写*/
  93.                 {
  94.                         printf("===========\n");
  95.                         printf("输出字符=\n");
  96.                         memset(str, 0, sizeof(str));
  97.                         strcpy(str, "wangxiancai");
  98.                         write(keyboard, str, strlen(str));
  99.                         printf("\n");
  100.                 }
  101.         }
  102.         
  103.         return 0;
  104. }

  105. /*功能:测试文件fd是否可读*/
  106. int isReadReady(int fd)
  107. {
  108.         int ret = -1;
  109.         fd_set readfdset;
  110.         struct timeval time;
  111.         memset(&time, 0, sizeof(struct timeval));
  112.         
  113.         FD_ZERO(&readfdset);
  114.         FD_SET(fd, &readfdset);
  115.         time.tv_sec = 0;
  116.         time.tv_usec = 0;
  117.         
  118.         ret = select(fd+1, &readfdset, NULL, NULL, &time);
  119.         printf("select ret=[%d]\n", ret);
  120.         if(ret < 0)
  121.         {
  122.                 printf("select err!\n");
  123.                 return -1;
  124.         }
  125.         ret = FD_ISSET(fd, &readfdset);
  126.         if(ret == 1)
  127.         {
  128.                 return 1;
  129.         }
  130.         else if(ret == 0)
  131.         {
  132.                 return 0;
  133.         }
  134. }

  135. /*功能:测试文件fd是否可写*/
  136. int isWriteReady(int fd)
  137. {
  138.         int ret = -1;
  139.         fd_set writefdset;
  140.         struct timeval time;
  141.         memset(&time, 0, sizeof(struct timeval));
  142.         
  143.         FD_ZERO(&writefdset);
  144.         FD_SET(fd, &writefdset);
  145.         time.tv_sec = 0;
  146.         time.tv_usec = 0;
  147.         
  148.         ret = select(fd+1, NULL, &writefdset, NULL, &time);
  149.         printf("select ret=[%d]\n", ret);
  150.         if(ret < 0)
  151.         {
  152.                 printf("select err!\n");
  153.                 return -1;
  154.         }
  155.         ret = FD_ISSET(fd, &writefdset);
  156.         if(ret == 1)
  157.         {
  158.                 return 1;
  159.         }
  160.         else if(ret == 0)
  161.         {
  162.                 return 0;
  163.         }
  164. }



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

上一篇:getservent函数

下一篇:最简单的CS架构代码

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