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

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).