A method, apparatus and readable storage medium for processing threads

By employing a master-slave thread pool structure between iSCSI and SCSI commands, the problems of file descriptor contention and reduced lifespan are resolved, and the stability of file descriptor and command lifespans is achieved.

CN115129493BActive Publication Date: 2026-06-26JINAN INSPUR DATA TECH CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
JINAN INSPUR DATA TECH CO LTD
Filing Date
2022-07-22
Publication Date
2026-06-26

AI Technical Summary

Technical Problem

In existing technologies, iSCSI and SCSI commands are processed in the same thread, leading to file descriptor contention and reduced lifespan, and the lifespan of commands cannot be guaranteed in all threads.

Method used

It adopts a thread pool structure with one main thread and multiple slave threads. The main thread handles SCSI protocol commands, and the slave threads handle iSCSI protocol commands. File descriptors are managed uniformly in the main thread to avoid switching between threads.

Benefits of technology

It effectively prevents read/write problems caused by file descriptor calls in multi-threaded environments, ensuring the lifecycle of file descriptors and commands.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115129493B_ABST
    Figure CN115129493B_ABST
Patent Text Reader

Abstract

The application discloses a method, device and readable storage medium for processing threads, and relates to the field of information technology. The method for processing threads comprises the following steps: receiving TGT service starting information, initializing a storage thread pool, receiving a TCP connection, forwarding a file descriptor of the TCP to a main thread, setting the main thread to a running state, and assigning a slave thread to a corresponding target object to run an iSCSI protocol instruction. Since all the file descriptors are run by the main thread, only the instruction needs to be issued when issuing, and the switching of the file descriptor is not needed. Compared with the previous switching mode of the file descriptor, the TCP file descriptor is processed in a thread, the read-write problem caused by the calling of the TCP file descriptor in multiple threads can be prevented, the life cycle of the file descriptor is ensured, and the life cycle of the instruction in all threads is also ensured.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of information technology, and in particular to a method, apparatus and readable storage medium for processing threads. Background Technology

[0002] In recent years, with the development of information technology and the replacement of data, the number of target objects that need to be processed has increased, and therefore the corresponding target-utils (TGT) technology has become increasingly mature. Currently, the TGT service embeds the Small Computer System Interface (SCSI) protocol into the Intel Small Computer System Interface (Intel Small Computer System Interface) protocol, and the parsing of the iSCSI protocol and the SCSI protocol both occur in the same thread.

[0003] In existing technologies, because iSCSI commands for the same target object may be received from different threads, and SCSI commands for the same target need to be processed in the same thread, file descriptors will switch between threads. This may lead to contention for file descriptors between threads and a reduction in the lifespan of file descriptors. Furthermore, while memory allocated for iSCSI and SCSI commands is requested in the same thread, it may be used in different threads, making it impossible to guarantee the lifespan of commands across all threads.

[0004] Given the above-mentioned technologies, finding a method for processing threads that does not affect the lifecycle of protocol instructions and file descriptors is a problem that urgently needs to be solved by those skilled in the art. Summary of the Invention

[0005] The purpose of this application is to provide a method for handling threads, in order to solve the problems that iSCSI commands under the same target object may be received from different threads, and SCSI commands under the same target need to be processed in the same thread. As a result, file descriptors will switch between threads, which may lead to contention for file descriptors between threads and a reduction in the lifespan of file descriptors. In addition, the memory allocated for iSCSI commands and SCSI commands is allocated in the same thread, but used in different threads, and the lifespan of the commands cannot be guaranteed in all threads.

[0006] To address the aforementioned technical problems, this application provides a method for processing threads, comprising:

[0007] Receive TGT service startup information and initialize storage thread pool, which is a pre-established thread pool including one main thread and multiple slave threads;

[0008] Receive a TCP connection and forward the TCP file descriptor to the main thread;

[0009] The main thread is set to the running state, and the slave thread is set to the waiting state. The running state indicates that the main thread can run SCSI protocol instructions but cannot run iSCSI protocol instructions, and the waiting state indicates that the slave thread can run iSCSI protocol instructions.

[0010] The slave thread in the waiting state is assigned to the corresponding target object so that iSCSI protocol instructions can be executed.

[0011] Preferably, after assigning the waiting slave thread to the corresponding target object to execute iSCSI protocol commands, the method further includes:

