📄 semaphor.c
字号:
/*
* File: semaphor.c
*
* uC/OS Real-time multitasking kernel for the MIPS processor.
*
* This program is an example of using semaphore to
* implement task rendevous.
*
* Created by Marco Graziano (marcog@crl.com).
*
*/
#include "ucos.h" /* uC/OS interface */
/* allocate memory for tasks' stacks */
#define STACKSIZE 2048
uint Stack1[STACKSIZE];
uint Stack2[STACKSIZE];
/* semaphores event control blocks */
OS_EVENT *Sem1;
OS_EVENT *Sem2;
/*
* Task running at the highest priority.
*/
void
Task1(void *i)
{
uint Reply;
uint Time;
for (;;) {
/* wait for the semaphore */
OSSemPend(Sem2, 100, &Reply);
/* print task's id */
/* putchar('1'); */
Time = OSTimeGet();
printf( "1 %d\n", Time );
/* signal the semaphore */
OSSemPost(Sem1);
}
}
void
Task2(void *i)
{
uint Reply;
uint Time;
for (;;) {
/* wait for the semaphore */
OSSemPend(Sem1, 100, &Reply);
/* print task's id */
/* putchar('2'); */
Time = OSTimeGet();
printf( "2 %d\n", Time );
/* signal the semaphore */
OSSemPost(Sem2);
}
}
/*
* Main function.
*/
int
main(int argc, char **argv)
{
char Id1 = '1';
char Id2 = '2';
/* needed by uC/OS */
OSInit();
/*
* create the semaphores
*/
Sem1 = OSSemCreate(1);
Sem2 = OSSemCreate(1);
/*
* create the tasks in uC/OS and assign decreasing
* priority to them
*/
OSTaskCreate(Task1, (void *)&Id1, (void *)&Stack1[STACKSIZE], 1);
OSTaskCreate(Task2, (void *)&Id2, (void *)&Stack2[STACKSIZE], 2);
CPUInit();
/* start the os */
OSStart();
/* never reached */
} /* main */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -