Unknown Storage Size Error for union semun

Discussion in 'C Programming' started by kvnsmnsn, Sep 22, 2007.

  1. kvnsmnsn

    kvnsmnsn Guest

    I'm taking a class on Internet Programming, and the lab I'm currently
    working on requires us to use threads and semaphores. I'm looking at
    a slide from that class titled "Unix Semaphore Code" that says:

    [] Creation
    o union semun arguments;
    o key_t key = 1;
    o int flags = 0777 | IPC_CREAT;
    o int semid = semget(key, 1, flags);
    o argument.val = initialvalue;
    o semctl(semid, 0, SETVAL, argument);
    [] Destruction
    o int ignored_int;
    o union semun ignored;
    o semctl(semid, ignored_int, IPC_RMID, ignored);

    I wanted to create an array of two semaphores, so I wrote the follow-
    ing <main> function:

    int main ( int argCount
    , char** arguments)
    {
    pthread_t Write[ 2];
    union semun argument; <=================
    key_t key = 1;
    int flags = 0700 | IPC_CREAT;
    int before[ 2];
    int pairs[ 2][ 2];
    int result;
    int parity;
    int end;
    printf( "Before creation of semaphores.\n");
    for (parity = EVEN; parity <= ODD; parity++)
    { before[ parity] = semget( key, 1, flags);
    argument.val = parity;
    semctl( before[ parity], 0, SETVAL, argument);
    }
    ...

    But when I try to compile it I get the error message
    "CountSharer.c:44: error: storage size of 'argument' isn't known".
    Line 44 is the line my arrow is pointing to up above. The slide up
    above is the only reference to <union semun> that I know of. Does
    anyone know what I need to do to my <argument> variable to keep this
    error from occurring?

    ---Kevin Simonson

    "You'll never get to heaven, or even to LA,
    if you don't believe there's a way."
    from _Why Not_
     
    kvnsmnsn, Sep 22, 2007
    #1
    1. Advertisements

  2. The error message isn't too helpful.
    The compiler doesn't know what a union senum is because you haven't include
    the header that defines it.
     
    Malcolm McLean, Sep 22, 2007
    #2
    1. Advertisements

  3. You need to "#include <sys/sem.h>", which has the definition of union semun
    as well as the prototypes for semctl(). You also need to to "#include
    <stdio.h>" to introduce the prototype of printf to your compiler, do avoid
    undefined behavoir.

    Bye, Jojo
     
    Joachim Schmitz, Sep 22, 2007
    #3
  4. You need to post in another group. A good string point might be
    comp.unix.programmer (standard C is the topic here).
    There is germ of a topical question here in that these errors usually
    mean you have forgotten to #include some header file that defines the
    types you are using.

    However, what header files you might have forgotten and how to use
    them is matter to comp.unix.programmer.
     
    Ben Bacarisse, Sep 22, 2007
    #4
  5. [...]

    Others have pointed out the missing #include directives. But your use
    of the names 'argCount' and 'arguments' is, in my opinion, poor style.
    It's perfectly legal to call the arguments to main() anything you
    like, but universal convention is to call them 'argc' and 'argv'. By
    using different names, you make your code harder to read.
     
    Keith Thompson, Sep 22, 2007
    #5
  6. A 2nd check revealed the following comment in my system's sem.h:
    /* Applications must declare the fourth argument for semctl themselves
    * (if it is needed). A standard recommendation for the declaration is
    *
    * union semun {
    * int val;
    * struct semid_ds *buf;
    * unsigned short int *array;
    * };
    */
    So you'd have to declare that union yourself. The manual page for semctl
    says the same, so check your system's documentation.

    Bye, Jojo
     
    Joachim Schmitz, Sep 23, 2007
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.