![]() |
Functions | |
int | qSemInit (const char *keyfile, int keyid, int nsems, bool ifexistdestroy) |
Initialize semaphore. | |
int | qSemGetId (const char *keyfile, int keyid) |
Get semaphore identifier by keyfile and keyid for the existing semaphore. | |
bool | qSemEnter (int semid, int semno) |
Turn on the flag of semaphore then entering critical section. | |
bool | qSemEnterNowait (int semid, int semno) |
Try to turn on the flag of semaphore. | |
bool | qSemEnterForce (int semid, int semno, int maxwaitms, bool *forceflag) |
Force to turn on the flag of semaphore. | |
bool | qSemLeave (int semid, int semno) |
Turn off the flag of semaphore then leaving critical section. | |
bool | qSemCheck (int semid, int semno) |
Get the status of semaphore. | |
bool | qSemFree (int semid) |
Release semaphore to system. |
[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);
int qSemInit | ( | const char * | keyfile, | |
int | keyid, | |||
int | nsems, | |||
bool | ifexistdestroy | |||
) |
Initialize semaphore.
keyfile | seed for generating unique IPC key | |
keyid | seed for generating unique IPC key | |
nsems | number of semaphore to initialize | |
ifexistdestroy | set to true to destroy if semaphore already exists |
int qSemGetId | ( | const char * | keyfile, | |
int | keyid | |||
) |
Get semaphore identifier by keyfile and keyid for the existing semaphore.
keyfile | seed for generating unique IPC key | |
keyid | seed for generating unique IPC key |
bool qSemEnter | ( | int | semid, | |
int | semno | |||
) |
Turn on the flag of semaphore then entering critical section.
semid | semaphore identifier | |
semno | semaphore number |
bool qSemEnterNowait | ( | int | semid, | |
int | semno | |||
) |
Try to turn on the flag of semaphore.
If it is already turned on, do not wait.
semid | semaphore identifier | |
semno | semaphore number |
bool qSemEnterForce | ( | int | semid, | |
int | semno, | |||
int | maxwaitms, | |||
bool * | forceflag | |||
) |
Force to turn on the flag of semaphore.
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 |
bool qSemLeave | ( | int | semid, | |
int | semno | |||
) |
Turn off the flag of semaphore then leaving critical section.
semid | semaphore identifier | |
semno | semaphore number |
bool qSemCheck | ( | int | semid, | |
int | semno | |||
) |
Get the status of semaphore.
semid | semaphore identifier | |
semno | semaphore number |
bool qSemFree | ( | int | semid | ) |
Release semaphore to system.
semid | semaphore identifier |
[Home] [About] [Examples] [Changes] [Download] [SVN Repository] [Install] [Reference] |