[0012] Management commands and read / write commands are categorized and placed into different queues;

[0013] The management instructions and the read / write instructions are processed according to a preset priority.

[0014] Preferably, the preset priority is that management commands are greater than read / write commands.

[0015] Preferably, after processing the management instructions and the read / write instructions, the method further includes:

[0016] The SCSI command is sent to the iSCSI protocol layer for packaging and then sent to the control terminal.

[0017] Preferably, after sending the SCSI command to the iSCSI protocol layer for packaging and then sending it to the control terminal, the method further includes:

[0018] Determine if the current operating environment is abnormal;

[0019] If not, then destroy according to the SCSI command in the iSCSI protocol layer;

[0020] If so, return to the step of determining whether the current operating environment is abnormal.

[0021] Preferably, it also includes;

[0022] After detecting that the TCP service connection is disconnected or after receiving the LOGOUT command, the iSCSI protocol layer file descriptor is released after the instructions being executed by the SCSI protocol layer have been processed.

[0023] Preferably, it further includes:

[0024] An alarm will be triggered if an instruction is detected to be interrupted during execution.

[0025] To address the above problems, this application also provides an apparatus for processing threads, comprising:

[0026] The startup module is used to receive TGT service startup information and initialize the storage thread pool, which is a pre-established thread pool including one main thread and multiple slave threads.

[0027] The receiving module is used to receive TCP connections and forward the TCP file descriptors to the main thread;

[0028] The setting module is used to set the main thread to the running state and the slave thread to the waiting state. The running state indicates that the main thread can run SCSI protocol instructions but cannot run iSCSI protocol instructions, and the waiting state indicates that the slave thread can run iSCSI protocol instructions.

[0029] The allocation module is used to allocate the slave thread in the waiting state to the corresponding target object so as to run iSCSI protocol instructions.

[0030] Preferably, the device further includes:

[0031] The classification module is used to categorize management commands and read / write commands and place them into different queues;

[0032] The processing module is used to process the management instructions and the read / write instructions according to a preset priority.

[0033] Preferably, the device further includes:

[0034] The packaging module is used to package SCSI commands into the iSCSI protocol layer and then send them to the control terminal.

[0035] Preferably, the device further includes:

[0036] The judgment module determines whether the current running environment is abnormal. If so, it enables the destruction module; otherwise, it returns to the judgment module.

[0037] The destruction module is used to destroy according to the SCSI instructions in the iSCSI protocol layer.

[0038] Preferably, the device further includes;

[0039] The release module is used to release the iSCSI protocol layer file descriptor after detecting that the TCP service connection is disconnected or after receiving the LOGOUT command, and after waiting for the instructions being executed by the SCSI protocol layer to be completed.

[0040] Preferably, the device further includes:

[0041] The alarm module is used to trigger an alarm if it detects that a command is interrupted during execution.

[0042] To address the aforementioned problems, this application also provides an apparatus for processing threads, including a memory for storing computer programs;

[0043] A processor, used to implement the steps of the method for processing threads as described above when executing the computer program.

[0044] To address the aforementioned problems, this application also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the processing thread method described above.

[0045] The thread processing method provided in this application receives TGT service startup information, initializes a storage thread pool (a pre-established pool including one main thread and multiple slave threads), receives TCP connections, forwards TCP file descriptors to the main thread, sets the main thread to the running state, and sets the slave threads to the waiting state. The waiting slave threads are then assigned to the corresponding target objects to execute iSCSI protocol commands. Since the main thread in the running state cannot execute iSCSI protocol commands, iSCSI protocol commands that need to be executed in different threads are assigned to slave threads. Because all file descriptors are run by the main thread, only commands need to be sent out, without switching file descriptors. Compared to previous methods that involved switching file descriptors, this solution handles TCP file descriptors in a single thread, preventing read / write problems caused by multi-threaded calls to TCP file descriptors, ensuring the lifecycle of file descriptors, and also ensuring the lifecycle of commands in all threads.

[0046] The device and computer-readable storage medium for processing threads provided in this application correspond to the method for processing threads described above, and have the same beneficial effects. Attached Figure Description

[0047] To more clearly illustrate the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0048] Figure 1 A flowchart of a method for processing threads provided in an embodiment of this application;

[0049] Figure 2 A schematic diagram of a device for processing threads provided in an embodiment of this application;

[0050] Figure 3 This is a schematic diagram of an apparatus for processing threads, provided for another embodiment of this application. Detailed Implementation

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

[0052] The core of this application is to provide a method for handling threads, in order to solve the problems that iSCSI commands under the same target object may be received from different threads, and SCSI commands under the same target need to be processed in the same thread. As a result, file descriptors will switch between threads, which may lead to competition for file descriptors between threads and a reduction in the lifespan of file descriptors. In addition, the memory allocated for iSCSI commands and SCSI commands is allocated in the same thread, but used in different threads, and the lifespan of the commands cannot be guaranteed in all threads.

[0053] To enable those skilled in the art to better understand the present application, the present application will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0054] Figure 1 A flowchart of a method for processing threads provided in an embodiment of this application is shown below. Figure 1 As shown, the method includes:

[0055] S10: Receive TGT service startup information and initialize the storage thread pool;

[0056] The storage thread pool is a pre-established pool consisting of one main thread and multiple slave threads. A thread is the smallest unit of computation that the operating system can schedule. It is contained within a process and is the actual unit of operation within that process. A thread refers to a single sequential flow of control within a process. Multiple threads can run concurrently within a process, each executing different tasks in parallel. Threads are also called lightweight processes, but lightweight processes more often refer to kernel threads, while user threads are referred to simply as threads. Threads are the basic unit of independent scheduling and dispatch. Threads can be kernel threads scheduled by the operating system kernel. In this embodiment, the specific definition parameters of the main thread and slave threads are not specifically limited.

[0057] A thread pool is a form of multi-processing thread where tasks are added to a queue during processing, and these tasks are automatically started after the thread is created. Thread pool threads are all background threads. Each thread uses a default stack size, runs at a default priority, and exists within a multi-threaded unit. If a thread is idle in managed code (e.g., waiting for an event), the thread pool will insert another helper thread to keep all processors busy. If all thread pool threads are always busy, but there is pending work in the queue, the thread pool will create another helper thread after a period of time, but the number of threads will never exceed the maximum value. Threads exceeding the maximum value can be queued, but they will only start after other threads have completed. This embodiment does not limit the pre-establishment steps and corresponding methods of the thread pool, and will not elaborate further.

[0058] It should be noted that each file process control block has a file descriptor table, which can be viewed as an array. The elements of this array are pointers to file structures, and the index of this array is the file descriptor. In the source code, `fd` is generally used as the identifier for the file descriptor. In this embodiment, no specific restrictions are placed on the startup information and the specific content of the thread pool initialization, etc.

[0059] S11: Receive TCP connection and forward the TCP file descriptor to the main thread;

[0060] The Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte-stream-based transport layer communication protocol designed to adapt to layered protocol hierarchies supporting multiple network applications. Pairs of processes in host computers connected to different but interconnected computer communication networks rely on TCP for reliable communication services. TCP assumes it can obtain simple, potentially unreliable datagram services from lower-level protocols. In principle, TCP should be able to operate over a variety of communication systems, from hardwired connections to packet-switched or circuit-switched networks. In this embodiment, it is a transport protocol specifically designed to provide reliable end-to-end byte streams over unreliable interconnected networks.

[0061] Each file process control block maintains a file descriptor table, which can be viewed as an array. The elements of this array are pointers to file structures, and the index of this array is the file descriptor. In the source code, `fd` is typically used as the identifier for a file descriptor.

[0062] S12: Set the main thread to running state and the slave thread to waiting state;

[0063] The running state indicates that the main thread can run SCSI protocol commands but cannot run iSCSI protocol commands, while the waiting state indicates that the slave thread can run iSCSI protocol commands.

[0064] It should be noted that in this embodiment, the main thread and the slave thread are set to different states so that the main thread cannot run iSCSI protocol commands and thus the commands are sent to the corresponding slave threads for processing. It should also be noted that there is only one main thread in this embodiment, and the number of slave threads is not limited.

[0065] S13: Assign the waiting slave thread to the corresponding target object so that iSCSI protocol commands can be executed.

