Thread: how to read current semaphore value

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    26

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

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    26
    i response to myself.....
    i don't init the sem_id value, just call the semget loosing the return value. so:
    Code:
    sem_id=semget(IPC_PRIVATE,1,IPC_CREAT|IPC_EXCL|S_IRUSR|S_IWUSR);
    damn me!

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    26
    but wait wait....

    Code:
    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
    Code:
    #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.
    Last edited by np2k; 05-24-2010 at 05:32 AM.

  4. #4

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    26
    yes...and then?
    I use semun array...have a look to my code

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> I use semun array...have a look to my code
    Let's have a look...
    Code:
      union semun argument;
      semctl(sem_id,0,GETALL,argument); /* argument.array not set. */
    Code:
      union semun arg;
      semctl(sem_id,0,GETALL,arg);  /* arg.array not set. */
    You must provide the storage for semun::array.

    gg

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    26
    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:
    Code:
    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...

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by np2k View Post
    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:
    Code:
    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.

    Code:
    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);

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    26
    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....
    Last edited by np2k; 05-24-2010 at 12:37 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by np2k View Post
    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.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    26
    Quote Originally Posted by tabstop View Post
    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....

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    26
    Quote Originally Posted by tabstop View Post
    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...

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by np2k View Post
    :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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  2. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  3. HelpMePlease
    By Prncess100 in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2002, 02:02 PM
  4. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 03:49 PM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM