Go Windows multi-process programming

Source: Internet
Author: User

Turn from: 2082255

I. Concept of the process

A process is an instance of a running program, the unit in which the system allocates resources (the thread is the unit of execution), including memory, open files, processors, peripherals, etc., and the process consists of two parts:

1. The kernel object of the process: that is, the PCB (Process control block) that we commonly speak, which is accessible only by the kernel, which is a data structure used by the operating system to manage processes, through which the operating system perceives and manages processes, and its members are responsible for maintaining various information about the process, including the state of the process (create, ready , run, sleep, hang, zombie, etc.), Message Queuing, and the place where the system is used to store statistical information about the process.

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 (Get kernel object handle of HPROCESSID corresponding process)
HANDLE OpenProcess (
DWORD dwdesiredaccess,//Access security properties
BOOL bInheritHandle,//Inheritance property
DWORD hprocessid); Process ID

3, terminating process
(1) The entry point function of the main thread returns
(2) The process itself terminates itself
& nbsp;             VOID ExitProcess (
                          UINT Fuexitcode); Exit code
(3) terminates its own process or other process
              BOOL TerminateProcess (
                       HANDLE hprocess,//process handle
                       UINT Fuexitcode); Exit code

4, gets the handle of the process's executable file or DLL
               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. Find the creator (thread or process) of a specified window and return the creator's ID. Which thread creates this window and returns the ID number of the thread (the thread identifier is the same as the process marker if the process has only one thread).
HANDLE GetWindowThreadProcessId (
HWND hwnd,//Window handle
Lpdword Lpdwprocessid); The ID of the process or thread that created 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 kernel object of the process, copy but not inherit. You do not have to call the CloseHandle () function to close this handle.
The pseudo-handle can only be used within the process, if you want to get the actual handle, communication between processes, you will need to convert, call DuplicateHandle, note that the actual handle after the use of the completion, You have to call CloseHandle to close.

   8. Converting a process's pseudo-handle to a real handle
HANDLE DuplicateHandle (
GetCurrentProcess (),
GetCurrentProcess (),
GetCurrentProcess (),
&hprocess,
0,
FALSE,
duplicate_same_access);

    Note: the instance handle must call the CloseHandle () function to close the handle, otherwise there is a handle leak.

  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: You can modify the priority class of any process running on the system as long as you have a handle to the process and sufficient permissions.

12. Gets the number of handles that the specified process has opened
BOOL Getprocesshandlecount (
HANDLE hprocess,//Handle
Pdword pdwhandlecount); Handle Count

  13. Get Environment variables
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. The common system environment variables are as follows:

1WINDIR://system Catalog-C:\WINDOWS2SYSTEMROOT://system Catalog-C:\WINDOWS3SystemDrive://system root directory-C:4Homedrive://Current User root directory-C:5UserProfile://Current User Directory-C:\Users\Kandy6HomePath://Current User path-\users\kandy7Tmp://Current User Temp folder-C:\Users\Kandy\AppData\Local\Temp8TEMP://Current User Temp folder-C:\Users\Kandy\AppData\Local\Temp9APPDATA://Current User Data folder-C:\Users\Kandy\AppData\RoamingTenProgramFiles://Program default installation directory-C:\Program Files (x86) OneCommonProgramFiles://file Universal Directory-C:\Program Files (x86) \common files AUSERNAME://Current user name-Kandy -AllUsersProfile://All users Files directory-C:\ProgramData -OS://operating system name-Windows_NT theCOMPUTERNAME://Computer name-kandy-pc -Number_of_processors://number of processors-4 -Processor_architecture://Processor chip architecture-x86 -Processor_level://Processor Model-6 +Processor_revision://Processor Revision number-3C03 -UserDomain://domain with user account-kandy-pc +COMSPEC://C:\WINDOWS\system32\cmd.exe APathext://Execute file type-. COM;. EXE;. BAT;. CMD;. VBS;. VBE;. JS;. JSE;. WSF;. WSH;. MSC atPATH://Search Path

14. Setting Environment variables
DWORD setenvironmentvariable (
LPCTSTR lpname,//Name of environment variable
LPCTSTR lpvalue); Buffer that holds the variable value string
Note: When the environment variable lpname does not exist and Lpvalue is not empty, a new environment variable is created.

Go Windows multi-process programming

Related Article

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.