[0066] SCSI is a standalone processor standard for system-level interfaces between computers and their peripherals. The SCSI standard defines commands, communication protocols, and the electrical characteristics of entities. Its primary application is in storage devices (such as hard drives and tape drives); however, SCSI can connect to devices including scanners, optical devices (like CDs and DVDs), printers, and more. SCSI commands list supported devices and SCSI peripherals. In this embodiment, the iSCSI protocol refers to the corresponding application requirements and target environment parameters.

[0067] The commands a computer program sends to the computer processor are called "instructions." The lowest-level instruction is a string of 0s and 1s, indicating that an actual operation should be performed. Depending on the instruction type, a specific memory location is called a "register," which contains data that can be retrieved from the instruction or a data storage location. Each programming language generally responds to only a single processor instruction. However, each high-level language, after being programmed, can respond to multiple processor instructions.

[0068] The thread processing method provided in this embodiment receives TGT service startup information, initializes a storage thread pool (a pre-established pool including one main thread and multiple slave threads), receives TCP connections, forwards TCP file descriptors to the main thread, sets the main thread to the running state, and sets the slave threads to the waiting state. The waiting slave threads are then assigned to the corresponding target objects to execute iSCSI protocol commands. Since the main thread in the running state cannot execute iSCSI protocol commands, iSCSI protocol commands that need to be executed in different threads are assigned to slave threads. Because all file descriptors are run by the main thread, only commands need to be sent out, without switching file descriptors. Compared to previous methods that involved switching file descriptors, this solution handles TCP file descriptors in a single thread, preventing read / write problems caused by multi-threaded calls to TCP file descriptors, ensuring the lifecycle of file descriptors, and also ensuring the lifecycle of commands in all threads.

[0069] Considering the subsequent processing of protocol commands, after assigning the waiting slave thread to the corresponding target object to execute iSCSI protocol commands, the following steps are also included:

[0070] Management commands and read / write commands are categorized and placed into different queues;

[0071] Management commands and read / write commands are processed according to preset priorities.

[0072] The default priority is that management commands take precedence over read / write commands.

[0073] A queue is a special type of linear list, unique in that it only allows deletion operations at the front of the list and insertion operations at the back. Like a stack, a queue is a linear list with restricted operations. The end where insertion operations are performed is called the tail, and the end where deletion operations are performed is called the head. A queue is called an empty queue when it contains no elements.

[0074] The data elements of a queue are also called queue elements. Inserting a queue element into a queue is called enqueueing, and deleting a queue element from a queue is called dequeueing. Because a queue only allows insertion at one end and deletion at the other, only the element that entered the queue earliest can be deleted first; therefore, a queue is also called a first-in, first-out (FIFO) linear list. In this embodiment, the specific type of queue and its elements are not specifically limited. It can be understood that the queue elements mentioned in this embodiment refer to the relevant instructions mentioned above.

[0075] This embodiment provides a method for processing subsequent instructions. By classifying management instructions and read / write instructions and placing them in different queues, the management instructions and read / write instructions are processed according to a preset priority. Management instructions are processed first, thereby preventing the problem of management instructions failing to execute within their validity period due to high pressure from read / write instructions during busy periods.

[0076] Considering memory limitations in the storage thread pool, a preferred solution is provided here, which, after processing management and read / write instructions, also includes:

[0077] The SCSI command is sent to the iSCSI protocol layer for packaging and then sent to the control terminal.

[0078] After the SCSI command is packaged at the iSCSI protocol layer and sent to the control terminal, it also includes:

[0079] Determine if the current operating environment is abnormal;

[0080] If not, it will be destroyed according to the SCSI instructions in the iSCSI protocol layer;

[0081] If so, return to the step of determining whether the current operating environment is abnormal.

[0082] It's important to note that a protocol, short for network protocol, is a set of rules that both communicating computers must follow. These rules govern how connections are established and how computers identify each other. Only by adhering to these rules can computers communicate with each other. Its three key elements are: syntax, semantics, and timing. To ensure data travels from source to destination over the network, all parties involved in network communication must follow the same rules, called a protocol. This is ultimately reflected in the format of data packets transmitted over the network. Protocols are often defined in several layers. This layered definition ensures that changes to one layer do not affect other layers. In this embodiment, the layers are the iSCSI protocol layer and the SCSI protocol layer.

