Thread: CreateProcess and Memory mapped files

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    7

    Exclamation CreateProcess and Memory mapped files

    hi everyone

    I'm taking a course in operating systems, and I have an assignment that uses multiple processes, and memmory mapped files.

    I have been able to create a memory mapped file (only took about 60 hours looking at the documentation), but I am unable to use create process. Currently I am trying to get the original process to run a hello world program as its child. whenever I run the program it just skips that section. I would really appreciate someone explaining in english how to use the createProcess comand(ie what all the fields mean). or better yet if someone has a SIMPLE program that uses Create Process that would be great thanks

    kevcri

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What OS?

    Kuphryn

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Got to be Windows, which other OS takes 10 arguments for a function to start a program?

    The good news is that most of them are usually set to NULL.
    Code:
    	STARTUPINFO si          = { 0 };
    	PROCESS_INFORMATION pi  = { 0 };
    	BOOL bRet;
    
    	si.cb = sizeof(si);
    	bRet = CreateProcess("C:\\windows\\system32\\notepad.exe", " arguments or NULL if none", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
    
    	if (!bRet) printf("Failed to start process with error %d.\n", GetLastError());
    
    	// If you want to wait for the process to end...
    	// WaitForSingleObject(pi.hProcess, INFINITE);
    
    	// Cleanup - This will not terminate the process if it is still running.
    	if (pi.hThread)  CloseHandle(pi.hThread);
    	if (pi.hProcess) CloseHandle(pi.hProcess);
    If you need clarification on some of the arguments or memory mapping in general please outline your specific queries. It is a bit hard to give a complete description of all ten arguments!

    Also consider specifying your operating system when posting OS related issues. If it relates to DOS, Linux, or Windows you may wish to post it in one of those boards.

    Have Fun!

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    7

    thanks

    that is helpful. sorry for not mentioning the OS, and I will post to the windows board next time. I just have 2 specific questions left.
    First, can I replace notepad.exe with some other program for example child.exe. (I assume so).

    second do I need to change any of the arguments in order for the child process to access the memory mapped files?.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Read this.
    Then look at the lpName parameter of CreateFileMapping().

    gg

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    7

    almost got it

    I'm almost there, I have been able to start the second process and I've ben able to create the memorymapped file and use it in the parent process. The only thing I can't do now is read the mapped file from the child process. here is the code for creating the files from the parent process
    Code:
     A1=CreateFileMapping(
      INVALID_HANDLE_VALUE,
      NULL,
      PAGE_READWRITE,
      0,
      0x01400000,
     "MAPPEDA1");
    
    TEST=MapViewOfFile(A1,					// handle to mapping object 
        FILE_MAP_ALL_ACCESS,               // read/write permission 
        0,                                 // max. object size 
        0,                                 // size of hFile 
        0);
    and this is the code to map the file view in the child process

    Code:
    TEST=MapViewOfFile(A1, // handle to mapping object 
        FILE_MAP_ALL_ACCESS,               // read/write permission 
        0,                                 // max. object size 
        0,                                 // size of hFile 
        0);
    I know that it's just a matter of understanding which handles name which pieces, Thank you all for all the help you've given so far

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What is "A1" in the child process?

    gg

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    7
    I declared A1 as a handle. It was my understanding that since memory mapped files share the same name space I the child process would know the mapped file by name.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    What I'm getting at is, do you call both CreateFileMapping() and MapViewOfFile() in both processes?

    gg

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    7

    no

    No, am I supposed to?

    I called only the functions I showed. am I supposed to call both in both?
    thanks
    kc

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >>I called only the functions I showed. am I supposed to call both in both?<<

    Yes.

    Under lpFileName in documentation for CreateFileMapping as referred by Codeplug
    If this parameter matches the name of an existing named mapping object, the function requests access to the mapping object
    This means that if a file mapping with the same name does not exist it is created, if it already exists it is opened.

    It is also possible to pass the file mapping handle to the child process to use. However, this is more complicated than simply opening it by name in the child process.

  12. #12
    Registered User
    Join Date
    Dec 2003
    Posts
    7

    i really think I'm close now

    I did what you said, and I'm not getting the error I was getting. the only problem is that the memory mapped file in the child process does not contain the same data as in the parent process.
    please check the declarations and make sure I'm using the right parameters

    parent process
    Code:
     A1=CreateFileMapping(
      INVALID_HANDLE_VALUE,
      NULL,
      PAGE_READWRITE,
      0,
      0x01400000,
     "MAPPEDA1");
    
    TEST=MapViewOfFile(A1,					// handle to mapping object 
        FILE_MAP_ALL_ACCESS,               // read/write permission 
        0,                                 // max. object size 
        0,                                 // size of hFile 
        0);  
    
    
    *((LPWORD)TEST)=50;
    
    printf ("%d",*((LPWORD)TEST));

    child process

    Code:
     A1=CreateFileMapping(
      INVALID_HANDLE_VALUE,
      NULL,
      PAGE_READWRITE,
      0,
      0x01400000,
     "MAPPEDA1");
    
    
    
    
    
    TEST=MapViewOfFile(A1, // handle to mapping object 
        FILE_MAP_ALL_ACCESS,               // read/write permission 
        0,                                 // max. object size 
        0,                                 // size of hFile 
        0);  
    
    
    
    printf ("%d",*((LPWORD)TEST));
    these both should print out 50 but I get 50 and 0

    thanks for all your help so far

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I mislead everyone.
    The child process should call OpenFileMapping() instead of CreateFileMapping().

    gg

  14. #14
    Registered User
    Join Date
    Dec 2003
    Posts
    7

    so close

    ok I tried using openfilemapping. now I'm back to getting the access violation error. I'm not really sure what the problem is now. here is what I have right now.
    parent process

    Code:
     A1=CreateFileMapping(
      INVALID_HANDLE_VALUE,
      NULL,
      PAGE_READWRITE,
      0,
      0x01400000,
     "Global\\MAPPEDA1");
    
    TEST=MapViewOfFile(A1,					// handle to mapping object 
        FILE_MAP_ALL_ACCESS,               // read/write permission 
        0,                                 // max. object size 
        0,                                 // size of hFile 
        0);

    Child
    Code:
    A1=OpenFileMapping(FILE_MAP_ALL_ACCESS,
    				FALSE,
    				"Global\\MAPPEDA1");
    
    TEST=MapViewOfFile(A1, // handle to mapping object 
        FILE_MAP_ALL_ACCESS,               // read/write permission 
        0,                                 // max. object size 
        0,                                 // size of hFile 
        0);

    I'm sure I'm close. thanks for all your help

  15. #15
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >>I mislead everyone.<<

    No you didn't. The child process can use OpenFileMapping but calling CreateFileMapping in both also works fine.

    kevcri, I'm not sure what is wrong with your revised code. It seems alright. Are you sure that you are not letting the parent close before the child has finished?

    Anyway, here is a working example. Simply compile and run twice or more. The first instance will create the mapping, subsequent instances will access it.

    Code:
    int main(void) {
    
    	HANDLE hMapping = NULL;
    	void * lpView   = NULL;
    
    	hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
    	                             0, 0x01400000, "MAPPEDA1");
    
    	if (!hMapping) {
    		printf("CreateFileMapping failed with error %d\n", GetLastError());
    	}
    	else {
    		lpView=MapViewOfFile(hMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
    
    		if (!lpView) {
    			printf("MapViewOfFile failed with error %d\n", GetLastError());
    		}
    		else {
    			printf("Current value of dword at lpView is %d\n", *((DWORD *) lpView));
    			*((DWORD *) lpView) += 50;
    			printf("Value of dword at lpView after addition is %d\n", *((DWORD *) lpView));
    		}
    	}
    
    	printf("\nPress ENTER to exit...\n");
    	getchar();
    
    	if (lpView)   UnmapViewOfFile(lpView);
    	if (hMapping) CloseHandle(hMapping);
    
    	return 0;
    }
    kevcri, notice the error checking, which will stop the access violation and tell you what is failing and why.

    Although you can use either CreateFileMapping or OpenFileMapping, you may wish to use OpenFileMapping in a child process so you can signal failure if the mapping does not already exist.

    OpenFileMapping also allows you to only specify the size, security attributes, etc in the parent process.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading process from memory
    By X PaYnE X in forum Windows Programming
    Replies: 15
    Last Post: 05-03-2009, 04:34 PM