3.3.2 Creation Flags

The creation flags argument to CreateProcess can control the way in which the process is created. These allow you to create a detached or suspended process, or for console applications, to create a new console window rather than using the console of the parent process. There are also flags that can be used by debuggers when executing a process to be debugged. For more information on these debugging flags, see Chapter 22, “Debugging.”

A detached process is one that executes in the background and does not have access to the Standard Input and Standard Output handles of its parent. A detached process is not affected if its parent process is killed (as in Ctrl-C), whereas child processes that are not detached are killed if the parent is killed. Note that normal child processes (i.e., not detached) are terminated automatically only if their parent is killed, not when their parent terminates normally. However, once the parent has terminated, the child will no longer have access to the console. Typically detached processes are used for tasks that do not interact with the user through keyboard or mouse input, or output to the screen. However, a detached process can create a new console by calling AllocConsole if it needs to interact with the user. The DETACHED_PROCESS flag is relevant only to console applications. Applications that create a new window, whether GUI applications that use CreateWindow or console applications created with the CREATE_NEW_CONSOLE flag, are always detached from their parent process.

If the CREATE_NEW_CONSOLE flag is used, the new process is similar to the detached process in that it cannot access the parent's console and it is not killed if the parent is killed. The difference is that a new console is created automatically.

A child process created with the CREATE_SUSPENDED creation flag will be started with its primary thread suspended. To begin execution of the suspended thread, call ResumeThread specifying the handle to the primary thread that is returned in the PROCESS_INFORMATION structure.

The parent process can specify the priority class of the child process by using one of the following creation flags: NORMAL_PRIORITY_CLASS, IDLE_PRIORITY_CLASS, or HIGH_PRIORITY_CLASS. If not specified, the default priority class is NORMAL_PRIORITY_CLASS. Use of the different priority classes is discussed below in the section on controlling thread priority.