vxworks信号量之疑

今天在运行一个信号量的程序时,感觉结果挺奇怪的,程序如下:

/* include files */
 
 #include "vxWorks.h"
 #include "wdLib.h"
 #include "stdio.h"
 #include "semLib.h"
 #include "taskLib.h"
 #include "usrLib.h"
 #include "sysLib.h"
 
 /* defines */
 
 #define TASK_PRIORITY 101
 #define TASK_STACK_SIZE 5000
 #define TIME_BETWEEN_INTERRUPTS 1 /* 1 tick */
 #define TASK_WORK_TIME 2 /* 2 ticks */
 #define NUM_OF_GIVES 30 
 
 /* globals */
 
 /* counting or binary semaphore ID */
 LOCAL SEM_ID semId = NULL; 
 
 /* watchdog ID */
 LOCAL WDOG_ID wdId = NULL; 
 
 /* tid of syncTask */
 LOCAL int syncTaskTid = 0; 
 
 /* Number of times semGive is called */
 LOCAL int numToGive = NUM_OF_GIVES;
 
 /* forward declaratiuon */
 void syncISR(int);/* ISR to unblock syncTask */
 void cleanUp (); /* cleanup routine */
 void syncTask (); /* task that needs to be synchronized
 * with external events */ 
 
 /******************************************
 * countingSemDemo - demonstrates
 task synchronization using counting
 * semaphores. User can also select to
 use binary semaphore instead of
 * counting semaphore in this demonstration,
 for comparision between the two
 * semaphores.
 *
 * RETURNS: OK or ERROR
 *
 */
 
 STATUS countingSemDemo (
	 char semType /* counting semaphore type
	 'c' or binary semaphore
 	* type 'b'
 	*/)
 {
 switch (semType)
 {
 	case 'c':
 	case 'C':
 		if ((semId = semCCreate(SEM_Q_PRIORITY,0)) == NULL)
 		{
 			perror ("semCCreate");
 			return (ERROR);
 		}
 		break;
 
	 case 'b':
	 case 'B':
		 if ((semId = semBCreate(SEM_Q_PRIORITY,SEM_EMPTY)) == NULL)
 		 {
 			perror ("semBCreate");
 			return (ERROR);
 		 }
 		 break;
 
	 default:
		 printf ("Unknown semType-- must be 'c' or 'b'\n");
	 	 return (ERROR);
 }
 
 if ((wdId = wdCreate()) == NULL)
 {
	 perror ("wdCreate");
 	 cleanUp ();
  	 return (ERROR);
 }
 
 
 if ((syncTaskTid = taskSpawn ("tsyncTask", TASK_PRIORITY,0,TASK_STACK_SIZE,(FUNCPTR) syncTask,0,0,0,0,0,0,0,0,0,0)) == ERROR)
 {
 	perror ("taskSpawn");
 	cleanUp();
 	return (ERROR);
 }
 
 /* watchdog simulates hardware interrupts */
 if (wdStart (wdId, TIME_BETWEEN_INTERRUPTS,(FUNCPTR) syncISR, numToGive)== ERROR)
 {
 	perror ("wdStart");
 	cleanUp ();
 	return (ERROR);
 }
 
 /* arbitrary delay to allow
 program to complete before clean up */
 taskDelay (sysClkRateGet() +((TASK_WORK_TIME + 2) * numToGive));
 
 cleanUp();
 return (OK);
 }
 
 
 /*************************************************
 * syncTask - synchronizes with interrupts using
 * counting or binarysemaphores.
 */
 
 void syncTask (void)
 {
 int eventCount = 0;
 
 FOREVER
 {
 if (semTake (semId, WAIT_FOREVER) == ERROR)
 {
 perror ("syncTask semTake");
 return;
 }
 
 /* Do "work" */
 taskDelay (TASK_WORK_TIME);
 semShow (semId,1);
 
 eventCount++;
 printf ("semaphore taken %d times\n", eventCount);
 }
 
 }
 
 /************************************************
 * syncISR - simulates a hardware device
 which generates interrupts very
 * quickly and synchronizes with
 syncTask using semaphores.
 */
 void syncISR(int times)
 {
 semGive (semId);
 times--;
 if (times > 0)
 wdStart (wdId, TIME_BETWEEN_INTERRUPTS,(FUNCPTR) syncISR, times);
 }
 
 /********************************************
 * cleanUP - deletes the syncTask, deletes the
 * semaphore and the watchdog timer previously created
 * by countingSemDemo.*/
 void cleanUp ()
 {
 if (syncTaskTid)
 taskDelete (syncTaskTid);
 if (semId)
 semDelete (semId);
 if (wdId)
 wdDelete (wdId);
 
 }

 

