Go Windows multi-process programming

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/bxhj3014/article/details/2082255

I. Concept of the process
A process is an instance of a running program (floating ~ ~ ~ ~ ~ ~ ~ ~ ~ ~), is the unit of the System allocating resources (threads are the units of execution), including memory, open files, processors, peripherals, etc., the process consists of two parts:
1, the kernel object of the process: that is, the PCB (Process control block) that we usually speak, the structure can only be accessed by the kernel, he is a data structure used by the operating system to manage processes, the operating system to perceive and manage processes through this data structure, its members are responsible for maintaining the process of various information, including the status of the process , run, sleep, hang, zombie, etc.), Message Queuing, and the place where the system is used to store statistical information about the process.
Note: The specific structure refers to the Linux TASK_STRUCT data structure.
2. Process address space: Contains code and data for all executable modules or DLL modules, as well as space for dynamic memory allocations, such as thread stacks and heap allocations. Total 4g,0-2g is the user area, 2-4G is the system area.
Second, the process of the creation process
1. System creation process kernel object (PCB Process control block).
2. The system creates a virtual address space for the new process, which loads the code and data of the executable file or any necessary DLL files into the address space of the process.
3. The system creates a thread kernel object (TCB thread control block) for the main thread of a new process.
4. The main thread begins to run by executing the C + + run-time startup code.
Note: In a Windows environment, try to use multithreading instead of multiple processes.
III. Process-related APIs
1. Create a process
BOOL CreateProcess (
Pctstr Psapplicationname,//Name of executable file
Ptstr pszcommandline,//command line string
Psecurity_attributes psaprocess,//Security of Process objects
Psecurity_attributes Psathread,//Security of Thread objects
BOOL binherithandles,//Handle inheritable
DWORD fdwcreate,//identifier (priority)
PVOID pvenvironment,//point to environment string
Pctstr Pszcurdir,//Sub-process current directory
Pstartupinfo Psistartinfo,
Pprocess_information ppiprocinfo); Process thread handle and ID
2. Open process
HANDLE OpenProcess (
DWORD dwdesiredaccess,//Access security properties
BOOL bInheritHandle,//Inheritance property
DWORD hprocessid); Process ID
Note: Gets the handle of the kernel object for the specified process Hpronessid
3. Terminate the process
(1), the entry point function of the main thread returns
(2), the process itself terminates itself
VOID ExitProcess (
UINT Fuexitcode); Exit code
(3), terminate own process or other process
BOOL TerminateProcess (
HANDLE hprocess,//process handle
UINT Fuexitcode); Exit code
III. Process-related APIs
4. Gets the handle of the executable file or DLL for the process
Hmodule GetModuleHandle (
Pctstr pszmodule); Module name
Note: When a parameter is passed NULL, the base address of the executable file in the process's address space is obtained.
5. Gets a process and thread identifier associated with the specified window
HANDLE GetWindowThreadProcessId (
HWND hwnd,//Window handle
Lpdword Lpdwprocessid); The process ID associated with the window
6. Get the running time of the process
Bool Getprocesstimes (
HANDLE hprocess,//process handle
Pfiletime Pftcreationtime,//Creation time
Pfiletime Pftexittime,//Exit time
Pfiletime Pftkerneltime,//Core time
Pfiletime pftusertime); User Time
Note: The time that is returned applies to all threads in a process (even threads that have terminated running).
7. Get a pseudo handle of the current process
HANDLE getcurrentprocess ();
Note: This function gets the pseudo-handle of the current process, usually with a value of-1, which can only identify the current process kernel object.
Can be copied, but not inherited. You do not have to call the CloseHandle () function to close this handle.
8. Converting a process's pseudo-handle to a real handle
HANDLE DuplicateHandle (
GetCurrentProcess (),
GetCurrentProcess (),
GetCurrentProcess (),
&hprocess,
0,
FALSE,
duplicate_same_access);
9. Get the current process ID
DWORD GetCurrentProcessId ();
10. Get Process Priority
DWORD Getpriorityclass (
HANDLE hprocess);
11. Change the priority class of the process
BOOL Setpriorityclass (
HANDLE hprocess,//process handle
DWORD fdwpriority); Relative process priority
Note 1: Relative thread priority
Real-time: Realtime_priority_class
High: High_priority_class
higher than normal; Above_normal_priority_class
Normal: Normal_priority_class
Below normal: Below_normal_priority_class
Idle: Idle_priority_class
Note 2: As long as you have a handle to the process and sufficient permissions, you can modify the optimization of any process running in the system
First class.
12. Get Process Handle Count
BOOL Getprocesshandlecount (
HANDLE hprocess,//Handle
Pdword pdwhandlecount); Handle Count
13. Get the Environment block
DWORD GetEnvironmentVariable (
LPCTSTR lpname,//Name of environment variable
LPTSTR Lpvalue,//buffer to store the returned string
DWORD cchvalue); Size of the buffer
Note: The return value is the length of the returned string, and the length of the desired string is returned when the cache is low
14. Setting the environment block
BOOL setenvironmentvariable (
LPCTSTR lpname,//Name of environment variable
LPCTSTR lpvalue); Buffer that holds the variable value string

Go Windows multi-process programming

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.