delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2003/01/24/10:30:27.1

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
X-MimeOLE: Produced By Microsoft Exchange V6.0.6249.0
content-class: urn:content-classes:message
MIME-Version: 1.0
Subject: Mmap and semaphores?
Date: Fri, 24 Jan 2003 10:21:29 -0500
Message-ID: <B64C24D6DF7BF040BAF59B923D06180B3FEFC8@exmbx01chel.cadence.com>
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
From: "Elliot Mednick" <elliot AT cadence DOT com>
To: <cygwin AT cygwin DOT com>
X-Received: By mailgate2.Cadence.COM as HAA09017 at Fri Jan 24 07:21:30 2003
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id h0OFLjl10306

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019