| 1 |
/*********************************************************************** |
| 2 |
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC * |
| 3 |
* Copyright (C) 2001 by New Riders Publishing * |
| 4 |
* See COPYRIGHT for license information. * |
| 5 |
***********************************************************************/ |
| 6 |
|
| 7 |
#include <sys/types.h> |
| 8 |
#include <sys/ipc.h> |
| 9 |
#include <sys/sem.h> |
| 10 |
|
| 11 |
/* We must define union semun ourselves. */ |
| 12 |
|
| 13 |
union semun { |
| 14 |
int val; |
| 15 |
struct semid_ds *buf; |
| 16 |
unsigned short int *array; |
| 17 |
struct seminfo *__buf; |
| 18 |
}; |
| 19 |
|
| 20 |
/* Initialize a binary semaphore with a value of one. */ |
| 21 |
|
| 22 |
int binary_semaphore_initialize (int semid) |
| 23 |
{ |
| 24 |
union semun argument; |
| 25 |
unsigned short values[1]; |
| 26 |
values[0] = 1; |
| 27 |
argument.array = values; |
| 28 |
return semctl (semid, 0, SETALL, argument); |
| 29 |
} |