public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* [QUERY] Help With Semaphore Error on Windows : Cygwin
@ 2023-02-16  4:04 Yeo Kai Wei
  2023-02-16 11:10 ` Takashi Yano
  2023-02-16 14:59 ` Corinna Vinschen
  0 siblings, 2 replies; 5+ messages in thread
From: Yeo Kai Wei @ 2023-02-16  4:04 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 723 bytes --]

Hi All,

Could I request for some help on some code?

I ran into an error with semaphores with some book code using Cygwin on 
Windows.

The code will throw an error with sem_post().

I compiled it with gcc -o memwriter memwriter.c -lrt -lpthread

$ ./memwriter
Shared memory address: 0x6fffffff0000 [0..511]
backing file: /dev/shm/shMemEx
semptr is address 0x7ffffcc18 with value 0
SEM_VALUE_MAX is 1147483648
sem_post: Invalid argument <--------------------------- ERROR


According to feedback, the above error does not turn up on Linux.

May I know if this is supposed to happen on Cygwin on Windows?

If not, how can I solve this?

I've also attached the code for reference.


Thank you.


Kind regards,

YEO Kai Wei


[-- Attachment #2: shmem.h --]
[-- Type: text/plain, Size: 171 bytes --]

#define ByteSize 512
#define BackingFile "/shMemEx"
#define AccessPerms 0644
#define SemaphoreName "mysemaphore"
#define MemContents "This is the way the world ends...\n"

[-- Attachment #3: memwriter.c --]
[-- Type: text/plain, Size: 1982 bytes --]

//gcc -o memwriter memwriter.c -lrt -lpthread

#include <stdio.h>

#include <stdlib.h>

#include <sys/mman.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

#include <semaphore.h>

#include <string.h>

#include <limits.h> //SEM_VALUE_MAX

#include "shmem.h"

void report_and_exit(const char* msg)
{
	perror(msg);

	exit(-1);
}

int main()
{
	
	//Create file descriptor
	int fd = shm_open(	BackingFile, 
				O_RDWR | O_CREAT, 	//Read, write, create if needed
				AccessPerms); 		//Access permission 0644

	//ERROR
	if(fd<0)
		report_and_exit("Can't open shared mem segment ... ");

	//Get the bytes
	ftruncate(fd, ByteSize);

	//Let system pick where to put the segments
	caddr_t memptr = mmap(	NULL, 			//Let system pick
				ByteSize, 		//How many bytes
				PROT_READ | PROT_WRITE, //Access protections
				MAP_SHARED, 		//Mapping visible 
				fd, 			//file descriptor
				0); 			//offset 0, start from 1st byte


	if((caddr_t) -1 == memptr)
		report_and_exit("Can't get segment...");

	fprintf(stderr, "Shared memory address: %p [0..%d]\n", memptr, ByteSize -1);
	
	fprintf(stderr, "backing file: /dev/shm%s\n", BackingFile);

	//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)
		report_and_exit("sem_open");

	//Copy some ASCII Bytes to the memory segment
	strcpy(memptr, MemContents);

	printf("semptr is address %p with value %i\n", &semptr, semptr);
	
	printf("SEM_VALUE_MAX is %i\n", SEM_VALUE_MAX);

	//sem_post increments the semaphore -1 ERROR, 0 success
	if(sem_post(semptr) < 0) 
		report_and_exit("sem_post");

	//Sleep
	sleep(12);

	//Cleanup. Unmap the storage
	munmap(memptr, ByteSize);

	close(fd);

	sem_close(semptr);

	//Unlink from backing file
	shm_unlink(BackingFile);

	return 0;
}



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [QUERY] Help With Semaphore Error on Windows : Cygwin
  2023-02-16  4:04 [QUERY] Help With Semaphore Error on Windows : Cygwin Yeo Kai Wei
@ 2023-02-16 11:10 ` Takashi Yano
  2023-02-16 14:59 ` Corinna Vinschen
  1 sibling, 0 replies; 5+ messages in thread
From: Takashi Yano @ 2023-02-16 11:10 UTC (permalink / raw)
  To: cygwin; +Cc: Yeo Kai Wei

On Thu, 16 Feb 2023 12:04:01 +0800
Yeo Kai Wei wrote:
> Hi All,
> 
> Could I request for some help on some code?
> 
> I ran into an error with semaphores with some book code using Cygwin on 
> Windows.
> 
> The code will throw an error with sem_post().
> 
> I compiled it with gcc -o memwriter memwriter.c -lrt -lpthread
> 
> $ ./memwriter
> Shared memory address: 0x6fffffff0000 [0..511]
> backing file: /dev/shm/shMemEx
> semptr is address 0x7ffffcc18 with value 0
> SEM_VALUE_MAX is 1147483648
> sem_post: Invalid argument <--------------------------- ERROR
> 
> 
> According to feedback, the above error does not turn up on Linux.
> 
> May I know if this is supposed to happen on Cygwin on Windows?
> 
> If not, how can I solve this?
> 
> I've also attached the code for reference.

Semaphore name which does not begin with slash (/) is not
portable.

See:
https://pubs.opengroup.org/onlinepubs/9699919799/functions/sem_open.html

Cygwin disallows that.

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [QUERY] Help With Semaphore Error on Windows : Cygwin
  2023-02-16  4:04 [QUERY] Help With Semaphore Error on Windows : Cygwin Yeo Kai Wei
  2023-02-16 11:10 ` Takashi Yano
@ 2023-02-16 14:59 ` Corinna Vinschen
  2023-02-17  4:56   ` Yeo Kai Wei
  1 sibling, 1 reply; 5+ messages in thread
From: Corinna Vinschen @ 2023-02-16 14:59 UTC (permalink / raw)
  To: Yeo Kai Wei; +Cc: cygwin

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [QUERY] Help With Semaphore Error on Windows : Cygwin
  2023-02-16 14:59 ` Corinna Vinschen
@ 2023-02-17  4:56   ` Yeo Kai Wei
  2023-02-17  8:52     ` Takashi Yano
  0 siblings, 1 reply; 5+ messages in thread
From: Yeo Kai Wei @ 2023-02-17  4:56 UTC (permalink / raw)
  To: cygwin

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [QUERY] Help With Semaphore Error on Windows : Cygwin
  2023-02-17  4:56   ` Yeo Kai Wei
@ 2023-02-17  8:52     ` Takashi Yano
  0 siblings, 0 replies; 5+ messages in thread
From: Takashi Yano @ 2023-02-17  8:52 UTC (permalink / raw)
  To: cygwin; +Cc: Yeo Kai Wei

On Fri, 17 Feb 2023 12:56:06 +0800
Yeo Kai Wei wrote:
> 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)

No. You should use:
  if (semptr == SEM_FAILED)

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-02-17  8:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-16  4:04 [QUERY] Help With Semaphore Error on Windows : Cygwin Yeo Kai Wei
2023-02-16 11:10 ` Takashi Yano
2023-02-16 14:59 ` Corinna Vinschen
2023-02-17  4:56   ` Yeo Kai Wei
2023-02-17  8:52     ` Takashi Yano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).