📄 mutexfix.c
字号:
#include "vxWorks.h"
#include "stdio.h"
#include "taskLib.h"
#include "semLib.h" /* Need to add this */
int v1;
int v2;
int count;
SEM_ID myMutexSem;
void makeMyMutexSem (void)
{
myMutexSem = semMCreate (SEM_Q_PRIORITY | SEM_INVERSION_SAFE |
SEM_DELETE_SAFE);
if (myMutexSem == NULL)
{
perror ("makeMyMutexSem");
exit (1);
}
}
void mutex1 (void)
{
/* Create semaphore to solve mutex problem */
if (myMutexSem == NULL)
{
printErr ("Error: semaphore not initialized\n");
printErr ("To initialize, type \"makeMyMutexSem()\"\n");
exit (1);
}
FOREVER
{
/* Must update v1 and v2 atomically */
semTake (myMutexSem, WAIT_FOREVER);
v1 = count;
v2 = count;
semGive (myMutexSem);
count++;
}
}
void mutex2 (void)
{
int same;
if (myMutexSem == NULL)
{
printErr ("Error: semaphore not initialized\n");
printErr ("To initialize, type \"makeMyMutexSem()\"\n");
exit (1);
}
FOREVER
{
/*
* This instruction actually corresponds to
* several assembler instructions. We must
* make it atomic.
*/
semTake (myMutexSem, WAIT_FOREVER);
same = (v1 == v2);
semGive (myMutexSem);
if (!same)
printf ("v1 = %d, v2 = %d\n", v1, v2);
taskDelay (1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -