VxWorks semaphore usage demo
PATCH 1/1 VxWorks semaphore usage demo
Hello again,
I'm not sure what is the preferred way to commit my changes, so before I commit i'm Attaching my patch in order to get you remarks and further instructions
Thanks
Signature · 4 lines
Ravid Baruch Naali [email protected] +972 4 6732729 +972 52 5830021
skins/vxworks/demos/sem.c +104 -0
@@ -0,0 +1,104 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <errno.h> +#include <vxworks/vxworks.h> +#include <sys/mman.h> + + +#define TASK_NUM 10 +#define STACK_SIZE 1024 +#define xnarch_printf printf + +SEM_ID semID, syncSEM; + +void taskMain (long arg1,long arg2,long arg3,long arg4,long arg5,long arg6,long arg7,long arg8,long arg9,long arg10) +{ +int localerrno; +//Waiting to start synchronized with all other spawned tasks +semTake (syncSEM,WAIT_FOREVER ); +//Taking the counting semaphore to indicate task started running, +//stall main from exiting +STATUS result =semTake (semID,WAIT_FOREVER ); +//Chaking for failures +if (result ==ERROR ){ + localerrno =errnoGet (); +//checking failure reason +switch (localerrno){ +case S_objLib_OBJ_ID_ERROR: +xnprintf ("Semaphore ID is invalid\n" ); +break ; +case S_objLib_OBJ_UNAVAILABLE: +xnprintf ("Semaphore unavailable (NO_WAIT)\n" ); +break ; +case S_objLib_OBJ_TIMEOUT: +xnprintf ("Timeout occured before semaphore was released\n" ); +break ; +case S_semLib_INVALID_OPTION: +xnprintf ("Semaphore type is invalid\n" ); +break ; +case EINTR : +xnprintf ("Received interup\n" ); +break ; +default : +break ; + } +return ; + } + +int i =10 ; +//Getting this task's ID +int ownID =taskIdSelf (); +//Getting this task's pririty +int priority; + result =taskPriorityGet (ownID, &priority); + +xnprintf ("My priority is %d\n" , priority); +while (i){ +printf ("My name is %s my tid=%d\n" ,taskName (ownID), ownID); +sleep (1 ); + i--; + } +//Signaling main that this task as finished +semGive (semID); +//Not realy required but releasing semaphore +semGive (syncSEM); +} + + +int main (int argc,char **argv) +{ + +mlockall (MCL_CURRENT |MCL_FUTURE ); +int tidStat; +int tids[TASK_NUM ]; +//Creating counting semaphore on which main waits until all tasks are done + semID =semCCreate (SEM_Q_PRIORITY ,TASK_NUM ); +if (!semID){ +xnprintf ("Failed to create semaphore\n" ); +exit (1 ); + } +//Creating a binary semaphore to sync all tasks + syncSEM =semBCreate (SEM_Q_PRIORITY ,SEM_EMPTY ); +if (!syncSEM){ +xnprintf ("Failed to create binary semaphore\n" ); +exit (1 ); + } +//creating all the tasks +int i; +for (i =0 ; i <TASK_NUM ; i++){ + tids[i] =taskSpawn (NULL ,25 -i,0 ,STACK_SIZE , taskMain,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ); +xnprintf ("Spawned task: %d\n" , tids[i]); + } +//Giving all tasks a signal to start +semFlush (syncSEM); +//Waiting for all task to complete their main routine +semTake (semID,WAIT_FOREVER ); +for (i =0 ; i <TASK_NUM ; i++) +taskDelete (tids[i]); +semDelete (semID); +semDelete (syncSEM); +}
skins/vxworks/demos/Makefile +4 -1
@@ -7,7 +7,7 @@ STD_CFLAGS := $(shell $(XENO_CONFIG) --xeno-cflags) STD_LDFLAGS := $(shell $(XENO_CONFIG) --xeno-ldflags) -lvxworks -STD_TARGETS := satch +STD_TARGETS := satch sem GCIC := $(prefix)/bin/gcic SIM_CFLAGS := -g
@@ -27,6 +27,9 @@ satch: satch.c $(CC) -o $@ $< $(STD_CFLAGS) $(STD_LDFLAGS) +sem: sem.c + $(CC) -o $@ $< $(STD_CFLAGS) $(STD_LDFLAGS) + satch_sim: satch_sim.o $(GCIC) -o $@ $< $(SIM_LDFLAGS)
Quoted 1 line · Ravid Baruch Naali
Hello again,
Hi,
Quoted 5 lines
I'm not sure what is the preferred way to commit my changes, so before I commit i'm Attaching my patch in order to get you remarks and further instructions
The synchronization on tasks completion with the semID semaphore seems broken. The main thread will get the semahore when the first task completes, not when all the tasks complete.
I see too many comments, for instance:
// Checking for failures
if (error == ERROR)
is useless, it is pretty obvious from reading the code that you are checking for failure. Comments should tell us things that are not obvious.
Now, about the style, this is C code, so use C style comments. I would also prefer that you use the kernel coding style, but this is not kernel code, so I am not sure the rule is to use the kernel coding style.
Signature · 1 line
Gilles Chanteperdrix
Quoted 19 lines · Gilles Chanteperdrix
Quoted 1 line · Ravid Baruch Naali
Hello again,
Hi,
Quoted 5 lines
I'm not sure what is the preferred way to commit my changes, so before I commit i'm Attaching my patch in order to get you remarks and further instructions
The synchronization on tasks completion with the semID semaphore seems broken. The main thread will get the semahore when the first task completes, not when all the tasks complete.
I see too many comments, for instance:
// Checking for failures
if (error == ERROR)
Quoted 8 lines
is useless, it is pretty obvious from reading the code that you are checking for failure. Comments should tell us things that are not obvious.
Now, about the style, this is C code, so use C style comments. I would also prefer that you use the kernel coding style, but this is not kernel code, so I am not sure the rule is to use the kernel coding style.
It is. We do want a common style regardless of the execution space. TIA,
Signature · 1 line
Philippe.
Quoted 25 lines · Philippe Gerum
Quoted 23 lines · Gilles Chanteperdrix
Quoted 2 lines · Ravid Baruch Naali
Hello again,
Hi,
Quoted 4 lines
I'm not sure what is the preferred way to commit my changes, so before I commit i'm Attaching my patch in order to get you remarks and further instructions
The synchronization on tasks completion with the semID semaphore seems broken. The main thread will get the semahore when the first task completes, not when all the tasks complete.
I see too many comments, for instance:
// Checking for failures
if (error == ERROR)is useless, it is pretty obvious from reading the code that you are checking for failure. Comments should tell us things that are not obvious.
Quoted 9 lines
Quoted 5 lines
Now, about the style, this is C code, so use C style comments. I would also prefer that you use the kernel coding style, but this is not kernel code, so I am not sure the rule is to use the kernel coding style.
It is. We do want a common style regardless of the execution space. TIA,
Thanks for the comments I'll adapt and fix it before I'll send it again.
Signature · 4 lines
Ravid Baruch Naali [email protected] +972 4 6732729 +972 52 5830021
Re sending the patch following Gilles comments
Quoted 30 lines · Gilles Chanteperdrix
Quoted 2 lines · Ravid Baruch Naali
Hello again,
Hi,
Quoted 4 lines
I'm not sure what is the preferred way to commit my changes, so before I commit i'm Attaching my patch in order to get you remarks and further instructions
The synchronization on tasks completion with the semID semaphore seems broken. The main thread will get the semahore when the first task completes, not when all the tasks complete.
I see too many comments, for instance:
// Checking for failures
if (error == ERROR)is useless, it is pretty obvious from reading the code that you are checking for failure. Comments should tell us things that are not obvious.
Now, about the style, this is C code, so use C style comments. I would also prefer that you use the kernel coding style, but this is not kernel code, so I am not sure the rule is to use the kernel coding style.
Signature · 4 lines
Ravid Baruch Naali [email protected] +972 4 6732729 +972 52 5830021
skins/vxworks/demos/sem.c +138 -0
@@ -0,0 +1,138 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <errno.h> +#include <vxworks/vxworks.h> +#include <sys/mman.h> + + +#define TASK_NUM 10 +#define STACK_SIZE 1024 +#define LOOPS 10 +#define xnarch_printf printf +#define TOP_PRIORITY 25 + +SEM_ID task_counter_sem; +SEM_ID task_sync_sem; + +static errno_to_messgae(char *failing_func); + +/* +* A shared main routine for all tasks other then the main +* each task waits for the signal from the main task +* then it notify it's existing +* +* It then loops and prints out LOOPS times it's name and tid +* when done it notifies +* +* None of the parameters are being used +*/ +void task_main_routine (long arg1,long arg2,long arg3,long arg4,long arg5,long arg6, +long arg7,long arg8,long arg9,long arg10) +{ +int localerrno; +STATUS result; +int priority; +int i =LOOPS ; +int ownID =taskIdSelf (); + +/*Waiting to start synchronized with all other spawned tasks*/ + result =semTake (task_sync_sem,WAIT_FOREVER ); +if (result ==ERROR ) { +errno_to_messgae ("semTake counter" ); +return ; + } +/*Taking the counting semaphore to indicate task started running stall +* main from exiting */ + result =semTake (task_counter_sem,WAIT_FOREVER ); +if (result ==ERROR ) { +errno_to_messgae ("semTake counter" ); +return ; + } + result =taskPriorityGet (ownID, &priority); + +xnprintf ("Task priority is %d\n" , priority); +while (i) { +xnprintf ("Task name is %s tid=%d\n" ,taskName (ownID), ownID); +sleep (1 ); + i--; + } +/*Signaling this task is done*/ +semGive (task_counter_sem); +} + +/* +* Main task: +* spawns TASK_NUM tasks synchronize their operation +* exit and clean up when all tasks are done +*/ +int main (int argc,char **argv) +{ +/*array holding all tasks ids*/ +int tids[TASK_NUM ]; +int i; +mlockall (MCL_CURRENT |MCL_FUTURE ); +/* counting semaphore inital value set to the number of tasks*/ + task_counter_sem =semCCreate (SEM_Q_PRIORITY ,TASK_NUM ); +if (!task_counter_sem) { +xnprintf ("Failed to create semaphore\n" ); +exit (1 ); + } +/* semaphore to synchronize all task operation */ + task_sync_sem =semBCreate (SEM_Q_PRIORITY ,SEM_EMPTY ); +if (!task_sync_sem) { +xnprintf ("Failed to create binary semaphore\n" ); +exit (1 ); + } +for (i =0 ; i <TASK_NUM ; i++) { + tids[i] =taskSpawn (NULL , (TOP_PRIORITY - i),0 ,STACK_SIZE , + task_main_routine,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ); +xnprintf ("Spawned task: %d\n" , tids[i]); + } +/* Signaling all tasks to run */ +semFlush (task_sync_sem); +/* Waiting for all task to complete their main routine */ +for (i =0 ; i <TASK_NUM ; i++) +semTake (task_counter_sem,WAIT_FOREVER ); +/* Cleanup */ +for (i =0 ; i <TASK_NUM ; i++) +taskDelete (tids[i]); +semDelete (task_counter_sem); +semDelete (task_sync_sem); +} + + +/* +* Prints out the meaning of an error with relation to the failing function +* +* parameter: +* a prefix string to the failure reason +*/ +static errno_to_messgae(char *failing_func) +{ +int localerrno =errnoGet (); +xnprintf ("%s: " , failing_func); +switch (localerrno){ +case S_objLib_OBJ_ID_ERROR: +xnprintf ("Semaphore ID is invalid\n" ); +break ; +case S_objLib_OBJ_UNAVAILABLE: +xnprintf ("Semaphore unavailable (NO_WAIT)\n" ); +break ; +case S_objLib_OBJ_TIMEOUT: +xnprintf ("Timeout occured before semaphore was" +" released\n" ); +break ; +case S_semLib_INVALID_OPTION: +xnprintf ("Semaphore type is invalid\n" ); +break ; +case EINTR : +xnprintf ("Received interup\n" ); +break ; +default : +break ; + } +}
skins/vxworks/demos/Makefile +4 -1
@@ -7,7 +7,7 @@ STD_CFLAGS := $(shell $(XENO_CONFIG) --xeno-cflags) STD_LDFLAGS := $(shell $(XENO_CONFIG) --xeno-ldflags) -lvxworks -STD_TARGETS := satch +STD_TARGETS := satch sem GCIC := $(prefix)/bin/gcic SIM_CFLAGS := -g
@@ -27,6 +27,9 @@ satch: satch.c $(CC) -o $@ $< $(STD_CFLAGS) $(STD_LDFLAGS) +sem: sem.c + $(CC) -o $@ $< $(STD_CFLAGS) $(STD_LDFLAGS) + satch_sim: satch_sim.o $(GCIC) -o $@ $< $(SIM_LDFLAGS)
Quoted 27 lines · Ravid Baruch Naali
Quoted 25 lines · Gilles Chanteperdrix
Quoted 2 lines · Ravid Baruch Naali
Hello again,
Hi,
Quoted 4 lines
I'm not sure what is the preferred way to commit my changes, so before I commit i'm Attaching my patch in order to get you remarks and further instructions
The synchronization on tasks completion with the semID semaphore seems broken. The main thread will get the semahore when the first task completes, not when all the tasks complete.
I see too many comments, for instance:
// Checking for failures
if (error == ERROR)is useless, it is pretty obvious from reading the code that you are checking for failure. Comments should tell us things that are not obvious.
Quoted 6 lines
Quoted 5 lines
Now, about the style, this is C code, so use C style comments. I would also prefer that you use the kernel coding style, but this is not kernel code, so I am not sure the rule is to use the kernel coding style.
Re sending the patch following Gilles comments
Looks good to me, except for the typo in the errno_to_message function. Philippe, could you apply this patch ? I am changing ISP, so I have not internet at home.
Signature · 1 line
Gilles Chanteperdrix
Quoted 33 lines · Gilles Chanteperdrix
Quoted 32 lines · Ravid Baruch Naali
Quoted 30 lines · Gilles Chanteperdrix
Quoted 2 lines · Ravid Baruch Naali
Hello again,
Hi,
Quoted 4 lines
I'm not sure what is the preferred way to commit my changes, so before I commit i'm Attaching my patch in order to get you remarks and further instructions
The synchronization on tasks completion with the semID semaphore seems broken. The main thread will get the semahore when the first task completes, not when all the tasks complete.
I see too many comments, for instance:
// Checking for failures
if (error == ERROR)is useless, it is pretty obvious from reading the code that you are checking for failure. Comments should tell us things that are not obvious.
Now, about the style, this is C code, so use C style comments. I would also prefer that you use the kernel coding style, but this is not kernel code, so I am not sure the rule is to use the kernel coding style.
Quoted 6 lines
Quoted 1 line
Re sending the patch following Gilles comments
Looks good to me, except for the typo in the
errno_to_messagefunction. Philippe, could you apply this patch ? I am changing ISP, so I have not internet at home.
Ok, will merge.
Signature · 1 line
Philippe.