Method and device for managing random numbers under Linux system

By adopting multi-threaded design and GPIO interrupt communication in Linux systems, the problem of low random number management efficiency is solved, efficient random number acquisition and storage is achieved, and system performance is improved.

CN119645586APending Publication Date: 2025-03-18WUHAN SHIP COMM RES INST (NO 722 RES INST OF CHINA STATE SHIPBUILDING CORP)
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411580735.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-07
Publication Date
2025-03-18

AI Technical Summary

Technical Problem

In Linux systems, applications and drivers have inefficient problems when obtaining, adding and deleting random numbers, especially when finding and storing random numbers. The traditional polling and sleeping method causes the application layer to be unable to perform other tasks asynchronously.

Method used

It adopts a multi-threaded design, and starts the thread to obtain random number and generate random number threads during initialization, and manages synchronously through semaphores and waiting queues. GPIO interrupts are used for asynchronous communication between drivers and applications. Random numbers are stored in the ring buffer at the application layer, and the hash table is used to improve search efficiency.

Benefits of technology

Improves the efficiency and performance of random number management, allows the application layer to perform other tasks asynchronously, reduces the workload of the device, and reduces the time for random number search through hash tables.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119645586A_ABST
    Figure CN119645586A_ABST
Patent Text Reader

Abstract

The invention discloses a management method and device for random numbers under a Linux system, and the method comprises the steps: starting a plurality of threads when an application program is initialized, initializing a semaphore and a waiting queue, and enabling the plurality of threads to comprise a random number obtaining thread and a random number generating thread; obtaining a random number thread to obtain a semaphore, and executing a business process for processing a random number; under the condition that the threads which do not obtain the semaphore exist in the multiple threads, the threads are dormant in a waiting queue; under the condition that the semaphore of each thread in the multiple threads is released, the threads dormant in the waiting queue are awakened, and the semaphore is obtained, so that the problem of low efficiency of searching the random numbers and storing the random numbers is solved, a multi-thread design is adopted in the process of using the random numbers and obtaining the random numbers, and the running efficiency and performance of a program are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of embedded Linux technology, and more specifically, to a random number management method and device under a Linux system. Background Art

[0002] Random numbers are used to provide masking protection for data in the device. The random numbers of the device are obtained from the random chip of the board hardware. The random numbers obtained from the random chip need to be effectively managed. Both the application program and the kernel driver program of the software architecture in the Linux system need to use random numbers. Since the application program and the driver program of the Linux system are isolated, there are differences in the methods of using random numbers.

[0003] At the same time, the operation of obtaining random numbers, adding random numbers, and deleting random numbers by the device application and driver should not affect the software functions and performance of the entire device. The polling method plus the sleep method used in the related technology cannot allow the application layer to asynchronously perform other tasks, and there is a problem of low efficiency in finding and storing random numbers. Summary of the invention

[0004] In response to at least one defect or improvement need in the prior art, the present invention provides a random number management method and device under a Linux system, which solves the problem of low efficiency in searching and storing random numbers. The process of using and obtaining random numbers adopts a multi-threaded design, which improves the operating efficiency and performance of the program.

[0005] To achieve the above-mentioned purpose, according to a first aspect of the present invention, a random number management method under a Linux system is provided, the method comprising: starting multiple threads when an application is initialized, initializing semaphores and waiting queues, wherein the multiple threads include a random number acquisition thread and a random number generation thread; the random number acquisition thread obtains a semaphore and executes a business process for processing random numbers; if there is a thread among the multiple threads that has not obtained a semaphore, the thread is put to sleep in the waiting queue; if the semaphore of each thread among the multiple threads is released, the thread sleeping in the waiting queue is awakened and the semaphore is obtained.

