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

📄 priorityinversion.c

📁 嵌入式VxWorks开发所需典型例程源代码
💻 C
字号:
/* include files*/

#include "vxWorks.h"
#include "taskLib.h"
#include "kernelLib.h"
#include "sysLib.h"
#include "logLib.h"
#include "stdio.h"
#include "semLib.h"


/* function defined */
void taskLow(void);
void taskMiddle(void);
void taskHigh(void);

/* global varibles*/
#define ITER  3
#define HIGH 101
#define MIDDLE 102
#define LOW 103
#define LONG_TIME 3000000
SEM_ID semMutex;

void Inversion(void)/*function to create three tasks*/
{
	int i,taskIdLow,taskIdMiddle,taskIdHigh;
	printf("\n\n.............#RUNNING#...............\n\n");
	
	/* CREATE a mutex semaphore*/
	semMutex=semMCreate(SEM_Q_PRIORITY);
	   
	/* Create three tasks ,each has a unique priority*/
	if( taskIdLow=taskSpawn("task1", LOW,0x100,20000,(FUNCPTR)taskLow,0,0,0,0,0,0,0,0,0,0)==ERROR)
	   printf("\nTaskSpawn taskone failed\n");
	if( taskIdMiddle=taskSpawn("task2", MIDDLE,0x100,20000,(FUNCPTR)taskMiddle,0,0,0,0,0,0,0,0,0,0)==ERROR)
	   printf("\nTaskSpawn tasktwo failed\n");
   	if( taskIdHigh=taskSpawn("task3", HIGH,0x100,20000,(FUNCPTR)taskHigh,0,0,0,0,0,0,0,0,0,0)==ERROR)
	   printf("\nTaskSpawn taskthree failed\n");
}

void taskLow(void)
{
	int i,j;
	for(i=0;i<ITER;i++)
	{
		semTake(semMutex,WAIT_FOREVER);/* WAIT indefinitely for the semaphore*/
		logMsg("taskLow locks the semaphore\n",0,0,0,0,0,0);
		
		for(j=0;j<LONG_TIME;j++);
		
		/* taskLow gives up the semaphore*/
		logMsg("tasklow unlocks the semaphore\n",0,0,0,0,0,0);
		semGive(semMutex);
		
	}
	logMsg("taskLow end!\n",0,0,0,0,0,0);
}

void taskMiddle(void)
{
	int i;
	taskDelay(5);/* make taskLow runs first*/
	
	for(i=0;i<LONG_TIME*10;i++)
	{
		if ( (i%LONG_TIME)==0 )
		        logMsg("TaskMiddle is running\n",0,0,0,0,0,0);
		
	}
	logMsg("taskMiddle end!\n",0,0,0,0,0,0);
}

void taskHigh(void)
{
	int i,j;
	taskDelay(6);
	
	for(i=0;i<ITER;i++)
	{
		logMsg("taskHigh wants to get the semaphore\n",0,0,0,0,0,0);
		semTake(semMutex,WAIT_FOREVER);
		
		logMsg("taskHigh wants to LOCK the semaphore\n",0,0,0,0,0,0);
		for(j=0;j<LONG_TIME;j++);/*Allow time to context switch*/
		semGive(semMutex);
	}
	logMsg("taskHigh end!\n",0,0,0,0,0,0);
}

⌨️ 快捷键说明

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