[0083] A computer communication network is composed of many interconnected nodes capable of information exchange and processing. For the entire network to function smoothly, each node must adhere to pre-agreed rules regarding data formats, timing, and other aspects. These rules, conventions, or standards established to enable network data exchange are called network protocols. A protocol is an agreement or communication rule designed by two communicating parties to achieve communication.

[0084] In this embodiment, the specific method for determining whether the runtime environment is abnormal is not limited. Generally, it involves collecting relevant parameters from past normal and abnormal operations to build a model, and then making a judgment based on the model. It can be understood that the runtime environment refers to an environment in which semi-compiled runtime code runs on the target machine. The runtime environment is a mode of operation that lies between a compiler and an interpreter.

[0085] Considering the memory requirements of thread pools and the need to prevent instruction interruptions, a preferred solution is provided here, which also includes:

[0086] After detecting a TCP service connection failure or receiving a LOGOUT command, the iSCSI protocol layer releases its file descriptors after the SCSI protocol layer has finished processing the instructions being executed.

[0087] It should be noted that after the instruction is executed, the file descriptor is released to ensure that the memory in the thread pool is not wasted.

[0088] Considering the need for timely handling of instruction exceptions, the following preferred solutions are provided:

[0089] An alarm will be triggered if an instruction is detected to be interrupted during execution.

[0090] In this embodiment, the method of detecting instruction interruption and the specific alarm method are not limited, so as to facilitate users to handle the abnormal interruption status of instruction execution in a timely manner.

[0091] In the above embodiments, the method for processing threads has been described in detail. This application also provides embodiments corresponding to the apparatus for processing threads. It should be noted that this application describes the embodiments of the apparatus from two perspectives: one is based on functional modules, and the other is based on hardware.

[0092] Figure 2 This is a schematic diagram of a device for processing threads provided in an embodiment of this application, as shown below. Figure 2 As shown, the device includes:

[0093] The startup module 10 is used to receive TGT service startup information and initialize the storage thread pool. The storage thread pool is a pre-established thread pool that includes one main thread and multiple slave threads.

[0094] Receive module 11 is used to receive TCP connections and forward TCP file descriptors to the main thread;

[0095] The setting module 12 is used to set the main thread to the running state and the slave thread to the waiting state. The running state indicates that the main thread can run SCSI protocol commands but cannot run iSCSI protocol commands, and the waiting state indicates that the slave thread can run iSCSI protocol commands.

[0096] The allocation module 13 is used to allocate slave threads in the waiting state to the corresponding target objects so that iSCSI protocol commands can be executed.

[0097] Preferably, the device further includes:

[0098] The classification module is used to categorize management commands and read / write commands and place them into different queues;

[0099] The processing module is used to process management commands and read / write commands according to preset priorities.

[0100] Preferably, the device further includes:

[0101] The packaging module is used to package SCSI commands into the iSCSI protocol layer and then send them to the control terminal.

[0102] Preferably, the device further includes:

[0103] The judgment module determines whether the current running environment is abnormal. If so, it enables the destruction module; otherwise, it returns to the judgment module.

[0104] The destruction module is used to destroy components according to SCSI commands in the iSCSI protocol layer.

[0105] Preferably, the device further includes;

[0106] The release module is used to release the iSCSI protocol layer file descriptor after detecting that the TCP service connection has been disconnected or after receiving the LOGOUT command, and after waiting for the instructions being executed by the SCSI protocol layer to be completed.

[0107] Preferably, the device further includes:

[0108] The alarm module is used to trigger an alarm if it detects that a command is interrupted during execution.

[0109] Since the embodiments of the apparatus and the embodiments of the method correspond to each other, please refer to the description of the embodiments of the method for the embodiments of the apparatus and their corresponding beneficial effects, which will not be repeated here.

[0110] The processing thread apparatus provided in this application receives TGT service startup information, initializes a storage thread pool (a pre-established pool including one main thread and multiple slave threads), receives TCP connections, forwards TCP file descriptors to the main thread, sets the main thread to the running state, and sets the slave threads to the waiting state. The waiting slave threads are then assigned to the corresponding target objects to execute iSCSI protocol commands. Since the main thread in the running state cannot execute iSCSI protocol commands, iSCSI protocol commands that need to be executed in different threads are assigned to slave threads. Because all file descriptors are run by the main thread, only commands need to be sent out, without switching file descriptors. Compared to previous methods of switching file descriptors, this scheme handles TCP file descriptors in a single thread, preventing read / write problems caused by multi-threaded calls to TCP file descriptors, ensuring the lifecycle of file descriptors, and also ensuring the lifecycle of commands in all threads.

