[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 ...)
bool qsem_leave(int semid, int semno)
Turn off the flag of semaphore then leaving critical section.
int qsem_getid(const char *keyfile, int keyid)
Get semaphore identifier by keyfile and keyid for the existing semaphore.
bool qsem_free(int semid)
Release semaphore to system.
int qsem_init(const char *keyfile, int keyid, int nsems, bool recreate)
Initialize semaphore.
bool qsem_enter(int semid, int semno)
Turn on the flag of semaphore then entering critical section.