运行结果:

 

->sp countingSemDemo,'c'

 


Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 2         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 1 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 3         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 2 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 4         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 3 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 5         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 4 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 6         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 5 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 7         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 6 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 8         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 7 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 9         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 8 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 10        
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 9 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 11        
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 10 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 12        
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 11 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 13        
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 12 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 14        
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 13 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 15        
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 14 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 15        
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 15 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 14        
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 16 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 13        
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 17 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 12        
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 18 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 11        
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 19 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 10        
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 20 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 9         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 21 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 8         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 22 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 7         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 23 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 6         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 24 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 5         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 25 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 4         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 26 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 3         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 27 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 2         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 28 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 1         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 29 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : COUNTING  
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
Count               : 0         
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 30 times

->sp countingCountDemo ‘b’

 

 

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 1 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 2 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 3 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 4 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 5 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 6 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 7 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 8 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 9 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 10 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 11 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 12 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 13 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 14 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : FULL      
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 15 times

Semaphore Id        : 0x23ce9d0   
Semaphore Type      : BINARY    
Task Queuing        : PRIORITY  
Pended Tasks        : 0         
State               : EMPTY     
Options             : 0x1	SEM_Q_PRIORITY

VxWorks Events
--------------
Registered Task     : NONE
Event(s) to Send    : N/A
Options             : N/A

semaphore taken 16 times


这个问题暂时放在这,以后慢慢研究,如果哪位高手能指点指点的话,不甚感激。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vxWorks中的信号量是一种同步工具,用于协调多个任务之间的访问资源。它可以用来控制对共享资源的访问,以防止资源竞争和数据冲突。 在vxWorks中,有两种类型的信号量:二进制信号量和计数信号量。二进制信号量用于互斥访问共享资源,当资源被一个任务占用时,其他任务将被阻塞。计数信号量用于多个任务之间的资源共享,它可以指定资源的可用数量,当资源不可用时,任务将被阻塞,直到资源可用。 使用vxWorks信号量时,首先需要创建一个信号量对象,并初始化它的值。可以使用`semBCreate()`函数创建二进制信号量,或使用`semCCreate()`函数创建计数信号量。然后,可以使用`semTake()`函数来获取信号量,表示任务要开始访问资源。如果信号量的值为0,任务将会被阻塞,直到信号量的值大于0。当任务完成对资源的访问后,需要使用`semGive()`函数释放信号量,表示资源可供其他任务使用。 在使用vxWorks信号量时,需要注意以下几点: 1. 保证每个任务在访问资源之前都使用`semTake()`函数获取信号量,以防止资源竞争。 2. 在任务完成对资源的访问后,必须使用`semGive()`函数释放信号量,以便其他任务可以使用资源。 3. 对于二进制信号量,每个任务应该在访问资源之前都获取到信号量,而在访问完成后都释放信号量。而对于计数信号量,任务可以多次获取和释放信号量,直到资源不可用为止。 4. 可以使用信号量来控制任务之间的执行顺序,比如一个任务等待另一个任务完成后才能执行。 总之,vxWorks中的信号量是一种有助于协调多个任务对资源访问的重要工具,通过获取和释放信号量,可以确保对资源的访问按照预期的顺序进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值