A method, apparatus, device, medium, and product for creating named semaphores.

By defining data structures for semaphores and atomic variables, and directly manipulating mapped memory for initialization, the compatibility issues caused by file link dependencies in existing technologies are resolved, achieving better compatibility and performance in the creation of named semaphores.

CN119690688BActive Publication Date: 2025-10-31CHINA MOBILE M2M +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411678342.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-11-22
Publication Date
2025-10-31
Estimated Expiration
2044-11-22

AI Technical Summary

Technical Problem

Existing methods for creating named semaphores rely on file linking operations, making them incompatible with operating systems that do not support file linking.

Method used

By defining a data structure containing semaphores and atomic variables, the system directly manipulates the mapped memory for initialization and uses atomic variables to achieve concurrency control and synchronization, thus avoiding reliance on file linking operations in the file system.

Benefits of technology

It enables the creation and initialization of named semaphores without relying on file links, resulting in better compatibility and performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119690688B_ABST
    Figure CN119690688B_ABST
Patent Text Reader

Abstract

This invention discloses a method, apparatus, device, medium, and product for creating named semaphores. The method involves creating a named semaphore file; setting the size of the named semaphore file to the size of a preset data structure; the data structure including a semaphore object and atomic variables, the atomic variables being used to synchronize the creation and initialization actions of multiple processes simultaneously; mapping the named semaphore file to the memory space of the processes, allowing different processes to access and use the named semaphore according to the data structure through the mapped memory address; and calling a preset interface to check and initialize the data structure. Using this invention, the creation and initialization of named semaphores can be achieved without relying on file links, resulting in better compatibility and performance.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of communication technology, and in particular to a method, apparatus, device, medium, and product for creating named semaphores. Background Technology

[0002] In multitasking operating systems, named semaphores are a common synchronization mechanism used in multi-process programming. Existing methods for creating named semaphores typically rely on file system operations, such as file creation, writing, and linking. A typical implementation includes the following steps: First, process the input semaphore name to obtain a named semaphore filename. Then, determine if the file already exists; if so, open and map it into the process's memory space. If not, execute a creation and initialization process, initializing the semaphore value in memory, creating a temporary file, and writing the initialized semaphore value to that file. Next, use file linking operations to atomically link the temporary files into the final semaphore file. Finally, map the file into the process's memory and access the mapped memory according to the semaphore data structure. This completes the creation and initialization of the named semaphore. Multiple processes can access the memory mapping of the same semaphore file within their respective processes using the same semaphore name, thus enabling cross-process access to the same named semaphore.

[0003] However, the inventors discovered that the existing technology has at least the following problems: the existing method uses the file link operation to link the temporary file containing the initialized semaphore into a semaphore file, thereby ensuring the correctness of the multi-process and multi-threaded creation process. This method relies on file linking and has compatibility issues. On operating systems that do not support file linking, this solution is not compatible. Summary of the Invention

[0004] The purpose of this invention is to provide a method, apparatus, device, medium, and product for creating named semaphores, which can create and initialize named semaphores without relying on file links, and has better compatibility and performance.

[0005] To achieve the above objectives, embodiments of the present invention provide a method for creating a named semaphore, comprising:

[0006] Obtain the named semaphore file;

[0007] The size of the named semaphore file is set to the size of a preset data structure; the data structure includes semaphore objects and atomic variables, and the atomic variables are used to synchronize the creation and initialization behaviors of multiple processes simultaneously.

[0008] The named semaphore file is mapped to the memory space of the process so that different processes can access and use the named semaphore through the mapped memory address according to the data structure.

[0009] Call the preset interface to check and initialize the data structure.

[0010] As an improvement to the above scheme, the acquisition of the named semaphore file includes:

[0011] Generate a named semaphore file name based on the semaphore name;

[0012] Check if the named semaphore file corresponding to the named semaphore file name exists;

[0013] If the named semaphore file does not exist, create the named semaphore file;

[0014] If the named semaphore file exists, open the named semaphore file.

[0015] As an improvement to the above scheme, the semaphore object includes a counter, a scope attribute, and a waiter count; wherein, the counter is used to represent the number of available resources, the scope attribute is used to represent whether the semaphore is exclusively used by this process, and the waiter count is used to represent the number of threads currently blocked waiting for the semaphore.

