Semaphore APIs.
More...
Go to the source code of this file.
|
int | qsem_init (const char *keyfile, int keyid, int nsems, bool recreate) |
| Initialize semaphore. More...
|
|
int | qsem_getid (const char *keyfile, int keyid) |
| Get semaphore identifier by keyfile and keyid for the existing semaphore. More...
|
|
bool | qsem_enter (int semid, int semno) |
| Turn on the flag of semaphore then entering critical section. More...
|
|
bool | qsem_enter_nowait (int semid, int semno) |
| Try to turn on the flag of semaphore. More...
|
|
bool | qsem_enter_force (int semid, int semno, int maxwaitms, bool *forceflag) |
| Force to turn on the flag of semaphore. More...
|
|
bool | qsem_leave (int semid, int semno) |
| Turn off the flag of semaphore then leaving critical section. More...
|
|
bool | qsem_check (int semid, int semno) |
| Get the status of semaphore. More...
|
|
bool | qsem_free (int semid) |
| Release semaphore to system. More...
|
|
Semaphore APIs.
- Note
[daemon main]
#define MAX_SEMAPHORES (2)
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;
}
(... child forking codes ...)
[forked child]
(... guaranteed as atomic procedure ...)
(... some codes ...)
(... guaranteed as atomic procedure ...)
[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;
}
(... guaranteed as atomic procedure ...)
Definition in file qsem.c.
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.
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.
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.
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.
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.
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.
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.
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.