📄 main.c
字号:
/* * main.c (2004-06-07) * * Copyright (C) 2004 * T-Engine Application Development Centre (TEADEC) * * To demonstrate the use of semaphore as an object indicating the * availability of a resource and its quantity as a numerical value */#include <basic.h>#include <tk/tkernel.h>#include <stdio.h>#ifdef DEBUG#include <util/tmonitor.h>#endifIMPORT void task1(INT, VP);ID taskid1 = -1;ID taskid2 = -1;ID sem_id = -1;ID flg_id = -1;/*****************************************************************task1 and task 2 trying to acquire resourcetask 1 wait for task 2 before rel sem and event flg. ****************************************************************/IMPORT void task1(INT stacd, VP exinf){ int cnt = 0; ER ercd = 0; UINT p_flg; printf("This is task %d, going for 20 loop\n", taskid1); while (cnt < 20) { printf("task %d try get semaphore\n", taskid1); tk_wai_sem(sem_id, 1, TMO_FEVR); printf("enter critical section task %d: cnt = %d\n", taskid1, cnt++); tk_dly_tsk(350); printf("delay task 1 over leave critical section\n"); tk_sig_sem(sem_id, 1); } printf("wait for flag\n\n"); ercd = tk_wai_flg(flg_id, 0x0001, TWF_ANDW | TWF_BITCLR, &p_flg, TMO_FEVR); if (ercd < 0) printf("wait flag error %x", ercd); tk_del_sem(sem_id); tk_del_flg(flg_id); printf("task 1 exit and del task, flg, semaphore now\n"); taskid1 = -1; sem_id = -1; flg_id = -1; tk_exd_tsk();}/*****************************************************************task1 and task 2 trying to acquire resource ****************************************************************/IMPORT void task2(INT stacd, VP exinf){ int cnt = 0; ER ercd = 0; printf("\tThis is task %d, going for 20 loop\n", taskid2); while (cnt < 20) { printf("\ttask %d try get semaphore\n", taskid2); tk_wai_sem(sem_id, 1, TMO_FEVR); printf("\tenter critical section task %d: cnt = %d\n", taskid2, cnt++); tk_dly_tsk(600); printf("\tdelay task 2 over leave critical section\n"); tk_sig_sem(sem_id, 1); } ercd = tk_set_flg(flg_id, 0x0001); if (ercd < 0) { printf("\tset flag error %x", ercd); } printf("\ttask 2 exit and del task now\n"); taskid2 = -1; tk_exd_tsk();}/******************************************************************************main******************************************************************************/EXPORT ER main( INT ac, UB *av[] ){ T_CTSK ctsk; T_CSEM sem; T_CFLG flg; ER ercd = 0;#ifdef DEBUG tm_monitor();#endif printf("main: (ac=%d)\n", ac); if (ac < 0) { if (taskid1 >= 0) { tk_ter_tsk(taskid1); tk_del_tsk(taskid1); } if (taskid2 >= 0) { tk_ter_tsk(taskid2); tk_del_tsk(taskid2); } goto ext; } sem.exinf = (VP)0x00000000; sem.sematr = TA_TFIFO | TA_FIRST; sem.isemcnt = 1; sem.maxsem = 1; sem_id = tk_cre_sem(&sem); printf("tk_cre_sem: (sem id = %d)\n", sem_id); if (sem_id < E_OK) goto ext; flg.exinf = (VP)0x00000000; flg.flgatr = TA_TFIFO | TA_FIRST; flg.iflgptn = 0x0000; flg_id = tk_cre_flg(&flg); printf("tk_cre_flg: (flg id = %d)\n", flg_id); if (flg_id < E_OK) { tk_del_sem(sem_id); goto ext; } ercd = tk_clr_flg(flg_id, 0x0000); if (ercd < 0) { tk_del_sem(sem_id); goto ext; } ctsk.exinf =(VP)0x74736574; ctsk.tskatr = TA_HLNG | TA_RNG0; ctsk.task = task1; ctsk.itskpri = 80; ctsk.stksz = 1024 * 4; taskid1 = tk_cre_tsk(&ctsk); printf("tk_cre_tsk: (task1id = %d)\n", taskid1); if (taskid1 < E_OK) { tk_del_sem(sem_id); tk_del_flg(flg_id); goto ext; } ctsk.exinf = (VP)0x74736574; ctsk.tskatr = TA_HLNG | TA_RNG0; ctsk.task = task2; ctsk.itskpri = 80; ctsk.stksz = 1024 * 4; taskid2 = tk_cre_tsk(&ctsk); printf("tk_cre_tsk: (task2id = %d)\n", taskid2); if (taskid2 < E_OK) { tk_del_sem(sem_id); tk_del_flg(flg_id); tk_del_tsk(taskid1); goto ext; } printf("start tasks now\n"); tk_sta_tsk(taskid1, 0); tk_dly_tsk(500); tk_sta_tsk(taskid2, 0); /*end*/ ext: printf("main ended\n\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -