71 .
synopsis =
"Example of librnr shared memory mutex.",
74 "The %P command forks to separate process, each with two threads that all " 75 "write to stdout. With mutexing enabled, the output should not be " 76 "intereleaved between the processes and threads." 88 .has_arg = no_argument,
94 .opt_desc =
"Disable shared memory mutex." 99 .long_opt =
"no-color",
101 .has_arg = no_argument,
107 .opt_desc =
"Disable color coding output." 121 static int init(
int argc,
char *argv[])
124 Argv0 = basename(argv[0]);
140 #define CHESHIRE_CAT 1
141 #define WHITE_QUEEN 2
143 #define N_AINW_CHARS 4
144 #define COLOR_RED_QUEEN LOG_COLOR_PRE LOG_COLOR_LIGHT_RED
146 #define COLOR_CHESHIRE_CAT LOG_COLOR_PRE LOG_COLOR_LIGHT_YELLOW 148 #define COLOR_WHITE_QUEEN LOG_COLOR_PRE "1;37m" 150 #define COLOR_MARCH_HARE LOG_COLOR_PRE LOG_COLOR_LIGHT_BLUE 152 #define COLOR_POST LOG_COLOR_POST 178 "Red Queen",
"Cheshire Cat",
179 "White Queen",
"March Hare" 198 "Off with their heads, off with their heads!",
199 "Only the insane equate pain with success.",
200 "But I don't forget and I don't forgive.",
201 "Perhaps as this is May I won't be raving mad at least not as March." 211 clock_gettime(CLOCK_REALTIME, &t);
228 r = (float)n / (
float)RAND_MAX;
229 t = (
uint_t)((
float)max * r);
265 int me = *(
int *)pArg;
281 sColorPre =
Color[me];
293 printf(
"%s", sColorPre); usleep(10000);
295 printf(
"%s", sText); usleep(10000);
296 printf(
"%s", sColorPost); usleep(10000);
398 int main(
int argc,
char *argv[])
402 if( !
init(argc, argv) )
407 switch( (pid = fork()) )
410 LOGERROR(
"Failed to fork child process.");
static void randseed()
Seed random number generator.
#define COLOR_POST
post color string
int OptsCvtArgBool(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options boolean argument to bool_t.
#define N_AINW_CHARS
number of Alice in Wonderland characters
char * Say[4]
Alice in Wonderland character text.
int AinWCharId[2][2]
Alice in Wonderland character ids.
int Who
who: child or parent process
#define WHITE_QUEEN
white queen identifier
static bool_t OptsNoMutex
do [not] disable mutex
Standard command-line options options and parsing.
Program Description Strings Info Structure.
int shm_mutex_init(key_t key, shm_mutex_t *pshmmutex)
Create and initialize a shared memory mutex.
int ThreadState[2]
thread run/die state
char * AinWCharName[4]
Alice in Wonderland character names.
u32_t uint_t
32-bit unsigned integer
#define LOGDIAG1(fmt,...)
Standard Diagnostic Level 1 logging.
#define COLOR_RED_QUEEN
red queen text color
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
#define CHESHIRE_CAT
cheshire cat identifier
const char * synopsis
Simple program synopsis string.
uint_t Seed
random generator seed
static char * Argv0
the command
#define WHO_PARENT
parent process
#define LOGERROR(fmt,...)
Standard Error logging.
#define N_PROCS
number of processes
static void randusleep(uint_t max)
Sleep thread for a random time.
Shared memory structure type.
char * Color[4]
Alice in Wonderland character text colors.
static const PkgInfo_T PkgInfo
#define RED_QUEEN
red queen identifier
key_t ShmKey
share memory key
#define OPTS_NO_SHORT
no short option equivalent
shm_mutex_t ShmMutex
share memory mutex
#define WHO_CHILD
child process
char * WhoName[2]
process name
#define N_THREADS
number of threads/process
static void * threadmain(void *pArg)
Thread main.
RoadNarrows Robotics common configuration file.
char ** OptsGet(const char *argv0, const PkgInfo_T *pPkgInfo, OptsPgmInfo_T *pPgmInfo, OptsInfo_T *pOptsInfo, bool_t bHasLogging, int *pargc, char *argv[])
Gets, validates, and sets all command line options.
Package version information.
int shm_mutex_lock(shm_mutex_t *pshmmutex)
Lock the mutex.
#define COLOR_CHESHIRE_CAT
cheshire cat text color
int shm_mutex_unlock(shm_mutex_t *pshmmutex)
Unlock a lock the mutex.
static void runExample(int who)
Run example for a process.
static void killThreads()
Kill all threads in process.
static bool_t OptsNoColor
do [not] disable color esc sequence
static OptsPgmInfo_T PgmInfo
Program information.
int shm_mutex_destroy(shm_mutex_t *pshmmutex)
Destroy a shared memory mutex.
char * OptsFmtBool(char *buf, size_t buflen, void *pOptVal)
Boolean option value string formatter.
#define COLOR_MARCH_HARE
march hare text color
#define MARCH_HARE
march hare identifier
static void unlock()
Unlock mutex.
#define COLOR_WHITE_QUEEN
white queen text color
static int createThreads()
Create all threads for process.
const char * long_opt
Long option string name.
static OptsInfo_T OptsInfo[]
Command line options information.
static int init(int argc, char *argv[])
Main initialization.
pthread_t Thread[2]
thread
int main(int argc, char *argv[])
Example main.
Short and Long Options Info.
static void lock()
Lock mutex.