The code is missing to at least grant itself read/write access to the semaphore set created.
To do so modifiy the code like this:
if (-1 == (semid = semget(IPC_PRIVATE, 1, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR)))
{
perror("semget() failed");
exit (EXIT_FAILURE);
}