base/libs/qdecoder/qSem.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/sem.h>
#include "qDecoder.h"
#include "qInternal.h"

Include dependency graph for qSem.c:

Go to the source code of this file.

FunctionsTitleTestFour

bool qSemCheck (int semid, int semno)
bool qSemEnter (int semid, int semno)
bool qSemEnterForce (int semid, int semno, int maxwaitms, bool *forceflag)
bool qSemEnterNowait (int semid, int semno)
bool qSemFree (int semid)
int qSemGetId (const char *keyfile, int keyid)
int qSemInit (const char *keyfile, int keyid, int nsems, bool ifexistdestroy)
bool qSemLeave (int semid, int semno)


Detailed Description

Semaphore Handling API

Note:
   [daemon main]
   #define MAX_SEMAPHORES (2)

   // create semaphores
   int semid = qSemInit("/some/file/for/generating/unique/key", 'q', MAX_SEMAPHORES, true);
   if(semid < 0) {
     printf("ERROR: Can't initialize semaphores.\n");
     return -1;
   }

   // fork childs
   (... child forking codes ...)

   // at the end of daemon, free semaphores
   if(semid >= 0) qSemFree(semid);

   [forked child]
   // critical section for resource 0
   qSemEnter(0);
   (... guaranteed as atomic procedure ...)
   qSemLeave(0);

   (... some codes ...)

   // critical section for resource 1
   qSemEnter(1);
   (... guaranteed as atomic procedure ...)
   qSemLeave(1);

   [other program which uses resource 1]
   int semid = qSemGetId("/some/file/for/generating/unique/key", 'q');
   if(semid < 0) {
     printf("ERROR: Can't get semaphore id.\n");
     return -1;
   }

   // critical section for resource 1
   qSemEnter(1);
   (... guaranteed as atomic procedure ...)
   qSemLeave(1);

Definition in file qSem.c.


Generated on Sat Sep 23 07:00:40 2017 for JSOC_Documentation by  doxygen 1.5.7.1