A multi-task mutual exclusion method and system based on file lock
The use of file locks and global variables addresses the complexity of existing methods by ensuring mutual exclusion across threads and processes, providing a reliable and easy-to-use solution for critical resource protection in multi-tasking environments.
Patent Information
- Application Number
- CN202210396058.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-15
- Publication Date
- 2025-03-18
- Estimated Expiration
- 2042-04-15
AI Technical Summary
Existing methods for protecting critical resources in multi-tasking environments, such as Pthread mutex locks and shared memory, are complex and limited in applicability across different operating systems like Linux and Android, failing to ensure proper locking and unlocking of resources, especially in cases of process exceptions or reboots.
A method and system utilizing file locks and global variables to manage locking and unlocking of device node files, ensuring mutual exclusion across multiple threads and processes, with a timeout mechanism to handle exceptional conditions and maintain lock consistency.
The solution provides a simple, versatile, and effective mechanism for protecting critical resources across multiple threads and processes, ensuring automatic lock release in case of process exceptions and preventing program restart issues, thus enhancing operational reliability and ease of use.
Smart Images

Figure CN114706691B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computers, and in particular to a multi-task mutual exclusion method and system based on file locks. Background Art
[0002] As network security is gaining more and more attention, security chips and systems are increasingly used in the Internet of Vehicles and the Internet of Things, and security chip middleware (SDK) is increasingly used in Linux systems. When multiple tasks are performed, the protection of critical area codes in the security chip middleware (such as the code that drives the peripheral interface of the security chip) becomes particularly important.
[0003] There are many existing methods for protecting critical resources, such as semaphores, Pthread mutexes, shared memory, etc. Although there are many methods, they are cumbersome to use and have limited application scope. For example, in the Linux system, Pthread mutexes are often used in multi-threads, but cannot be used in multi-processes. Shared memory must be involved to implement multi-processes, which is complicated to operate. But even with the participation of shared memory, since shared memory cannot be used in the Android system, the Pthread mutex based on shared memory cannot be used to protect the critical area in the Android system. The unnamed semaphore based on shared memory is similar to the Pthread mutex based on shared memory. The named semaphore can realize multi-thread and multi-process mutual exclusion in the Linux system, but when the process exits abnormally, the lock cannot be released automatically. And when the program is restarted, the lock cannot be successfully added, which brings a lot of uncertainty to the program operation.
[0004] Therefore, it is necessary to provide a multi-task mutual exclusion method and system based on file locks to make up for the shortcomings of existing critical section protection. Summary of the invention
[0005] The purpose of the present invention is to provide a multi-task mutual exclusion method and system based on file locks in view of the deficiencies in the prior art, which can realize multi-thread and multi-process mutual exclusion, and the implementation method is simple and has good versatility.
[0006] In order to achieve the above object, the first aspect of the present invention proposes a multi-task mutual exclusion method based on file lock, comprising the following steps:
[0007] When multiple tasks access the critical section of the same device node file operation, open the device node file and obtain the file descriptor fd; based on the file lock, lock the device node file corresponding to the file descriptor fd. After the locking operation is successful, determine whether the value of the global variable is the initial value. If not, delay the judgment until the value of the global variable is determined to be the initial value, and then use the file descriptor fd to update the value of the global variable; after the locking operation fails, enter the blocking waiting state, delay and repeat the locking operation until the locking is successful;
[0008] After completing the task, obtain the file descriptor fd by checking the value of the global variable, unlock the device node file corresponding to the file descriptor fd based on the file lock, and after the unlocking is successful, close the device node file corresponding to the file descriptor fd, and update the value of the global variable to the initial value.
[0009] Based on the above, each thread task has a unique global variable. When multiple thread tasks operating on the same device node file access the critical section, the corresponding operations are:
[0010] Process Task 1 opens the device node file before Process Task 2, obtains the file descriptor fd, and locks the device node file corresponding to the file descriptor fd. If the lock is successful and the value of the global variable is determined to be the initial value, the value of its own global variable is updated with the file descriptor fd. After Process Task 1, Process Task 2 opens the device node file, obtains the file descriptor fd, and locks the device node file corresponding to the file descriptor fd. If the lock fails, it enters the blocked waiting state and delays the attempt to lock the operation.
[0011] After processing the transaction, thread task 1 performs the unlocking operation, and after the unlocking is successful, closes the device node file and updates the value of the global variable to the initial value; process task 2 locks successfully and determines that the value of the global variable is the initial value, and updates the value of the global variable with the second file descriptor fd;
[0012] After thread task 2 completes the transaction, it performs the unlocking operation, and after the unlocking is successful, it closes the device node file and updates the value of the global variable to the initial value.
[0013] Based on the above, after thread task 2 is locked successfully, if it is determined that the value of the global variable is not the initial value, the value of the global variable is judged delayed until the value of the global variable is determined to be the initial value, and then the value of the global variable is updated using the second file descriptor fd.
[0014] The present invention also provides a multi-task mutual exclusion system based on file lock, comprising:
[0015] The file opening unit is used to open the device node file and obtain the file descriptor fd when the task accesses the critical section;
[0016] The file lock unit is used to lock the device node file corresponding to the file descriptor fd based on the file lock, and after the lock fails, enter the blocking waiting state and delay the lock attempt; or after the lock succeeds, determine whether the value of the global variable is the initial value, and when the value of the global variable is the initial value, call the global variable modification unit to update the value of the global variable with the file descriptor fd, and when the value of the global variable is not the initial value, delay the determination;
[0017] The file lock unlocking unit is used to obtain the file descriptor fd by checking the value of the global variable after the task is completed, unlock the device node file corresponding to the file descriptor fd based on the file lock, and after the unlocking is successful, close the device node file corresponding to the file descriptor fd, and call the global variable modification unit to update the value of the global variable to the initial value.
[0018] A third aspect of the present invention provides an electronic device, comprising:
[0019] a memory having a computer program stored thereon;
[0020] A processor is used to execute the computer program in the memory to implement the steps of the aforementioned method.
[0021] A fourth aspect of the present invention provides a computer-readable storage medium having a computer program stored thereon, wherein the program implements the steps of the aforementioned method when executed by a processor.
[0022] The present invention proposes a multi-task mutual exclusion method and system based on file lock, which uses file lock and global variable to protect critical resources, can realize multi-process mutual exclusion, and can realize multi-process mutual exclusion, is not only applicable in multi-threading and multi-process, but also file descriptor fd is applicable in mainstream operating systems such as Linux and Android, and has good versatility. In addition, it is simple to operate, and is provided with a timeout judgment process, which can automatically release the lock when the timeout is caused by abnormal exit of the process or closing of the protected document, and there is no problem of being unable to lock when the program is restarted, so it is more convenient to use.
[0023] Additional aspects and advantages of the present invention will become apparent from the following description or may be learned by practice of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] The above and / or additional aspects and advantages of the present invention will become apparent and easily understood from the description of the embodiments in conjunction with the following drawings, in which:
[0025] Figure 1 A multi-task mutual exclusion flow chart based on file lock of the present invention is shown;
[0026] Figure 2The schematic diagram shows the application of the multi-task mutual exclusion process of the present invention in the process task.
[0027] Figure 3 A schematic diagram showing the application of the multi-task mutual exclusion process of the present invention in thread tasks is shown. DETAILED DESCRIPTION
[0028] Security chip: It is a chip that can independently generate keys, encrypt and decrypt. It has an independent processor and storage unit inside, which can store keys and feature data, provide encryption and security authentication services for computers, and has peripherals such as SPI, IIC, USB, SD, 7816, etc.
[0029] Security chip middleware SDK: A component that provides common security services between platform software and application software. It provides a set of API interfaces for application software to call, and uses platform software (such as Linux peripheral drivers) to communicate data with the security chip.
[0030] Critical section: A program segment that accesses shared resources (such as shared devices or shared memory).
[0031] The prototype of file lock flock is int flock (int fd, int operation). The parameter fd is the file descriptor of the file to be locked (unlocked). The parameter operation has four states:
[0032] LOCK_SH, shared lock, multiple processes can use the same lock, often used as a read shared lock;
[0033] LOCK_EX, exclusive lock, only one process is allowed to use it at the same time, often used as a write lock;
[0034] LOCK_UN, release the lock;
[0035] LOCK_NB, no blocking.
[0036] In order to more clearly understand the above-mentioned purpose, features and advantages of the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings and specific embodiments. It should be noted that the embodiments of the present application and the features in the embodiments can be combined with each other without conflict.
[0037] In the following description, many specific details are set forth to facilitate a full understanding of the present invention. However, the present invention may also be implemented in other ways different from those described herein. Therefore, the protection scope of the present invention is not limited to the specific embodiments disclosed below.
[0038] Example 1
[0039] This embodiment proposes a multi-task mutual exclusion method based on file locks, such as Figure 1 As shown, the method includes:
[0040] When multiple tasks access the critical section of the same device node file operation, such as Figure 1 As shown, the device node file is opened to obtain the file descriptor fd; the device node file corresponding to the file descriptor fd is locked based on the file lock flock; if the locking operation fails, it enters the blocking waiting state and repeats the locking operation with a delay until the locking is successful;
[0041] In the present application, the locking operation is flock(fd,LOCK_EX|LOCK_NB). Since the attribute value operation of the locking operation passes LOCK_EX|LOCK_NB, it is exclusive and non-blocking. When a process task / thread task attempts a locking operation, if the device node file is not locked by other process tasks / thread tasks, the locking is successful and 0 is returned. If the device node file has been locked by another process task / thread task, the locking fails and an error -1 is returned. At this time, a delay is made according to the preset interval to determine whether the preset locking timeout time is exceeded. If it is not exceeded, the locking operation is continued to be attempted until the locking is successful. If it is exceeded, the operation is directly timed out and a timeout error code is returned. The error code is saved in errno and is EWOULDBLOCK.
[0042] After the locking operation is successful, it is determined whether the value of the global variable mutexfd is the initial value. If not, the judgment is delayed until the value of the global variable mutexfd is determined to be the initial value. The value of the global variable mutexfd is updated with the file descriptor fd. At this time, the global variable mutexfd saves the file descriptor fd of the locked device node file, which is used for unlocking;
[0043] It can be understood that the current step is mainly to prevent the global variable mutexfd from being tampered with in advance when multi-threaded tasks are processed; specifically, if this step is skipped, the global variable mutexfd is not updated to the initial value (such as -1) by other thread tasks, that is, the global variable mutexfd is not released, then the current thread task updates the value of the global variable mutexfd with the current file descriptor fd after the locking operation is successful; at this time, if the global variable mutexfd released by other thread tasks is the newly saved fd, this will cause the new fd to be lost, and it will also cause no fd to be available when the current task is unlocked; therefore, it is necessary to ensure that the global variable mutexfd is in the released state, and then update the value of the global variable with the file descriptor fd. That is, the global variable is the file mutex lock variable, and the global variable being released means that there is no thread currently performing a file locking operation, and the global variable not being released means that there is a thread currently performing a file locking operation.
[0044] After completing the task, if Figure 1 As shown, the file descriptor fd is obtained by checking the value of the global variable, and the device node file corresponding to the file descriptor fd is unlocked based on the file lock (fd, LOCK_UN). After the unlocking is successful, the device node file corresponding to the file descriptor fd is closed, and the value of the global variable is updated to the initial value.
[0045] It can be understood that file locks are generally widely used in multi-process tasks, but in order to achieve mutual exclusion when locking multi-threaded tasks, it is necessary to use a different file descriptor fd each time the lock is added; therefore, each thread task in this application must open the file once and obtain a new fd before performing the locking operation, so as to ensure that the fd locked each time is different, thereby achieving thread mutual exclusion.
[0046] Obviously, the method of using file lock to protect critical section proposed by the present invention is effective in both multi-process tasks and multi-threaded tasks. In multi-process tasks or multi-threaded tasks, when there is a critical section to be protected, such as the SPI device file of the Linux system, etc., a locking operation can be performed before accessing the critical section, and an unlocking operation can be performed after executing the critical section code, so that the purpose of protecting the critical section can be achieved.
[0047] And because the file lock is versatile, it is applicable to mainstream operating systems such as Linux and Android. After the process exits abnormally or the protected document is closed, the lock is automatically released, and there is no problem of being unable to lock when the program is restarted, so the operation is simple and convenient.
[0048] Furthermore, each process task has a unique global variable. When using it, when multiple process tasks operating on the same device node file access the critical section, such as Figure 2As shown, the corresponding operations are:
[0049] Process Task 1 opens the device node file before Process Task 2, obtains the file descriptor fd, and locks the device node file corresponding to the file descriptor fd. If the lock is successful and the value of the global variable is determined to be the initial value, the value of its own global variable is updated with the file descriptor fd. Process Task 2 opens the device node file after Process Task 1, obtains the file descriptor fd, and locks the device node file corresponding to the file descriptor fd. If the lock fails, it enters the blocked waiting state and delays the attempt to lock.
[0050] After processing the transaction, process task 1 performs the unlocking operation, and after the unlocking is successful, it closes the device node file corresponding to the file descriptor fd and updates the value of the global variable to the initial value; process task 2 locks successfully and determines that the value of the global variable is the initial value, and uses the file descriptor fd to update the value of its own global variable;
[0051] After processing the transaction, process task 2 performs the unlocking operation, and after the unlocking is successful, closes the device node file corresponding to the file descriptor fd, and updates the value of the global variable to the initial value.
[0052] Furthermore, each thread task has a unique global variable, so when using it, when multiple thread tasks operating on the same device node file access the critical section, such as Figure 3 As shown, the corresponding operations are:
[0053] Thread task 1 opens the device node file before thread task 2, obtains the first file descriptor fd, and performs a lock operation on the device node file based on the first file descriptor fd. The lock is successful and the value of the global variable is determined to be the initial value, and the value of the global variable is updated with the first file descriptor fd; thread task 2 opens the device node file after thread task 1, obtains the second file descriptor fd, and performs a lock operation on the device node file based on the second file descriptor fd. The lock fails, and the device node file enters a blocked waiting state, and delays the attempt to lock the operation;
[0054] After processing the transaction, thread task 1 performs the unlocking operation, and after the unlocking is successful, closes the device node file and updates the value of the global variable to the initial value; process task 2 locks successfully and determines that the value of the global variable is the initial value, and updates the value of the global variable with the second file descriptor fd;
[0055] After thread task 2 completes the transaction, it performs the unlocking operation, and after the unlocking is successful, it closes the device node file and updates the value of the global variable to the initial value.
[0056] It should be noted that after thread task 2 successfully locks, if the value of the global variable is determined not to be the initial value, the determination of the value of the global variable is delayed until the value of the global variable is determined to be the initial value, and then the value of the global variable is updated with the second file descriptor fd, thereby preventing the global variable mutexfd from being tampered with in advance by thread task 1 when thread task 2 is processing.
[0057] Example 2
[0058] This embodiment provides a multi-task mutual exclusion system based on file locks, including:
[0059] The file opening unit is used to open the device node file and obtain the file descriptor fd when the task accesses the critical section;
[0060] The file lock unit is used to lock the device node file corresponding to the file descriptor fd based on the file lock, and after the lock fails, enter the blocking waiting state and delay the lock attempt; or after the lock succeeds, determine whether the value of the global variable is the initial value, and when the value of the global variable is the initial value, call the global variable modification unit to update the value of the global variable with the file descriptor fd, and when the value of the global variable is not the initial value, delay the determination;
[0061] The file lock unlocking unit is used to obtain the file descriptor fd by checking the value of the global variable after the task is completed, unlock the device node file corresponding to the file descriptor fd based on the file lock, and after the unlocking is successful, close the device node file corresponding to the file descriptor fd, and call the global variable modification unit to update the value of the global variable to the initial value.
[0062] Each process task has a unique global variable. In the specific application process, when multiple process tasks operate on the same device node file, the corresponding operations are:
[0063] Process task 1 calls the file opening unit to open the device node file before process task 2, obtains the file descriptor fd, calls the file lock unit to lock the device node file corresponding to the file descriptor fd, and after the lock is successful and the value of its own global variable is determined to be the initial value, calls the global variable modification unit to update the value of its own global variable with the file descriptor fd;
[0064] After process task 1, process task 2 calls the file opening unit to open the device node file, obtains the file descriptor fd, calls the file lock unit to lock the device node file corresponding to the file descriptor fd, and if the lock fails, enters a blocking waiting state and delays the attempt to lock the file.
[0065] After processing the transaction, process task 1 calls the file lock unlocking unit to perform the unlocking operation, closes the device node file corresponding to the file descriptor fd after the unlocking is successful, and calls the global variable modification unit to update the value of its own global variable to the initial value;
[0066] Process task 2 calls the file lock unit to lock the device node file corresponding to the file descriptor fd successfully, and when it is determined that the value of its own global variable is the initial value, calls the global variable modification unit to update the value of its own global variable with the file descriptor fd;
[0067] After processing the transaction, process task 2 calls the file lock unlocking unit to perform the unlocking operation, closes the device node file corresponding to the file descriptor fd after the unlocking is successful, and calls the global variable modification unit to update the value of its own global variable to the initial value.
[0068] Each thread task has a unique global variable. In the specific application process, when multi-threaded tasks operating on the same device node file access the critical section, the corresponding operations are:
[0069] Thread task 1 calls the file opening unit to open the device node file to obtain the first file descriptor fd before thread task 2, and calls the file lock unit to lock the device node file based on the first file descriptor fd. When the lock is successful and the value of the global variable is determined to be the initial value, the global variable modification unit is called to update the value of the global variable with the first file descriptor fd;
[0070] After thread task 1, thread task 2 calls the file opening unit to open the device node file, obtains the second file descriptor fd, and calls the file lock unit to lock the device node file based on the second file descriptor fd. If the lock fails, it enters a blocked waiting state and delays the attempt to lock the file.
[0071] After thread task 1 completes the transaction, it calls the file lock unlocking unit to perform the unlocking operation, and after the unlocking is successful, it closes the device node file corresponding to the first file descriptor fd, and calls the global variable modification unit to update the value of the global variable to the initial value;
[0072] Process task 2 calls the file lock unit to lock the device node file successfully, and when it is determined that the value of the global variable is the initial value, calls the global variable modification unit to update the value of the global variable with the second file descriptor fd;
[0073] After thread task 2 completes the transaction, it calls the file lock unlocking unit to perform the unlocking operation, and after the unlocking is successful, it closes the device node file and calls the global variable modification unit to update the value of the global variable to the initial value.
[0074] Furthermore, after thread task 2 is locked successfully, if it is determined that the value of the global variable is not the initial value, the determination of the value of the global variable is delayed until the value of the global variable is determined to be the initial value, and then the global variable modification unit is called to update the value of the global variable to the initial value.
[0075] Example 3
[0076] This embodiment provides an electronic device, including:
[0077] a memory having a computer program stored thereon;
[0078] A processor is used to execute the computer program in the memory to implement the steps of the method described in Example 1.
[0079] Example 4
[0080] This embodiment provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the steps of the method described in Embodiment 1 are implemented.
[0081] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed by the present invention, which should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A multi-task mutual exclusion method based on file lock, characterized in that: The following steps are involved: When multiple tasks access the critical section of the same device node file operation, open the device node file and obtain the file descriptor fd; based on the file lock, lock the device node file corresponding to the file descriptor fd. After the locking operation is successful, determine whether the value of the global variable is the initial value. If not, delay the judgment until the value of the global variable is determined to be the initial value, and then use the file descriptor fd to update the value of the global variable; after the locking operation fails, enter the blocking waiting state, delay and repeat the locking operation until the locking is successful; After completing the task, obtain the file descriptor fd by checking the value of the global variable, unlock the device node file corresponding to the file descriptor fd based on the file lock, and after the unlocking is successful, close the device node file corresponding to the file descriptor fd, and update the value of the global variable to the initial value.
2. A multi-task mutual exclusion method based on file lock according to claim 1, characterized in that: Each process task has a unique global variable. When multiple process tasks operating on the same device node file access the critical section, the corresponding operations are: Process Task 1 opens the device node file before Process Task 2, obtains the file descriptor fd, and locks the device node file corresponding to the file descriptor fd. If the lock is successful and the value of the global variable is determined to be the initial value, the value of its own global variable is updated with the file descriptor fd. Process Task 2 opens the device node file after Process Task 1, obtains the file descriptor fd, and locks the device node file corresponding to the file descriptor fd. If the lock fails, it enters the blocked waiting state and delays the attempt to lock. After processing the transaction, process task 1 performs the unlocking operation, and after the unlocking is successful, it closes the device node file corresponding to the file descriptor fd and updates the value of the global variable to the initial value; process task 2 locks successfully and determines that the value of the global variable is the initial value, and uses the file descriptor fd to update the value of its own global variable; After processing the transaction, process task 2 performs the unlocking operation, and after the unlocking is successful, closes the device node file corresponding to the file descriptor fd, and updates the value of the global variable to the initial value.
3. A multi-task mutual exclusion method based on file lock according to claim 1 or 2, characterized in that: Each thread task has a unique global variable. When multiple thread tasks operating on the same device node file access the critical section, the corresponding operations are: Thread task 1 opens the device node file before thread task 2, obtains the first file descriptor fd, and performs a lock operation on the device node file based on the first file descriptor fd. If the lock is successful and the value of the global variable is determined to be the initial value, the value of the global variable is updated with the first file descriptor fd. Thread task 2 opens the device node file after thread task 1, obtains the second file descriptor fd, and performs a lock operation on the device node file based on the second file descriptor fd. If the lock fails, it enters a blocked waiting state and delays the attempt to lock the file. After processing the transaction, thread task 1 performs the unlocking operation, and after the unlocking is successful, closes the device node file and updates the value of the global variable to the initial value; process task 2 locks successfully and determines that the value of the global variable is the initial value, and updates the value of the global variable with the second file descriptor fd; After thread task 2 completes the transaction, it performs the unlocking operation, and after the unlocking is successful, it closes the device node file and updates the value of the global variable to the initial value.
4. A multi-task mutual exclusion method based on file lock according to claim 3, characterized in that: After thread task 2 is locked successfully, if it is determined that the value of the global variable is not the initial value, the value of the global variable is judged delayed until the value of the global variable is determined to be the initial value, and then the value of the global variable is updated using the second file descriptor fd.
5. A multi-task mutual exclusion system based on file lock, characterized by: include The file opening unit is used to open the device node file and obtain the file descriptor fd when the task accesses the critical section; The file lock unit is used to lock the device node file corresponding to the file descriptor fd based on the file lock, and after the lock fails, enter the blocking waiting state and delay the lock attempt; or after the lock succeeds, determine whether the value of the global variable is the initial value, and when the value of the global variable is the initial value, call the global variable modification unit to update the value of the global variable with the file descriptor fd, and when the value of the global variable is not the initial value, delay the determination; The file lock unlocking unit is used to obtain the file descriptor fd by checking the value of the global variable after the task is completed, unlock the device node file corresponding to the file descriptor fd based on the file lock, and after the unlocking is successful, close the device node file corresponding to the file descriptor fd, and call the global variable modification unit to update the value of the global variable to the initial value.
6. A multi-task mutual exclusion system according to claim 5, characterized in that: Each process task has a unique global variable. When multiple process tasks operate on the same device node file, the corresponding operations are: Process task 1 calls the file opening unit to open the device node file before process task 2, obtains the file descriptor fd, calls the file lock unit to lock the device node file corresponding to the file descriptor fd, and after the lock is successful and the value of its own global variable is determined to be the initial value, calls the global variable modification unit to update the value of its own global variable with the file descriptor fd; After process task 1, process task 2 calls the file opening unit to open the device node file, obtains the file descriptor fd, calls the file lock unit to lock the device node file corresponding to the file descriptor fd, and if the lock fails, enters a blocking waiting state and delays the attempt to lock the file. After processing the transaction, process task 1 calls the file lock unlocking unit to perform the unlocking operation, closes the device node file corresponding to the file descriptor fd after the unlocking is successful, and calls the global variable modification unit to update the value of its own global variable to the initial value; Process task 2 calls the file lock unit to lock the device node file corresponding to the file descriptor fd successfully, and when it is determined that the value of its own global variable is the initial value, calls the global variable modification unit to update the value of its own global variable with the file descriptor fd; After processing the transaction, process task 2 calls the file lock unlocking unit to perform the unlocking operation, closes the device node file corresponding to the file descriptor fd after the unlocking is successful, and calls the global variable modification unit to update the value of its own global variable to the initial value.
7. A multi-task mutual exclusion system according to claim 5, characterized in that: Each thread task has a unique global variable. When multiple thread tasks operating on the same device node file access the critical section, the corresponding operations are: Thread task 1 calls the file opening unit to open the device node file to obtain the first file descriptor fd before thread task 2, and calls the file lock unit to lock the device node file based on the first file descriptor fd. When the lock is successful and the value of the global variable is determined to be the initial value, the global variable modification unit is called to update the value of the global variable with the first file descriptor fd; After thread task 1, thread task 2 calls the file opening unit to open the device node file, obtains the second file descriptor fd, and calls the file lock unit to lock the device node file based on the second file descriptor fd. If the lock fails, it enters a blocked waiting state and delays the attempt to lock the file. After thread task 1 completes the transaction, it calls the file lock unlocking unit to perform the unlocking operation, and after the unlocking is successful, it closes the device node file corresponding to the first file descriptor fd, and calls the global variable modification unit to update the value of the global variable to the initial value; Process task 2 calls the file lock unit to lock the device node file successfully, and when it is determined that the value of the global variable is the initial value, calls the global variable modification unit to update the value of the global variable with the second file descriptor fd; After thread task 2 completes the transaction, it calls the file lock unlocking unit to perform the unlocking operation, and after the unlocking is successful, it closes the device node file and calls the global variable modification unit to update the value of the global variable to the initial value.
8. A multi-task mutual exclusion system based on file lock according to claim 7, characterized in that: After thread task 2 is locked successfully, if it is determined that the value of the global variable is not the initial value, the determination of the value of the global variable is delayed until the value of the global variable is determined to be the initial value, and then the global variable modification unit is called to update the value of the global variable to the initial value.
9. An electronic device, characterized in that: include: a memory having a computer program stored thereon; A processor, configured to execute the computer program in the memory to implement the steps of the method according to any one of claims 1 to 4.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the steps of the method described in any one of claims 1 to 4 are implemented.
Citation Information
Patent Citations
Distributed file system-based fusion lock management method and system
CN107423403A
Method, system and related components for setting cluster file lock
CN109408477A