Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

Semaphore support

Hi,

I'm currently trying to initialize a semaphore by making a call to sem_init(), but instead of returning a non-zero value and printing a error condition, Nios II reboots.

struct sem_t *msem
if (sem_init(msem, 0, 0) != 0)
   printf("Semaphore not initialized\n");

Is there something specific that needs to be configured in the kernel that provides support for semaphores?

Any feedback would be appreciated.

5 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi bjskill,

    The sem_init() routine initializes the semaphore object that the first argument points to. So you're passing

    in an un-initialized pointer ... and something ends up getting trashed. Try:

    /*struct sem_t *msem */

    struct sem_t msem

    /*if (sem_init(msem, 0, 0) != 0)*/

    if (sem_init (&msem, 0, 0) != 0)

    printf("Semaphore not initialized\n");

    Regards,

    --Scott
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi smcnutt,

    I made some errors when I made my original post. When SemInit() is called something seems to get trashed and cause Nios to reboot:

    SemInit(myThreadName, &mySem, "mySemaphore");

    void SemInit(char *who, sem_t *mSem, char *label)
    {
       printf("%s is initializing %s\n", who, label);  // Debug message
       if (sem_init(mSem, 0, 0) != 0)
          printf("Failed when initializing semaphore.\n");  // Error message
       printf("%s has initialized %s\n", who, label);  // Debug message
       return;
    }

    I don't see either the debug or error messages displayed before the reboot.

    Thanks for your help.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I'm not sure if this is the problem, but you may need to turn on IPC support in the kernel in order for semaphore support to work.

    Take a look at:

    .../altera/kits/nios2/examples/software/linux/apps/samples/thread

    for an example of semaphores...
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi Ken,

    I think that might be the problem. However, I only get an "undefined reference to 'sem_init'" error from the linker when DEBUG=1 and it attempts to generate the .gdb file. When DEBUG=0, the linker does not report any errors or warnings when it generates the application.

    What path do I need to follow within the kernel configuration menu to check and see if IPC support is turned on? The only choice in the menu options that I found that was related to semaphores is "General Setup --> POSIX message queues". Is that right?

    I'll also take a look at those linux examples to make sure I'm initializing the semaphores correctly.

    Thanks for your help.

    UPDATE: (8/24/05):

    I enabled the "General Setup -> POSIX message queues" from the kernel configuration menu, performed a new build, and downloaded the new vmlinux.bin file. However, the application still causes a Nios2 reboot as soon as sem_init() is called. Is there something else I'm missing?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Sorry for the late reply, how about you post your full code and I'll run it on my board to see what happens?