[0016] As an improvement to the above scheme, the atomic variable includes the following states: uninitialized, being initialized, and initialization completed.

[0017] As an improvement to the above solution, the step of calling a preset interface to check and initialize the data structure includes:

[0018] Call the atomic comparison modification interface to compare whether the atomic variable is in an uninitialized state;

[0019] When the atomic variable is in an uninitialized state, the atomic variable is modified to be in the initialization state;

[0020] Set the counter value of the semaphore object to the initial value of the semaphore input by the user;

[0021] The atomic write operation is invoked to modify the atomic variable to the initialization complete state and returns a success status code.

[0022] The step of calling a preset interface to check and initialize the data structure also includes:

[0023] When the atomic variable is not in an uninitialized state, or when modifying the atomic variable to an initializing state fails, the atomic read interface is called to determine whether the atomic variable is in an initializing state.

[0024] When the atomic variable is in the initialization state, determine whether the atomic variable has changed to the initialization completed state within a preset loop count threshold;

[0025] If yes, return a success status code; otherwise, return a failure status code.

[0026] As an improvement to the above solution, the method further includes:

[0027] The address of the semaphore object and the file information of the named semaphore file are bound and saved.

[0028] As an improvement to the above solution, the step of binding and saving the address of the semaphore object and the file information of the named semaphore file includes:

[0029] Obtain the inode number of the named semaphore file in the file system;

[0030] The inode number is stored in pairs with the address of the semaphore object.

[0031] This invention also provides a device for creating named semaphores, comprising:

[0032] The file acquisition module is used to acquire named semaphore files;

[0033] The file size setting module is used to set the size of the named semaphore file to the size of a preset data structure; the data structure includes semaphore objects and atomic variables, and the atomic variables are used to synchronize the creation and initialization behaviors of multiple processes simultaneously.

[0034] The file mapping module is used to map the named semaphore file to the memory space of the process, so that different processes can access and use the named semaphore through the mapped memory address according to the data structure.

[0035] The initialization module is used to call a preset interface to check and initialize the data structure.

[0036] This invention also provides a named semaphore creation device, including a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor. When the processor executes the computer program, it implements the named semaphore creation method as described in any of the preceding embodiments.

[0037] This invention also provides a computer-readable storage medium comprising a stored computer program, wherein, when the computer program is executed, it controls the device where the computer-readable storage medium is located to perform the method for creating a named semaphore as described in any of the preceding embodiments.

[0038] This invention also provides a computer program product, which includes a computer program or computer instructions. When the computer program or computer instructions are executed by a processor, they implement the method for creating a named semaphore as described in any of the above embodiments.

[0039] Compared with existing technologies, the method, apparatus, device, medium, and product for creating named semaphores disclosed in this invention create a named semaphore file, setting the size of the named semaphore file to the size of a preset data structure. The data structure includes a semaphore object and atomic variables, where the atomic variables are used to synchronize the creation and initialization actions of multiple processes simultaneously. The named semaphore file is mapped to the memory space of the processes, allowing different processes to access and use the named semaphore through the mapped memory address according to the data structure. A preset interface is called to check and initialize the data structure. This invention defines a data structure containing semaphores and atomic variables, performs initialization by directly manipulating mapped memory, and achieves concurrent control and synchronization of the semaphore initialization process through atomic variables. It achieves the creation and initialization of named semaphores without relying on file linking operations in the file system, resulting in better compatibility and performance. Attached Figure Description

[0040] Figure 1 This is a flowchart illustrating a method for creating a named semaphore according to an embodiment of the present invention;

[0041] Figure 2 This is a flowchart illustrating the creation and initialization of data structures according to an embodiment of the present invention;

[0042] Figure 3 This is a schematic diagram of the structure of a named semaphore creation device provided in an embodiment of the present invention;

[0043] Figure 4 This is a schematic diagram of the structure of a named semaphore creation device provided in an embodiment of the present invention. Detailed Implementation

[0044] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0045] In the description of this application, it should be understood that the terms "center", "upper", "lower", "front", "rear", "left", "right", "vertical", "horizontal", "top", "bottom", "inner", "outer", etc., indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings. They are only for the convenience of describing this application and simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on this application.

[0046] The terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of this application, unless otherwise stated, "a plurality of" means two or more.

