PDA

View Full Version : how to read current semaphore value



np2k
05-24-2010, 04:33 AM
Hi all. I'm using semaphore for processes.
Here a simple 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:


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:


...
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!

np2k
05-24-2010, 04:43 AM
i response to myself.....
i don't init the sem_id value, just call the semget loosing the return value. so:


sem_id=semget(IPC_PRIVATE,1,IPC_CREAT|IPC_EXCL|S_I RUSR|S_IWUSR);


damn me!

np2k
05-24-2010, 05:27 AM
but wait wait....



unsigned short int get_semvalue(int sem_id) {
union semun argument;
semctl(sem_id,0,GETALL,argument);
return argument.array[0];
}


if I use this function the program doesn't work.....
:( why?

this is the complete program


#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

union semun {
int val;
struct semid_ds* buf;
unsigned short * array;
struct seminfo* __buf;
};

int get_semvalue(int sem_id) {
union semun argument;
semctl(sem_id,0,GETALL,argument);
return (int)argument.array[0];
}


int main() {
int sem_id;
union semun argument;
unsigned short int values[1];
values[0] = 12;
argument.array = values;
sem_id = semget(IPC_PRIVATE,1,IPC_CREAT|IPC_EXCL|S_IRUSR|S_ IWUSR);
semctl(sem_id,0,SETALL,argument);

printf("%d\n",get_semvalue(sem_id)); //WRONG VALUE
union semun arg;
semctl(sem_id,0,GETALL,arg);
printf("<<<<<<%d>>>>>>>\n",arg.array[0]); //RIGHT VALUE

semctl(sem_id,1,IPC_RMID,argument);
return 0;
}

if i use the function it doesn't work, while with the second "manual" method it works.

Codeplug
05-24-2010, 08:01 AM
SETALL and GETALL both require semun::array to be set.
http://www.opengroup.org/onlinepubs/9699919799/functions/semctl.html

gg

np2k
05-24-2010, 08:10 AM
yes...and then?
I use semun array...have a look to my code

Codeplug
05-24-2010, 10:07 AM
>> I use semun array...have a look to my code
Let's have a look...


union semun argument;
semctl(sem_id,0,GETALL,argument); /* argument.array not set. */



union semun arg;
semctl(sem_id,0,GETALL,arg); /* arg.array not set. */
You must provide the storage for semun::array.

gg

np2k
05-24-2010, 11:37 AM
first of all i must set arg.array if i use SETALL...with GETALL cmd i want the information into senum argument...

then -reapet- I can retrieve the current value of the sem with these lines:


union semun arg;
semctl(sem_id,0,GETALL,arg);
printf("<<<<<<%d>>>>>>>\n",arg.array[0]); //RIGHT VALUE


but if i use my ontop get_semvalue function the program doesn't work...

tabstop
05-24-2010, 12:20 PM
first of all i must set arg.array if i use SETALL...with GETALL cmd i want the information into senum argument...

then -reapet- I can retrieve the current value of the sem with these lines:


union semun arg;
semctl(sem_id,0,GETALL,arg);
printf("<<<<<<%d>>>>>>>\n",arg.array[0]); //RIGHT VALUE


but if i use my ontop get_semvalue function the program doesn't work...

You may, by being very unlucky, get the answer you want here. That doesn't mean this snippet is correct.



union semun arg;
arg.array =malloc(some_number_of_bytes); /*how nice to actually have somewhere to store the answer*/
semctl(sem_id,0,GETALL,arg);

np2k
05-24-2010, 12:32 PM
1. I don't understand....why should be my work allocating the space for argument.array? It should be responsibility of the semctl function allocate this space...
2. and why the code I post works without any malloc? it always works...i don't believe that it's luck....

tabstop
05-24-2010, 12:40 PM
1. I don't understand....why should be my work allocating the space for argument.array? It should be responsibility of the semctl function allocate this space...

That's pretty much against the whole spirit of nearly all *nix system functions ever created. You give them the memory, and they put the information where it belongs.

np2k
05-24-2010, 01:09 PM
That's pretty much against the whole spirit of nearly all *nix system functions ever created. You give them the memory, and they put the information where it belongs.

ok, but...there is the question 2...without malloc the things work....

tabstop
05-24-2010, 01:17 PM
I'm not on a linux system so I can't check. What if you do arg.array = 2 before the call?

If you are (un)lucky that the random value in arg.array is large enough to be reasonably interpreted as a pointer, then your just overwriting some other chunk of memory somewhere else.

np2k
05-24-2010, 03:39 PM
I'm not on a linux system so I can't check. What if you do arg.array = 2 before the call?

seg fault


If you are (un)lucky that the random value in arg.array is large enough to be reasonably interpreted as a pointer, then your just overwriting some other chunk of memory somewhere else.
:S it seems very strange...

brewbuck
05-24-2010, 09:55 PM
If you find yourself wanting to obtain the value of a semaphore, you should be thinking "I'm about to create a bug."

There is no such thing as "the value of a semaphore." By the time you've read the value, the value may have changed. It is inherently wrong. Semaphores are waited upon, and signaled. That's it.

tabstop
05-24-2010, 10:10 PM
:S it seems very strange...
Only if somehow you've gotten to this point in your programming experience without learning/wondering that data has to be stored somewhere in order to be used. An int* is not room to store an array, it merely indicates an address of where some memory can be found.

np2k
05-25-2010, 02:23 AM
If you find yourself wanting to obtain the value of a semaphore, you should be thinking "I'm about to create a bug."

There is no such thing as "the value of a semaphore." By the time you've read the value, the value may have changed. It is inherently wrong. Semaphores are waited upon, and signaled. That's it.

yes but I need the sem value for debugging my software

@tabstop: I know what an int* is...the strange -for me- is that I'm so lucky to get no error using arg.array without any allocation

tabstop
05-25-2010, 07:13 AM
@tabstop: I know what an int* is...the strange -for me- is that I'm so lucky to get no error using arg.array without any allocation

I don't know what (if anything) I've been doing recently, but I've been getting large values in my uninitialized values recently. You must be doing the same thing. But it's still pretty random.