|< Windows Process & Threads Programming 9 | Main | Windows Processes & Threads: Synchronization 1 >| Site Index | Download |


 

 

 

 

 

 

MODULE U4

PROCESSES AND THREADS: Win32/WINDOWS APIs

Part 10: More Program Examples

 

 

 

 

 

 

 

 

 

 

What do we have in this Module?

  1. Process and Thread Reference

  2. Process and Thread Functions

  3. Obsolete Functions

  4. Process and Thread Structures

  5. Job Object Structures

  6. Process and Thread Macros

  7. Process Creation Flags

 

 

My Training Period: xx hours. Before you begin, read some instruction here.

 

The expected abilities:

  • Able to understand and use various processes and threads functions (Windows APIs/Win32).

  • Able to build and run your own simple Windows processes and threads programs.

  • Able to understand, build and run your own simple multithreading programs.

  • Able to extract, understand and use functions information from MSDN documentation.

Note: For Multithreaded program examples, you have to set your project to Multithread project type.

 

Process and Thread Reference

 

If you need to explore more, the following elements are used with processes, threads and their related things. Find a Visual C++ / .Net compiler, its MSDN documentation and start digging, more to be hacked :o)

 

Process and Thread Functions

 

The following functions are used with processes.

 

 

Function

Description

CommandLineToArgvW()

Parses a Unicode command-line string.

CreateProcess()

Creates a new process and its primary thread.

CreateProcessAsUser()

Creates a new process and its primary thread. The new process runs in the security context of the specified user.

CreateProcessWithLogonW()

Creates a new process and its primary thread. The new process then runs the specified executable file in the security context of the specified credentials (user, domain, and password).

CreateProcessWithTokenW()

Creates a new process and its primary thread. The new process runs in the security context of the specified token.

ExitProcess()

Ends a process and all its threads.

FreeEnvironmentStrings()

Frees a block of environment strings.

GetCommandLine()

Retrieves the command-line string for the current process.

GetCurrentProcess()

Retrieves a pseudo handle for the current process.

GetCurrentProcessId()

Retrieves the process identifier of the calling process.

GetEnvironmentStrings()

Retrieves the environment block for the current process.

GetEnvironmentVariable()

Retrieves the value of the specified variable from the environment block of the calling process.

GetExitCodeProcess()

Retrieves the termination status of the specified process.

GetGuiResources()

Retrieves the count of handles to graphical user interface (GUI) objects in use by the specified process.

GetLogicalProcessorInformation()

Retrieves information about logical processors and related hardware.

GetPriorityClass()

Retrieves the priority class for the specified process.

GetProcessAffinityMask()

Retrieves a process affinity mask for the specified process and the system affinity mask for the system.

GetProcessHandleCount()

Retrieves the number of open handles that belong to the specified process.

GetProcessId()

Retrieves the process identifier of the specified process.

GetProcessIdOfThread()

Retrieves the process identifier of the process associated with the specified thread.

GetProcessIoCounters()

Retrieves accounting information for all I/O operations performed by the specified process.

GetProcessPriorityBoost()

Retrieves the priority boost control state of the specified process.

GetProcessShutdownParameters()

Retrieves shutdown parameters for the currently calling process.

GetProcessTimes()

Retrieves timing information about for the specified process.

GetProcessVersion()

Retrieves the major and minor version numbers of the system on which the specified process expects to run.

GetProcessWorkingSetSize()

Retrieves the minimum and maximum working set sizes of the specified process.

GetProcessWorkingSetSizeEx()

Retrieves the minimum and maximum working set sizes of the specified process.

GetStartupInfo()

Retrieves the contents of the STARTUPINFO structure that was specified when the calling process was created.

OpenProcess()

Opens an existing process object.

NeedCurrentDirectoryForExePath()

Determines whether the current directory should be included in the search path for the specified executable.

SetEnvironmentVariable()

Sets the value of an environment variable for the current process.

SetPriorityClass()

Sets the priority class for the specified process.

SetProcessAffinityMask()

Sets a processor affinity mask for the threads of a specified process.

SetProcessPriorityBoost()

Disables the ability of the system to temporarily boost the priority of the threads of the specified process.

SetProcessShutdownParameters()

Sets shutdown parameters for the currently calling process.