[0006] In an exemplary embodiment, after starting multiple threads when the application is initialized and initializing the semaphore and the waiting queue, the method further includes: when the ring buffer is empty, the generated random number is stored in the ring buffer, and the random number acquisition thread sleeps and waits in the waiting queue, wherein the ring buffer storage area is used to store random numbers; when the ring buffer is full, the random number generation thread no longer puts random numbers into the ring buffer.

[0007] In an exemplary implementation, before the random number acquisition thread obtains a semaphore and executes a business process for processing random numbers, the method further includes: acquiring the random number used in the driver from the thread that generates random numbers in the application layer; and monitoring the level change of GPIO by setting an interrupt program in the driver, wherein the GPIO level change indicates that the driver needs to obtain a random number.

[0008] In an exemplary embodiment, after monitoring the level change of GPIO by setting an interrupt program in the driver, the method further includes: when the device receives a network message that cannot be masked by a random number, the network message triggers the GPIO pin to generate a high level, wherein the GPIO pin is at a low level when the device is turned on; the GPIO high level triggers the kernel driver to send a signal to the application, and after receiving the signal, the application registers an interrupt service function in the thread that generates random numbers.

[0009] In an exemplary embodiment, after the application receives the signal and registers an interrupt service function after the thread that generates the random number, the method further includes: after the thread that generates the random number obtains the interrupt signal, executing the interrupt service program to send the updated random number to the kernel to provide masking for the network message.

[0010] In an exemplary embodiment, the method also includes: storing the random numbers obtained by the driver from the application layer in the driver's hash table, wherein the hash table hash function maps the key value to the storage bucket; if the kernel driver fails to obtain the random number of the application layer's generation thread three times, then there is a fault in the channel between the kernel driver and the application layer; clearing the random numbers already stored in the kernel, wherein the clearing work using the kernel's work queue is performed by the kernel thread.

[0011] According to a second aspect of the present invention, a random number management device under a Linux system is also provided, comprising: an initialization unit, used to start multiple threads when an application is initialized, and initialize semaphores and waiting queues, wherein the multiple threads include random number acquisition threads and random number generation threads; an execution unit, used to acquire random number threads to obtain semaphores, and execute a business process for processing random numbers; a sleep unit, used to put the threads to sleep in the waiting queue if there are threads among the multiple threads that have not acquired semaphores; and a first acquisition unit, used to wake up the threads sleeping in the waiting queue and acquire the semaphore if the semaphore of each thread among the multiple threads is released.

[0012] According to a third aspect of the present invention, a computer-readable storage medium is provided, in which a computer program is stored, wherein the computer program is configured to execute the random number management method under the Linux system when running.

[0013] According to a fourth aspect of the present invention, there is also provided an electronic device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, wherein the processor executes the random number management method under the Linux system through the computer program.

[0014] In general, the above technical solutions conceived by the present invention can achieve the following beneficial effects compared with the prior art:

[0015] (1) The present invention provides a random number management method under a Linux system. The random numbers are used and generated in an application program using a multi-threaded design mode. The random numbers are stored in a ring buffer of the application layer. The threads are synchronized using a waiting queue and a semaphore. The communication between the driver program obtaining random numbers and the application program generating random numbers uses a general-purpose input / output (GPIO) interrupt, thereby solving the problem of low efficiency in searching for random numbers and storing random numbers. The process of using random numbers and obtaining random numbers adopts a multi-threaded design, thereby improving the running efficiency and performance of the program.

[0016] (2) By adopting a random number management method under a Linux system provided by the present invention, the process of using and obtaining random numbers adopts a multi-threaded design, which improves the running efficiency and performance of the program. The threads of the kernel and application layers communicate asynchronously through GPIO interrupts, rather than the traditional polling method plus sleep method, so that the application layer can asynchronously execute other tasks and improve device performance. The kernel driver uses the memory address to obtain the hash bucket, which greatly reduces hash collisions and improves the efficiency of searching and storing random numbers. The kernel driver uses the work queue to delay the execution of time-consuming tasks, avoiding device operation jams. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0018] Figure 1 A flowchart of an optional random number management method under a Linux system provided in an embodiment of the present application;

