qLibc

Functions
qsem.c File Reference

Semaphore APIs. More...

Go to the source code of this file.

## Functions
int | qsem_init (const char *keyfile, int keyid, int nsems, bool recreate)
| Initialize semaphore.
int | qsem_getid (const char *keyfile, int keyid)
| Get semaphore identifier by keyfile and keyid for the existing semaphore.
bool | qsem_enter (int semid, int semno)
| Turn on the flag of semaphore then entering critical section.
bool | qsem_enter_nowait (int semid, int semno)
| Try to turn on the flag of semaphore.
bool | qsem_enter_force (int semid, int semno, int maxwaitms, bool *forceflag)
| Force to turn on the flag of semaphore.
bool | qsem_leave (int semid, int semno)
| Turn off the flag of semaphore then leaving critical section.
bool | qsem_check (int semid, int semno)
| Get the status of semaphore.
bool | qsem_free (int semid)
| Release semaphore to system.

## Detailed Description

Semaphore APIs.

Note
  [daemon main]
  #define MAX_SEMAPHORES (2)
  // create semaphores
  int semid = qsem_init("/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) qsem_free(semid);
  [forked child]
  // critical section for resource 0
  qsem_enter(semid, 0);
  (... guaranteed as atomic procedure ...)
  qsem_leave(semid, 0);
  (... some codes ...)
  // critical section for resource 1
  qsem_enter(semid, 1);
  (... guaranteed as atomic procedure ...)
  qsem_leave(semid, 1);
  [other program which uses resource 1]
  int semid = qsem_getid("/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
  qsem_enter(semid, 1);
  (... guaranteed as atomic procedure ...)
  qsem_leave(semid, 1);
  qsem_leave
  bool qsem_leave(int semid, int semno)
  Turn off the flag of semaphore then leaving critical section.
  Definition qsem.c:263
  qsem_getid
  int qsem_getid(const char *keyfile, int keyid)
  Get semaphore identifier by keyfile and keyid for the existing semaphore.
  Definition qsem.c:155
  qsem_free
  bool qsem_free(int semid)
  Release semaphore to system.
  Definition qsem.c:298
  qsem_init
  int qsem_init(const char *keyfile, int keyid, int nsems, bool recreate)
  Initialize semaphore.
  Definition qsem.c:102
  qsem_enter
  bool qsem_enter(int semid, int semno)
  Turn on the flag of semaphore then entering critical section.
  Definition qsem.c:180

Definition in file qsem.c.

## Function Documentation

## ◆ qsem_init()

int qsem_init | ( | const char * | keyfile,
|  | int | keyid,
|  | int | nsems,
|  | bool | recreate
| ) |  |

Initialize semaphore.

Parameters

  keyfile | seed for generating unique IPC key
  keyid | seed for generating unique IPC key
  nsems | number of semaphores to initialize
  recreate | set to true to re-create semaphore if exists

Returns
  non-negative shared memory identifier if successful, otherwise returns -1

int semid = qsem_init("/tmp/mydaemon.pid", 'q', 10, true);

Definition at line 102 of file qsem.c.

## ◆ qsem_getid()

int qsem_getid | ( | const char * | keyfile,
|  | int | keyid
| ) |  |

Get semaphore identifier by keyfile and keyid for the existing semaphore.

Parameters

  keyfile | seed for generating unique IPC key
  keyid | seed for generating unique IPC key

Returns
  non-negative shared memory identifier if successful, otherwise returns -1

Definition at line 155 of file qsem.c.

## ◆ qsem_enter()

bool qsem_enter | ( | int | semid,
|  | int | semno
| ) |  |

Turn on the flag of semaphore then entering critical section.

Parameters

  semid | semaphore identifier
  semno | semaphore number

Returns
  true if successful, otherwise returns false

Note
  If the semaphore is already turned on, this will wait until released

Definition at line 180 of file qsem.c.

## ◆ qsem_enter_nowait()

bool qsem_enter_nowait | ( | int | semid,
|  | int | semno
| ) |  |

Try to turn on the flag of semaphore.

If it is already turned on, do not wait.

Parameters

  semid | semaphore identifier
  semno | semaphore number

Returns
  true if successful, otherwise(already turned on by other) returns false

Definition at line 202 of file qsem.c.

## ◆ qsem_enter_force()

bool qsem_enter_force | ( | int | semid,
|  | int | semno,
|  | int | maxwaitms,
|  | bool * | forceflag
| ) |  |

Force to turn on the flag of semaphore.

Parameters

  semid | semaphore identifier
  semno | semaphore number
  maxwaitms | maximum waiting micro-seconds to release
  forceflag | status will be stored, it can be NULL if you don't need this information

Returns
  true if successful, otherwise returns false

Note
  This will wait the semaphore to be released with in maxwaitms. If it it released by locker normally with in maxwaitms, forceflag will be set to false. But if maximum maxwaitms is exceed and the semaphore is released forcely, forceflag will be set to true.

Definition at line 232 of file qsem.c.

## ◆ qsem_leave()

bool qsem_leave | ( | int | semid,
|  | int | semno
| ) |  |

Turn off the flag of semaphore then leaving critical section.

Parameters

  semid | semaphore identifier
  semno | semaphore number

Returns
  true if successful, otherwise returns false

Definition at line 263 of file qsem.c.

## ◆ qsem_check()

bool qsem_check | ( | int | semid,
|  | int | semno
| ) |  |

Get the status of semaphore.

Parameters

  semid | semaphore identifier
  semno | semaphore number

Returns
  true for the flag on, false for the flag off

Definition at line 285 of file qsem.c.

## ◆ qsem_free()

bool qsem_free | ( | int | semid | ) |

Release semaphore to system.

Parameters

  semid | semaphore identifier

Returns
  true if successful, otherwise returns false

Definition at line 298 of file qsem.c.