how to read current semaphore value
Hi all. I'm using semaphore for processes.
Here a simple code:
Code:
...
//include declarations
...
union semun {
int val;
struct semid_ds* buf;
unsigned short * array;
struct seminfo* __buf;
};
int main() {
//allocating&init semaphore
int sem_id;
union semun argument;
unsigned short int values[1];
values[0] = 12;
argument.array = values;
semget(IPC_PRIVATE,1,IPC_CREAT|IPC_EXCL|S_IRUSR|S_IWUSR);
semctl(sem_id,0,SETALL,argument);
//at this point the sem value is 12
//now i want read this value
...HOW?...
return 0;
}
In man semctl page I read:
Quote:
GETALL Return semval (i.e., the current value) for all semaphores of the set into arg.array. The argument semnum is ignored. The calling process must have read permission on the semaphore set.
So I write some code like this:
Code:
...
union senum arg;
//debug line: printf("%d",arg.array[0]);
semctl(sem_id,0,GETALL,arg);
printf("%d",arg.array[0]);
but the function semctl seems to have no effects: in fact the debug line printf prints the same value of the second printf...
what's the problem?
thank you!