[0019] Figure 2 A schematic diagram of an optional process for processing random number inter-thread communication provided in an embodiment of the present application;

[0020] Figure 3 A schematic diagram of an optional process of storing random numbers in a ring buffer provided in an embodiment of the present application;

[0021] Figure 4 A schematic diagram of a flow chart of an optional driver program communicating with an application layer via a GPIO interrupt provided in an embodiment of the present application;

[0022] Figure 5 A schematic diagram of the structure of an optional random number management device under a Linux system provided in an embodiment of the present application;

[0023] Figure 6 A schematic diagram of the structure of an optional electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0024] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention. In addition, the technical features involved in the various embodiments of the present invention described below can be combined with each other as long as they do not conflict with each other.

[0025] The terms "first", "second", "third", etc. in the specification and claims of this application and the above-mentioned drawings are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but optionally includes steps or units that are not listed, or optionally includes other steps or units inherent to these processes, methods, products or devices.

[0026] According to one aspect of the embodiments of the present application, a random number management method in a Linux system is provided. Figure 1 The present invention describes a random number management method under a Linux system provided by an embodiment of the present application.

[0027] Figure 1 is a flow chart of an optional random number management method under Linux system provided by an embodiment of the present application, such as Figure 1 As shown, the process of the method may include the following steps:

[0028] S102, when the application is initialized, multiple threads are started to initialize the semaphore and the waiting queue, wherein the multiple threads include a random number acquisition thread and a random number generation thread;

[0029] S104, obtaining a random number thread to obtain a semaphore and executing a business process for processing random numbers;

[0030] S106, if there is a thread among the multiple threads that has not obtained the semaphore, put the thread to sleep in the waiting queue;

[0031] S108, when the semaphore of each thread among the multiple threads is released, wake up the thread sleeping in the waiting queue and obtain the semaphore.

[0032] The embodiment of the present application provides a random number management method under a Linux system, which can be applied to the scenario of providing masking protection for data in a device.

[0033] For example, there will be multiple business functions in an application that need to use random numbers. If each business function uses random numbers by starting different processes, if there are M processes (P1-PM) that need to use random numbers, and each process uses random numbers for N seconds (including obtaining random numbers from the server that generates random numbers, and masking or unmasking the random numbers after use), then the entire device program will need M*N seconds to use random numbers. This large time overhead will greatly affect the device's working performance.

[0034] The design is multi-threaded. The use of random numbers and the generation of random numbers are different threads in one process. The communication efficiency between threads is higher than that between processes, and multiple threads using random numbers can run at the same time. Assuming that there are M threads (T1-TM) using random numbers, the longest time for M threads to use random numbers is N seconds, then the time the entire device uses random numbers is the maximum time of one thread, N seconds. Random numbers are shared data for multiple threads. The threads of random numbers are protected by semaphores. The thread that obtains the semaphore can process the random number, and other threads that have not obtained the semaphore sleep in the waiting queue. After the thread uses the random number, it will send a signal to wake up other threads. The awakened thread obtains the semaphore and then processes the random number.

[0035] Furthermore, a multi-threaded design pattern can be adopted for using and generating random numbers in an application. The random numbers are stored in a circular buffer at the application layer, and waiting queues and semaphores are used for synchronization between threads. GPIO interrupts are used for communication between threads for obtaining random numbers by the driver and generating random numbers by the application. A hash table is used to store the obtained random numbers by the driver, and the hash bucket is calculated by the memory address of the obtained random numbers. The addition and deletion of random numbers in the driver uses the kernel's work queue.