[0047] In the description of this application, it should be noted that, unless otherwise expressly specified and limited, the terms "installation," "connection," and "linking" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; and they can refer to the internal connection between two components. Those skilled in the art can understand the specific meaning of the above terms in this application based on the specific circumstances.

[0048] See Figure 1 This is a flowchart illustrating a method for creating a named semaphore according to an embodiment of the present invention. The embodiment of the present invention provides a method for creating a named semaphore, including steps S11 to S14:

[0049] S11. Obtain the named semaphore file;

[0050] S12. Set the size of the named semaphore file to the size of a preset data structure; the data structure includes semaphore objects and atomic variables, and the atomic variables are used to synchronize the creation and initialization behaviors of multiple processes simultaneously.

[0051] S13. Map the named semaphore file to the memory space of the process so that different processes can access and use the named semaphore through the mapped memory address according to the data structure;

[0052] S14. Call the preset interface to check and initialize the data structure.

[0053] In this embodiment of the invention, a data structure is first designed, including a semaphore object `sem` member and an atomic variable `lock` member. The semaphore object is used to support operations that support the semantics of the semaphore itself; its specific definition depends on the semaphore implementation. The atomic variable is used to synchronize creation and initialization behaviors that may occur simultaneously in multiple processes or threads. The specific data structure design is as follows:

[0054] struct sem_file__mgmt

[0055] {

[0056] sem_tsem;

[0057] atomict_tlock;

[0058] };

[0059] Then, combining the aforementioned data structure, a named semaphore is created and initialized. First, a named semaphore file is created, with its size set to `sizeof(struct sem_file__mgmt)`, meaning the file size is equal to the size of the `struct sem_file_mgmt` data structure. A memory mapping interface, such as `mmap`, is called to map the named semaphore file into the process's memory space. The mapped size is equal to the size of the `struct sem_file_mgmt` data structure. The resulting memory address can then be accessed and used within the `struct sem_file_mgmt` data structure, where the address of the `sem` member is the semaphore's address. Finally, the pre-designed interface `check_and_init_sem_mgmt` is called to check and initialize the data structure, ensuring that the semaphore object `sem` has been initialized, thus completing the creation and initialization of the named semaphore. Multiple processes can access the memory mapping of the same semaphore file within their respective processes using the same semaphore name, thereby enabling cross-process access to the same named semaphore.

[0060] Using the technical means of this invention, a data structure containing semaphores and atomic variables is defined. It is initialized by directly manipulating mapped memory, and the concurrent control and synchronization of the semaphore initialization process is realized through atomic variables. It can realize the creation and initialization of named semaphores without relying on file link-related operations of the file system, and has better compatibility and performance.

[0061] As a preferred embodiment, the present invention further implements the above embodiments, and step S11, namely, obtaining the named semaphore file, includes steps S111 to S114:

[0062] S111. Generate a named semaphore file name based on the semaphore name;

[0063] S112. Check if the named semaphore file corresponding to the named semaphore file name exists;

[0064] S113. If the named semaphore file does not exist, create the named semaphore file;

[0065] S114. When the named semaphore file exists, open the named semaphore file.

[0066] In this embodiment of the invention, a named semaphore file name is obtained by processing the semaphore name input by the user. The path of the file name is recommended to be a memory file system path so that the semaphore file can be automatically cleared after the system restarts or is powered off.

[0067] The system uses the `open` function (a standard file operation interface) to check if the named semaphore file corresponding to the generated named semaphore filename exists. If the file exists, it is opened; otherwise, it is created and appropriate permissions are set. Further steps include setting the size of the named semaphore file.

[0068] As a preferred embodiment, the present invention further implements the above embodiments and further defines the designed data structure. The semaphore object sem members include a counter, a scope attribute private, and a waiter count.

[0069] The counter is used to represent the number of available resources, the scope attribute private is used to indicate whether the semaphore is exclusively used by this process, and the waiters count is used to represent the number of threads currently blocked waiting for the semaphore.

[0070] The atomic variable lock member includes the following states: uninitialized, initializing, and initialization completed, represented by 0, 1, and 2 respectively. That is, 0 represents uninitialized, 1 represents initializing, and 2 represents initialization completed.

