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

📄 synchronize.c

📁 软件简介 1.vxworks中有关多任务信号量的例子 2.本软件基于c语言开发 3.开发平台为windriver公司的tornada开发平台 4.经过交叉调试后测试成功
💻 C
字号:
/* synchronize.c - intertask synchronization using binary semaphores. 
 * DESCRIPTION
 * Creates two (semId1 and semId2) binary semaphore for intertask
 * synchronization between two tasks (taskA and taskB). taskA need to execute
 * an event (event A). On completion of event A, taskB needs to execute
 * another event (event B). On completion of the event B, taskA needs to
 * execute event A. This process needs to be done iteratively. synchronaze
 * executes this process.
 *
 */

#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;
LOCAL SEM_ID semId2;
LOCAL int numTimes = 3;
LOCAL BOOL notDone;
LOCAL STATUS taskA();
LOCAL STATUS taskB();

STATUS synchronize()
    {
    notDone = TRUE;

    /* semaphore semId1 is availble after creation */
    if ((semId1 = semBCreate(SEM_Q_PRIORITY,SEM_FULL)) == NULL)
	{
	perror ("synchronize: Error in creating semId1 semaphore");
	return (ERROR);
	}

    /* semaphore semId2 is not availble after creation */
    if ((semId2 = semBCreate(SEM_Q_PRIORITY,SEM_EMPTY)) == NULL)
	{
	perror ("synchronize: Error in creating semId2 semaphore");
	return (ERROR);
	}

    /* Spawn taskA */
    if (taskSpawn ("tTaskA",TASK_PRI,0,TASK_STACK_SIZE,(FUNCPTR)taskA,
		0,0,0,0,0,0,0,0,0,0) == ERROR)
	{
	perror ("synchronize: Error in spawning taskA");
	return (ERROR);
	}

    /* Spawn taskB */
    if (taskSpawn ("tTasB",TASK_PRI,0,TASK_STACK_SIZE,(FUNCPTR)taskB,
		0,0,0,0,0,0,0,0,0,0) == ERROR)
	{
	perror ("synchronize: Error in spawning taskB");
	return (ERROR);
	}

    while(notDone)
	taskDelay(sysClkRateGet());

    /* Delete created semaphores */
    if (semDelete(semId1) == ERROR)
	{
	perror ("synchronize: Error in deleting semId1 semaphore");
	return (ERROR);
	}
    if (semDelete(semId2) == ERROR)
	{
	perror ("synchronize: Error in deleting semId2 semaphore");
	return (ERROR);
	}

    printf ("\n\n synchronize now completed \n");
    return (OK);
    }


LOCAL STATUS taskA()
    {
    int count;

    for (count = 0; count < numTimes; count++ )
	{
	if (semTake (semId1,WAIT_FOREVER) == ERROR)
	    {
	    perror ("taskA: Error in semaTake");
	    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()));
	printf ("taskA: I'm done, taskB can now proceed; Releaseing semId2 semaphore\n\n");
	
	/* make taskB available */
	if (semGive (semId2) == ERROR)
	    {
	    perror ("taskA: Error in semGive");
	    return (ERROR);
	    }
	}
    return (OK);
    }



LOCAL STATUS taskB()
    {
    int count;

    for (count = 0; count < numTimes; count++ )
	{
	if (semTake (semId2,WAIT_FOREVER) == ERROR)
	    {
	    perror ("taskB: Error in semaTake");
	    return (ERROR);
	    }
	printf ("taskB: Synchronize with taskA's release of semId2 - %d times\n",
		(count+1));
	printf ("This is task <%s>: Event B now done\n", taskName(taskIdSelf()));
	printf ("taskB: I'm done, taskA can now proceed; Releasing semId1 semaphore\n\n\n");
	
	/* make taskA available */
	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 + -