CN116431358A - Resource access method and device, electronic equipment and storage medium - Google Patents

Resource access method and device, electronic equipment and storage medium Download PDF

Info

Publication number
CN116431358A
CN116431358A CN202310402970.3A CN202310402970A CN116431358A CN 116431358 A CN116431358 A CN 116431358A CN 202310402970 A CN202310402970 A CN 202310402970A CN 116431358 A CN116431358 A CN 116431358A
Authority
CN
China
Prior art keywords
value
shared resource
preset
access
semaphore
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202310402970.3A
Other languages
Chinese (zh)
Inventor
曹云涛
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Nanjing Horizon Integrated Circuit Co ltd
Original Assignee
Nanjing Horizon Integrated Circuit Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Nanjing Horizon Integrated Circuit Co ltd filed Critical Nanjing Horizon Integrated Circuit Co ltd
Priority to CN202310402970.3A priority Critical patent/CN116431358A/en
Publication of CN116431358A publication Critical patent/CN116431358A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5027Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
    • G06F9/5038Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals considering the execution order of a plurality of tasks, e.g. taking priority or time dependency constraints into consideration
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Multi Processors (AREA)

Abstract

A resource access method, a device, an electronic device and a storage medium are disclosed, and relate to the technical field of computers. The method comprises the following steps: determining a semaphore value of a first shared resource corresponding to a first process; starting timing according to the signal quantity value, and reading a preset parameter corresponding to the first shared resource to obtain a first value; reading a preset parameter according to a first size relation between the first time length of timing and the preset time length to obtain a second value; and controlling the first process to access the first shared resource according to the second magnitude relation between the first value and the second value. The method and the device can ensure that each process normally accesses the shared resource, thereby improving the effective utilization rate of the shared resource and reducing the possibility of abnormality of each process.

Description

Resource access method and device, electronic equipment and storage medium
Technical Field
The disclosure relates to the technical field of computers, and in particular relates to a resource access method, a resource access device, electronic equipment and a storage medium.
Background
In order to improve the utilization of computer resources, some shared resources are usually provided in a computer. Each shared resource may be accessed by multiple processes.
To ensure that only one of the mutually exclusive processes accesses the shared resource at a time, a semaphore (semaphore) mechanism may be employed. However, using the semaphore mechanism may have the possibility that the process may not update the value of the semaphore used by the semaphore mechanism when the process ends accessing the shared resource due to an emergency, thereby causing other processes other than the process to also not access the shared resource.
Disclosure of Invention
Currently, the related scheme provides that each process determines that the shared resource cannot be accessed according to the value of the signal quantity, and when the duration of waiting for accessing the shared resource exceeds the preset duration, the process can directly access the shared resource without considering the value of the signal quantity. However, if two or more processes wait for access to the shared resource for more than a preset period of time, then the two or more processes access the shared resource. Thereby causing the two or more processes to interfere with each other and causing the two or more processes to be abnormal.
The present disclosure has been made in order to solve the above technical problems. The embodiment of the disclosure provides a resource access method, a device, electronic equipment and a storage medium, which can ensure that each process normally accesses shared resources and improve the effective utilization rate of the shared resources.
According to a first aspect of the present disclosure, there is provided a resource access method, the method comprising: firstly, determining a semaphore value of a first shared resource corresponding to a first process; then, starting timing according to the signal quantity value, and reading a preset parameter corresponding to the first shared resource to obtain a first value; reading a preset parameter according to a first size relation between the first time length of timing and the preset time length to obtain a second value; and finally, controlling the first process to access the first shared resource according to the second magnitude relation between the first value and the second value.
According to a second aspect of the present disclosure, there is provided a resource access device comprising: the signal quantity acquisition module is used for determining a signal quantity value of a first shared resource corresponding to the first process; the parameter acquisition module is used for starting timing according to the semaphore value determined by the semaphore acquisition module and reading a preset parameter corresponding to the first shared resource to obtain a first value; reading a preset parameter according to a first size relation between the first time length of timing and the preset time length to obtain a second value; and the access control module is used for controlling the first process to access the first shared resource according to the second magnitude relation between the first value and the second value obtained by the parameter obtaining module.
According to a third aspect of the present disclosure, there is provided a computer-readable storage medium storing a computer program for executing the resource access method provided in the first aspect described above.
According to a fourth aspect of the present disclosure, there is provided an electronic device comprising: a processor; a memory for storing processor-executable instructions; a processor configured to read the executable instructions from the memory and execute the instructions to implement the resource access method provided in the first aspect.
According to a fifth aspect of the present disclosure, there is provided a computer program product which, when executed by a processor, performs the resource access method provided in the first aspect above.
Based on the resource access method, the device, the electronic equipment and the storage medium provided by the disclosure, when the value of the signal quantity of the first shared resource indicates that the first process cannot access the first shared resource, the computer starts timing and reads the value (namely, the first value) of the preset parameter corresponding to the first shared resource. When the time length reaches the preset time length, the first process does not access the first shared resource, but reads the value (namely the second value) of the preset parameter, and then determines that the first process accesses or does not access the first shared resource according to the change of the value of the preset parameter before and after the time length. Compared with the method that the first process accesses the first shared resource when the time length reaches the preset time length, the resource access method provided by the embodiment of the disclosure can reduce the occurrence of the simultaneous access of a plurality of processes to the shared resource because the time length reaches the preset time length, thereby reducing the occurrence of mutual interference of the processes to the access of the shared resource. Furthermore, the effective utilization rate of the shared resource can be improved, and the possibility of abnormality of a plurality of processes can be reduced.
Drawings
FIG. 1 is a schematic illustration of multiple processes accessing a shared resource provided by a related scheme;
FIG. 2 is a flow diagram of a method of resource access provided by the present disclosure;
FIG. 3 is a flow diagram of another resource access method provided by the present disclosure;
FIG. 4 is a schematic diagram of multiple processes accessing a shared resource provided by the present disclosure;
FIG. 5 is a flow diagram of yet another resource access method provided by the present disclosure;
FIG. 6 is a schematic diagram of a resource access device provided by the present disclosure;
fig. 7 is a second schematic structural diagram of a resource access device provided in the present disclosure;
fig. 8 is a schematic structural diagram of an electronic device provided in the present disclosure.
Detailed Description
For the purpose of illustrating the present disclosure, exemplary embodiments of the present disclosure will be described in detail below with reference to the accompanying drawings. It should be apparent that the described embodiments are only some of the embodiments of the present disclosure and not all of the embodiments of the present disclosure, and that the present disclosure is not limited by the example embodiments described herein.
It should be noted that: the relative arrangement of the components and steps, numerical expressions and numerical values set forth in these embodiments do not limit the scope of the present disclosure unless it is specifically stated otherwise.
Summary of the application
Currently, computers provide a number of shared resources, each of which (e.g., shared memory, shared files, or device files) is available for use by multiple processes. However, since multiple processes in a computer may interfere with each other when accessing a shared resource at the same time, for example, the processes may write a shared file at the same time, each process may not guarantee that the shared file stores data written by itself or written by other processes except itself. Thus, only one process of the plurality of processes accesses the same shared resource at a time, i.e., the plurality of processes use the same shared resource mutually exclusively. The multiple processes may also be referred to as mutually exclusive multiple processes.
To ensure that only one of the mutually exclusive processes accesses the shared resource at a time, a semaphore (semaphore) mechanism may be employed. The semaphore mechanism is that a pointer is provided with a semaphore for each shared resource, which semaphore corresponds to a semaphore for controlling access to the shared resource by multiple processes.
Specifically, each of the plurality of processes performs wait operations based on the semaphore mechanism. The wait operation includes: each process checks the value of the semaphore corresponding to the shared resource before accessing the shared resource; if the value of the semaphore is a first preset value, the process cannot access the shared resource; if the value of the semaphore is a second preset value, the process can access the shared resource, and the value of the semaphore is modified to be the first preset value, so that other processes except the process can not access the shared resource during the period that the process accesses the shared resource; then, after the process finishes accessing the shared resource, the value of the semaphore is further modified to a second preset value, so that other processes except the process can access the shared resource.
Wherein a process modifies the value of a semaphore after accessing a shared resource may be referred to as the process releasing the semaphore. Other processes than the process wait for the process to release the semaphore before accessing the shared resource.
However, there may be a possibility that a process does not modify the value of the semaphore when the process finishes accessing the shared resource due to an emergency in the process of executing the wait operation, so that other processes except the process cannot access the shared resource. For example, a process is terminated (which may be referred to as a process kill) or aborted (which may be referred to as a process coredump) in accessing the shared resource, then the process ends access to the shared resource, but does not modify the value of the semaphore.
In this regard, the related schemes propose that each process may perform a wait for timeout operation while performing wait operations. The timeout waiting operation includes: each process determines that the shared resource cannot be accessed according to the value of the semaphore, and when the duration of waiting for accessing the shared resource exceeds the preset duration (namely, waiting for timeout), the process can directly access the shared resource without considering the value of the semaphore. However, if the duration of the multiple processes waiting to access the shared resource exceeds the preset duration, the multiple processes all access the shared resource (for example, the multiple processes perform modification operations on the shared resource at the same time), the multiple processes interfere with each other, so that the multiple processes are caused to be abnormal, for example, running errors of any one process and running crashes of any one process.
In summary, this implementation is known to have multiple processes accessing a shared resource at the same time. When a plurality of processes access a shared resource at the same time, the processes interfere with each other, so that the processes are abnormal, and the effective utilization rate of the shared resource is reduced.
Illustratively, as shown in fig. 1, a process a in the computer determines that the first shared resource cannot be accessed according to the value of the signal quantity corresponding to the first shared resource, and starts timing at time t 1; the duration of time counted starting at time t1 may be referred to as a first duration corresponding to process a. The process B determines that the first shared resource cannot be accessed according to the value of the semaphore and starts timing at the time t 2; the duration of time counted starting at time t2 may be referred to as a second duration corresponding to process B. Then, at time t3, if the first time length corresponding to the process a exceeds the preset time length, the process a accesses the first shared resource. At time t4, if the second duration corresponding to the process B exceeds the preset duration, the process B accesses the first shared resource.
It can be known that, from the time t4, both the process a and the process B access the first shared resource due to the fact that the time duration exceeds the preset duration. When the process A and the process B access the first shared resource at the same time, the process A and the process B interfere with each other, so that the process A and the process B are abnormal.
Exemplary System
The resource access method provided by the embodiment of the disclosure can be applied to a scene that a plurality of mutually exclusive processes in a computer access the same shared resource.
The resource access method provided by the embodiments of the present disclosure may be implemented by using any operating system (e.g., windows TM Operating System or Linux TM Operating system) may also be executed by the resource access device. The resource access means may be located in the electronic device, as part of the electronic device (e.g. a CPU, a microprocessor chip or a hardware accelerator chip).
Exemplary method
Fig. 2 is a flow chart illustrating a resource access method according to an exemplary embodiment of the present disclosure. The resource access method provided in this embodiment may be applied to an electronic device, as shown in fig. 2, and the method may include the following steps S201 to S204.
S201, determining a semaphore value of a first shared resource corresponding to a first process.
Before a first process in the electronic device 100 prepares to access a first shared resource, it needs to query the semaphore of the first shared resource to determine whether the first process can access the first shared resource according to the semaphore of the first shared resource. For example, the electronic device 100 reads the semaphore of the first shared resource to obtain a value of the semaphore (may be simply referred to as a semaphore value). The first process may be any process in the electronic device 100. The first shared resource may be any shared resource that the first process can access.
In some embodiments, the value of the semaphore for the first shared resource is used to indicate that the first shared resource is accessible or that the first shared resource is not accessible. For example, the semaphore for the first shared resource may be a non-negative integer, where a value of 0 indicates that the first shared resource is not accessible, and where a value of non-zero indicates that the first shared resource is accessible.
In some embodiments, each process in the electronic device 100 may set the semaphore value for the first shared resource to a first preset value after accessing the first shared resource. The semaphore value is a first predetermined value indicating that the first shared resource is not accessible. Second, each process may set the value of the semaphore to a second preset value after ending access to the first shared resource. When the signal quantity value is a second preset value, the first shared resource is in an idle state and can be accessed.
For example, the semaphore of the first shared resource may be a non-negative integer, and the first preset value and the second preset value are both non-negative integers. For example, if the initial value of the semaphore of the first shared resource is 1, the first preset value is 0, and the second preset value is 1, each process accesses the first shared resource when the semaphore value is 1, and subtracts 1 from the semaphore value; then, after ending the access to the first shared resource, the semaphore value may be incremented by 1. For example, each process may implement adding 1 to the value of the semaphore by performing a post operation.
S202, starting timing according to the signal quantity value, and reading a preset parameter corresponding to the first shared resource to obtain a first value.
The electronic device 100 determines from the semaphore value whether the first process accesses the first shared resource or does not access the first shared resource. If the electronic device 100 determines that the first process does not access the first shared resource, timing is started, and a preset parameter corresponding to the first shared resource is read to obtain a first value. The duration of time that the electronic device 100 starts counting when determining that the first process does not access the first shared resource may be referred to as a first duration of time.
In some embodiments, the electronic device 100 includes a preset parameter corresponding to the first shared resource, for example, the name of the preset parameter is mutex_value. The value of the preset parameter is variable. The wait timeout refers to that a duration that one process waits for accessing the first shared resource exceeds a preset duration. For example, if the initial value of the preset parameter corresponding to the first shared resource is 0, the value of the preset parameter may represent the number of times the first shared resource is accessed due to the timeout of the process waiting.
In some embodiments, the preset parameter may be an integer. For example, the data type of the preset parameter is an integer type int or an unsigned integer type (uint).
In some embodiments, the electronic device 100 may include a preset shared memory for storing the values of the preset parameters. The process (including the first process) in the electronic device 100 capable of accessing the first shared resource may access the preset shared memory to read the value of the preset parameter in the preset shared memory.
The storage capacity of the preset shared memory (i.e., the size of the preset shared memory) depends on the storage capacity required by the preset parameter (i.e., the size of the preset parameter). For example, if the data type of the preset parameter is uint and the data bit width of the preset parameter is 32, the storage capacity of the preset shared memory for storing the preset parameter may be 4 bytes.
For example, the electronic device 100 may read the value in the preset shared memory through the first process to obtain the first value. The first value may also be saved by the first process in a stack (which may be referred to as a local stack) used by the first process.
In some embodiments, after the electronic device 100 obtains the semaphore value, if it is determined that the semaphore value is equal to the first preset value, the electronic device 100 starts timing and reads the preset parameter to obtain the first value. If the semaphore value is determined to be equal to the second preset value, the electronic device 100 controls the first process to access the first shared resource and sets the semaphore value to the first preset value. Wherein the semaphore value being equal to the second preset value characterizes the first shared resource as being in an idle state.
If the semaphore value is equal to the first preset value, which indicates that there may be other processes other than the first process accessing the first shared resource (i.e., the first shared resource is in an accessed state), the electronic device 100 controls the first process not to access the first shared resource. If the semaphore value is equal to the second preset value, indicating that the first shared resource is in an idle state, the electronic device 100 controls the first process to access the first shared resource.
It is appreciated that the electronic device 100 may control the first process to access the first shared resource in the idle state. The first process has no other process interference in the process of accessing the first shared resource in the idle state, so that the first process can normally access the first shared resource, thereby improving the effective utilization rate of the shared resource and reducing the possibility of abnormality of the first process.
Further, after determining that the signal quantity value is equal to the first preset value, the electronic device 100 may start to perform wait operation in addition to starting to time and read the preset parameters. The wait operation may include: determining whether the semaphore value has changed (e.g., from a first preset value to a second preset value), and controlling the first process to access the first shared resource if the changed semaphore value indicates that the first shared resource is in an idle state (e.g., the changed semaphore value is the second preset value).
In some embodiments, the electronic device 100 may read the preset parameter through an atomic variable (e.g., atomic variable) to obtain the first value.
It should be noted that operations performed by atomic variables all belong to atomic operations. An operation in which an atomic operation is not interrupted by a thread scheduling mechanism. The atomic operation, once started, runs all the way to the end without any switching in between. Further, it can be seen that the electronic device 100 is not interrupted in the process of reading the preset parameter through the atomic variable, so that the reading of the preset parameter can be ensured to be completed, and the first value is ensured to be obtained.
S203, reading a preset parameter according to the relationship between the first time length and the preset time length to obtain a second value.
After the electronic device 100 begins to time, the first duration of the time may be compared to a preset duration to determine whether the first process waits for a timeout. If the relationship between the first duration and the preset duration indicates that the first duration is greater than or equal to the preset duration, and indicates that the first process waits for timeout, the electronic device 100 may read the preset parameter to obtain a second value, so as to determine whether the first process accesses the first shared resource according to the first value and the second value of the preset parameter. The size relationship between the first time period and the preset time period may be referred to as a first size relationship.
For example, the electronic device 100 may read the value in the preset shared memory through the first process to obtain the second value. The second value may also be saved by the first process in a stack used by the first process (i.e., a local stack).
In some embodiments, after the semaphore value indicates that the first shared resource is in an accessed state (e.g., the semaphore value is a first preset value), the electronic device 100 may begin timing, begin performing wait operations, and read the preset parameter to obtain the first value. Further, if the semaphore value changes (e.g., from a first preset value to a second preset value) before the counted first duration does not reach the preset duration and the changed semaphore value indicates that the first shared resource is changed from the accessed state to the idle state, the electronic device 100 may implement the first process to access the first shared resource in the idle state and set the semaphore value to the first preset value by performing the wait operation. If the semaphore value does not change after the first time period is counted up to the preset time period, it indicates that the first shared resource may still be accessed, or indicates that the first shared resource is in an idle state but the semaphore value is not modified, the electronic device 100 may further read the preset parameter to obtain a second value, and further determine whether the first process accesses the first shared resource according to the first value and the second value.
In some embodiments, the electronic device 100 may read the preset parameter through an atomic variable (e.g., atomic variable) to obtain the second value.
It can be appreciated that, since the atomic variable is not interrupted during the execution of the step, the step of reading the preset parameter is performed by the atomic variable, so that the completion of reading the preset parameter can be ensured, thereby ensuring that the second value is obtained.
S204, controlling the first process to access the first shared resource according to the magnitude relation between the first numerical value and the second numerical value.
The electronic device 100 can compare whether the first value and the second value are equal to determine whether the first process accesses the first shared resource. Wherein the magnitude relation of the first value and the second value may be referred to as a second magnitude relation.
Illustratively, the electronic device 100 compares, by the first process, the first value and the second value stored in the stack used by the first process (i.e., the local stack).
It may be appreciated that when the first shared resource is in the accessed state and the first time period of the timer reaches the preset duration (i.e. the first process waits for timeout), the electronic device 100 determines whether to access the first shared resource according to the values of the preset parameters before and after the preset duration. Compared with the first process accessing the first shared resource when waiting for overtime (namely executing overtime waiting operation), the first process further determines whether to access the first shared resource according to the numerical values of the preset parameters before and after the preset duration when waiting for overtime, so that the condition that a plurality of processes access the first shared resource at the same time due to overtime waiting can be avoided, the effective utilization rate of the shared resource can be improved, and the abnormal condition of a plurality of processes caused by mutual interference of the processes on the access of the first shared resource can be avoided.
Referring to fig. 3, on the basis of the embodiment shown in fig. 2, S204 in the resource access method disclosed in the embodiment of the disclosure may include S301, and the method may further include S302 after S203.
And S301, controlling the first process to access the first shared resource in response to the second magnitude relation indicating that the first value is equal to the second value.
The electronic device 100 may control the first process to access the first shared resource in response to the second magnitude relationship (i.e., the magnitude relationship of the first and second values) indicating that the first value is equal to the second value.
In some embodiments, each process (e.g., a first process) in electronic device 100 includes a piece of code for accessing one shared resource (e.g., a first shared resource), and the piece of code is in a one-to-one correspondence with the shared resource. This piece of code may be referred to as a critical section, where entry of each process into the critical section indicates that each process accesses this shared resource. Each process may include a plurality of different critical sections for accessing different shared resources.
For example, the electronic device 100 controls the first process to enter a critical section corresponding to the first shared resource, so as to enable the first process to access the first shared resource.
S302, controlling the first process not to access the first shared resource in response to the second magnitude relation indicating that the first value is not equal to the second value.
The electronic device 100 controls the first process not to access the first shared resource in response to the second size relationship indicating that the first value is not equal to the second value.
It will be appreciated that the value of the preset parameter is different before and after a process waiting for a timeout accesses the first shared resource, that is, a process may modify the value of the preset parameter after waiting for a timeout accesses the first shared resource. Furthermore, if the first value and the second value of the preset parameter are different, the first process waits for the timeout to access the first shared resource, and the first process does not access the first shared resource even if the first process waits for the timeout to access the first shared resource, so that the first process can be prevented from interfering with the access of other processes to the first shared resource. Therefore, the effective utilization rate of the first shared resource can be improved, and abnormal conditions of other processes caused by the interference of the first process can be avoided.
If the first value and the second value of the preset parameter are equal, which means that no other process except the first process accesses the first shared resource in the process that the first process waits for accessing the first shared resource, the first process accesses the first shared resource after waiting for overtime, so that only the first process accesses the first shared resource due to overtime waiting, and the first process can normally access the first shared resource without interference of other processes. Therefore, the effective utilization rate of the first shared resource can be improved, and the abnormal situation of the first process caused by the interference of other processes can be avoided.
In some embodiments, a process may modify the value of the preset parameter after waiting for the timeout to access the first shared resource, so that the first process may also modify the value of the preset parameter after accessing the first shared resource to indicate that there is a timeout waiting process (i.e., the first process) accessing the first shared resource.
For example, referring to fig. 3, the resource access method provided in the embodiment of the present disclosure may further include S303-S305 after S301.
S303, setting the numerical value of the preset parameter as a third numerical value according to a preset rule; the third value and the second value are different.
In addition to controlling the first process to access the first shared resource, the electronic device 100 may modify the value of the preset parameter according to a preset rule when the first time period of the timer is greater than or equal to the preset time period and the first value and the second value of the preset parameter are equal. For example, the electronic device 100 modifies the value of the preset parameter through the first process. Wherein the preset rule comprises adding 1 or subtracting 1.
It should be noted that, after accessing the first shared resource under the condition that the value of the preset parameter is unchanged, all processes in the electronic device 100 modify the value of the preset parameter according to the same preset rule, and the preset rule is 1 or 1 is added or subtracted, so that the value of the preset parameter may be changed in a single direction, for example, increased or decreased. Therefore, after each time the numerical value of the preset parameter is modified, the numerical value of the modified preset parameter is changed compared with all numerical values before modification.
In some embodiments, in a case where the initial value of the preset parameter is 0 and the preset rule is 1, the value of the preset parameter is equal to the number of times the first shared resource is accessed due to the process waiting timeout.
S304, responding to the second time length corresponding to the second process to be longer than or equal to the preset time length, and reading the preset parameters to obtain a third numerical value.
The electronic device 100 may include a second process that is different from the first process. After the first process begins to wait for access to the first shared resource, the second process also begins to wait for access to the first shared resource. The same process as the first process accessing the first shared resource, and before the second process accesses the first shared resource accurately, the electronic device 100 firstly queries the semaphore of the first shared resource to obtain a semaphore value corresponding to the second process; then, the electronic device 100 determines that the second process accesses or does not access the first shared resource according to the semaphore value corresponding to the second process. If it is determined that the second process does not access the first shared resource, the electronic device 100 starts timing, and reads a preset parameter corresponding to the first shared resource to obtain a fourth value. The time period for determining that the second process does not access the first shared resource to start timing may be referred to as a second time period corresponding to the second process. Then, if the second time period corresponding to the second process is greater than or equal to the preset time period, the second process waits for timeout, and the electronic device 100 reads the preset parameter to obtain a third value.
It may be appreciated that, since the second process starts to wait for access to the first shared resource after the first process, after determining that the first process does not access the first shared resource to read the preset parameter to obtain the first value, the electronic device 100 determines that the second process does not access the first shared resource and reads the preset parameter to obtain the fourth value. The fourth value obtained by reading the preset parameter after the electronic device 100 reads the preset parameter to obtain the first value may be equal to the first value.
In addition, when the first time period corresponding to the first process is greater than or equal to the preset time period, the electronic device 100 controls the first process to access the first shared resource and set the value of the preset parameter to a third value different from the second value. The second process starts to wait for accessing the first shared resource after the first process, so that it is known that the second time period corresponding to the second process is longer than or equal to the preset time period after the first time period corresponding to the first process is longer than or equal to the preset time period. That is, after the electronic device 100 sets the value of the preset parameter to the third value, the second time period corresponding to the second process is greater than or equal to the preset time period. Further, the value obtained by the electronic device 100 reading the preset parameter when the second time period corresponding to the second process is greater than or equal to the preset time period may be the third value.
S305, based on the third numerical value, controlling the second process not to access the first shared resource.
The electronic device 100 starts timing when determining that the second process does not access the first shared resource, and reads a preset parameter corresponding to the first shared resource to obtain a fourth value; and then, when the second time length corresponding to the second process is greater than or equal to the preset time length, reading the preset parameter to obtain a third value. Wherein, since the fourth value may be equal to the first value, the third value is different from the second value, and the second value is equal to the first value, it is known that the third value and the fourth value are different. And in response to the third value not being equal to the fourth value, the electronic device 100 may control the second process not to access the first shared resource.
It will be appreciated that the preset parameter is accessed by the first process waiting for the timeout to access the first shared resource and modify the value of the preset parameter if the value of the period from when the first process starts to wait until the timeout is unchanged. Furthermore, if other processes (for example, the second process) except the first process are waiting during the waiting period of the first process, the preset parameters are changed when the waiting time of the other processes is overtime, and the other processes cannot access the first shared resource, so that the interference of the other processes to the first process accessing the first shared resource due to the overtime waiting access can be avoided. Thus, the normal access of the first process to the first shared resource can be ensured.
By taking a first process as a process a and a second process as a process B as an example, a process of accessing a first shared resource by using the resource access method provided by the embodiment of the present disclosure is described. As shown in fig. 4, a process a in the electronic device 100 determines, at time t1, that the first shared resource cannot be accessed according to the signal quantity value of the first shared resource, starts timing, and reads a preset parameter to obtain a first value v1; then, the process a waits for timeout at time t3, and reads the preset parameter to obtain a second value v2. Assuming v1 is equal to v2, process A accesses the first shared resource and modifies the preset parameter to a third value v3. v3 is not equal to v2 and v1.
The process B determines that the first shared resource cannot be accessed according to the signal quantity value of the first shared resource at the time t2, starts timing, and reads a preset parameter to obtain a fourth value v4; v4 may be equal to v1. Then, the process B waits for overtime at the time t4, and reads the preset parameters to obtain a fifth value v5; v5 may be equal to v3.
Wherein, since v4 is equal to v1, v5 is equal to v3, and v3 is not equal to v1, it is known that v4 is not equal to v 5. v4 is not equal to v5, process B does not access the first shared resource and redetermines the semaphore value for the first shared resource. If process B determines from the redetermined semaphore value that the first shared resource cannot be accessed, process B waits again for access to the first shared resource.
It is known that the process a waits for the timeout to access the first shared resource, and in the process a accessing the first shared resource, the process B also waits for the timeout, but the process B does not access the first shared resource, but newly waits for the access to the first shared resource. Process B does not interfere with process a's access to the first shared resource.
In some embodiments, the electronic device 100 may set the value of the preset parameter to the third value according to the preset rule by using the atomic variable.
It will be appreciated that, since the atomic variable is not interrupted during the execution of the step, the execution of the step of modifying the value of the preset parameter by the atomic variable ensures that the modification of the value of the preset parameter is completed.
In some embodiments, the first process waits for a timeout, and in a case where the value of the preset parameter changes from when the first process starts to wait until when the value of the preset parameter changes, the first process does not access the first shared resource, and the first process may also wait for accessing the first shared resource again until the first process ends when accessing the first shared resource.
For example, referring to fig. 3, the resource access method provided in the embodiment of the present disclosure may re-execute S201 after S302, that is, re-determine the semaphore value of the first shared resource.
The electronic device 100 reads the preset parameter to obtain a second value, and re-determines the signal quantity value of the first shared resource under the condition that the second value is not equal to the first value; if the new semaphore value indicates that the first shared resource is in the accessed state, restarting the timing, and re-reading the preset parameter to obtain a new first value.
It should be noted that, since the interval between the time when the electronic device 100 reads the preset parameter to obtain the second value and the time when the electronic device re-reads the preset parameter to obtain the new first value is shorter, the new first value may be equal to the second value. Alternatively, the electronic device 100 may not perform the step of "re-reading the preset parameter to obtain the new first value", but take the second value as the new first value.
For example, taking an example that the first shared resource may be accessed when the semaphore value of the first shared resource is 0 and the first shared resource may not be accessed when the semaphore value is 1, please refer to fig. 5, S202 in the resource access method provided in the embodiment of the disclosure may include S501, the method may further include S502 after S202, S203 may include S503, and S303 may include S504 on the basis of the embodiment shown in fig. 3.
S501, starting timing in response to the signal quantity value being 0, and reading preset parameters in a preset shared memory to obtain a first value.
S502, in response to the first time length of the timing being smaller than the preset time length and the semaphore value being 1, controlling the first process to access the first shared resource.
In addition to controlling the first process to access the first shared resource, the electronic device 100 may decrease the semaphore value by 1 when the first time period is less than the preset time period and the semaphore value becomes 1.
S503, in response to the first time length of the timing being greater than or equal to the preset time length and the signal quantity value being 0, reading preset parameters in the preset shared memory to obtain a second value.
S504, adding 1 to a preset parameter in a preset shared memory.
In response to the first value being equal to the second value, the electronic device 100 adds 1 to the preset parameter in addition to controlling the first process to access the first shared resource.
It can be understood that when the semaphore value is equal to the first preset value, it indicates that there may be other processes other than the first process accessing the first shared resource, and the first process does not access the first shared resource, counts time and reads the preset parameter corresponding to the first shared resource. After waiting for the preset time length, the other processes accessing the first shared resource before timing end the access to the first shared resource with high probability, and whether to access the first shared resource is further determined according to whether the numerical value changes in the process that the first process waits for the preset time length according to the preset parameters. If the value of the preset parameter is unchanged, the first process accesses the first shared resource without other processes accessing the first shared resource in the process of waiting for the preset time period, so that only the first process accesses the first shared resource independently and is not interfered.
Exemplary apparatus
The embodiment of the disclosure also provides a resource access device under the condition that each function module is divided by corresponding each function. Fig. 6 is a schematic structural diagram of a resource access device according to an embodiment of the present disclosure. The apparatus may include: a semaphore acquisition module 61, a parameter acquisition module 62 and an access control module 63.
The semaphore obtaining module 61 is configured to determine a semaphore value of a first shared resource corresponding to the first process. A parameter acquisition module 62 for: starting timing according to the semaphore value determined by the semaphore acquisition module 61, and reading a preset parameter corresponding to the first shared resource to obtain a first value; and reading the preset parameters according to the first size relation between the first time length of the timing and the preset time length to obtain a second value. The access control module 63 is configured to control the first process to access the first shared resource according to the second magnitude relation between the first value and the second value obtained by the parameter obtaining module 62.
In some embodiments, referring to fig. 7 in conjunction with fig. 6, the access control module 63 may include a first access control unit 631. The first access control unit 631 is configured to control the first process to access the first shared resource in response to the second size relationship indicating that the first value is equal to the second value.
In some embodiments, referring to fig. 7 in conjunction with fig. 6, the access control module 63 may include a preset parameter setting unit 632 and a second access control unit 633. Wherein, the preset parameter setting unit 632 is configured to set, after controlling the first process to access the first shared resource, a value of the preset parameter to a third value according to a preset rule; the third value and the second value are different.
The parameter obtaining module 62 is further configured to read a preset parameter in response to the second time period corresponding to the second process being greater than or equal to the preset time period, so as to obtain a third value. The second access control module 63 is configured to control the second process not to access the first shared resource based on the third value.
In some embodiments, the preset parameter setting unit 632 is specifically configured to set, by the atomic variable, the value of the preset parameter to the third value according to the preset rule.
In some embodiments, the first access control unit 631 is further configured to control the first process not to access the first shared resource in response to the second size relationship indicating that the first value is not equal to the second value. The semaphore acquisition module 61 is further configured to redetermine a semaphore value of the first shared resource.
In some embodiments, referring to fig. 7 in conjunction with fig. 6, the apparatus may include a semaphore setting module 64.
The parameter obtaining module 62 is specifically configured to determine that the signal magnitude value is equal to a first preset value, start timing, and read a preset parameter corresponding to the first shared resource to obtain the first value.
The first access control unit 631 is further configured to determine that the semaphore value is equal to a second preset value, and control the first process to access the first shared resource; the semaphore setting module 64 is configured to set the semaphore value to a first preset value. The semaphore value being equal to the second preset value characterizes the first shared resource as being in an idle state.
In some embodiments, the parameter obtaining module 62 is specifically configured to obtain the first value by reading the preset parameter through the atomic variable. The parameter obtaining module 62 is specifically configured to obtain a second value by reading a preset parameter through an atomic variable.
In some embodiments, the parameter obtaining module 62 is specifically configured to, in response to the first size relationship indicating that the first time period is greater than or equal to the preset time period, read the preset parameter to obtain the second value.
The beneficial technical effects corresponding to the exemplary embodiments of the present apparatus may refer to the corresponding beneficial technical effects of the foregoing exemplary method section, and will not be described herein.
Exemplary electronic device
Fig. 8 is a schematic diagram of a possible structure of an electronic device according to an exemplary embodiment, where the electronic device may be the above-mentioned resource access device, or may be a terminal or a server including the resource access device. As shown in fig. 8, the electronic device 80 includes at least one processor 81 and a memory 82.
Wherein the processor 81 may be a Central Processing Unit (CPU) or other form of processing unit having data processing and/or instruction execution capabilities, and may control other components in the electronic device 80 to perform desired functions.
Memory 82 may include one or more computer program products that may include various forms of computer-readable storage media, such as volatile memory and/or non-volatile memory. Volatile memory can include, for example, random access memory (random access memory, RAM) and/or cache memory (cache), among others. The nonvolatile memory may include, for example, a read-only memory (ROM), a hard disk, a flash memory, and the like. One or more computer program instructions may be stored on a computer readable storage medium and the processor 81 may execute the one or more computer program instructions to implement the resource access methods and/or other desired functions of the various embodiments of the present disclosure above.
In one example, the electronic device 80 may further include: an input device 83 and an output device 84, which are interconnected by a bus system and/or other forms of connection mechanisms (not shown).
The input device 83 may also include, for example, a keyboard, a mouse, and the like.
The output means 84 may output various information to the outside, which may include, for example, a display, a speaker, a printer, and a communication network and a remote output device connected thereto, etc.
Of course, only some of the components of the electronic device 80 relevant to the present disclosure are shown in fig. 8, with components such as buses, input/output interfaces, etc. omitted for simplicity. In addition, the electronic device 80 may include any other suitable components depending on the particular application.
Exemplary computer program product and computer readable storage Medium
In addition to the methods and apparatus described above, embodiments of the present disclosure may also provide a computer program product comprising computer program instructions which, when executed by a processor, cause the processor to perform the steps in the resource access method of the various embodiments of the present disclosure described in the "exemplary methods" section above.
The computer program product may write program code for performing the operations of embodiments of the present disclosure in any combination of one or more programming languages, including an object oriented programming language such as Java, C++ or the like and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computing device, partly on the user's device, as a stand-alone software package, partly on the user's computing device, partly on a remote computing device, or entirely on the remote computing device or server.
Furthermore, embodiments of the present disclosure may also be a computer-readable storage medium, having stored thereon computer program instructions that, when executed by a processor, cause the processor to perform the steps in the resource access method of the various embodiments of the present disclosure described in the "exemplary method" section above.
A computer readable storage medium may employ any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium may be, for example but not limited to, a system, apparatus, or device including electronic, magnetic, optical, electromagnetic, infrared, or semiconductor, or a combination of any of the foregoing. More specific examples (a non-exhaustive list) of the readable storage medium would include the following: an electrical connection having one or more wires, a portable disk, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EEPROM), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
The basic principles of the present disclosure have been described above in connection with specific embodiments, but the advantages, benefits, effects, etc. mentioned in this disclosure are merely examples and are not to be considered as necessarily possessed by the various embodiments of the present disclosure. Furthermore, the specific details disclosed herein are for purposes of illustration and understanding only, and are not intended to be limiting, since the disclosure is not necessarily limited to practice with the specific details described.
Various modifications and alterations to this disclosure may be made by those skilled in the art without departing from the spirit and scope of this application. Thus, the present disclosure is intended to include such modifications and alterations insofar as they come within the scope of the appended claims or the equivalents thereof.

