Im trying to understand semaphores so I've search the internet a bit and found this snip of code
Code:
#define open 1
#define close 0
union semun
{
int val;
struct semid_ds *buf;
ushort *array;
}arg;
int main( void )
{
int semID;
ushort startvalue[3] = {close, close, open};
ushort readvalue[3];
semID = semget(IPC_PRIVATE, 1, 0666);
if (semID == -1)
perror(" ** call to semget!");
arg.array = &(startvalue[0]);
if (semctl(semID,1,SETALL, arg) == -1)
perror(" ** call to semctl!");
arg.array = &(readvalue[0]);
if (semctl(semID,1,GETALL, arg) == -1)
perror(" ** call to semctl!");
- I dont get why they use a union here.
- What does the program need the variables val and *buf (inside union) for ?
- They make two arrays of ushort but they only seem to use the first element... why???
- If I understand correctly the two lines with arg.array basiclly makes the *array point to 0. Why couldnt I instead write
int test = 0;
arg.array = &(test);