semctl union

This is a discussion on semctl union within the C Programming forums, part of the General Programming Boards category; Im trying to understand semaphores so I've search the internet a bit and found this snip of code Code: #define ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    semctl union

    Im trying to understand semaphores so I've search the internet a bit and found this snip of code
    Code:
    #define open 1
    #define close 0
    union semun
    {
      int val;
      struct semid_ds *buf;
      ushort          *array;
    }arg;
    
    
    int main( void )
    {
      int semID;
      ushort startvalue[3] = {close, close, open};
      ushort readvalue[3];
      semID = semget(IPC_PRIVATE, 1, 0666);
      if (semID == -1)
    	perror(" ** call to semget!");
    
       arg.array = &(startvalue[0]);
       if (semctl(semID,1,SETALL, arg) == -1)
    	perror(" ** call to semctl!");
    
       arg.array = &(readvalue[0]);
       if (semctl(semID,1,GETALL, arg) == -1)
    	perror(" ** call to semctl!");
    - I dont get why they use a union here.
    - What does the program need the variables val and *buf (inside union) for ?
    - They make two arrays of ushort but they only seem to use the first element... why???
    - If I understand correctly the two lines with arg.array basiclly makes the *array point to 0. Why couldnt I instead write
    int test = 0;
    arg.array = &(test);

  2. #2
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    Try "The Linux Programmerís Guide" available for download at http://www.tldp.org/guides.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 12:56 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. "generic" union or something
    By Raven Arkadon in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2005, 09:55 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Sorting a Union
    By andy in forum C Programming
    Replies: 4
    Last Post: 11-21-2001, 10:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21