[0036] Figure 2 A schematic diagram of an optional process for processing random number inter-thread communication provided in an embodiment of the present application is as follows: Figure 2 As shown in the figure, when the application is initialized, multiple threads will be started, and semaphores and waiting queues will be initialized. Multiple threads include threads for obtaining random numbers and threads for generating random numbers. Each thread needs to obtain a semaphore before it can execute the business process of processing random numbers. Threads that do not obtain a semaphore will sleep in the waiting queue, but will not be blocked in the waiting queue. Only when the semaphore is released, other threads sleeping in the waiting queue will be awakened and obtain the semaphore.

[0037] Through the above steps S102 to S108, multiple threads are started when the application is initialized, and the semaphore and the waiting queue are initialized, wherein the multiple threads include a random number acquisition thread and a random number generation thread; the random number acquisition thread obtains the semaphore and executes the business process of processing the random number; when there is a thread that has not obtained the semaphore among the multiple threads, the thread is put to sleep in the waiting queue; when the semaphore of each thread among the multiple threads is released, the thread sleeping in the waiting queue is awakened and the semaphore is obtained, thereby solving the problem of low efficiency in searching for random numbers and storing random numbers, and the process of using random numbers and obtaining random numbers adopts a multi-threaded design, which improves the running efficiency and performance of the program.

[0038] In an exemplary embodiment, after starting multiple threads when the application is initialized and initializing the semaphore and the waiting queue, the method further includes:

[0039] S11, when the ring buffer is empty, the generated random number is stored in the ring buffer, and the thread for obtaining the random number is dormant and waits in the waiting queue, wherein the ring buffer storage area is used to store the random number;

[0040] S12, when the ring buffer is full, the random number generating thread no longer puts random numbers into the ring buffer.

[0041] In this embodiment, Figure 3 A schematic diagram of an optional process of storing random numbers in a ring buffer provided in an embodiment of the present application is shown in FIG. Figure 3 As shown, the generated random numbers are stored in the ring buffer. When the ring buffer is empty, the random number obtaining thread cannot obtain the random number and needs to sleep and wait; when the ring buffer is full, the random number generating thread can no longer put the random number into the ring buffer.

[0042] In an exemplary embodiment, before the random number thread obtains a semaphore and executes a business process for processing random numbers, the method further includes:

[0043] S21, obtaining a random number used in the driver from a thread generating random numbers in the application layer;

[0044] S22, monitoring the level change of GPIO by setting an interrupt program in the driver, wherein the level change of GPIO indicates that the driver needs to obtain a random number.

[0045] Generally, the random numbers used in the driver are obtained from the thread that generates random numbers in the application layer. If the thread in the application layer uses polling to query whether the driver needs to use random numbers, this method will occupy a lot of CPU resources and reduce the performance of the entire device.

[0046] In this embodiment, an interrupt program is set in the driver to monitor the level change of GPIO (that is, it can be triggered by a high level or a low level). The GPIO level change indicates that the driver needs to obtain a random number. The driver notifies the application program through an asynchronous interrupt to generate a random number and sends it to the driver program. If there is no GPIO level change, the thread of the application layer can perform other tasks without having to keep polling the driver layer to see if a random number is needed.

[0047] Through this embodiment, the threads of the kernel and the application layer communicate asynchronously through GPIO interrupts, rather than the traditional polling method plus sleep method, so that the application layer can asynchronously execute other tasks and improve device performance.

[0048] In an exemplary implementation, after monitoring the level change of the GPIO by setting an interrupt program in the driver, the method further includes:

[0049] S31, when the device receives a network message that cannot be masked by a random number, the network message triggers the GPIO pin to generate a high level, wherein the GPIO pin is at a low level when the device is powered on;

[0050] S32, the GPIO high level triggers the kernel driver to send a signal to the application. After receiving the signal, the application registers an interrupt service function in the thread that generates random numbers.

[0051] In an exemplary implementation, after the application receives the signal, registering the interrupt service function after the thread generating the random number, the method further includes:

[0052] S41, after receiving the interrupt signal, the thread generating the random number executes the interrupt service program to send the updated random number to the kernel to provide masking for the network message.