SetProcessWorkingSetSize()

Sets the minimum and maximum working set sizes for the specified process.

SetProcessWorkingSetSizeEx()

Sets the minimum and maximum working set sizes for the specified process.

TerminateProcess()

Terminates the specified process and all of its threads.

 

Table 1

 

The following functions are used to enumerate processes.

 

Function

Description

EnumProcesses()

Retrieves the process identifier for each process object in the system.

Process32First()

Retrieves information about the first process encountered in a system snapshot.

Process32Next()

Retrieves information about the next process recorded in a system snapshot.

WTSEnumerateProcesses()

Retrieves information about the active processes on the specified terminal server.

 

Table 2

 

The following functions are used with threads.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Function

Description

AttachThreadInput()

Attaches the input processing mechanism of one thread to that of another thread.

CreateRemoteThread()

Creates a thread that runs in the virtual address space of another process.

CreateThread()

Creates a thread to execute within the virtual address space of the calling process.

ExitThread()

Ends a thread.

GetCurrentThread()

Retrieves a pseudo handle for the current thread.

GetCurrentThreadId()

Retrieves the thread identifier of the calling thread.

GetExitCodeThread()

Retrieves the termination status of the specified thread.

GetThreadId()

Retrieves the thread identifier of the specified thread.

GetThreadIOPendingFlag()

Determines whether a specified thread has any I/O requests pending.

GetThreadPriority()

Retrieves the priority value for the specified thread.

GetThreadPriorityBoost()

Retrieves the priority boost control state of the specified thread.

GetThreadStartInformation()

Retrieves the Win32 initialization information for the specified thread.

GetThreadTimes()

Retrieves timing information for the specified thread.

OpenThread()

Opens an existing thread object.

ResumeThread()

Decrements a thread's suspend count.

SetThreadAffinityMask()

Sets a processor affinity mask for the specified thread.

SetThreadIdealProcessor()

Specifies a preferred processor for a thread.

SetThreadPriority()

Sets the priority value for the specified thread.

SetThreadPriorityBoost()

Disables the ability of the system to temporarily boost the priority of a thread.

Sleep()

Suspends the execution of the current thread for a specified interval.

SleepEx()

Suspends the current thread until the specified condition is met.

SuspendThread()

Suspends the specified thread.

SwitchToThread()

Causes the calling thread to yield execution to another thread that is ready to run on the current processor.

TerminateThread()

Terminates a thread.

ThreadProc()

An application-defined function that serves as the starting address for a thread.

TlsAlloc()

Allocates a thread local storage (TLS) index.

TlsFree()

Releases a TLS index.

TlsGetValue()

Retrieves the value in the calling thread's TLS slot for a specified TLS index.

TlsSetValue()

Stores a value in the calling thread's TLS slot for a specified TLS index.

WaitForInputIdle()

Waits until the specified process is waiting for user input with no input pending, or until the time-out interval has elapsed.

 

Table 3

 

The following functions are used with job objects.

 

Function

Description

AssignProcessToJobObject()

Associates a process with an existing job object.

CreateJobObject()

Creates or opens a job object.

IsProcessInJob()

Determines if the process is running in the specified job.

OpenJobObject()

Opens an existing job object.

QueryInformationJobObject()

Retrieves limit and job state information from the job object.

SetInformationJobObject()

Set limits for a job object.

TerminateJobObject()

Terminates all processes currently associated with the job.

UserHandleGrantAccess()

Grants or denies access to a handle to a User object to a job that has a user-interface restriction.

 

Table 4

 

The following functions are used with the thread pool.

 

Function

Description

BindIoCompletionCallback()

Associates the I/O completion port owned by the thread pool with the specified file handle. On completion of an I/O request involving this file, a non-I/O worker thread will execute the specified callback function.

QueueUserWorkItem()

Queues a work item to a worker thread in the thread pool.

 

Table 5

 

The following functions are used with fibers.

 

Function

Description

ConvertFiberToThread()

Converts the current fiber into a thread.

ConvertThreadToFiber()

Converts the current thread into a fiber.

ConvertThreadToFiberEx()

Converts the current thread into a fiber.

CreateFiber()

Allocates a fiber object, assigns it a stack, and sets up execution to begin at the specified start address.