[0071] In the process of creating and initializing a named semaphore, the size of the named semaphore file is set to sizeof(struct sem_file__mgmt) by calling the regular file operation interface ftruncate. That is, the size of the data structure struct sem_file_mgmt. If the file has no content before, the newly expanded content will be automatically filled with zeros. If it already has content, the content remains unchanged. This ensures that the initial value of lock is always 0, the counter of sem is 0, the waiter count is 0, and the scope attribute private is 0, that is, it is shared across processes.

[0072] As a preferred embodiment, this invention further implements the above embodiments by further defining the steps for checking and initializing the data structure. Therefore, step S14, i.e., calling the preset interface to check and initialize the data structure, includes steps S141 to S148:

[0073] S141. Call the atomic comparison modification interface to compare whether the atomic variable is in an uninitialized state;

[0074] S142. When the atomic variable is in an uninitialized state, modify the atomic variable to an initialization state;

[0075] S143. Set the counter value of the semaphore object to the initial value of the semaphore input by the user;

[0076] S144. Call the atomic write operation to modify the atomic variable to the initialization completed state and return a success status code.

[0077] S145. When the atomic variable is not in an uninitialized state, or when modifying the atomic variable to an initializing state fails, call the atomic read interface to determine whether the atomic variable is in an initializing state.

[0078] S146. When the atomic variable is in the initialization state, determine whether the atomic variable has changed to the initialization completed state within a preset loop count threshold.

[0079] S147. If yes, return a success status code;

[0080] S148. If not, return a failure status code.

[0081] See Figure 2 This is a flowchart illustrating the creation and initialization of data structures according to an embodiment of the present invention. The process of using the designed check_and_init_sem_mgmt interface to check and initialize the data structures in this embodiment can be summarized as follows:

[0082] The atomic comparison modification interface `atomic_cmpxchg` is called to compare the `lock` value with 0. If it is 0, it is modified to 1. If the `lock` value is not 0, the modification fails. If `atomic_cmpxchg` successfully modifies the `lock` value, it indicates that the semaphore has not yet been initialized and is currently exclusively used by the program. Therefore, the `counter` value of the `sem` member is set to the user-provided initial semaphore value `value`. Then, the atomic write operation `atomic_write` is called to modify the `lock` value to 2, indicating that the semaphore initialization is complete, and a success is returned. If `atomic_cmpxchg` fails to modify the `lock` value, the following steps are executed.

[0083] Call the atomic read interface `atomic_read` to read the `lock` value and check if it is 1. If it is, execute a loop a finite number of times, for example, 1000 times, repeatedly calling `atomic_read` to read the `lock` value and checking if it is equal to 2. If it is, end the loop and return success. If the `lock` value is still not 2 after the loop ends, return failure. Otherwise, execute the following steps.

[0084] Call the atomic_read interface to read the lock value, and check if it is 2. If it is, it means that the semaphore has been initialized beforehand, and the system returns success. If the lock value is not 2, the system returns failure.

[0085] At this point, the program can guarantee that the semaphore object sem has been initialized.

[0086] Using the technical means of this invention, a data structure containing semaphores and atomic variables is defined. The atomic variables have three states: uninitialized, initializing, and initialization complete. Initialization is performed by directly manipulating mapped memory, and concurrent control and synchronization of the semaphore initialization process are achieved through atomic variables. This allows for the creation and initialization of named semaphores without relying on file link operations in the file system, resulting in better compatibility and performance.

[0087] As a preferred embodiment, the present invention is further implemented based on the above embodiments, and the method further includes step S15:

[0088] S15. Bind and save the address of the semaphore object and the file information of the named semaphore file.

[0089] In this embodiment of the invention, after the semaphore object sem has been initialized, in order to ensure that the same process returns the same semaphore address when opening the same named semaphore multiple times, it is necessary to save the sem address and the named semaphore file information in pairs.

[0090] Preferably, the step of binding and saving the address of the semaphore object and the file information of the named semaphore file includes steps S151 to S152:

[0091] S151. Obtain the inode number of the named semaphore file in the file system;

[0092] S152. Store the inode number and the address of the semaphore object in pairs.

[0093] In this embodiment of the invention, the inode number st_ino of the named semaphore file in the file system is obtained, and the inode number st_ino and the sem address are stored in pairs. Each time the named semaphore is opened, the obtained st_ino is compared with the st_ino already recorded in the process. If st_ino already exists, the newly created sem address can be released, and the sem address corresponding to the st_ino in the record can be returned directly.

