Chapter: Multicore Application Programming For Windows, Linux, and Oracle Solaris - Windows Threading

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Inheriting Handles in Child Processes

Child processes can inherit the handles to resources owned by the parent process. In this instance, the handles are identical values, but the parent process needs to pass these values to the child.

Inheriting Handles in Child Processes

 

Child processes can inherit the handles to resources owned by the parent process. In this instance, the handles are identical values, but the parent process needs to pass these values to the child. The simplest way of achieving this is to pass the values through the com-mand line.

 

The other constraint on this is that the handles must have been created with the property of being inherited by the child process, and the child process must be created with the parameter that enables it to inherit the handles from its parent.

 

Listing 6.28 shows an example of passing a handle to shared memory to the child process through the command line. The program is broadly the same as the one shown in Listing 6.27. However, there are a number of changes indicated in bold.

 

Listing 6.28   Passing a Handle to Shared Memory to a Child Process

#include <Windows.h>

 

int _tmain( int argc, _TCHAR* argv[] )

 

{

 

STARTUPINFO startup_info;

 

PROCESS_INFORMATION process_info;

 

SECURITY_ATTRIBUTES secat;

 

HANDLE filehandle;

 

TCHAR ID[] = TEXT("Local\\foo"); wchar_t* memory;

 

if          ( argc==1         )          

{          // Parent           process           

            secat.nLength = sizeof(secat);            // Set up security attibutes

            secat.bInheritHandle = true;   // So handle can be inherited

            secat.lpSecurityDescriptor = NULL;

 

filehandle = CreateFileMapping( INVALID_HANDLE_VALUE, &secat, PAGE_READWRITE, 0, 1024, ID );

 

memory = (wchar_t*)MapViewOfFile( filehandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);

 

// Setup command line using shared memory

 

swprintf( memory, 1024, L"\"%s\" %i", argv[0], filehandle);

 

printf( "First process memory: %S handle:%i\n", memory, filehandle);

 

ZeroMemory( &process_info, sizeof(process_info) ); ZeroMemory( &startup_info, sizeof(startup_info) ); startup_info.cb = sizeof(startup_info);

 

// Start child process

 

CreateProcess( NULL, memory, 0, 0, true, 0, 0, 0, &startup_info, &process_info);

WaitForSingleObject( process_info.hProcess, INFINITE );

 

UnmapViewOfFile( memory );

 

CloseHandle( filehandle );

 

}

 

else

 

{

 

filehandle=(HANDLE)_wtoi( argv[1] ); // Get handle from argv[1] memory = (wchar_t*)MapViewOfFile( filehandle,

 

FILE_MAP_ALL_ACCESS, 0, 0, 0 );

 

printf( "Second process memory: %S handle: %i\n", memory, filehandle );

 

UnmapViewOfFile( memory );

 

CloseHandle( filehandle );

 

}

 

getchar(); return 0;

}

 

The first important change is the use of SECURITY_ATTRIBUTES when the mapping object is created. These security attributes have the bInheritHandle property set to true, which will allow the handle to the mapping object to be inherited by any child processes.

 

The command line to the child process is built up out of the process name, which is argv[0] and the handle of the mapping object.

 

The final change in creating the child process is that now the call to CreateProcess() passes true for the parameter, which determines whether the child process should inherit the handles of the parent process.

 

The code for the child process is similar to the previous code with the exception that the handle to the mapping object is extracted from the command-line parameter argv[1].


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.