Hi All,

Thanks for the help.

It works now.

So, the 2 things that were required

1. Adding the '/' in front of the semaphore name so that Cygwin can recognise it

#define SemaphoreName "/mysemaphore"

2. Changing the if check from if(semptr == (void*) -1) to if(semptr == (sem_t*) 0)


Thank you very much, Takashi and Corinna.

Kind Regards,
YEO Kai Wei

On 16/2/2023 10:59 pm, Corinna Vinschen wrote:
Hi Kai,

Apart from what Takashi already wrote, there's another bug in this code:

On Feb 16 12:04, Yeo Kai Wei via Cygwin wrote:
#define ByteSize 512
#define BackingFile "/shMemEx"
#define AccessPerms 0644
#define SemaphoreName "mysemaphore"
                         ^^^^^^^^^^^^

What Takashi wrote.  The reason that you don't notice that sem_open
actuially failed is...

        //Create the semaphore
        sem_t* semptr = sem_open(       SemaphoreName,//name
                                        O_CREAT, //create semaphore
                                        AccessPerms, //protection permissions
                                        0);     //Initial value

        //ERROR
        if(semptr == (void*) -1)
                      ^^^^^^^^^^
                      This.

Why do you test for -1?  If you read the POSIX man page for sem_open,
you'll see this:

   Upon successful completion, the sem_open() function shall return the
   address of the semaphore. Otherwise, it shall return a value of
   SEM_FAILED [...]
   ^^^^^^^^^^

SEM_FAILED is not necessarily -1.  On Cygwin it's defined as

   #define SEM_FAILED ((sem_t *) 0)

in /usr/include/semaphore.h.

So your code just seems to fail in sem_post, but actually that's
because sem_open failed and your code checks for the wrong return
value.


HTH,
Corinna

--
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

Reply via email to