[0094] By employing the technical means of this invention, by storing the address of the semaphore object and the file information of the named semaphore file in pairs, it is possible to ensure that the same process returns the same semaphore address when opening the same named semaphore multiple times.

[0095] See Figure 3 This is a schematic diagram of a named semaphore creation device provided in an embodiment of the present invention. The present invention also provides a named semaphore creation device 20, comprising:

[0096] File acquisition module 21 is used to acquire named semaphore files;

[0097] File size setting module 22 is used to set the size of the named semaphore file to the size of a preset data structure; the data structure includes semaphore objects and atomic variables, and the atomic variables are used to synchronize the creation and initialization behaviors of multiple processes simultaneously.

[0098] File mapping module 23 is used to map the named semaphore file to the memory space of a process, so that different processes can access and use the named semaphore through the mapped memory address according to the data structure.

[0099] Initialization module 24 is used to call a preset interface to check and initialize the data structure.

[0100] In a preferred embodiment, the file acquisition module 21 is specifically used for:

[0101] Generate a named semaphore file name based on the semaphore name;

[0102] Check if the named semaphore file corresponding to the named semaphore file name exists;

[0103] If the named semaphore file does not exist, create the named semaphore file;

[0104] If the named semaphore file exists, open the named semaphore file.

[0105] In a preferred embodiment, the semaphore object includes a counter, a scope attribute, and a waiter count; wherein, the counter is used to represent the number of available resources, the scope attribute is used to represent whether the semaphore is exclusively used by this process, and the waiter count is used to represent the number of threads currently blocked waiting for the semaphore.

[0106] The atomic variables include the following states: uninitialized, initializing, and initialization completed.

[0107] In a preferred embodiment, the initialization module 24 is specifically used for:

[0108] Call the atomic comparison modification interface to compare whether the atomic variable is in an uninitialized state;

[0109] When the atomic variable is in an uninitialized state, the atomic variable is modified to be in the initialization state;

[0110] Set the counter value of the semaphore object to the initial value of the semaphore input by the user;

[0111] The atomic write operation is invoked to modify the atomic variable to the initialization complete state and returns a success status code.

[0112] When the atomic variable is not in an uninitialized state, or when modifying the atomic variable to an initializing state fails, the atomic read interface is called to determine whether the atomic variable is in an initializing state.

[0113] When the atomic variable is in the initialization state, determine whether the atomic variable has changed to the initialization completed state within a preset loop count threshold;

[0114] If yes, return a success status code; otherwise, return a failure status code.

[0115] In a preferred embodiment, the device 20 further includes:

[0116] The file saving module is used to bind and save the address of the semaphore object and the file information of the named semaphore file.

[0117] Preferably, the file saving module is specifically used for:

[0118] Obtain the inode number of the named semaphore file in the file system;

[0119] The inode number is stored in pairs with the address of the semaphore object.

[0120] It should be noted that the named semaphore creation apparatus provided in this embodiment of the invention is used to execute all the process steps of the named semaphore creation method of the above embodiment. The working principles and beneficial effects of the two are one-to-one, so they will not be described again.

[0121] Using the technical means of this invention, a data structure containing semaphores and atomic variables is defined. The atomic variables have three states: uninitialized, initializing, and initialization complete. Initialization is performed by directly manipulating mapped memory, and concurrent control and synchronization of the semaphore initialization process are achieved through atomic variables. This allows for the creation and initialization of named semaphores without relying on file link operations in the file system, resulting in better compatibility and performance.

[0122] See Figure 4 This is a schematic diagram of a named semaphore creation device provided in an embodiment of the present invention. The present invention also provides a named semaphore creation device 30, including a processor 31, a memory 32, and a computer program stored in the memory and configured to be executed by the processor. When the processor executes the computer program, it implements the named semaphore creation method as described in any of the above embodiments.

[0123] This invention also provides a computer-readable storage medium comprising a stored computer program, wherein, when the computer program is executed, it controls the device where the computer-readable storage medium is located to perform a method for creating a named semaphore as described in any of the above embodiments.

