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

Creating Processes

To create a new process, the first process calls CreateProcess(), which takes a number of parameters.

Creating Processes

 

To create a new process, the first process calls CreateProcess(), which takes a number of parameters. The three critical parameters are the name (and any parameters) of the process to be run, together with a pointer to a STARTUPINFO structure and a PROCESS_ INFORMATION structure. Table 6.4 shows the full parameters to this function.

 

Table 6.4      Parameters Passed to CreateProcess()


The first parameter is the application name, and the second parameter is the com-mand line. These two parameters work together. If either is null, the other is used as the entire command line. Otherwise, the application named by the first argument is executed but passed the command line given by the second argument. This means that the first parameter of the command line should be a repeat of the name of the application.

 

There are two further considerations: The Unicode version of this function, CreateProcessW(), is called, and the command line can be modified; hence, it should always be stored in a variable rather than a constant string. Second, if the application name is null, then the application executed will be the first whitespace-delimited text in the command line. If the path to the application contains a space, then the entire path and name should be enclosed in quotes.

 

The third and fourth parameters are optional pointers to SECURITY_ATTRIBUTES. The first gives the attributes for the created process, and the second of these is attributes for the first thread of the created process. These attributes principally determine whether child processes of the created process will inherit any handles owned by the created process. A null value for these pointers provides the created process with the default attributes.

 

Both the STARTUPINFO and PROCESS_INFORMATION structures should be zero-filled before the call. The cb member of the STARTUPINFO structure needs to be set to the size of the structure. The call to CreateProcess() will record information in these struc-tures. The most important information will be the handle of the new process that is recorded in the hProcess member of the PROCESS_INFORMATION structure.

 

Listing 6.25 shows the steps necessary to create a new process. If the application is started with a command-line parameter, the process will print this out and then create a child process without any parameters. If the process is started without any parameters, it prints out a message indicating this and exits.

 

Listing 6.25   Starting a New Process

#include <Windows.h>

 

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

 

{

 

STARTUPINFO startup_info;

 

PROCESS_INFORMATION process_info;

 

if ( argc>1 )

 

{

 

wprintf( L"Argument %s\n", argv[1] ); wprintf( L"Starting child process\n" );

 

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

 

if (CreateProcess( argv[0], 0, 0, 0, 0, 0, 0, 0,

 

&startup_info, &process_info )==0 )

 

{

 

printf( "ERROR %i\n", GetLastError() );

 

}

 

WaitForSingleObject( process_info.hProcess, INFINITE );

 

}

 

else

 

{

 

printf( "No arguments\n" );

 

}

 

getchar();

 

}

 

 

The handle of the created process is returned in process_info.hProcess. This handle is used in the call to WaitForSingleObject(). This call returns when the child process exits.

 

The call to getchar() at the end of the code is there to wait for the Enter key to be pressed before the process exits.

 

To pass arguments to a child process, it is necessary to repeat the application name as the first command-line parameter. The entire command line gets passed to the child process. Listing 6.26 shows the situation where the child process is started with a single additional argument.

 

Listing 6.26   Passing Arguments to a New Process

 

#include <Windows.h>

 

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

 

{

 

STARTUPINFO startup_info;

 

PROCESS_INFORMATION process_info;

 

if ( argc==1 )

 

{

 

printf( "No arguments given starting child process\n" );

 

wchar_t argument[256];

 

wsprintf( argument, L"\"%s\" Hello", argv[0] );

 

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

 

if (CreateProcess( argv[0], argument, 0, 0, 0, 0, 0, 0, &startup_info, &process_info )==0 )

{

 

printf( "ERROR %i\n", GetLastError() );

}

 

WaitForSingleObject( process_info.hProcess, INFINITE );

 

}

 

else

 

{

 

wprintf( L"Argument %s\n", argv[1] );

 

}

 

getchar();

 

}

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Multicore Application Programming For Windows, Linux, and Oracle Solaris : Windows Threading : Creating Processes |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

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