CreateFiberEx()

Allocates a fiber object, assigns it a stack, and sets up execution to begin at the specified start address.

DeleteFiber()

Deletes an existing fiber.

FiberProc()

An application-defined function used with the CreateFiber function.

FlsAlloc()

Allocates a fiber local storage (FLS) index.

FlsFree()

Releases an FLS index.

FlsGetValue()

Retrieves the value in the calling fiber's FLS slot for a specified FLS index.

FlsSetValue()

Stores a value in the calling fiber's FLS slot for a specified FLS index.

SwitchToFiber()

Schedules a fiber.

 

Table 6

 

The following functions provide NUMA support.

 

Note: The traditional model for multiprocessor support is symmetric multiprocessor (SMP). In this model, each processor has equal access to memory and I/O. As more processors are added, the processor bus becomes a limitation for system performance. System designers are now using non-uniform memory access (NUMA) to increase processor speed without increasing the load on the processor bus. The architecture is non-uniform because each processor is close to some parts of memory and farther from other parts of memory. The processor quickly gains access to the memory it is close to, while it can take longer to gain access to memory that is farther away. In a NUMA system, CPUs are arranged in smaller systems called nodes. Each node has its own processors and memory, and is connected to the larger system through a cache-coherent interconnect bus. The system attempts to improve performance by scheduling threads on processors that are in the same node as the memory being used. It attempts to satisfy memory-allocation requests from within the node, but will allocate memory from other nodes if necessary. It also provides an API to make the topology of the system available to applications. You can improve the performance of your applications by using the NUMA functions to optimize scheduling and memory usage.

 

Function

Description

GetNumaAvailableMemoryNode()

Retrieves the amount of memory available in the specified node.

GetNumaHighestNodeNumber()

Retrieves the node that currently has the highest number.

GetNumaNodeProcessorMask()

Retrieves the processor mask for the specified node.

GetNumaProcessorNode()

Retrieves the node number for the specified processor.

 

Table 7

 

Obsolete Functions

 

This function is provided only for compatibility with 16-bit versions of Windows: WinExec().

 

Process and Thread Structures

 

The following structures are used with processes and threads:

  1. IO_COUNTERS

  2. LOGICAL_PROCESSOR_RELATIONSHIP

  3. PROCESS_INFORMATION

  4. STARTUPINFO

  5. SYSTEM_LOGICAL_PROCESSOR_INFORMATION

  6. TEB

 

Job Object Structures

 

The following structures are used with job objects.

  1. JOBOBJECT_ASSOCIATE_COMPLETION_PORT

  2. JOBOBJECT_BASIC_ACCOUNTING_INFORMATION

  3. JOBOBJECT_BASIC_AND_IO_ACCOUNTING_INFORMATION

  4. JOBOBJECT_BASIC_LIMIT_INFORMATION

  5. JOBOBJECT_BASIC_PROCESS_ID_LIST

  6. JOBOBJECT_BASIC_UI_RESTRICTIONS

  7. JOBOBJECT_END_OF_JOB_TIME_INFORMATION

  8. JOBOBJECT_EXTENDED_LIMIT_INFORMATION

  9. JOBOBJECT_SECURITY_LIMIT_INFORMATION

 

Process and Thread Macros

 

The following macros are used with fibers:

Process Creation Flags

 

The following process creation flags are used by the CreateProcess() and CreateProcessAsUser() functions. They can be specified in any combination, except as noted.

 

Value

Meaning

CREATE_BREAKAWAY_FROM_JOB

The child processes of a process associated with a job are not associated with the job.

If the calling process is not associated with a job, this flag has no effect. If the calling process is associated with a job, the job must set the JOB_OBJECT_LIMIT_BREAKAWAY_OK limit.

Windows NT and Windows Me/98/95:  This value is not supported.

CREATE_DEFAULT_ERROR_MODE

The new process does not inherit the error mode of the calling process. Instead, the new process gets the current default error mode. An application sets the current default error mode by calling SetErrorMode(). This flag is particularly useful for multi-threaded shell applications that run with hard errors disabled. The default behavior is for the new process to inherit the error mode of the caller. Setting this flag changes that default behavior.

CREATE_NEW_CONSOLE

