|
From: <jmk...@us...> - 2003-08-14 04:43:12
|
Update of /cvsroot/emc/rtapi/examples/semaphore
In directory sc8-pr-cvs1:/tmp/cvs-serv13345/examples/semaphore
Modified Files:
README common.h master.c slave.c
Log Message:
Converted shared memory and semaphores from handles to IDs. Added more /proc entries. Examples work under RTAI. Still need to convert fifos.
Index: README
===================================================================
RCS file: /cvsroot/emc/rtapi/examples/semaphore/README,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -C2 -d -r1.1.1.1 -r1.2
*** README 8 Jul 2003 15:54:39 -0000 1.1.1.1
--- README 14 Aug 2003 04:08:26 -0000 1.2
***************
*** 2,20 ****
processes.
The master process (master.c) creates a semaphore,
'master_sem', and runs a period timer task every millisecond that
gives the semaphore and accumulates a local count of the number of
! times it has been given.
The slave process (slave.c) creates a task that blocks on the global
semaphore 'master_sem' and accumulates a local count of the number of
! times it has been taken. The slave task is not attached to a timer,
! accomplished by passing a 0 for the 'period_nsec' argument of
! rtapi_task_start() and then manually running its first occurrence by
! calling rtapi_task_resume().
Since the slave process references a global variable, 'master_sem',
declared in common.h and defined in master.c, master.o must be loaded
! before slave.o or an undefined symbol will result.
The cumulative counts are printed to the console when the tasks are
--- 2,36 ----
processes.
+ NOTE: The basic concepts of this example haven't changes since
+ this was originally written, but some details have changed.
+ See the notes below.
+
The master process (master.c) creates a semaphore,
'master_sem', and runs a period timer task every millisecond that
gives the semaphore and accumulates a local count of the number of
! times it has been given.
! NOTE: Changed to every 1 second to make prints to the kernel message
! log readable.
The slave process (slave.c) creates a task that blocks on the global
semaphore 'master_sem' and accumulates a local count of the number of
! times it has been taken.
! NOTE: The semaphore is no longer global, instead the master and
! slave share it by opening it with the same key.
!
! The slave task is not attached to a timer, accomplished by passing
! a 0 for the 'period_nsec' argument of rtapi_task_start() and then
! manually running its first occurrence by calling rtapi_task_resume().
! NOTE: The slave is still not attached to a timer. I refer to this
! as being "free-running". However the task_new and task_start api
! has changed, you no longer call task_start for a free-running task,
! instead you call task_resume only.
Since the slave process references a global variable, 'master_sem',
declared in common.h and defined in master.c, master.o must be loaded
! before slave.o or an undefined symbol will result.
! NOTE: No longer true. Since both modules open the semaphore using
! a key, they can be loaded in any order. The semaphore is not global,
! and the only thing in common.h is a #define for the key.
The cumulative counts are printed to the console when the tasks are
***************
*** 32,36 ****
order), and prints the tail of the log file to show output like this:
! Jun 24 07:28:49 letti kernel: slavetask: slave count is 1037
! Jun 24 07:28:49 letti kernel: mastertask: master count is 1048
--- 48,52 ----
order), and prints the tail of the log file to show output like this:
! Jun 24 07:28:49 letti kernel: slavetask: slave count is 1037
! Jun 24 07:28:49 letti kernel: mastertask: master count is 1048
Index: common.h
===================================================================
RCS file: /cvsroot/emc/rtapi/examples/semaphore/common.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** common.h 23 Jul 2003 01:22:49 -0000 1.2
--- common.h 14 Aug 2003 04:08:26 -0000 1.3
***************
*** 2,6 ****
#define COMMON_H
! extern rtapi_sem_handle master_sem; /* the global semaphore */
#endif /* COMMON_H */
--- 2,6 ----
#define COMMON_H
! #define SEM_KEY 101 /* the key used to open the semaphore */
#endif /* COMMON_H */
Index: master.c
===================================================================
RCS file: /cvsroot/emc/rtapi/examples/semaphore/master.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** master.c 13 Aug 2003 05:55:07 -0000 1.9
--- master.c 14 Aug 2003 04:08:26 -0000 1.10
***************
*** 9,15 ****
#include "rtapi.h"
#include "rtapi_app.h" /* rtapi_app_main,exit() */
! #include "common.h" /* master_sem decl */
! rtapi_sem_handle master_sem; /* the global semaphore */
static int master_task; /* the task ID */
static unsigned int master_count = 0;
--- 9,15 ----
#include "rtapi.h"
#include "rtapi_app.h" /* rtapi_app_main,exit() */
! #include "common.h" /* semaphore key */
! static int master_sem; /* the semaphore ID */
static int master_task; /* the task ID */
static unsigned int master_count = 0;
***************
*** 42,48 ****
/* create the semaphore */
! retval = rtapi_sem_new( &master_sem );
! if ( retval != RTAPI_SUCCESS ) {
! rtapi_print( "sem master init: rtapi_sem_new returned %d\n", retval );
return -1;
}
--- 42,48 ----
/* create the semaphore */
! master_sem = rtapi_sem_new( SEM_KEY );
! if ( master_sem < 0 ) {
! rtapi_print( "sem master init: rtapi_sem_new returned %d\n", master_sem );
return -1;
}
Index: slave.c
===================================================================
RCS file: /cvsroot/emc/rtapi/examples/semaphore/slave.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** slave.c 13 Aug 2003 05:55:07 -0000 1.7
--- slave.c 14 Aug 2003 04:08:26 -0000 1.8
***************
*** 9,14 ****
#include "rtapi.h"
#include "rtapi_app.h" /* rtapi_app_main,exit() */
! #include "common.h" /* master_sem */
static int slave_task; /* the task ID */
static unsigned int slave_count = 0;
--- 9,15 ----
#include "rtapi.h"
#include "rtapi_app.h" /* rtapi_app_main,exit() */
! #include "common.h" /* semaphore key */
+ static int slave_sem; /* the semaphore ID */
static int slave_task; /* the task ID */
static unsigned int slave_count = 0;
***************
*** 19,23 ****
{
while (1) {
! rtapi_sem_take(master_sem);
slave_count++;
rtapi_print ( "slave: got sem, count = %d\n", slave_count );
--- 20,24 ----
{
while (1) {
! rtapi_sem_take(slave_sem);
slave_count++;
rtapi_print ( "slave: got sem, count = %d\n", slave_count );
***************
*** 37,40 ****
--- 38,48 ----
}
+ /* open the semaphore */
+ slave_sem = rtapi_sem_new( SEM_KEY );
+ if ( slave_sem < 0 ) {
+ rtapi_print( "sem slave init: rtapi_sem_new returned %d\n", slave_sem );
+ return -1;
+ }
+
/* set the task priority to the lowest one; the master is higher */
slave_prio = rtapi_prio_lowest();
***************
*** 76,79 ****
--- 84,92 ----
}
+ retval = rtapi_sem_delete( slave_sem );
+ if ( retval != RTAPI_SUCCESS ) {
+ rtapi_print("sem slave exit: rtapi_sem_delete returned %d\n", retval );
+ return;
+ }
rtapi_print("sem slave exit: slave count is %d\n", slave_count);
|