Claims (11)

1. A method of resource access, the method comprising:
determining a semaphore value of a first shared resource corresponding to a first process;
starting timing according to the semaphore value, and reading a preset parameter corresponding to the first shared resource to obtain a first value;
reading the preset parameters according to a first size relation between the first time length of the timing and the preset time length to obtain a second value;
And controlling the first process to access the first shared resource according to the second magnitude relation between the first value and the second value.
2. The method of claim 1, wherein the controlling the first process to access the first shared resource according to the second size relationship of the first value and the second value comprises:
controlling the first process to access the first shared resource in response to the second size relationship indicating that the first value is equal to the second value.
3. The method of claim 1 or 2, wherein after controlling the first process to access the first shared resource, the method further comprises:
setting the value of the preset parameter as a third value according to a preset rule; the third value and the second value are different;
reading the preset parameters to obtain the third numerical value in response to the second time length corresponding to the second process being greater than or equal to the preset time length;
and controlling the second process not to access the first shared resource based on the third value.
4. A method according to claim 3, wherein setting the value of the preset parameter to a third value according to a preset rule comprises:
And setting the numerical value of the preset parameter to be the third numerical value according to the preset rule through an atomic variable.
5. The method according to claim 1 or 2, wherein the method further comprises:
controlling the first process not to access the first shared resource in response to the second size relationship indicating that the first value is not equal to the second value;
and re-determining the semaphore value of the first shared resource.
6. The method according to claim 1 or 2, wherein starting timing according to the semaphore value, and reading a preset parameter corresponding to the first shared resource to obtain a first value, including: HP2226622BJ
Determining that the signal quantity value is equal to a first preset value, starting timing, and reading the preset parameter corresponding to the first shared resource to obtain the first value;
the method further comprises the steps of:
determining that the semaphore value is equal to a second preset value, controlling the first process to access the first shared resource, and setting the semaphore value to the first preset value; the semaphore value being equal to the second preset value characterizes the first shared resource as being in an idle state.
7. The method according to claim 1 or 2, wherein the reading the preset parameter corresponding to the first shared resource to obtain the first value includes:
reading the preset parameters through atomic variables to obtain the first numerical value;
the reading the preset parameters to obtain a second value includes:
and reading the preset parameters through the atomic variables to obtain the second numerical value.
8. The method according to claim 1 or 2, wherein the reading the preset parameter according to the first magnitude relation between the first time period and the preset time period to obtain the second value includes:
and reading the preset parameters to obtain a second value in response to the first size relation indicating that the first time length is greater than or equal to the preset time length.
9. A resource access device, comprising:
the signal quantity acquisition module is used for determining a signal quantity value of a first shared resource corresponding to the first process;
the parameter acquisition module is used for starting timing according to the semaphore value determined by the semaphore acquisition module and reading a preset parameter corresponding to the first shared resource to obtain a first value; reading the preset parameters according to a first size relation between the first time length of the timing and the preset time length to obtain a second value;
And the access control module is used for controlling the first process to access the first shared resource according to the second magnitude relation between the first numerical value and the second numerical value obtained by the parameter obtaining module.
10. A computer readable storage medium storing a computer program for executing the resource access method of any of the preceding claims 1-8.
11. An electronic device, the electronic device comprising:
a processor;
a memory for storing the processor-executable instructions;
the processor is configured to read the executable instructions from the memory and execute the executable instructions to implement the resource access method of any of the preceding claims 1-8.
CN202310402970.3A 2023-04-14 2023-04-14 Resource access method and device, electronic equipment and storage medium Pending CN116431358A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202310402970.3A CN116431358A (en) 2023-04-14 2023-04-14 Resource access method and device, electronic equipment and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202310402970.3A CN116431358A (en) 2023-04-14 2023-04-14 Resource access method and device, electronic equipment and storage medium

