The following is the code my professor used to initialize a semaphore, but I have some doubts:
Why didn't he just do semctl(semid, value, SETVAL)? According to the man pages, a semaphore is specified by semid and semnum, and presumably semid is the semaphore ID and semnum is the initial value of the semaphore. My confusion is confounded by the fact that the fourth argument to Init takes a union in which one of the fields is val--the value for SETVAL. Is his implementation right? The down and up operations are performed on semaphore specified by semid.
void Init (int semid, int value)
struct semid_ds *buf;
arg.val = value;
if (semctl (0, semid, SETVAL, arg) < 0)
perror ("semctl SETVAL");