[0124] This invention also provides a computer program product, which includes a computer program or computer instructions. When the computer program or computer instructions are executed by a processor, they implement the method for creating a named semaphore as described in any of the above embodiments.

[0125] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The program can be stored in a computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. The storage medium can be a magnetic disk, optical disk, read-only memory (ROM), or random access memory (RAM), etc.

[0126] The above description represents the preferred embodiments of the present invention. It should be noted that those skilled in the art can make various improvements and modifications without departing from the principles of the present invention, and these improvements and modifications are also considered to be within the scope of protection of the present invention.

Claims

1. A method for creating a named semaphore, characterized in that, include: Obtain the named semaphore file; The size of the named semaphore file is set to the size of a preset data structure; The data structure includes semaphore objects and atomic variables, and the atomic variables are used to synchronize the creation and initialization behaviors of multiple processes simultaneously. The named semaphore file is mapped to the memory space of the process so that different processes can access and use the named semaphore through the mapped memory address according to the data structure. Call the preset interface to check and initialize the data structure; The atomic variables include the following states: uninitialized, initializing, and initialization completed; The step of calling a preset interface to check and initialize the data structure includes: Call the atomic comparison modification interface to compare whether the atomic variable is in an uninitialized state; When the atomic variable is in an uninitialized state, the atomic variable is modified to be in the initialization state; Set the counter value of the semaphore object to the initial value of the semaphore input by the user; The atomic write operation is invoked to modify the atomic variable to the initialization complete state and a success status code is returned; When the atomic variable is not in an uninitialized state, or when modifying the atomic variable to an initializing state fails, the atomic read interface is called to determine whether the atomic variable is in an initializing state. When the atomic variable is in the initialization state, determine whether the atomic variable has changed to the initialization completed state within a preset loop count threshold; If yes, return a success status code; otherwise, return a failure status code.

2. The method for creating a named semaphore as described in claim 1, characterized in that, The acquisition of the named semaphore file includes: Generate a named semaphore file name based on the semaphore name; Check if the named semaphore file corresponding to the named semaphore file name exists; If the named semaphore file does not exist, create the named semaphore file; If the named semaphore file exists, open the named semaphore file.

3. The method for creating a named semaphore as described in claim 1 or 2, characterized in that, The method further includes: The address of the semaphore object and the file information of the named semaphore file are bound and saved.

4. A device for creating a named semaphore, characterized in that, include: The file acquisition module is used to acquire named semaphore files; The file size setting module is used to set the size of the named semaphore file to the size of a preset data structure; the data structure includes semaphore objects and atomic variables, and the atomic variables are used to synchronize the creation and initialization behaviors of multiple processes simultaneously. The file mapping module is used to map the named semaphore file to the memory space of the process, so that different processes can access and use the named semaphore through the mapped memory address according to the data structure. An initialization module is used to call a preset interface to check and initialize the data structure; The atomic variables include the following states: uninitialized, initializing, and initialization completed; The initialization module is specifically used for: Call the atomic comparison modification interface to compare whether the atomic variable is in an uninitialized state; When the atomic variable is in an uninitialized state, the atomic variable is modified to be in the initialization state; Set the counter value of the semaphore object to the initial value of the semaphore input by the user; The atomic write operation is invoked to modify the atomic variable to the initialization complete state and a success status code is returned; When the atomic variable is not in an uninitialized state, or when modifying the atomic variable to an initializing state fails, the atomic read interface is called to determine whether the atomic variable is in an initializing state. When the atomic variable is in the initialization state, determine whether the atomic variable has changed to the initialization completed state within a preset loop count threshold; If yes, return a success status code; otherwise, return a failure status code.

5. A device for creating named semaphores, characterized in that, It includes a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein the processor, when executing the computer program, implements the method for creating a named semaphore as described in any one of claims 1 to 3.

6. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored computer program, wherein, when the computer program is executed, it controls the device on which the computer-readable storage medium is located to perform the method for creating a named semaphore as described in any one of claims 1 to 3.

7. A computer program product, characterized in that, The computer program product includes a computer program or computer instructions, which, when executed by a processor, implement the method for creating a named semaphore as described in any one of claims 1 to 3.

Citation Information

Patent Citations

  • Resource allocation method and resource allocation device for invoking function

    CN103927191A

  • Inter-process thread mutex method

    CN105511969A