This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

CreateFileMapping/MapViewOfFile and fork


As part of an application we're developing, we use
CreateFileMapping/MapViewOfFile to be able to share information among
processes.  I noticed today that if I try to reference the map
immediately after calling fork() but before calling exec, that my
application will crash.

I should be able to resolve this in my app but I was just curious if
this was unexpected behavior or not.

I've attached my cygcheck.out and will include some test code at the end.

######################
sdaley@sdaley2 ~
$ gcc -Wall parent_test.cpp -o parent_test

sdaley@sdaley2 ~
$ ./parent_test.exe
Dumping map in parent
--> length = 11
--> name = test-string
Dumping map in child
   17 [main] parent_test 2460 _cygtls::handle_exceptions: Error while dumping
state (probably corrupted stack)

sdaley@sdaley2 ~
$
######################

Sean

##############################
parent.cpp
##############################

#include <stdio.h>
#include <unistd.h>
#include <windows.h>

#define BUF_SIZE 1024

void *view;
HANDLE hFileMapping;

typedef struct usrInfo {
   int usrlen;
   unsigned char data[1];
} usrInfo;

static void* create_file_map();
static void dump_file_map();

int main(int argc, char **argv)
{
   void *shmp = create_file_map();

   if (shmp == NULL)
   {
       fprintf(stderr, "shmp == NULL\n");
       exit(1);
   }

   usrInfo *usr = (usrInfo*)shmp;
   usr->usrlen = strlen("test-string");
   unsigned char *p = usr->data;
   memcpy(p, "test-string", usr->usrlen);
   p[usr->usrlen] = '\0';

   printf("Dumping map in parent\n");
   dump_file_map();

   if (fork() == 0)
   {
       //const char *args[2];
       //args[0] = "child_test";
       //args[1] = NULL;
       //execvp("./child_test", (char **)args);
       //printf("execvp failed\n");
       printf("Dumping map in child\n");
       dump_file_map();
       exit(1);
   }

sleep(5);

   UnmapViewOfFile(view);
   CloseHandle(hFileMapping);

   return 0;
}

static void
dump_file_map()
{
   usrInfo *usr = (usrInfo*)view;
   if (usr != NULL)
   {
       printf(" --> length = %d\n", usr->usrlen);
       printf(" --> name = %s\n", usr->data);
   }
}

static void *
create_file_map()
{
   SECURITY_ATTRIBUTES sa;

   sa.nLength = sizeof(sa);
   sa.lpSecurityDescriptor = NULL;
   sa.bInheritHandle = TRUE;

   hFileMapping = CreateFileMapping(INVALID_HANDLE_VALUE,
                                    &sa, PAGE_READWRITE, 0, BUF_SIZE,
                                    "TEST-PAGE");

   if (hFileMapping == 0)
   {
       fprintf(stderr, "CreateFileMapping failed\n");
       return NULL;
   }

view = MapViewOfFile(hFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);

   return view;
}

Attachment: cygcheck.out
Description: Binary data

--
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/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]