Sharing kernel objects across process boundaries

Source: Internet
Author: User

I am reading windows core programming and will summarize it later. --------- Xiaof

Let's talk about why we need to implement this function.
1. Using file ing objects, data blocks can be shared between different processes on the same computer.
2. processes running on different computers on the network send messages to each other using the mail slot and named pipe.
3. mutex. Semaphores. Event, allowing threads in different processes to synchronize.
There are three methods for implementation: using an object handle to inherit (parent-child process), naming an object, and copying an object handle.
Use object handle inheritance
The object handle inheritance method can be used only in the parent-child process relationship.
The procedure is as follows:
1. When a parent process creates a kernel object, it must specify to the system whether the object handle wants the child process to inherit. (Object Inheritance is incorrect.
This is the object handle inheritance.
To create an inherited handle, you need to allocate and initialize the security_attributes structure and pass the structure address to the create * function.
For example, create a mutex object:
Security_attributes SA;
SA. nlength = sizeof (SA );
SA. lpsecuritydescriptor = NULL;
SA. binherithandle = true; // make the returned handle inheritable
Handle hmutex = createmutex (& SA, false, null );
Annotation: security_attributes structure member variable, nlength structure size; lpsecuritydescriptor is the safe access descriptor, if you want
Modify the value of memory access restriction for the created object. If binherithandle is true, it can be inherited.
2. Create a sub-process
Bool CreateProcess (pctstr pszapplicationname, ptstr pszcommandline,..., bool binherithandles ,······);
Note: When binherithandles is regenerated into a process, this value determines whether the sub-process can inherit the "inherited handle" in the parent process handle table ";
If binherithandles is true, the system will do three things: 1. Create a sub-process and create a new process handle table; 2: traverse the parent process handle table.
The inherited handle is copied to the sub-process handle table (the location is also the same, completely consistent); 3, the system will increase the use count of the kernel object.
Note: the inheritance of the object handle only occurs when the child process is generated. If the parent process creates another kernel object, its handle is also set to an inherited handle. Running
The child process does not inherit this kernel object.
Sub-processes do not know the handle of their inherited kernel objects. In the sub-process documentation, the Chinese and English documents pointed out that he wanted to get the kernel formation handle when generating from another process.
3. To get the desired kernel object handle for a sub-process, the most common method is to pass the handle value to the sub-process as a command line parameter; Process Communication Technology (waitforinputidle function ); the parent process reminds me of adding environment variables in the Environment fast;

Sort out your ideas and continue later ------ xiaof

Object Name
Many kernel objects can be named.
1. Create a kernel object in process:
Example:
Handle createmutex (
Pecurity_attributes PSA,
Bool binitialowner,
Pctstr pszname );
The last parameter pszname, which is used to input null, is equivalent to letting the system create an unnamed kernel object. The Inheritance technology can be used to implement shared kernel objects. For example:
If you want to use the object name, you must assign a value to this function.
The paname value is the address of "0 as the terminator name string. This name can be up to max_path bytes.
For example, to create a mutex:
Handle hmutex = createmutex (null, false, text ("xiaof_object ");
Note: Microsoft does not provide any mechanism to ensure the uniqueness of the kernel object name (solution: Unique name)
So if the object named xiaof_object fails
Note: For example, when creating a signal object:
Handle hsem = createsemaphore (null, 1,1, text ("xiaof_object ");
DWORD dwerrorcode = getlasterror ();
Result: The value of dwerrorcode is 6 (error_invalid_handle), and the handle is incorrect.

2. Create process B in another process:
Create an object named xiaof_object in process B.
The system will check whether there are xiaof_object objects. If so, check the object type. If the types are the same, run a security check to verify whether the caller has full access permissions to the object. If yes, the system will find a blank item in process B's handle table and initialize it to point to the existing kernel object. If the object type does not match or the call is rejected, it will fail (return NULL );

After the create * object function is called successfully, no object is created. In fact, process B allocates a new handle, which indicates the existing objects in the kernel.
Kernel Object count + 1;


You can also use the open * function.
The parameter is the same as create. If no kernel object with the same name is found, the function returns NULL, and getlasterror returns 2 (error_file_not_found). If an object with the same name is found but of different types, the function returns NULL, and getlasterror returns 6, (error_invalid_handle); if the same name is the same, the system checks whether the requested access (determined by dwdesiredaccess) is allowed. If all processes pass, the system and the newly called Process Handle table, and the kernel object count + 1;


The difference between create * and open *: If the kernel object does not exist, create * will create the kernel object, while open * will only return failure.

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.