[0111] Figure 3 A schematic diagram of a processing thread apparatus is provided for another embodiment of this application, as shown below. Figure 3 As shown, the device for processing threads includes: a memory 20 for storing computer programs;

[0112] Processor 21 is configured to implement the steps of the method for processing threads as described in the above embodiments when executing a computer program.

[0113] The device for processing threads provided in this embodiment may include, but is not limited to, smartphones, tablets, laptops, or desktop computers.

[0114] The processor 21 may include one or more processing cores, such as a quad-core processor or an octa-core processor. The processor 21 may be implemented using at least one hardware form selected from Digital Signal Processor (DSP), Field-Programmable Gate Array (FPGA), and Programmable Logic Array (PLA). The processor 21 may also include a main processor and a coprocessor. The main processor, also known as the Central Processing Unit (CPU), is used to process data in the wake-up state; the coprocessor is a low-power processor used to process data in the standby state. In some embodiments, the processor 21 may integrate a Graphics Processing Unit (GPU), which is responsible for rendering and drawing the content to be displayed on the screen. In some embodiments, the processor 21 may also include an Artificial Intelligence (AI) processor, which handles computational operations related to machine learning.

[0115] The memory 20 may include one or more computer-readable storage media, which may be non-transitory. The memory 20 may also include high-speed random access memory and non-volatile memory, such as one or more disk storage devices or flash memory devices. In this embodiment, the memory 20 is used to store at least the following computer program 201, which, after being loaded and executed by the processor 21, is capable of implementing the relevant steps of the processing thread method disclosed in any of the foregoing embodiments. In addition, the resources stored in the memory 20 may also include an operating system 202 and data 203, and the storage method may be temporary or permanent storage. The operating system 202 may include Windows, Unix, Linux, etc. The data 203 may include, but is not limited to, the data involved in the aforementioned processing thread method.

[0116] In some embodiments, the device for processing threads may further include a display screen 22, an input / output interface 23, a communication interface 24, a power supply 25, and a communication bus 26.

[0117] Those skilled in the art will understand that Figure 3 The structure shown does not constitute a limitation on the device for processing threads and may include more or fewer components than illustrated.

[0118] The processing thread apparatus provided in this application includes a memory and a processor. When the processor executes a program stored in the memory, it can implement the following method: the processing thread method involved in the above embodiments.

[0119] The processing device provided in this application includes a memory and a processor. When the program stored in the memory is executed by the processor, it can receive TGT service startup information, initialize a storage thread pool (a pre-established thread pool including one main thread and multiple slave threads), receive TCP connections, forward TCP file descriptors to the main thread, set the main thread to the running state, and set the slave threads to the waiting state. The waiting slave threads are then assigned to the corresponding target objects to execute iSCSI protocol commands. Since the main thread in the running state cannot execute iSCSI protocol commands, iSCSI protocol commands that need to be executed in different threads are assigned to slave threads for execution. Because all file descriptors are run by the main thread, only commands need to be issued during deployment, without switching file descriptors. Compared to previous methods of switching file descriptors, this solution handles TCP file descriptors in a single thread, preventing read / write problems caused by multi-threaded calls to TCP file descriptors, ensuring the lifecycle of file descriptors, and also ensuring the lifecycle of commands in all threads.

[0120] Since the embodiments of the apparatus and the embodiments of the method correspond to each other, please refer to the description of the embodiments of the method for the embodiments of the apparatus and their corresponding beneficial effects, which will not be repeated here.

[0121] Finally, this application also provides an embodiment corresponding to a computer-readable storage medium. The computer-readable storage medium stores a computer program, which, when executed by a processor, implements the steps described in the above method embodiments.

[0122] It is understood that if the methods in the above embodiments are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, 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. This computer software product is stored in a storage medium and executes all or part of the steps of the methods in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0123] Since the embodiments of the readable storage medium portion correspond to the embodiments of the method portion, the embodiments of the apparatus portion are described in the method portion description and will not be repeated here.