[0053] In the embodiment of the present application, the GPIO interrupt needs to add a GPIO pin to the hardware circuit. For example, a level jump trigger can be used, that is, a change from a low level to a high level will trigger the GPIO interrupt. When the device is turned on, the GPIO pin is at a low level. When the device receives a network message that cannot be masked by a random number, it means that there is no random number in the kernel, and the message will trigger the pin to generate a high level.

[0054] The high level of GPIO will trigger the kernel driver to send a signal to the application, informing the application that the kernel needs to send a random number. The application registers the interrupt service function in the thread that generates the random number. When the thread gets the interrupt signal, the interrupt service program will be executed to send a new random number to the kernel, thereby providing cover for the network message.

[0055] Through this embodiment, the communication between the driver program obtaining random numbers and the application program generating random numbers uses GPIO interrupts, which allows the application layer to asynchronously execute other tasks, thereby improving device performance.

[0056] In an exemplary embodiment, the method further comprises:

[0057] S51, the driver program stores the random number obtained from the application layer in the driver's hash table, wherein the hash table hash function maps the key value to the storage bucket;

[0058] S52, if the kernel driver fails to obtain the random number of the application layer generation thread three times, then there is a fault in the channel between the kernel driver and the application layer;

[0059] S53, clearing the random numbers stored in the kernel, wherein the clearing work of the kernel work queue is performed by the kernel thread.

[0060] In this embodiment, the random number obtained by the driver from the application layer is stored in the driver's hash table. The hash table is a data structure that uses a hash function to map key values ​​to storage buckets, and the hash bucket size is Volume. The key value in the implementation of this application is the storage address addr of the random number obtained by the kernel driver, which is obtained by the kernel application memory address function. The hash function is: hash (x) = addr% Volume. Since the value obtained by each application for the memory address is different, the calculation of the hash function can greatly reduce hash collisions and improve the speed of the kernel driver to find random numbers.

[0061] Furthermore, if the kernel driver fails to obtain the random number of the application layer's generation thread three times, it means that there is a fault in the channel between the kernel driver and the application layer. At this time, the random numbers stored in the kernel need to be cleared. Since the random data stored in the driver is large, the clearing work will be time-consuming. If the clearing work is performed immediately in the kernel, it will cause the entire device to freeze. Therefore, the kernel's random number clearing work is not suitable for immediate call. After the kernel program completes the main work task, it submits the process of clearing the kernel random number to a low-priority thread for execution. Here, the kernel's work queue is used to hand over the clearing work to the kernel thread for execution.

[0062] Through this embodiment, the kernel driver uses the memory address to obtain the hash bucket, which greatly reduces hash collisions and improves the efficiency of searching for random numbers and storing random numbers.

[0063] According to another aspect of the embodiments of the present application, a management device for implementing the random number management method under the above Linux system is also provided. Figure 5 is a schematic diagram of the structure of an optional random number management device under a Linux system according to an embodiment of the present application, such as Figure 5 As shown, the device may include:

[0064] Initialization unit 502, used to start multiple threads when initializing the application program, and initialize the semaphore and the waiting queue, wherein the multiple threads include a random number acquisition thread and a random number generation thread;

[0065] An execution unit 504 is used to obtain a semaphore from a random number thread and execute a business process for processing random numbers;

[0066] A sleeping unit 506 is configured to, if there is a thread among the multiple threads that has not acquired the semaphore, put the thread to sleep in the waiting queue;

[0067] The first acquisition unit 508 is configured to wake up the thread dormant in the waiting queue and acquire the semaphore when the semaphore of each thread among the multiple threads is released.

[0068] It should be noted that the initialization unit 502 in this embodiment can be used to execute the above step S102, the execution unit 504 in this embodiment can be used to execute the above step S104, the sleep unit 506 in this embodiment can be used to execute the above step S106, and the first acquisition unit 508 in this embodiment can be used to execute the above step S108.

