Windows multi-process Programming

Source: Internet
Author: User

I. Process Concept
A process is an instance of a running program (floating ~~~), Is the unit in which the system allocates resources (the thread is the unit of execution), including memory, opened files, processors, peripherals, etc., the process consists of two parts:
1. Process kernel object: The PCB (Process Control Block) we usually call. This structure can only be accessed by this kernel. It is a data structure used by the operating system to manage processes, the operating system uses this data structure to perceive and manage processes. Its members are responsible for maintaining various information about processes, including the status of processes (creation, readiness, running, sleep, suspension, and freezing) and message queues. It is also used by the system to store statistics about processes.
NOTE: For the specific structure, refer to the task_struct data structure of Linux.
2. process address space: Contains code and data of all executable modules or DLL modules, as well as dynamic memory allocated space, such as thread stack and heap allocated space. There are 4G in total, 0-2G is the user zone, and 2-4G is the system zone.
2. process creation process
1. The system creates a process Kernel Object (PCB Process Control Block ).
2. The system creates a virtual address space for a new process to load the code and data of executable files or any necessary DLL files to the address space of the process.
3. The system creates a thread Kernel Object (TCB thread control block) for the main thread of the new process ).
4. Run the C/C ++ runtime startup code to run the main thread.
Note: In Windows, try to use multiple threads instead of multiple processes.
3. process-related APIs
1. Create a process
Bool CreateProcess (
Pctstr psapplicationname, // name of the executable file
Ptstr pszcommandline, // command line string
Psecurity_attributes psaprocess, // process Object Security
Psecurity_attributes psathread, // security of the thread object
Bool binherithandles, // The handle can be inherited
DWORD fdwcreate, // identifier (priority)
Pvoid pvenvironment, // point to the Environment string
Pctstr pszcurdir, // current directory of the sub-process
Pstartupinfo psistartinfo,
Pprocess_information ppiprocinfo); // process thread handle and ID
2. Open the process
Handle OpenProcess (
DWORD dwdesiredaccess, // Access Security Attribute
Bool binherithandle, // inherits attributes
DWORD hprocessid); // process ID
Note: Get the handle of the kernel object of the process specified by hpronessid
3. Terminate the process
(1) The entry point function of the main thread returns
(2) terminate the process itself
Void exitprocess (
Uint fuexitcode); // exit code
(3) terminate its own process or other processes
Bool terminateprocess (
Handle hprocess, // Process Handle
Uint fuexitcode); // exit code
3. process-related APIs
4. Obtain the handle of the executable file or DLL of the process.
Hmodule getmodulehandle (
Pctstr pszmodule); // Module name
Note: When the parameter data transmission is null, it obtains the base address of the executable file in the process address space.
5. Obtain a process and thread identifier associated with the specified window.
Handle getwindowthreadprocessid (
Hwnd, // window handle
Lpdword lpdwprocessid); // process ID associated with the window
6. Obtain the running time of the process.
Bool getprocesstimes (
Handle hprocess, // Process Handle
Pfiletime pftcreationtime, // Creation Time
Pfiletime pftexittime, // Exit Time
Pfiletime pftkerneltime, // Kernel Time
Pfiletime pftusertime); // user time
Note: The returned time is applicable to all threads in a process (or even threads that have terminated the operation ).
7. Obtain a pseudo handle of the current process
Handle getcurrentprocess ();
Note: This function obtains the pseudo handle of the current process. Generally, the value is-1. It can only identify the kernel object of the current process,
It can be copied but cannot be inherited. You do not have to call the closehandle () function to close the handle.
8. Convert the pseudo handle of a process into a real handle.
Handle duplicatehandle (
Getcurrentprocess (),
Getcurrentprocess (),
Getcurrentprocess (),
& Hprocess,
0,
False,
Duplicate_same_access );
9. Get the ID of the current process
DWORD getcurrentprocessid ();
10. Obtain the process priority
DWORD getpriorityclass (
Handle hprocess );
11. Modify the priority of a process
Bool setpriorityclass (
Handle hprocess, // Process Handle
DWORD fdwpriority); // relative process priority
Note 1: relative thread priority
Real-time: realtime_priority_class
Height: high_priority_class
Higher than normal; above_normal_priority_class
Normal: normal_priority_class
Lower than normal: below_normal_priority_class
Idle: idle_priority_class
NOTE 2: As long as you have a process handle and sufficient permissions, you can modify the optimization of any process running in the system.
First-level class.
12. Obtain Process Handle count
Bool getprocesshandlecount (
Handle hprocess, // handle
Pdword pdwhandlecount); // handle count
13. Obtain the environment Block
DWORD getenvironmentvariable (
Lptstr lpname, // environment variable name
Lptstr lpvalue, // buffer for storing the returned string
DWORD cchvalue); // buffer size
Note: The returned value is the length of the returned string. If the cache is insufficient, the length of the required string is returned.
14. Set the environment Block
Bool setenvironmentvariable (
Lptstr lpname, // environment variable name
Lpctstr lpvalue); // buffer for storing variable value strings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.