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

📄 binarysemdemo.c

📁 2410/vxworks/tornado下的基本实验包括 serial,ramdrv,interrupt,multi-tasking,FTP,TCP,UDP
💻 C
字号:
/* synchronizeDemo.c - 用二进制信号量同步任务  */   #include "vxWorks.h" #include "taskLib.h" #include "semLib.h" #include "stdio.h" #include "sysLib.h"  #define  TASK_PRI                 98     /* 任务优先级 */ #define  TASK_STACK_SIZE          5000   /* 任务栈 */  LOCAL SEM_ID semId1;            /*binary semaphore 1 的ID*/ LOCAL SEM_ID semId2;            /* binary semaphore 2 的ID*/ LOCAL int numTimes = 2;         /* 反复次数*/ LOCAL BOOL notDone;              /*结束标志*/   LOCAL STATUS taskA (); LOCAL STATUS taskB ();  /*****************************************************************************  *  synchronizeDemo - 主函数  *  *  描述  *  创建两个二进制信号量(semId1 和semId2).同步两个任务(TaskA和TaskB)  *  *  返回值:  *  OK   *  ERROR  *  *  */  STATUS synchronizeDemo ()     {     notDone = TRUE;       /* 创建二进制信号量semId1,并使之可用*/     if ((semId1 = semBCreate (SEM_Q_PRIORITY, SEM_FULL)) == NULL)         {         perror ("synchronizeDemo: Error in creating semId1 semaphore");         return (ERROR);         }      /* 创建二进制信号量semId1,并使之不可用*/     if ((semId2 = semBCreate (SEM_Q_PRIORITY, SEM_EMPTY)) == NULL)         {         perror ("synchronizeDemo: Error in creating semId2 semaphore");         return (ERROR);         }      /* 创建 taskA*/     if (taskSpawn ("tTaskA", TASK_PRI, 0, TASK_STACK_SIZE, (FUNCPTR) taskA, 0,                     0, 0, 0, 0, 0, 0, 0, 0, 0) == ERROR)         {         perror ("synchronizeDemo: Error in spawning taskA");         return (ERROR);         }      /* 创建 taskB*/     if (taskSpawn ("tTaskB", TASK_PRI, 0, TASK_STACK_SIZE, (FUNCPTR) taskB, 0,                    0, 0, 0, 0, 0, 0, 0, 0, 0) == ERROR)         {         perror ("synchronizeDemo: Error in spawning taskB");         return (ERROR);         }          /* 轮循检查是否完成 */     while (notDone)          taskDelay (sysClkRateGet());  /* 延时 1秒,sysClkRateGet()返回1秒钟的tick值*/         /* 删除信号量 */     if (semDelete (semId1) == ERROR)         {         perror ("syncronizeDemo: Error in deleting semId1 semaphore");         return (ERROR);         }     if (semDelete (semId2) == ERROR)        {        perror ("syncronizeDemo: Error in deleting semId1 semaphore");        return (ERROR);        }     printf ("\n\n synchronizeDemo now completed \n");          return (OK);     }  /*****************************************************************************  *  taskA - 等待semId1变为可用, 执行EventA,执行完毕后,释放semId2,  *				以实现任务同步  *  *  返回值: OK or ERROR  *  */  LOCAL STATUS taskA ()     {     int count;      for (count = 0; count < numTimes; count++)         {         /*等待semId1变为可用,永远等待*/         if (semTake (semId1, WAIT_FOREVER) == ERROR)             {             perror ("taskA: Error in semTake");             return (ERROR);             }          printf ("taskA: Started first by taking the semId1 semaphore - %d times\n",                 (count + 1));         printf("This is task  <%s> : Event A now done\n", taskName (taskIdSelf()));		/*taskName (taskIdSelf()) 得到任务名:tTaskA*/         printf("taskA: I'm done, taskB can now proceed; Releasing semId2 semaphore\n\n");		  /*释放semId2的控制权*/		          if (semGive (semId2) == ERROR)             {             perror ("taskA: Error in semGive");             return (ERROR);             }         }         return (OK);     }   /*****************************************************************************  *  taskA - 等待semId2变为可用, 执行EventB,执行完毕后,释放semId1,  *				以实现任务同步  *  *  返回值: OK or ERROR  *  */ LOCAL STATUS taskB()     {     int count;      for (count = 0; count < numTimes; count++)         {         /*等待semId2变为可用,永远等待*/                  if (semTake (semId2,WAIT_FOREVER) == ERROR)             {             perror ("taskB: Error in semTake");             return (ERROR);             }          printf ("taskB: Synchronized with taskA's release of semId2 - %d times\n",                 (count + 1 ));         printf("This is task  <%s> : Event B now done\n", taskName (taskIdSelf()));	/*taskName (taskIdSelf()) 得到任务名:tTaskB*/         printf("taskB: I'm done, taskA can now proceed; Releasing semId1 semaphore\n\n\n");		  /*释放semId2的控制权*/		 				          if (semGive (semId1) == ERROR)             {             perror ("taskB: Error in semGive");             return (ERROR);             }         }         notDone = FALSE;         return (OK);     } 

⌨️ 快捷键说明

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