Reputation: 35
I have created a System V
semaphore using semget
#define SEM_ID 250
...
sem_set_id = semget(SEM_ID, 1, IPC_CREAT | 0660);
if (sem_set_id == -1) {
perror("main: semget");
exit(1);
now as per my understanding this creates a semaphore set which has many semaphores in it.
But how to identify a particular semaphore to set its value using semctl
...
int iter=0;
for(iter=0;iter<no_of_jobs;iter++)
{
int rc=semctl(semid,iter,SETVAL, sem_val);
if(rc==-1)
{printf("Error:semctl\n");
exit(1);
}
}
The above code runs for iter=0
but fails for other. In struct semun sem_val
I have set the value sem_val.val=1
;
Upvotes: 1
Views: 1834
Reputation: 753695
The second argument to semget()
is the number of semaphores in the semaphore set. You specified 1
, so you got one semaphore. Accessing anything other than the one semaphore is going to lead to errors.
Change the 1
to 10
, say, and you should be OK to iterate over semaphores 0..9 inclusive.
Upvotes: 2