[0069] Through the above module, multiple threads are started when the application is initialized, and the semaphore and the waiting queue are initialized, wherein the multiple threads include a random number acquisition thread and a random number generation thread; the random number acquisition thread obtains the semaphore and executes the business process of processing the random number; when there is a thread that has not obtained the semaphore among the multiple threads, the thread is put to sleep in the waiting queue; when the semaphore of each thread among the multiple threads is released, the thread sleeping in the waiting queue is awakened and the semaphore is obtained, which solves the problem of low efficiency in searching and storing random numbers, and the process of using and obtaining random numbers adopts a multi-threaded design, which improves the running efficiency and performance of the program.

[0070] In an exemplary embodiment, the apparatus further comprises:

[0071] A first storage unit is used for storing the generated random number in the ring buffer when the ring buffer is empty, and the random number acquisition thread is dormant and waiting in the waiting queue, wherein the ring buffer storage area is used for storing random numbers;

[0072] The second storage unit is used for preventing the random number generating thread from putting any more random numbers into the ring buffer when the ring buffer is full.

[0073] In an exemplary embodiment, the apparatus further comprises:

[0074] A second acquisition unit is used to acquire the random number used in the driver from the thread that generates the random number in the application layer;

[0075] The monitoring unit is used to monitor the level change of GPIO by setting an interrupt program in the driver, wherein the GPIO level change represents that the driver needs to obtain a random number.

[0076] In an exemplary embodiment, the apparatus further comprises:

[0077] A trigger unit, configured to trigger a GPIO pin to generate a high level when a network message received by the device cannot be masked by a random number, wherein the GPIO pin is at a low level when the device is powered on;

[0078] The sending unit is used for the GPIO high level to trigger the kernel driver to send a signal to the application program. After receiving the signal, the application program registers an interrupt service function in the thread that generates random numbers.

[0079] In an exemplary embodiment, the apparatus further comprises:

[0080] The masking unit is used for executing an interrupt service program to send an updated random number to the kernel after the thread generating the random number obtains an interrupt signal, thereby providing masking for the network message.

[0081] In an exemplary embodiment, the apparatus further comprises:

[0082] A third storage unit is used for storing the random number obtained by the driver from the application layer in a hash table of the driver, wherein the hash function of the hash table maps the key value to the storage bucket;

[0083] A third acquisition unit is used for determining that a channel between the kernel driver and the application layer has a fault if the kernel driver fails to acquire the random number of the application layer generation thread three times;

[0084] The clearing unit is used to clear the random numbers stored in the kernel, wherein the clearing work of the kernel work queue is performed by the kernel thread.

[0085] It should be noted here that the examples and scenarios implemented by the above-mentioned modules and corresponding steps are the same, but are not limited to the contents disclosed in the above-mentioned embodiments. It should be noted that the above-mentioned modules as part of the device can run in a hardware environment and can be implemented by software or hardware, wherein the hardware environment includes a network environment.

[0086] According to another aspect of the embodiments of the present application, a storage medium is further provided. Optionally, in this embodiment, the storage medium can be used to execute the program code of any of the random number management methods under Linux system in the embodiments of the present application.

[0087] Optionally, in this embodiment, the storage medium is configured to store program codes for executing the following steps:

[0088] S1, when the application is initialized, multiple threads are started to initialize the semaphore and the waiting queue, wherein the multiple threads include a random number acquisition thread and a random number generation thread;

[0089] S2, obtain the random number thread to obtain the semaphore and execute the business process of processing random numbers;

[0090] S3, when there is a thread among multiple threads that has not obtained the semaphore, the thread is put to sleep in the waiting queue;

[0091] S4, when the semaphore of each thread in the multiple threads is released, wake up the thread sleeping in the waiting queue and obtain the semaphore.

[0092] Optionally, the specific examples in this embodiment may refer to the examples described in the above embodiments, which will not be described in detail in this embodiment.

