⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectdemo.c

📁 2410/vxworks/tornado下的基本实验包括 serial,ramdrv,interrupt,multi-tasking,FTP,TCP,UDP
💻 C
字号:
/* selectDemo.c - 使用select 的例子*/   #include "vxWorks.h" #include "taskLib.h" #include "selectLib.h" #include <string.h>  #include <ioLib.h>   #include <stdio.h>   #include <fioLib.h> #include <pipeDrv.h>  #include <sysLib.h>   #define BUFFERSIZE     25                               /* 缓冲区大小 */ #define NUM_FDS     2                                /* 监听的设备个数   */ #define ONE_SEC     1 #define STRING1 "SelectDemo: Message #1"  #define STRING2 "SelectDemo: Message #2" #define STRING3 "Quitting SelectDemo" #define MAX_NUM_MSG 2                  /* 管道中最多的消息个数*/ #define MSG_SIZE    100                           /* 消息的最大值*/ LOCAL int inFd1;                   /*/pipe/1  文件描述符*/   LOCAL int inFd2;                   /* /pipe/2 文件描述符*/   /******************************************************************************* * selectOnReadFds - selectOnReadFds 任务函数 * 这个任务调用select 监听两个管道,直到其中一个变为可读, * 然后从中读取信息. * * RETURNS: OK or ERROR */  STATUS selectOnReadFds ()     {     char receiveString [BUFFERSIZE];      /* 接收消息缓冲区 */     struct fd_set readFds;     struct fd_set saveFds;     int width;     int bytesRead;     int numFds;                                              int notFinished = TRUE;      width = 0;     FD_ZERO (&saveFds);    /* 初始化,清空文件描述字集合*/      /* width 变量是被测试的最大文件描述符的位数,     		这里是inFd1和inFd2中的大者+1      */     FD_SET (inFd1, &saveFds); /* 设置 inFd1 对应位*/     FD_SET (inFd2, &saveFds); /* 设置 inFd2 对应位*/          width = (inFd1 > inFd2) ? inFd1 : inFd2;     width++;       while (notFinished)         {         readFds = saveFds;         printf ("selectOnReadFds: Number of bits (in fd_set struct) to be tested = %d\n", width);          /* 任务因等待多文件描述符而阻塞,通过使用select()函数直到             一个或多个文件描述符变为有效          */          if ((numFds = select (width, &readFds, NULL, NULL, NULL)) == ERROR)             {             perror (" ERROR in select");                return (ERROR);             }         else              printf ("selectOnReadFds: Number of file descriptors ready for reading = %d\n", numFds);          if (FD_ISSET (inFd1, &readFds))             {             /* /pipe/1 处于可读状态 */             bzero (receiveString, BUFFERSIZE);             if ((bytesRead = read (inFd1, receiveString, BUFFERSIZE)) > OK)                 printf ("selectOnReadFds: Message read from /pipe/1 device - \"%s\"\n",(char *) receiveString);             if (strcmp (receiveString, STRING3) == 0)                 notFinished = FALSE;             }             else              printf ("selectOnReadFds: The fd inFd1 of /pipe/1 dev. is not READY for reading\n");          if (FD_ISSET (inFd2, &readFds))             {             /* /pipe/2  处于可读状态*/             bzero (receiveString, BUFFERSIZE);             if ((bytesRead = read (inFd2, receiveString, BUFFERSIZE)) > OK)                 printf ("selectOnReadFds: Message read from /pipe/2 device - \"%s\"\n", (char *) receiveString);             }             else              printf ("selectOnReadFds: The fd inFd2 of /pipe/2 dev. is not READY for reading\n");         printf ("\n");         }          return (OK);     }  /******************************************************************************  *  * selectDemoInit - 初始化管道设备函数   *  * 这个程序初始化管道设备,使之可进行读/写操作	  *  */  void selectDemoInit ()     {      if (pipeDevCreate ("/pipe/1",MAX_NUM_MSG, MSG_SIZE) == ERROR)         perror ("selectDemoInit: error in creating\"/pipe/1\"");     else          printf ("\"/pipe/1\" pipe device created\n");      if (pipeDevCreate ("/pipe/2", MAX_NUM_MSG, MSG_SIZE) == ERROR)         perror ("selectDemoInit: error in creating \"/pipe/2\"");      else          printf ("\"/pipe/2\" pipe device created\n\n");       if ((inFd1 = open ("/pipe/1", UPDATE, 0644)) == ERROR)          perror (" Error opening file \"/pipe/1\"") ;      else          printf ("Value of the fd for /pipe/1 = %d \n", inFd1);       if ((inFd2 = open ("/pipe/2", UPDATE, 0644)) == ERROR)          perror (" Error opening file \"/pipe/1\"") ;      else          printf ("Value of the fd for /pipe/2 = %d \n\n", inFd2);      }      /******************************************************************************  *  * selectDemo -主函数  *  * 这个程序基于管道设备示范了select()的操作  * selectDemo程序初始化管道设备并创建和激活selectOnReadFds任务.然后  * 往管道设备中写入消息. selectOnReadFds任务基于select()函数,从处于可读  * 状态的管道设备中读取消息.  *  * RETURNS: OK or ERROR  */  STATUS selectDemo ()     {     char input[80];     int makeFdsReady;     int notDone = TRUE;     /* 结束标志 */        selectDemoInit ();      /* 初始化管道设备*/      /* 创建任务 selectOnReadFds */     if ((taskSpawn ("selectOnReadFds", 100, VX_SUPERVISOR_MODE | VX_STDIO,            20000, (FUNCPTR) selectOnReadFds, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))                                                                        == ERROR)         {         perror ("selectDemo: Spawning selectOnReadFds task failed");         return (ERROR);          }      while (notDone)         {         /* 释放CPU一段时间,以便selectOnReadFds能执行          */         taskDelay (sysClkRateGet () * ONE_SEC);          makeFdsReady = NONE;         printf ("0 : To quit this Demo\n");         printf ("1 : To write a message to \"/pipe/1\"\n");         printf ("2 : To write a message to \"/pipe/2\"\n");         printf ("3 : To write a message to \"/pipe/1\" and \"/pipe/2\"\n");         printf ("Enter your choice : ");         gets (input);         sscanf (input, "%d", &makeFdsReady);         printf ("\n");              switch (makeFdsReady)             {             case 1:  /* 往 /pipe/1 设备写消息 */                 printf ("Writing the message \"%s\" to /pipe/1 device\n", (char *) STRING1);                 if (write (inFd1, STRING1, sizeof (STRING1)) == ERROR)                     perror ("selectDemo: Writing msg to pipe 1 failed");                 break;              case 2:  /*往 /pipe/2 设备写消息*/                 printf ("Writing the message \"%s\" to /pipe/2 device\n", (char *) STRING2);                 if (write (inFd2, STRING2, sizeof (STRING2)) == ERROR)                     perror ("selectDemo: Writing msg to pipe 2 failed");                 break;                       case 3:  /* 往 /pipe/1 and /pipe/2 设备中写消息*/                 printf ("Writing the message \"%s\" to /pipe/1 device\n", (char *) STRING1);                 if (write (inFd1, STRING1, sizeof (STRING1)) == ERROR)                     perror ("selectDemo: Writing msg to pipe 1 failed");                 printf ("Writing the message \"%s\" to /pipe/2 device\n", (char *) STRING2);                 if (write (inFd2, STRING2, sizeof (STRING2)) == ERROR)                     perror ("selectDemo: Writing msg to pipe 2 failed");                 break;                       case 0:  /* 往 /pipe/1设备中写停止演示消息 */                 notDone = FALSE;                 printf ("Writing the message \"%s\" to /pipe/1 device\n", (char *) STRING3);                 if (write (inFd1, STRING3, sizeof (STRING3)) == ERROR)                     perror ("selectDemo: Writing msg to pipe 1 failed");                 taskDelay (sysClkRateGet () * ONE_SEC);                 break;              default :                 printf ("Unrecognized Choice \n");                 break;             }                              printf ("\n");         }          close (inFd1);         close (inFd2);          return (OK);     }    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -