have written two simple functions, one for setting a semaphore to an arbitrary value (Reset) and another one to make the process calling it wait for the semaphore to be zero:
int R(int semid, int semnum, int val) // reset
{
union semun arg;
arg.val = val;
if( semctl(sem_id,semnum,SETVAL,arg) == -1 )
{
fprintf (stderr, "Errore inizializzazione del semaforo\n%s\n",
strerror(errno) );
}
}
int Z(int semid, int semnum) // wait for zero
{
printf("waiting for zero on %d\n",semid);
struct sembuf cmd;
cmd.sem_num = semnum;
cmd.sem_op = 0;
cmd.sem_flg = 0;
if(semop(semid, &cmd, 1)<0) {ERROR}
return 0;
}
The process that is using those functions as a code that looks like this:
while(1) {
R(sem_id_counter,0,N_PROC_DEFAULT); // (N_PROC_DEFAULT=5)
/* list of tasks not having anything to do with sem_id_coutner */
Z(sem_id_counter,0);
}
What happens is that the above process just keeps executing the loop, while I want it to stop at the end, when it calls the function Z, waiting for the semaphore to become 0 before resetting it to its initial content and restart.
Where am I going wrong? Thanks [ solved: stupid typo sem_id instead of semid ]