The new process has a new console, instead of inheriting its parent's console (the default).  This value cannot be used with DETACHED_PROCESS.

CREATE_NEW_PROCESS_GROUP

The new process is the root process of a new process group. The process group includes all processes that are descendants of this root process. The process identifier of the new process group is the same as the process identifier, which is returned in the lpProcessInformation parameter. Process groups are used by the GenerateConsoleCtrlEvent() function to enable sending a CTRL+BREAK signal to a group of console processes. If this flag is specified, CTRL+C signals will be disabled for all processes within the new process group. Windows Server 2003:  This flag is ignored if specified with CREATE_NEW_CONSOLE.

CREATE_NO_WINDOW

The process is a console application that is run without a console window. This flag is valid only when starting a console application. This flag cannot be used with MS-DOS-based applications. For Windows Me/98/95:  This value is not supported.

CREATE_PRESERVE_CODE_AUTHZ_LEVEL

Allows the caller to execute a child process that bypasses the process restrictions that would normally be applied automatically to the process. Windows 2000/NT and Windows Me/98/95:  This value is not supported.

CREATE_SEPARATE_WOW_VDM

This flag is valid only when starting a 16-bit Windows-based application. If set, the new process runs in a private Virtual DOS Machine (VDM). By default, all 16-bit Windows-based applications run as threads in a single, shared VDM. The advantage of running separately is that a crash only terminates the single VDM; any other programs running in distinct VDMs continue to function normally. Also, 16-bit Windows-based applications that are run in separate VDMs have separate input queues. That means that if one application stops responding momentarily, applications in separate VDMs continue to receive input. The disadvantage of running separately is that it takes significantly more memory to do so. You should use this flag only if the user requests that 16-bit applications should run in their own VDM. For Windows Me/98/95:  This value is not supported.

CREATE_SHARED_WOW_VDM

The flag is valid only when starting a 16-bit Windows-based application. If the DefaultSeparateVDM() switch in the Windows section of WIN.INI is TRUE, this flag overrides the switch. The new process is run in the shared Virtual DOS Machine. For Windows Me/98/95:  This value is not supported.

CREATE_SUSPENDED

The primary thread of the new process is created in a suspended state, and does not run until the ResumeThread() function is called.

CREATE_UNICODE_ENVIRONMENT

If this flag is set, the environment block pointed to by lpEnvironment uses Unicode characters. Otherwise, the environment block uses ANSI characters.

Windows Me/98/95:  This value is not supported.

DEBUG_ONLY_THIS_PROCESS

The calling thread starts and debugs the new process. It can receive all related debug events using the WaitForDebugEvent() function.

DEBUG_PROCESS

The calling thread starts and debugs the new process and all any child processes of the new process that are created with DEBUG_PROCESS. It can receive all related debug events using the WaitForDebugEvent() function. If this flag is combined with DEBUG_ONLY_THIS_PROCESS, the caller debugs only the new process. For Windows Me/98/95:  This flag is not valid if the new process is a 16-bit application.

DETACHED_PROCESS

For console processes, the new process does not inherit its parent's console (the default). The new process can call the AllocConsole() function at a later time to create a console. This value cannot be used with CREATE_NEW_CONSOLE.

 

Table 8

 

On 32-bit Windows, 16-bit applications are simulated by ntvdm.exe, not run as individual processes. Therefore, the process creation flags apply to ntvdm.exe. Because ntvdm.exe persists after you run the first 16-bit application, when you launch another 16-bit application, the new creation flags are not applied, except for CREATE_NEW_CONSOLE and CREATE_SEPARATE_WOW_VDM, which create a new ntvdm.exe.

 

 

--------------------End Windows Processes & Threads U/U1/U2/U3/U4---------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Further reading and digging:

 

  1. Microsoft Visual C++, online MSDN.

  2. For Multibytes, Unicode characters and Localization please refer to Locale, wide characters & Unicode (Story) and Windows users & groups programming tutorials (Implementation).

  3. Structure, enum, union and typedef story can be found C/C++ struct, enum, union & typedef.

  4. Check the best selling C / C++ and Windows books at Amazon.com.

 

 

 

 

 

 

 

 |< Windows Process & Threads Programming 9 | Main | Windows Processes & Threads: Synchronization 1 >| Site Index | Download |