Processes and Threads
This section details the Windows API functions and concepts related to managing processes and threads, the fundamental units of execution within the Windows operating system.
Processes
A process is an instance of a running program. It has its own private virtual address space, code, data, and system resources such as handles to files, devices, and other objects. Each process is identified by a unique Process ID (PID).
Creating and Managing Processes
The following functions are commonly used for process management:
Function | Description |
---|---|
CreateProcess |
Creates a new process and its primary thread. |
ExitProcess |
Terminates the calling process and all of its threads. |
TerminateProcess |
Terminates a specified process. |
GetProcessId |
Retrieves the identifier of the specified process. |
OpenProcess |
Retrieves a handle to a process that is specified by the identifier. |
GetCurrentProcess |
Returns a pseudo-handle for the current process. |
Threads
A thread is the basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack. A process can have one or more threads, all sharing the same address space and resources.
Creating and Managing Threads
Key functions for thread operations include:
Function | Description |
---|---|
CreateThread |
Creates a new thread to execute within the virtual address space of the calling process. |
ExitThread |
Terminates the calling thread. |
TerminateThread |
Terminates a specified thread. |
GetThreadId |
Retrieves the identifier of the specified thread. |
OpenThread |
Retrieves a handle to a thread that is specified by the identifier. |
GetCurrentThread |
Returns a pseudo-handle for the current thread. |
Sleep |
Suspends the current thread for a specified interval. |
Synchronization
When multiple threads access shared resources, synchronization mechanisms are crucial to prevent race conditions and ensure data integrity. Common synchronization objects include:
- Mutexes (Mutual Exclusion Objects): Allow only one thread to access a resource at a time.
- Semaphores: Control access to a pool of resources by maintaining a count.
- Events: Signal the occurrence of an event to one or more threads.
- Critical Sections: Lightweight synchronization objects for exclusive access to resources within a single process.
WaitForSingleObject
or WaitForMultipleObjects
to wait for the state of an object to become signaled.
Thread Pools
Thread pools provide a managed environment for executing callback functions on a pool of worker threads. This can improve application performance by reducing the overhead of thread creation and destruction.
// Example of creating a process
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (!CreateProcess(L"C:\\Path\\To\\Your\\Program.exe", // No module name (use command line)
NULL, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi)) // Pointer to PROCESS_INFORMATION structure
{
printf("CreateProcess failed (%d).\n", GetLastError());
return;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);