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

📄 pipedemo.c

📁 2410/vxworks/tornado下的基本实验包括 serial,ramdrv,interrupt,multi-tasking,FTP,TCP,UDP
💻 C
字号:
/* pipeDemo.c - 用PIPE进行任务间通信的例子*/  /* includes */  #include "vxWorks.h" #include "taskLib.h" #include "sysLib.h" #include "stdio.h" #include "ioLib.h" #include "pipeDrv.h"  #define  CONSUMER_TASK_PRI          99   /* consumer task 优先级*/ #define  PRODUCER_TASK_PRI          98   /* producer task 优先级*/ #define  TASK_STACK_SIZE          5000   /*任务栈大小 */  #define PIPE_NAME		"/pipe/server"  /* 管道设备的文件名*/  struct msg {                             /* 传递的消息结构体 */         int tid;                         		 /* 任务号*/                  int value;                       	 /* 传递的值 */         };  LOCAL int  pipeFd;                   /* 管道对应的文件描述符*/ LOCAL int numMsg = 3;                    /*队列中 最大消息个数*/ LOCAL BOOL notDone;                      /* 完成标志*/  /* function prototypes */  LOCAL STATUS producerTask ();           /* producer task */ LOCAL STATUS consumerTask ();           /* consumer task */  /*****************************************************************************  *  msgQDemo - 利用管道通信的主程序   *  *  DESCRIPTION  *  在producerTask 和consumerTask 之间创建一个管道,  *  producerTask 通过管道发送消息,   *  consumerTask通过管道接收 消息.  *  当consumerTask 接收完所有消息,关闭管道  *  *  RETURNS: OK or ERROR  *  *  EXAMPLE  *  *  -> sp pipeDemo  *  */  STATUS pipeDemo()     {     notDone = TRUE;  /* initialize the global flag */	  /*创建管道*/		     if (pipeDevCreate (PIPE_NAME, numMsg, sizeof (struct msg)) == ERROR)         {         perror ("Error in creating pipe");          }      /* 打开管道 */     if ((pipeFd = open (PIPE_NAME, O_RDWR, 0)) == ERROR)         {         perror  ("Error in opening pipe device");         return (ERROR);         } 	       /* 创建 producerTask task */     if (taskSpawn ("tProducerTask_pipe", PRODUCER_TASK_PRI, 0, TASK_STACK_SIZE,                     (FUNCPTR) producerTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)                      == ERROR)         {         perror ("producerTask: Error in spawning demoTask");         return (ERROR);         }       /* 创建consumerTask task */     if (taskSpawn ("tConsumerTask_pipe", CONSUMER_TASK_PRI, 0, TASK_STACK_SIZE,                     (FUNCPTR) consumerTask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)                     == ERROR)         {         perror ("consumerTask: Error in spawning demoTask");         return (ERROR);         }      /* 轮询,等待consumerTask 接收完所有消息*/     while (notDone)         taskDelay (sysClkRateGet ());	  /*关闭管道*/	 		close (pipeFd);      return (OK);     }   /*****************************************************************************  *  producerTask - 通过消息队列发送消息  *  *  RETURNS: OK or ERROR  *  */  LOCAL STATUS producerTask (void)     {     int count;     int value;     struct msg producedItem;           /* 发送缓存*/       printf ("producerTask started: task id = %#x \n", taskIdSelf ());      /*产生  numMsg 个消息并发送*/      for (count = 1; count <= numMsg; count++)         {         value = count * 10;             /* 给消息结构体赋值 */         producedItem.tid = taskIdSelf ();		/*任务号*/         producedItem.value = value;				          /* 发送消息*/         if ((write (pipeFd, (char *)&producedItem, sizeof (producedItem))) == ERROR)             {                 perror ("Error in sending the message");                 return (ERROR);             }         else             printf ("ProducerTask: tid = %#x, produced value = %d \n",                                                        taskIdSelf (), value);         }      return (OK);     }  /*****************************************************************************  *  consumerTask - 通过消息队列接收消息  *  *  RETURNS: OK or ERROR  *  */  LOCAL STATUS consumerTask (void)     {     int count;     struct msg consumedItem;           /* 接收缓存 */      printf ("\n\nConsumerTask: Started -  task id = %#x\n", taskIdSelf());      /* 接收 numMsg 个消息*/     for (count = 1; count <= numMsg; count++)         {         /* 接受消息 */         if (read (pipeFd, (char *)&consumedItem, sizeof (consumedItem)) == ERROR)             {             perror ("Error in receiving the message");             return (ERROR);             }         else              printf ("ConsumerTask: Consuming msg of value %d from tid = %#x\n",                             consumedItem.value, consumedItem.tid);         }      notDone = FALSE;   /*设置notDone,通知主任务关闭管道*/     return (OK);     }    

⌨️ 快捷键说明

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