[0093] Among them, computer-readable storage media may include, but are not limited to, any type of disk, including floppy disks, optical disks, DVDs, CD-ROMs, microdrives and magneto-optical disks, ROMs, RAMs, EPROMs, EEPROMs, DRAMs, VRAMs, flash memory devices, magnetic or optical cards, nanosystems (including molecular memory ICs), or any type of medium or device suitable for storing instructions and / or data.

[0094] According to another aspect of the embodiment of the present application, an electronic device for implementing the random number management method under the above Linux system is also provided. The electronic device can be a server, a terminal, or a combination thereof.

[0095] Figure 6 is a schematic diagram of the structure of an optional electronic device according to an embodiment of the present application, such as Figure 6 As shown, it includes a processor 602, a communication interface 604, a memory 606 and a communication bus 608, wherein the processor 602, the communication interface 604, and the memory 606 communicate with each other through the communication bus 608, wherein,

[0096] Memory 606, used to store computer programs;

[0097] The processor 602 is used to implement the following steps when executing the computer program stored in the memory 706:

[0098] S1, when the application is initialized, multiple threads are started to initialize the semaphore and the waiting queue, wherein the multiple threads include a random number acquisition thread and a random number generation thread;

[0099] S2, obtain the random number thread to obtain the semaphore and execute the business process of processing random numbers;

[0100] S3, when there is a thread among multiple threads that has not obtained the semaphore, the thread is put to sleep in the waiting queue;

[0101] S4, when the semaphore of each thread in the multiple threads is released, wake up the thread sleeping in the waiting queue and obtain the semaphore.

[0102] Optionally, the communication bus may be a PCI (Peripheral Component Interconnect) bus, or an EISA (Extended Industry Standard Architecture) bus, etc. The communication bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 6The communication interface is used for communication between the electronic device and other devices.

[0103] The memory may include RAM, or may include non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.

[0104] As an example, the memory 606 may include, but is not limited to, the initialization unit 502, the execution unit 504, the sleep unit 506, and the first acquisition unit 508 in the random number management device under the Linux system. In addition, other module units in the random number management device under the Linux system may also be included but are not limited to, which will not be repeated in this example.

[0105] The above-mentioned processor can be a general-purpose processor, which can include but not be limited to: CPU (Central Processing Unit), NP (Network Processor), etc.; it can also be DSP (Digital Signal Processing), ASIC (Application Specific Integrated Circuit), FPGA (Field-Programmable Gate Array) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.

[0106] Optionally, the specific examples in this embodiment may refer to the examples described in the above embodiments, and this embodiment will not be described in detail here.

[0107] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described order of actions, because according to the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.

[0108] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0109] In the several embodiments provided in the present application, it should be understood that the disclosed devices can be implemented in other ways. For example, the device embodiments described above are only schematic, such as the division of the units, which is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some service interfaces, and the indirect coupling or communication connection of devices or units can be electrical or other forms.

[0110] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0111] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0112] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable memory. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a memory, including a number of instructions to enable a computer device (which can be a personal computer, server or network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned memory includes: U disk, read-only memory (ROM), random access memory (RAM), mobile hard disk, disk or optical disk and other media that can store program code.

[0113] A person of ordinary skill in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by instructing related hardware through a program, and the program can be stored in a computer-readable memory, which can include: a flash drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.

[0114] The above is only an exemplary embodiment of the present disclosure, and the scope of the present disclosure cannot be limited thereto. That is, any equivalent changes and modifications made according to the teachings of the present disclosure are still within the scope of the present disclosure. After considering the specification and practicing the disclosure here, those skilled in the art will easily think of the implementation scheme of the present disclosure. This application is intended to cover any modification, use or adaptation of the present disclosure, which follows the general principles of the present disclosure and includes common knowledge or customary technical means in the technical field not recorded in the present disclosure. The description and examples are regarded as exemplary only, and the scope and spirit of the present disclosure are defined by the claims.

[0115] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0116] It will be easily understood by those skilled in the art that the above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.

