mmapped memory lost after fork

Tenedor Roquefort fourchettebleu@yahoo.com
Wed Jul 14 19:52:00 GMT 2004


I'm using Cygwin 1.5.10-3 and have found what seems to
be a fork/mmap bug. I have two examples where a forked
child cannot access memory that was mmapped by the
parent. The problem seems to arise when the parent
munmaps some pages (different from the ones the child
will try to access) before forking.

In the example below, the parent mmaps 2 pages,
munmaps the first page, writes to the second page and
forks. Then both parent and child try to access the
second page, the parent succeeds but the child dies
trying. The examples work on Linux without the child
dying. Has this problem been documented before and is
there a known fix?

Thanks.



#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>

int main ()
{
    char *p;
    int sz = 0x2000;
    int keep = 0x1000;
    int offset;
    int magic = 0x33;
    int pid;
    int rv;

    p = (char *) mmap (0, sz, PROT_READ|PROT_WRITE,
                   MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
    printf ("mmap() = %p\n", p);

    /* keep upper chunk -> segv */
    rv = munmap (p, keep);
    offset = keep + 0x42;
    printf ("munmap() = %d\n",rv);

    p[offset] = magic;

    pid = fork ();

    switch (pid)
    {
    case (-1):
        printf ("fork() failed\n");
        break;
    case (0):
        printf ("child touching %p\n", &(p[offset]));
        printf ("child M[%p] = 0x%x\n",
                &(p[offset]), p[offset]);
        break;
    default:
        printf ("parent touching %p\n", &(p[offset]));
        printf ("parent M[%p] = 0x%x\n",
                &(p[offset]), p[offset]);
        break;
    }

    return 0;
}



		
__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mmap1.c
Type: application/octet-stream
Size: 1130 bytes
Desc: mmap1.c
URL: <http://cygwin.com/pipermail/cygwin/attachments/20040714/fa71203e/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mmap2.c
Type: application/octet-stream
Size: 1027 bytes
Desc: mmap2.c
URL: <http://cygwin.com/pipermail/cygwin/attachments/20040714/fa71203e/attachment-0001.obj>
-------------- next part --------------
--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


More information about the Cygwin mailing list