c - Change priority on Mutexes -
i'm new rtos programming , i'm having problems priority when using mutexes.
i have following priorities established.
#define t_hi_priority 10 #define t_me_priority 50
and want code run task "tmeprioritytask" highest priority , "thiprioritytask" medium priority. "tloprioritytask" commented and, therefore, should not run now.
#include <stdio.h> #include "main.h" #include "vxworks.h" #include "semlib.h" #include "tasklib.h" sem_id semmutex; // named semaphore object char alphabet[27]; // memory resource have exclusive access void thiprioritytask (void) { int i; // enter critical region - other tasks wanting access alphabet[] should // wait available semaphore semtake (semmutex, wait_forever); // write alphabet global array (i= 0; < 26; i++) alphabet[i] = 'a' + i; alphabet[i] = '\0'; printf("high priority.\n-counting alphabet...\n"); // leave critical region semgive (semmutex); } void tmeprioritytask (void) { // enter critical region semtake (semmutex, wait_forever); //medium priority task enters printf("medium priority.\n-just entering...\n"); // leave critical region semgive (semmutex); } /*void tloprioritytask (void) { // enter critical region semtake (semmutex, wait_forever); // array members guaranteed stable while being read task printf("low priority\n"); printf ("-alphabet= %s ", alphabet); // leave critical region semgive (semmutex); }*/ void main (void) { //create binary semaphore full (available) semmutex = sembcreate (sem_q_priority, sem_full); // spawn high priority task taskspawn ("hi_priority_task", t_me_priority, vx_fp_task, 10000, (funcptr) thiprioritytask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); // spawn medium priority task taskspawn ("me_priority_task", t_hi_priority, vx_fp_task, 10000, (funcptr) tmeprioritytask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); // spawn low priority task //taskspawn ("lo_priority_task", t_lo_priority, vx_fp_task, 10000, (funcptr) tloprioritytask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); }
i've tried change priorities when spawning tasks doesn't seem work, @ least doesn't change on screen. i'm using vxworks rtos.
thank you.
if want guarantee 2 threads run in specific order must use proper synchronization of threads. setting priority , hoping works poor design, might work, break (for example if make blocking call there nothing preventing other thread running.)
here easiest way modify code guarantee tmeprioritytask thread runs completion before thiprioritytask allowed run regardless of priority or else in threads.
#include <stdio.h> #include "main.h" #include "vxworks.h" #include "semlib.h" #include "tasklib.h" sem_id semmutex; // named semaphore object char alphabet[27]; // memory resource have exclusive access void thiprioritytask (void) { int i; // enter critical region - other tasks wanting access alphabet[] should // wait available semaphore semtake (semmutex, wait_forever); // write alphabet global array (i= 0; < 26; i++) alphabet[i] = 'a' + i; alphabet[i] = '\0'; printf("high priority.\n-counting alphabet...\n"); // leave critical region semgive (semmutex); } void tmeprioritytask (void) { // enter critical region //semtake (semmutex, wait_forever); //medium priority task enters printf("medium priority.\n-just entering...\n"); // leave critical region semgive (semmutex); } /*void tloprioritytask (void) { // enter critical region semtake (semmutex, wait_forever); // array members guaranteed stable while being read task printf("low priority\n"); printf ("-alphabet= %s ", alphabet); // leave critical region semgive (semmutex); }*/ void main (void) { //create binary semaphore full (available) semmutex = sembcreate (sem_q_priority, sem_empty); // spawn high priority task taskspawn ("hi_priority_task", t_me_priority, vx_fp_task, 10000, (funcptr) thiprioritytask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); // spawn medium priority task taskspawn ("me_priority_task", t_hi_priority, vx_fp_task, 10000, (funcptr) tmeprioritytask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); // spawn low priority task //taskspawn ("lo_priority_task", t_lo_priority, vx_fp_task, 10000, (funcptr) tloprioritytask, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); }
all did, create semaphore sem_empty, , remove semtake tmeprioritytask.
this way thiprioritytask can run @ priority, , block on semtake until tmeprioritytask issues semgive. furthermore tmeprioritytask thread can other blocking calls (like i/o calls example) , thiprioritytask thread still not able run until semgive.
checkout vxworks api reference sembcreate , read section entitled synchronization.
Comments
Post a Comment