Claims

1. A random number management method under Linux system, characterized in that: include: When the application is initialized, multiple threads are started to initialize the semaphore and the waiting queue, wherein the multiple threads include a random number acquisition thread and a random number generation thread; Get the random number thread to get the semaphore and execute the business process of processing random numbers; If there is a thread among the multiple threads that has not acquired the semaphore, putting the thread to sleep in the waiting queue; When the semaphore of each of the multiple threads is released, the thread sleeping in the waiting queue is awakened and the semaphore is acquired.

2. The random number management method under Linux system as claimed in claim 1, characterized in that: When the application is initialized, multiple threads are started, and after the semaphores and waiting queues are initialized, the method further includes: When the ring buffer is empty, the generated random number is stored in the ring buffer, and the random number acquisition thread sleeps and waits in the waiting queue, wherein the ring buffer storage area is used to store random numbers; When the ring buffer is full, the random number generating thread no longer puts random numbers into the ring buffer.

3. The random number management method under Linux system as claimed in claim 1, characterized in that: Before the random number acquisition thread obtains a semaphore and executes a business process for processing random numbers, the method further includes: Get the random number used in the driver from the thread that generates random numbers in the application layer; An interrupt program is set in the driver to monitor the level change of GPIO, wherein the GPIO level change indicates that the driver needs to obtain a random number.

4. The random number management method under Linux system as claimed in claim 3, characterized in that: After monitoring the level change of the GPIO by setting an interrupt program in the driver, the method further includes: When the device receives a network message that cannot be masked by a random number, the network message triggers the GPIO pin to generate a high level, wherein the GPIO pin is at a low level when the device is turned on; The GPIO high level triggers the kernel driver to send a signal to the application program, and the application program registers an interrupt service function in the thread generating random numbers after receiving the signal.

5. The random number management method under Linux system as claimed in claim 4, characterized in that: After the application receives the signal, registering an interrupt service function after the thread generating the random number, the method further comprises: After the thread generating the random number obtains the interrupt signal, the interrupt service program is executed to send the updated random number to the kernel to provide masking for the network message.

6. The random number management method under Linux system as claimed in claim 3, characterized in that: The method further comprises: The random number obtained by the driver from the application layer is stored in the driver's hash table, wherein the hash table hash function maps the key value to the storage bucket; If the kernel driver fails to obtain the random number of the application layer's generation thread three times, there is a fault in the channel between the kernel driver and the application layer; The random numbers stored in the kernel are cleared, wherein the clearing work of the kernel work queue is performed by the kernel thread.

7. A random number management device under Linux system, characterized in that: include: An initialization unit, used to start multiple threads when the application is initialized, and initialize the semaphore and the waiting queue, wherein the multiple threads include a random number acquisition thread and a random number generation thread; An execution unit is used to obtain a semaphore from a random number thread and execute a business process for processing random numbers; A sleeping unit, configured to put the thread to sleep in the waiting queue if there is a thread among the multiple threads that has not acquired the semaphore; The first acquisition unit is used to wake up the thread sleeping in the waiting queue and acquire the semaphore when the semaphore of each thread among the multiple threads is released.

8. The random number management method under Linux system as claimed in claim 7, characterized in that: The device also includes: A first storage unit is used to store the generated random number in the ring buffer when the ring buffer is empty, and the random number acquisition thread sleeps and waits in the waiting queue, wherein the ring buffer storage area is used to store random numbers; The second storage unit is used for preventing the random number generating thread from putting any more random numbers into the ring buffer when the ring buffer is full.

9. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored program, wherein the program executes the method according to any one of claims 1 to 6 when executed.

10. An electronic device comprising a memory and a processor, characterized in that: A computer program is stored in the memory, and the processor is configured to execute the method according to any one of claims 1 to 6 through the computer program.