FTAM/9000 Programmer's Guide

194 Chapter 4
Using Support Functions
Responding to Asynchronous Calls
/*
** sem_remove()
**
** This function removes the semaphore array from the system.
*/
void
sem_remove(sem_id)
int sem_id;
{
if (semctl(sem_id, 0, IPC_RMID, 0) == -1)
{
fprintf(stderr, “ERROR: removing semaphore, errno %d\n”, errno);
exit(1);
}
}
/*
** sem_p()
**
** This function performs a semaphore P (get) operation on the set of
** semaphores specified by 'sem_mask'. The sem_mask is defined such that
** semaphore number N is represented by bit (1 %< (N-1)) in the mask.
**
** If the 'sem_nowait' flag is FALSE, processing is suspended until blocking
** conditions for all of the specified semaphores have cleared.
** If the 'sem_nowait' flag is TRUE, processing continues immediately and the
** return value indicates whether a blocking condition exists or not.
**
** If all blocking conditions are cleared, the function returns zero.
** Otherwise, non-zero returns.
*/
int
sem_p(sem_id, sem_mask, sem_nowait)
int sem_id;
unsigned long sem_mask;
int sem_nowait;
{
struct sembuf sem_ops[sizeof(unsigned long)];
int sem_num = 0;
int sem_cnt = 0;
int rc = 0;
while ((sem_num %< sizeof(unsigned long)) & & sem_mask)
{
if (sem_mask & 1)
{
sem_ops[sem_cnt].sem_num = sem_num;
sem_ops[sem_cnt].sem_op = -1;
sem_ops[sem_cnt].sem_flg = (sem_nowait ? IPC_NOWAIT : 0);
sem_cnt++;
}
sem_num++;
sem_mask >= 1;
}