[0124] The computer-readable storage medium provided in this application, when executed by a computer program, can achieve the following: receiving TGT service startup information, initializing a storage thread pool (a pre-established thread pool including one main thread and multiple slave threads), receiving TCP connections, forwarding TCP file descriptors to the main thread, setting the main thread to a running state, setting the slave threads to a waiting state, and assigning the waiting slave threads to the corresponding target objects for running iSCSI protocol commands. Since the main thread in the running state cannot execute iSCSI protocol commands, iSCSI protocol commands that need to be executed in different threads are assigned to slave threads for execution. Because all file descriptors are run by the main thread, only commands need to be sent out without switching file descriptors. Compared with previous methods of switching file descriptors, this solution handles TCP file descriptors in a single thread, preventing read / write problems caused by multi-threaded calls to TCP file descriptors, ensuring the lifecycle of file descriptors, and also ensuring the lifecycle of commands in all threads.

[0125] The foregoing has provided a detailed description of a method, apparatus, and computer-readable storage medium for processing threads provided in this application. The various embodiments in the specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to in the method section. It should be noted that those skilled in the art can make several improvements and modifications to this application without departing from the principles of this application, and these improvements and modifications also fall within the protection scope of the claims of this application.

[0126] It should also be noted that, in this specification, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes the element.

Claims

1. A method for handling threads, characterized in that, include: Receive TGT service startup information and initialize storage thread pool, which is a pre-established thread pool including one main thread and multiple slave threads; Receive a TCP connection and forward the TCP file descriptor to the main thread; wherein, all file descriptors are executed by the main thread, and only instructions are sent when they are sent out; The main thread is set to the running state, and the slave thread is set to the waiting state. The running state indicates that the main thread is running SCSI protocol instructions but cannot run iSCSI protocol instructions. The waiting state indicates that the slave thread is running iSCSI protocol instructions. The waiting thread is assigned to the corresponding target object to execute the iSCSI protocol instructions; After detecting that the TCP connection is broken or after receiving the LOGOUT command, the iSCSI protocol layer file descriptor is released after the SCSI protocol layer has finished processing the instructions being executed.

2. The method for processing threads according to claim 1, characterized in that, After assigning the waiting slave thread to the corresponding target object to execute the iSCSI protocol instructions, the method further includes: Management commands and read / write commands are categorized and placed into different queues; The management instructions and the read / write instructions are processed according to a preset priority.

3. The method for processing threads according to claim 2, characterized in that, The preset priority is that the management command is greater than the read / write command.

4. The method for processing threads according to claim 2, characterized in that, After processing the management instructions and the read / write instructions, the process further includes: The SCSI protocol commands are sent to the iSCSI protocol layer for packaging and then sent to the control terminal.

5. The method for processing threads according to claim 4, characterized in that, After sending the SCSI protocol command to the iSCSI protocol layer for packaging and then sending it to the control terminal, the method further includes: Determine if the current operating environment is abnormal; If not, it is destroyed according to the SCSI protocol instructions in the iSCSI protocol layer; If so, return to the step of determining whether the current operating environment is abnormal.

6. The method for processing threads according to claim 1, characterized in that, Also includes: An alarm is triggered after an interruption is detected during instruction execution.

7. An apparatus for processing threads, characterized in that, include: The startup module is used to receive TGT service startup information and initialize the storage thread pool, which is a pre-established thread pool including one main thread and multiple slave threads. The receiving module is used to receive TCP connections and forward the TCP file descriptors to the main thread; wherein, all file descriptors are handled by the main thread, and only instructions are sent when they are sent out. The setting module is used to set the main thread to the running state and the slave thread to the waiting state. The running state indicates that the main thread is running SCSI protocol instructions but cannot run iSCSI protocol instructions. The waiting state indicates that the slave thread is running iSCSI protocol instructions. The allocation module is used to allocate the slave thread in the waiting state to the corresponding target object so as to execute the iSCSI protocol instructions; The release module is used to release the iSCSI protocol layer file descriptor after detecting that the TCP connection is broken or after receiving the LOGOUT command, and after waiting for the instructions being executed by the SCSI protocol layer to be completed.

8. An apparatus for processing threads, characterized in that, Includes memory used to store computer programs; A processor, configured to implement the steps of the method for processing threads as described in any one of claims 1 to 6 when executing the computer program.

9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the steps of the method for processing threads as described in any one of claims 1 to 6.