Publications (1)

Publication Number Publication Date
CN116431358A true CN116431358A (en) 2023-07-14

Family

ID=87094009

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202310402970.3A Pending CN116431358A (en) 2023-04-14 2023-04-14 Resource access method and device, electronic equipment and storage medium

Country Status (1)

Country Link
CN (1) CN116431358A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117077115A (en) * 2023-10-13 2023-11-17 沐曦集成电路(上海)有限公司 Cross-language multi-process interaction method, electronic equipment and medium in chip verification stage

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117077115A (en) * 2023-10-13 2023-11-17 沐曦集成电路(上海)有限公司 Cross-language multi-process interaction method, electronic equipment and medium in chip verification stage
CN117077115B (en) * 2023-10-13 2023-12-15 沐曦集成电路(上海)有限公司 Cross-language multi-process interaction method, electronic equipment and medium in chip verification stage

Similar Documents

Publication Publication Date Title
US9235444B2 (en) Dynamically adjusting global heap allocation in multi-thread environment
US20070118838A1 (en) Task execution controller, task execution control method, and program
US9239742B2 (en) Embedded systems and methods for threads and buffer management thereof
CN116431358A (en) Resource access method and device, electronic equipment and storage medium
WO2017193964A1 (en) Component upgrade method, apparatus and system
CN114168490A (en) Method for determining memory recovery threshold and related equipment
KR102251451B1 (en) Information processing device, tuning method and tuning program stored in recording medium
CN111124921A (en) Memory out-of-range detection method, device, equipment and storage medium
CN111459691A (en) Read-write method and device for shared memory
JPWO2011096163A1 (en) Information processing system, exclusive control method, and exclusive control program
KR102543951B1 (en) Instruction executing method and apparatus, electronic device, and computer-readable storage medium
US9442724B2 (en) Start control apparatus for controlling a start of an information device by using an interrupt generation code, information device, and start control method
CN112540886A (en) CPU load value detection method and device
JP5332716B2 (en) Startup control method, information processing apparatus, and program
US12032474B2 (en) Computer-readable recording medium storing acceleration test program, acceleration test method, and acceleration test apparatus
US20180373582A1 (en) Data access device and access error notification method
CN112805978A (en) Enhanced anchor protocol for event stream processing
CN112988365B (en) Thread management method, device, equipment and medium
US20230315284A1 (en) Live firmware update of flash memory
US11681527B2 (en) Electronic device and multiplexing method of spatial
CN114422521B (en) Method and device for determining master node, electronic equipment and storage medium
US20220415405A1 (en) Memory-control circuit and method for controlling erasing operation of flash memory
JPS6327733B2 (en)
CN114675968A (en) Thread running method, system and device and readable storage medium
CN117992064A (en) Application compiling method and device, storage medium and electronic equipment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination