Multi-process programming (creation and end of the process)

xiaoxiao2021-03-06  26

1. Differences between processes and threads

The process is independent, which is manifested in memory space, context; thread runs in the process space.

Generally speaking (not using a special technology) process is unable to break through the memory space within the process boundary; threads, the threads generated by the same process shared the same memory space due to the process space. (Figure 1)

The two codes in the same process cannot be executed at the same time, unless the thread is introduced.

Threads are processed, and the threads generated by the process when the process exits is forced and cleared.

Thread occupied resources less than the resources occupied by the process.

Process and threads can have priority.

The process in the thread system is also a thread. The process can be understood as the first thread of a program.

2. Implement the creation of the process Bool CreateProcess

Lpctstr lpapplicationname, // Execute program file name

LPTSTR LPCOMMANDLINE, // Parameter Line

LPSecurity_attributes lpprocessattributes, // Process security parameters

LPSecurity_attributes lpthreadattributes, // thread security parameters

Bool binherithandles, // inheritance mark

DWORD DWCREATIONFLAGS, // Create tag

LPVOID LPENVIRONMENT, / / ​​environment variable

LPCTSTR LPCURRENTDIRECTORY, / / ​​Run the initial directory of the child process

LPStartupinfo lpstartupinfo, // Create related parameters for this child process

LPPROCESS_INFORMATION LPPROCESSINFORMATION // created information used to create sub-process

);

3. Implement the end of the process

Bool TerminateProcess

Handle hprocess, // process handle

Uint uExitcode // exit code

);

Focus:

CreateProcess's LPStartupinfo, parameter structure

Typedef struct _startupinfo {// si

DWORD CB; // Structural length

Lptstr lpreserved; // retain

LPTSTR LPDESKTOP; / /

LPTSTR LPTITLE; / / If the program is displayed for the console process

DWORD DWX; // Window location

DWORD DWY; // Window location

DWORD DWXSIZE; // Window Size

DWORD dwysize; // window size

DWORD dwxcountchars; // Console window word symbol width

DWORD DWYCOUNTCHARS; // Console window word symbol height

DWORD DWFILLATTRIBUTE; // Console Window Fill Mode

DWORD dwflags; // Create tag

Word wshowwindow; // window display tag is like a tag in showwindow

Word CBRESERVED2; //

LPBYTE LPRESERVED2; //

Handle HSTDINPUT; / / Standard Enter Handle

Handle hstdoutput; // Standard output handle

Handle hstderror; // Standard error handle

} Startupinfo, * lpstartupinfo;

If you want to work in the structure, you must set the dwflags correctly. For example, dwflags contains StartF_USSize to indicate dwxsize and dwysize, including startf_useposition, which is valid, DWX and DWY.

LPPROCESSINFORMATION parameters in CreateProcess

LPPROCESSINFORMATION: Used to receive related information after the process is created, the structure is filled out by the system. Typedef struct _process_information {// pihandle hprocess; // process handle

Handle Hthread; // Main thread handle

DWORD DWPROCESSID; // Process ID

DWORD DWTHREADID; // Main thread ID of the process

} Process_information;

prompt:

1.

Startupinfo Si;

StartupInfo must initialize 0

Should be Startupinfo Si = {0};

Otherwise, the system will prompt the error.

2.

C: //winnt/notepad.exe must double slash

in conclusion

Use CreateProcess to perform a program under Windows, but it does not use the dos shell to explain it, so it cannot identify the ">" operation symbol, just use it as a command line parameter that runs it as a run.

explain:

1. CREATEPROCESS parameter description

LPApplicationName: For the executor's file name, if you want to use a parameter when you create a process, the parameter can be NULL.

LPCommandline: For parameter rows, if the parameters can be null, set the following settings when there is parameter to the process: lpapplicationName = null; lpcommandline = para, such as lpcommandline = "c: //windows/notepad.exe c: // autoexec .bat.

LPPRocessAttributes, LPTHREADATIADATTRIBUTES: The created process and thread security properties are described, and if null is used to represent the default security description.

BinheritHandles: Indicates whether the handle in the current process can be inherited by the sub-process that can be created.

DWCREATIONFLAGS: Represents the creation tag, which can set the creation status and priority of the process via this tag. Commonly used tags:

CREATE_NEW_CONSOLE: Create a new console for the child process.

· CREATE_SUSPENDED: The child process is hang when it is created.

High_Priority_Class / Normal_Priority_Class: High / ordinary priority.

LpenVironment: Indicates the environment variable used by the child process. If null, the same environment variable is used to use the current process.

LpCurrentDirectory: Indicates the initial directory of the sub-process run.

LPSTARTUPINFO: Used to set various properties when creating a child process. :

LPPROCESSINFORMATION: Used to receive information after the process is created

2. Error handling problem

Find the error code in WineError.h, you can know the general meaning of the error

Ready-made error message processing function: formatMessage (). The following is an example of error information corresponding to error encoding returned by getLastError (): lpvoid lpmsgbuf;

转载请注明原文地址:https://www.9cbs.com/read-65172.html

New Post(0)