Mail Archives: cygwin/2003/01/24/10:30:27.1
Hello,
I'm try to write a client server application that used shared memory and
asemaphores. Appended is a simple test case. It works under Solaris
using the -lposix4 library. But it doesn't work under Cygwin. Given
the lack of any otherquestions about this, I imagine that I'm doing
something wrong, or it's functionality is limited.
The server maps a file as shared, inits a semaphore, then waits. The
client, which you start after the server waits, shares the same file
(this part works) and attempts to post the semaphore. The server ought
to -- but doesn't -- wake up. It does in Solaris.
Win2000, SR3. Two cygwin processes.
Thanks to anyone that can help!
Server code: (run first)
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <semaphore.h>
#include <sys/mman.h>
struct shared {
sem_t mutex;
char buffer[100];
} shared;
main () {
int fd;
struct shared *ptr;
sem_t play;
int i;
fd = open ("foo", O_RDWR | O_CREAT | O_TRUNC);
printf ("Opened, fd is %d\n", fd);
write (fd, &shared, sizeof (struct shared));
ptr = (struct shared *) mmap (NULL, sizeof (struct shared), PROT_READ
| PROT_WRITE, MAP_SHARED, fd, 0);
printf ("Now sharing, ptr is %p\n", ptr);
printf ("Initiating semaphore\n");
i = sem_init (&(ptr->mutex), 1, 0);
strcpy (ptr->buffer, "This is a test");
printf ("Waiting on semaphore\n");
sem_wait (&(ptr->mutex));
printf ("Done waiting on semaphore\n");
printf ("Pausing...\n");
sleep (1);
ptr->buffer[1] = 'a';
sleep (1);
ptr->buffer[2] = 'a';
sleep (1);
ptr->buffer[3] = 'a';
sleep (1);
ptr->buffer[4] = 'a';
sleep (1);
ptr->buffer[5] = 'a';
sleep (1);
ptr->buffer[6] = 'a';
sleep (1);
ptr->buffer[7] = 'a';
printf ("destroying semaphore\n");
close (fd);
i = sem_destroy (&(ptr->mutex));
sleep (10);
}
Client code: (run after server waits)
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <semaphore.h>
#include <sys/mman.h>
struct shared {
sem_t mutex;
char buffer[100];
} shared;
main () {
int fd;
struct shared *ptr;
fd = open ("foo", O_RDWR);
printf ("Opened, fd is %d\n", fd);
// write (fd, &shared, sizeof (struct shared));
ptr = (struct shared *) mmap (NULL, sizeof (struct shared), PROT_READ
| PROT_WRITE, MAP_SHARED, fd, 0);
printf ("Now sharing, ptr is %p\n", ptr);
sem_post (&(ptr->mutex));
printf ("This is in memory: %s\n", ptr->buffer);
close (fd);
}
--
Elliot Mednick 270 Billerica Road
Lead Architect, Embedded Systems Design Chelmsford, MA 01824
Cadence Design Systems (978) 262-6501
elliot AT cadence DOT com
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
- Raw text -