delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2014/01/30/14:47:01

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:from:to:subject:date:message-id:references
:in-reply-to:content-type:content-transfer-encoding
:mime-version; q=dns; s=default; b=PuOE8ugHUcQkhkI4rU8k5VhRb/s/i
F/3nOQ88Bl8M1CP+R3CpJHVwhwsVfMVusQ5QqPnxheaYknHU36/aHwIp+0xOqths
vHpimGs60tn0EA9OFJWGj8ZwgP8amCNsPnM8lOQ6cfklqO4J30ZkXp6K8rcwLIre
MHH4Un4oTQJ65Y=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:from:to:subject:date:message-id:references
:in-reply-to:content-type:content-transfer-encoding
:mime-version; s=default; bh=A8n7kg0z9e3m7KG44hL3BjRTpPU=; b=mbI
fLA8g1UBTVYtzW/rWEtee1sqa8tPt4eCeH6VDEFLy3WaLUkGJFdpYZm1JQTGaQSc
PgfhbmRDf4uTKBQ3V87O3ZnjkNRTq5ig+ZegEFqKMD7K5yRzGb21XGQZbRmSuCaF
KUVgoQdVF8X4ru15FQ+f4IC9/nXRTMOBlriRmTnU=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/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
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL,BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2
X-HELO: nihxway2out.hub.nih.gov
X-IronPortListener: Outbound_SMTP
X-IronPort-Anti-Spam-Filtered: true
X-IronPort-Anti-Spam-Result: AicFAOSq6lKcKEeu/2dsb2JhbABZgwyBD70kgQwWdIIlAQEBAwESKEQLAgEIDRUUEB8TJQIEGxqHWwihOapyF45ROIMkgRQEjmOQNYsvgy2CKg
From: "Lavrentiev, Anton (NIH/NLM/NCBI) [C]" <lavr AT ncbi DOT nlm DOT nih DOT gov>
To: "cygwin AT cygwin DOT com" <cygwin AT cygwin DOT com>
Subject: RE: second call to mmap() results in error
Date: Thu, 30 Jan 2014 19:46:31 +0000
Message-ID: <5F8AAC04F9616747BC4CC0E803D5907D0C477869@MLBXv04.nih.gov>
References: <20140129181250 DOT GW2821 AT calimero DOT vinschen DOT de> <52E95786 DOT 8050606 AT gmail DOT com> <20140130095822 DOT GY2821 AT calimero DOT vinschen DOT de> <001801cf1db6$66c47c40$344d74c0$@lbmsys.com> <20140130124558 DOT GA2821 AT calimero DOT vinschen DOT de> <003801cf1dbd$fc3ffc70$f4bff550$@lbmsys.com> <20140130170142 DOT GD2821 AT calimero DOT vinschen DOT de> <5F8AAC04F9616747BC4CC0E803D5907D0C4763CC AT MLBXv04 DOT nih DOT gov> <52EA9A53 DOT 2050203 AT cygwin DOT com> <5F8AAC04F9616747BC4CC0E803D5907D0C4764C5 AT MLBXv04 DOT nih DOT gov> <20140130183956 DOT GA3573 AT ednor DOT casa DOT cgf DOT cx> <5F8AAC04F9616747BC4CC0E803D5907D0C4764ED AT MLBXv04 DOT nih DOT gov> <52EA9EA2 DOT 5030202 AT cygwin DOT com> <5F8AAC04F9616747BC4CC0E803D5907D0C476528 AT MLBXv04 DOT nih DOT gov> <52EAA171 DOT 406 AT cygwin DOT com> <5F8AAC04F9616747BC4CC0E803D5907D0C4765D8 AT MLBXv04 DOT nih DOT gov> <52EAA848 DOT 7070808 AT cygwin DOT com>
In-Reply-To: <52EAA848.7070808@cygwin.com>
MIME-Version: 1.0
X-IsSubscribed: yes
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id s0UJkunQ025655

> So if the question is why do you need to include ftruncate() in the code,
> take a look here:

No, that was not the question.  W/o ftruncate(), _referencing_ the memory
becomes a problem (and the original code did not do that, but simply mmap'ed
two files).

> > Adding the <unistd.h> fixes the problem in all its iterations

OP privately wrote:

> The problem is also fixed by correct type-casting in the calls to mmap() ...
> 
>  mmap1 = mmap(NULL, (size_t) 524304, PROT_WRITE | PROT_READ, MAP_SHARED,
> shm_fd1, 0);
> 
> The address returned from the call to mmap() -- with or without the
> ftruncate() -- was bad without the (size_t) specification.

(note "with or without ftruncate()")

And I ask again (last time, I promise), how on Earth adding <unistd.h> to
the code below would make the code suddenly working?  (Like I said before,
that code worked for me, with -Wall showing nothing, from the very beginning.)

Anton Lavrentiev
Contractor NIH/NLM/NCBI


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <fcntl.h>
#include <sys/mman.h>

int main()
{
        int shm_fd1, shm_fd2;         
        char *mmap1, *mmap2;
 /*   get fd for each block of memory  */
  shm_fd1 = shm_open("/block1", O_CREAT | O_RDWR, 0666);
  if (shm_fd1 == -1) {
                fprintf(stderr, "Couldn't get fd for block1 (%s)\n",
strerror(errno));
                exit(1);
  }
  shm_fd2 = shm_open("/block2", O_CREAT | O_RDWR, 0666);
  if (shm_fd2 == -1) {
                fprintf(stderr, "Couldn't get fd for /UNI_queue (%s)\n",
strerror(errno));
                exit(1);
  }
  /*  map each block  */
  mmap1 = mmap(NULL, 524304, PROT_WRITE | PROT_READ, MAP_SHARED, shm_fd1,
0);
        if (mmap1 == (char *)-1) {
                fprintf(stderr, "Couldn't map memory for /block1 (%s)\n",
strerror(errno));
                exit(1);
        }
  mmap2 = mmap(NULL, 524304, PROT_WRITE | PROT_READ, MAP_SHARED, shm_fd2,
0);
        if (mmap2 == (char *)-1) {
                fprintf(stderr, "Couldn't map memory for /block2 (%s)\n",
strerror(errno));
                exit(1);
        }
        fprintf(stdout, "Shared memory initialized\n");
        exit(0);
}



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


- Raw text -


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