Abstract: This paper presents a multi-tasking management of multi-process management technology, the process of mutually exclusive operation of the process of creation and the end to give more detail.
Key words: VC + + 6.0; process; environment variable; child process
Process
Under the current process is an operating system loaded into memory, the application is running examples. Each is a process by the kernel address space objects and components, the system kernel objects can be stored in its process of the relevant statistical information and management systems in order to the process, and address space includes all program modules code and data as well as thread stack, stack space allocated dynamically allocated space. There is only one process is not completed any action on their own, must have at least one run in their environment threads by its responsible for the implementation of the process address space in the code. At the same time in the process that started at the same time launched a thread, the thread known as the main thread or execution threads, which can continue to create thread of threads. If the main thread out, then there is no existing process may be, the system will automatically complete the withdrawal of the process and the release of their address space.
Loaded into the process address space of every executable file or a dynamic link library files will be allocated a map associated with the overall situation of the only examples handle (Hinstance). The case is a record handle the actual process of loading the location of a basic memory address. Examples handle in the process of import procedures function WinMain () in the adoption of a parameter HINSTANCE hinstExe transmission, which is the actual value of the basic process address space address. The VC + + links procedures link the procedures, the default address the basic address space is 0 x00400000, if not there is no need to revise the general value. In the proceedings, you can GetModuleHandle () function modules are designated by the use of the basic address space.
Of the creation process
Through the process of creating CreateProcess () function to achieve, CreateProcess () through the creation of a new process and in the address space within the main thread running to start and run a new procedure. Specific, in the implementation of CreateProcess () function, the first by the operating system kernel is responsible for the creation of a process object, initialized to count 1, and immediately create a new process for the virtual address space. Will be followed, or any other executable file the necessary dynamic link library files to loading code and data in the address space. In the creation of the main thread, the first by the system is also responsible for the creation of a threaded kernel object, and initialized to 1. Finally the main thread and start the process of implementation of the import function WinMain (), to complete the process of the creation and implementation of threads.
CreateProcess () function prototype of the following statement:
BOOL CreateProcess (
LPCTSTR lpApplicationName, / / executable modules of
LPTSTR lpCommandLine, / / command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, / / process safety attributes
LPSECURITY_ATTRIBUTES lpThreadAttributes, / / thread safety attributes
BOOL bInheritHandles, / / handle succession signs
DWORD dwCreationFlags, / / create signs
LPVOID lpEnvironment, / / point to the new environment block pointer
LPCTSTR lpCurrentDirectory, / / refers to the current directory name indicator
LPSTARTUPINFO lpStartupInfo, / / information structure at the start of the indicators
LPPROCESS_INFORMATION lpProcessInformation / / process information at the structure of the Guidelines
);
In the process of, a specific function or module can function in the form of different threads to achieve. For the same process, these functions, there are threads in the address space of the same, and in the implementation of, usually associated with some of the data for processing. If there is some error algorithm, it would be possible with the destruction of their address space, a number of other important elements, which will cause more serious consequences. For the protection of the contents of address space can be considered for those who need the address space access to the data in the operation of another part of a decentralization process address space to run, and only allowed to visit the original process address space in the relevant data. Specific, in the process through CreateProcess () function to create a sub-process, in the process of all of the treatment process only parent process address space in the relevant data for a visit, so that we can protect the parent process address space in the current process of tasks unrelated to the implementation of all the data. In such cases, reflected by the process of the role of the same functions and more similar threads, can be regarded as a father during the process of running a process. For this reason, which requires that the parent process to control the process of the start-up, implementation and exit. Below this code is demonstrated this process:
/ / Variable
CString sCommandLine;
Char cWindowsDirectory [MAX_PATH];
Char cCommandLine [MAX_PATH];
DWORD dwExitCode;
PROCESS_INFORMATION pi;
STARTUPINFO si = (sizeof (si));
/ / Get Windows directory
GetWindowsDirectory (cWindowsDirectory, MAX_PATH);
/ / Start "Notepad" program from the command line
SCommandLine = CString (cWindowsDirectory) + "\ \ NotePad.exe";
:: Strcpy (cCommandLine, sCommandLine);
/ / Start "notepad" as a child process
BOOL ret = CreateProcess (NULL, cCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, & si, & pi);
If (ret) (
/ / Close the main thread of the process handle
CloseHandle (pi.hThread);
/ / Wait for the process of the withdrawal of
WaitForSingleObject (pi.hProcess, INFINITE);
/ / Get out of the process of code
GetExitCodeProcess (pi.hProcess, & dwExitCode);
/ / Handle the process of closing
CloseHandle (pi.hProcess);
)
Despite code first adopted CreateProcess () to create a Windows comes with the "notebook" of procedures for the process, sub-process was initiated after parent process through WaitForSingleObject () function wait for the end of its implementation in the process of not withdraw from the process before the father has been in obstruction state, the process here of the role of the same function in a similar single-threaded. Once the process of the withdrawal of WaitForSingleObject () function by waiting pi.hProcess object will be notified Father process will continue, if necessary, through GetExitCodeProcess () to get the process of the withdrawal of the code.
In contrast, the situation is more father in the process initiated after the end of the process do not engage in any exchange of information and communications, created by the process of the implementation of both the success of the process has nothing to do with the father. Many large software in the design also used for such thinking, some of the features will be entirely through independent applications to complete, when the need to implement a certain operation as long as the main program initiated by the corresponding sub-process can be, the specific handling the work by the child process to complete. Such process of the creation of a more simple, for example, the above code simply remove those pairs handle pi.hProcess waiting process can be:
BOOL ret = CreateProcess (NULL, cCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, & si, & pi);
If (ret) (
/ / Close the main thread of the process handle
CloseHandle (pi.hThread);
/ / Handle the process of closing
CloseHandle (pi.hProcess);
)
DwCreationFlags parameters can be passed in the creation process of the process of setting priorities. In front of sample code in the creation of the use of the process is the default priority, If you want to set a high priority, can be amended as follows:
BOOL ret = CreateProcess (NULL, cCommandLine, NULL, NULL, FALSE, HIGH_PRIORITY_CLASS, NULL, NULL, & si, & pi);
If the process did not create special sets priorities, you can SetPriorityClass () function to dynamically set, the function needs to be operating handle and process identifier as a priority entrance parameters, function prototype is:
BOOL SetPriorityClass (HANDLE hProcess, DWORD dwPriorityClass);
The priority not previously set the example code, in-process was initiated by the father after the process of dynamic change its priority settings:
SetPriorityClass (pi.hProcess, HIGH_PRIORITY_CLASS);
Or by the process of its own in its post-change priority settings, it should be noted that at this time should be set up to handle the process of the process of its own handle, through GetCurrentProcess () function to obtain:
HANDLE hProcess = GetCurrentProcess ();
SetPriorityClass (hProcess, HIGH_PRIORITY_CLASS);
Mutex the process of running
Under normal circumstances, the operation of a process is generally not affect other processes are running. But for certain special requirements, such as exclusive use of a serial port hardware requirements on the procedures in its operations during the process to allow other use of this port equipment operating procedures, and such procedures are usually not allowed to run with a number of examples of a process. This leads to a process of mutually exclusive issues.
Mutex process to achieve the core idea is relatively simple: start the process in the current system, check first whether there are examples of this process, and if not, the process will be successful in creating and setting examples already exist identification markings. This process will be created after the adoption of the marking its known examples already exist to ensure that the process can only exist in the system an example. Specific memory mapping document can be taken, in a famous incident, as well as the overall situation of the famous mutex shared variables, and other methods to achieve. Below each of them on the famous mutually exclusive representative of the overall situation and shared variables of these two methods are introduced:
/ / Create mutually exclusive of
HANDLE m_hMutex = CreateMutex (NULL, FALSE, "Sample07");
/ / Error code inspection
If (GetLastError () == ERROR_ALREADY_EXISTS) (
/ / If there has been mutually exclusive of the release handle and reduction of Mutual Exclusion
CloseHandle (m_hMutex);
M_hMutex = NULL;
/ / Exit procedures
Return FALSE;
)
The code above mutex of the famous demonstration of the process used in mutex. The core code is CreateMutex () on the creation of the famous mutex. CreateMutex () function can be used to create a known or unknown quantity of the mutex object, the function prototype is:
HANDLE CreateMutex (
LPSECURITY_ATTRIBUTES lpMutexAttributes, / / pointer at the security attributes
BOOL bInitialOwner, / / initialization mutex object owner
LPCTSTR lpName / / mutex object at a target
);
If successful implementation function will return a mutex object of the handle. If CreateMutex () implementation of the same name already exists in the mutex, the function will return to this already exist in the mutex handle, and can GetLastError () ERROR_ALREADY_EXIST get the wrong code. Thus, by detecting the error code can be achieved ERROR_ALREADY_EXIST CreateMutex () to process the mutex.
Use global variables sharing methods are mainly in the procedures adopted by the MFC framework to achieve the compiler. # Pragma data_seg through pre-compiler directive to create a new section in this section can be volatile keyword definition of a variable, and it must carry out its initialization. Volatile designated keywords for external process variables can visit. Finally, in order to make the process variables can play a role in the process of mutual exclusion, but also be configured for sharing variables, while allowing a read, write access. This can be done through # pragma comment pre-compiler directive to inform the compiler. Below is the use of global variables process mutex code list:
# Pragma data_seg ( "Shared")
Int volatile g_lAppInstance = 0;
# Pragma data_seg ()
# Pragma comment (linker, "/ section: Shared, RWS")
……
If (+ + g_lAppInstance> 1)
Return FALSE;
Despite the role of the code is in the process of the commencement of the overall share plus a variable g_nAppInstancd, if we find that its value is more than 1, then return FALSE to inform the end of the process. It should be pointed out in particular that is, in order to make the code more than two can really play a role in the process mutually exclusive and must be placed in the entrance of application code, that is, application examples of the initialization function InitInstance () at the beginning.
The end of the process
Only provide for a process address space and kernel objects, and its operation is in its address space through the main thread of reflection. When the main thread to return to the entry point function, the process will come to an end. This process is the way the end of the normal process of the withdrawal of all threads in the process can get the right resources removal. In addition to the normal process launched this way, sometimes in the process also need to enforce the code by the end of this process or other processes running. ExitProcess () function can be in the process of the use of a thread, and immediately terminate the operation of this process. ExitProcess () function prototype is:
VOID ExitProcess (UINT uExitCode);
UExitCode parameters for the process set out code. The function is mandatory in the implementation of the process after that has been concluded, in any subsequent code will not be executed. Although ExitProcess () function can be concluded at the same time notification process associated with the dynamic link library, but because of its implementation of this mandatory, making ExitProcess () function will exist in the use of potential safety problems. For example, if the procedure call ExitProcess () function has been used before new operators for a period of memory, it will be because ExitProcess () function can not be mandatory and delete operators will be released, causing memory leakage. Given ExitProcess () function of the mandatory and insecurity, when in use must attention.
ExitProcess () only enforcing the withdrawal process, if we have a mandatory process of the other end of the process needs to be used TerminateProcess () to achieve. And ExitProcess () different TerminateProcess () function implementation, the process of termination of any other procedure on the withdrawal of the notice. In other words, the termination of the process is not in operation before the end of the withdrawal before ending work. Therefore, usually only when other methods are not any forced to withdraw from the process when it will consider the use of TerminateProcess () to the end of compulsory process. Below is TerminateProcess () function prototype:
BOOL TerminateProcess (HANDLE hProcess, UINT uExitCode);
UExitCode hProcess parameters for the process and handle and exit code. If this is the end of the process, you can GetCurrentProcess () access to the handle. TerminateProcess () is the implementation of asynchronous, and calling upon return was not sure whether the termination process has been really withdraw, if the call TerminateProcess () details the process of this concern, through WaitForSingleObject () to wait for the real end of the process.
Summary
Multi-process management is a multi-task the important contents of the text in some of these basic concepts and its major technologies, such as the process of creation and the end of the process of running the mutex and made a more detailed introduction. Through this paper, readers should be able to process more than a preliminary management awareness. |