Thread processing method and device, electronic equipment and readable medium

By responding to the calling command of the specified system call in the target program, determining the thread's namespace identity and generating a globally unique thread identity, the problem of thread PID duplication is solved, resulting in the inability to accurately find thread-related information, and the precise search of thread information is achieved.

CN120010958APending Publication Date: 2025-05-16龙芯中科(成都)技术有限公司
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411930875.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-25
Publication Date
2025-05-16

AI Technical Summary

Technical Problem

In the prior art, the PID duplicate name of a thread makes it impossible to accurately find thread-related information.

Method used

By responding to the calling command specified in the target program, the namespace identity of the thread is determined, and based on this, a globally unique thread identity is generated, and an information index item including the thread identity is set for the thread, so as to find the information index item of the thread identity through the thread identity.

Benefits of technology

Ensure that thread identification is globally unique, avoiding the rename of searching for different threads, thereby avoiding the problem of not being able to accurately find thread-related information.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120010958A_ABST
    Figure CN120010958A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a thread processing method and device, electronic equipment and a readable medium, and relates to the technical field of computers.The method comprises the steps that in response to a calling command called by any specified system in a target program, a namespace identifier of a thread created by calling of the specified system is determined; a specified system call is used to create a system call. Generating a thread identifier for the thread based on the namespace identifier; the thread identifier is a globally unique identifier. And setting an information index entry including the thread identifier for the thread to search the information index entry of the thread through the thread identifier so as to obtain the thread related information of the thread. Therefore, due to the fact that the thread identification is globally unique, namely the search basis of the thread related information is not repeated, the situation that the search basis of different threads is duplicated in name is avoided, and the problem that the thread related information cannot be accurately searched can be avoided.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a thread processing method, device, electronic device and readable medium. Background Art

[0002] At present, in order to create a new thread, a call command is often set in the program code to call a specified system call and then create a system call. During the execution of the program, if a call command is executed, it is necessary to trigger the call of the specified system call to create a new thread. When a new thread is created, a thread identifier (ProcessIdentifier, PID) is assigned to the thread.

[0003] In the prior art, the thread PID is used as the search basis, and the corresponding thread related information needs to be searched based on the thread PID. However, in some special environments, sometimes the PIDs of different threads have the same name, that is, the search basis has the same name, which makes it impossible to accurately find the thread related information. Summary of the invention

[0004] The embodiments of the present invention provide a thread processing method, device, electronic device and readable medium, which can solve the problem that thread-related information cannot be accurately found.

[0005] In order to solve the above problem, an embodiment of the present invention discloses a thread processing method, which includes:

[0006] In response to a call command of any specified system call in the target program, determining a namespace identifier of a thread created by the specified system call; the specified system call is used to create a system call;

[0007] Generate a thread identifier for the thread based on the namespace identifier; the thread identifier is a globally unique identifier;

[0008] An information index item including the thread identifier is set for the thread, so as to search for the information index item of the thread through the thread identifier, thereby obtaining thread-related information of the thread.

[0009] Optionally, determining the namespace identifier of the thread created by the specified system call includes:

[0010] In the case where it is detected that the calling parameters of the designated system call include a newly created parameter, a preset value is added to the current namespace identifier to obtain an update identifier; the newly created parameter is used to indicate the creation of a new namespace for the thread;

[0011] Determine the update identifier as the namespace identifier of the thread;

[0012] Then, generating a thread identifier for the thread based on the namespace identifier includes:

[0013] Obtaining a thread descriptor PID assigned to the thread, and obtaining a namespace identifier of the thread;

[0014] The thread identifier is generated based on the PID and the namespace identifier.

[0015] Optionally, setting an information index item including the thread identifier for the thread includes:

[0016] Obtain thread related information defined for the thread as target information;

[0017] An information index item including the thread identifier and the target information is generated for the thread.

[0018] Optionally, the method further comprises:

[0019] Obtaining an original thread name set for the thread; the original thread name is used to represent the thread name defined for the specified system call;

[0020] Composing a target thread name according to the original thread name and the namespace identifier of the thread;

[0021] Modify the thread name of the thread to the target thread name;

[0022] Then, obtaining the namespace identifier of the thread includes:

[0023] The namespace identifier is obtained from the thread name.

[0024] Optionally, forming a target thread name according to the original thread name and the namespace identifier of the thread includes:

[0025] Converting the namespace identifier into character form;

[0026] According to a preset format, the namespace identifier in character form is integrated with the original thread name to obtain the target thread name.

[0027] Optionally, the method further comprises:

[0028] In response to the name acquisition request, acquiring the current thread name of the thread;

[0029] The namespace identifier of the thread carried in the current thread name is cleared to obtain the original thread name.

[0030] Optionally, the method further comprises:

[0031] In response to the name modification request, obtaining a new thread name carried in the name modification request;

[0032] The new thread name and the namespace identifier of the thread are integrated into the thread name of the thread.

[0033] On the other hand, an embodiment of the present invention discloses a thread processing device, the device comprising:

[0034] A determination module, configured to determine, in response to a call command of any specified system call in the target program, a namespace identifier of a thread created by the specified system call; the specified system call is used to create a system call;

[0035] A first generating module, configured to generate a thread identifier for the thread based on the namespace identifier; the thread identifier is a globally unique identifier;

[0036] The first setting module is used to set information index items including the thread identifier for the thread, so as to search for the information index items of the thread through the thread identifier, thereby obtaining thread-related information of the thread.

[0037] Optionally, the determining module is specifically configured to:

[0038] In the case where it is detected that the calling parameters of the designated system call include a newly created parameter, a preset value is added to the current namespace identifier to obtain an update identifier; the newly created parameter is used to indicate the creation of a new namespace for the thread;

[0039] Determine the update identifier as the namespace identifier of the thread;

[0040] The first generating module is specifically used for:

[0041] Obtaining a thread descriptor PID assigned to the thread, and obtaining a namespace identifier of the thread;

[0042] The thread identifier is generated based on the PID and the namespace identifier.

[0043] Optionally, the first setting module is specifically used to:

[0044] Obtain thread related information defined for the thread as target information;

[0045] An information index item including the thread identifier and the target information is generated for the thread.

[0046] Optionally, the device further comprises:

[0047] A first acquisition module is used to acquire an original thread name set for the thread; the original thread name is used to represent the thread name defined for the specified system call;

[0048] A second generation module, used for forming a target thread name according to the original thread name and the namespace identifier of the thread;

[0049] A second setting module, used for modifying the thread name of the thread to the target thread name;

[0050] The first generating module is further specifically used to: obtain the namespace identifier from the thread name.

[0051] Optionally, the second generating module is specifically used to:

[0052] Converting the namespace identifier into character form;

[0053] According to a preset format, the namespace identifier in character form is integrated with the original thread name to obtain the target thread name.

[0054] Optionally, the device further comprises:

[0055] A second acquisition module, configured to acquire the current thread name of the thread in response to the name acquisition request;

[0056] The clearing module is used to clear the namespace identifier of the thread carried in the current thread name to obtain the original thread name.

[0057] Optionally, the device further comprises:

[0058] A third acquisition module, configured to respond to the name modification request and acquire a new thread name carried in the name modification request;

[0059] An integration module is used to integrate the new thread name and the namespace identifier of the thread into the thread name of the thread.

[0060] On the other hand, an embodiment of the present invention discloses an electronic device, comprising: a processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface communicate with each other through the communication bus; the memory is used to store at least one executable instruction, and the executable instruction enables the processor to execute the aforementioned method.

[0061] The embodiment of the present invention further discloses a machine-readable medium on which instructions are stored. When executed by one or more processors, the processors are enabled to execute the method described above.

[0062] The embodiment of the present invention includes the following advantages: In the thread processing method provided by the embodiment of the present invention, in response to the call command of any specified system call in the target program, the namespace identifier of the thread created by the specified system call is determined; the specified system call is used to create the system call. A thread identifier is generated for the thread based on the namespace identifier; the thread identifier is a globally unique identifier. An information index item including the thread identifier is set for the thread, so that the information index item of the thread is searched through the thread identifier, thereby obtaining the thread-related information of the thread. In this way, since the thread identifier is globally unique, that is, the search basis for thread-related information is not repeated, the situation where the search basis for different threads has the same name is avoided, and therefore, the problem of not being able to accurately find the thread-related information can be avoided. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0064] Figure 1 is a flowchart of a thread processing method provided by an embodiment of the present invention;

[0065] Figure 2 This is a schematic diagram of thread name setting provided by an embodiment of the present invention;

[0066] Figure 3 is a block diagram of a thread processing device provided by an embodiment of the present invention;

[0067] Figure 4 It is a structural schematic diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0068] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0069] Figure 1 is a flowchart of a thread processing method provided by an embodiment of the present invention. Figure 1 As shown, the thread processing method may include the following steps:

[0070] Step 101: In response to a call command of any designated system call in a target program, determine a namespace identifier of a thread created by the designated system call; the designated system call is used to create a system call.

[0071] Step 102: Generate a thread identifier for the thread based on the namespace identifier; the thread identifier is a globally unique identifier.

[0072] Step 103: setting an information index item including the thread identifier for the thread, so as to search for the information index item of the thread through the thread identifier, thereby obtaining thread-related information of the thread.

[0073] In an embodiment of the present invention, the thread processing method can be applied to an electronic device, the target program can be any program in the electronic device, the designated system call is used to create a system call, the designated system call can be called a thread creation system call, specifically, the designated system call can be a system call provided by the operating system of the electronic device, and each thread in the target program can be created based on the designated system call called in the target program. Among them, a thread is the smallest unit for scheduling by the operating system, also known as a lightweight process or a subprocess, and is the smallest unit of a program execution flow.

[0074] Specifically, the designated system call may be a clone system call provided by the Linux operating system, wherein the clone system call is a system call provided by the operating system kernel for creating threads, which allows the creation of child threads that share different resources with the parent thread. In the case of executing a call command of a designated system call defined in the code of the target program, the above method may be executed to create a thread. The call command defined in the code of the target program is used to create a thread for the target program in the electronic device by triggering a designated system call.

[0075] In some special application scenarios, the system call in the target program will be intercepted by the operating system kernel based on the secure computing mode (Seccomp) mechanism. Specifically, when intercepting the target program, after intercepting a certain system call of the thread, the signal processing function can be replaced with a proxy function, that is, the proxy function is called first, the preset processing logic is performed in the proxy function, and then the signal processing function is called. Exemplarily, it is assumed that the system call number of the system call intercepted in the target program is: __NR_rt_sigaction and __NR_clone. After intercepting the target program calling the sigaction system call for setting the signal processing method, the signal processing function corresponding to the sigaction system call can be replaced with the first proxy function, the first proxy function is called, and the first preset operation is performed in the first proxy function, for example, signal filtering, signal conversion and other operations are performed, and finally the signal processing function corresponding to the sigaction system call is called to respond to the sigaction system call. Among them, the system call number of the sigaction system call is __NR_rt_sigaction. After intercepting the clone system call called by the target program, first use the second proxy function to replace the signal processing function corresponding to the clone system call, and call the second proxy function to perform the second preset operation in the second proxy function, such as information recording and other operations. After the execution is completed, resume the execution of the system call, and then collect and save some information in the thread generated after the system call, and finally jump back to the intercepted target program for execution. Among them, the collected information can be saved in the form of a log, and the collected information can be used to analyze the program running process. The clone system call is the aforementioned specified system call, and the system call number of the clone system call is __NR_clone.

[0076] Furthermore, in the related art, the thread identifier PID of the thread is directly used to set an information index item including the PID of the thread for the thread, so as to search the information index item of the thread through the PID, thereby obtaining the thread-related information of the thread. Specifically, when the interception program calls the signal processing function corresponding to the thread, the PID of the thread is often used as the search basis to search for the thread-related information of the thread according to the PID of the thread. Among them, the thread-related information includes the signal processing function corresponding to the thread. However, when the calling parameters of the specified system call include new parameters, the operating system kernel will create a new PID namespace for the thread again. This will cause the PID of the thread to be the same as the existing PID of the thread corresponding to the target program, and the PIDs of different threads will have the same name. Therefore, when the electronic device needs to find thread-related information, it cannot accurately find the information index item of the thread based on the PID, and thus cannot accurately find the thread-related information.

[0077] Specifically, the thread created by the specified system call refers to the thread created by the specified system call called by the call command executed this time, that is, the thread created this time. The namespace identifier can be used to uniquely identify the PID namespace, and the namespace identifier of the thread created by the specified system call is used to mark the namespace of the created thread. The next time the call command of the specified system call in the target program is responded, the thread can be created again, and the namespace identifier of the re-created thread is used to mark the namespace of the re-created thread.

[0078] Among them, the namespace can be specifically called a PID namespace, which is a feature provided by the Linux operating system kernel. It allows multiple threads to have their own PID space, so that threads in different PID namespaces can have the same PID without conflict. In the same PID namespace, different threads have different PIDs. However, threads in different PID namespaces are allowed to have the same PID, that is, the PID in the PID namespace is locally unique. It can be seen that only based on the thread PID index thread related information, it may be impossible to accurately find the thread related information because the thread PID has the same name as the PID of other threads.

[0079] In an embodiment of the present invention, a globally unique thread identifier can be generated for the created thread in combination with a namespace identifier. The globally unique thread identifier of a thread means that the thread identifiers of other threads of the target program are different from the thread identifier of the thread created this time. In an embodiment of the present invention, in response to any call command of a target program to a specified system call, the above method can be executed by setting different namespace identifiers for different namespaces, and generating globally unique thread identifiers for each thread according to the PID of any thread of the target program and the namespace identifier.

[0080] Furthermore, the globally unique thread identifier generated for the thread in combination with the namespace identifier and thread-related information can be combined into an information index item. The information index item includes the thread identifier and the thread-related information. In this way, when it is necessary to find thread-related information later, the information index item of the thread is found by the thread identifier, and the thread-related information is obtained from the found information index item, thereby realizing the search for thread-related information of the thread based on the thread identifier. Since the thread identifier is globally unique, that is, the search basis for thread-related information is not repeated, the situation where the search basis for different threads has the same name is avoided, and therefore, the problem of not being able to accurately find thread-related information can be avoided.

[0081] In summary, in the thread processing method provided by the embodiment of the present invention, in response to a call command of any specified system call in the target program, the namespace identifier of the thread created by the specified system call is determined; the specified system call is used to create the system call. A thread identifier is generated for the thread based on the namespace identifier; the thread identifier is a globally unique identifier. Information index items including the thread identifier are set for the thread, so that the information index items of the thread are searched through the thread identifier, thereby obtaining thread-related information of the thread. In this way, since the thread identifier is globally unique, that is, the search basis for thread-related information is not repeated, the situation where the search basis for different threads has the same name is avoided, and therefore, the problem of not being able to accurately find the thread-related information can be avoided.

[0082] Optionally, the step of determining the namespace identifier of the thread created by the specified system call may specifically include:

[0083] Step 1011: When it is detected that the calling parameters of the designated system call include a new parameter, a preset value is added to the current namespace identifier to obtain an update identifier; the new parameter is used to indicate the creation of a new namespace for the thread.

[0084] Step 1012: Determine the update identifier as the namespace identifier of the thread.

[0085] In an embodiment of the present invention, the namespace identifier is updated by adding a preset value to the current namespace identifier. The update identifier refers to the updated namespace identifier. After the update is completed, the current namespace identifier becomes the updated identifier. The new parameter can be a CLONE_NEWPID parameter, which is used to indicate that when executing the specified system call, a new PID namespace is created for the created thread. Among them, when a new PID namespace is created, the original namespace still exists. Specifically, the call parameters of the specified system call are the parameters included in the call command, and the parameters included in the call command are passed to the called specified system call. In response to the call command of any specified system call in the code of the executed target program, the operating system kernel can detect the parameters of the call command to detect whether it includes the string "CLONE_NEWPID". If it does, the corresponding instruction is executed to create a new PID namespace and add a preset value to the current namespace identifier. Otherwise, the detection of the parameters of other call commands continues.

[0086] Exemplarily, the clone system call can be intercepted by the operating system kernel based on the seccomp mechanism, and the call parameters of the intercepted clone system call can be detected whether the string "CLONE_NEWPID" is included in the call parameters. Specifically, it can be detected whether the clone_flags parameter of the call command contains the string "CLONE_NEWPID". If it is included, the updated namespace identifier is generated by updating. In the embodiment of the present invention, when the operating system kernel detects that a new parameter is included, the namespace identifier is set for the newly created thread, and then a globally unique thread identifier is set for the thread based on the namespace identifier. In this way, since the namespace identifier can be set by the operating system kernel, it does not depend on the user state environment, but only depends on the operating system kernel, so a globally unique thread identifier can be easily set for the thread.

[0087] The call command in the code of the target program can be keyword matched. If "CLONE_NEWPID" is matched, it can be determined that the call parameter includes the CLONE_NEWPID parameter. Otherwise, the call command in the code continues to be monitored. Exemplarily, assume that the call command of the currently executed specified system call is: int child_pid = clone (child_func, child_stack + STACK_SIZE, CLONE_NEWPID | SIGCHLD, NULL); accordingly, after matching, the call command can be parsed, and the string "CLONE_NEWPID" can be matched from the parsing result. Therefore, it can be determined that the call parameter includes the CLONE_NEWPID parameter. Accordingly, it is necessary to create a new PID namespace for the child thread created this time, and the space size allocated for the child thread is STACK_SIZE. Child_stack represents a pointer to the space allocated for the child thread, SIGCHLD represents sending a SIGCHLD signal to the parent thread when the child thread ends, and NULL represents that no additional data needs to be passed to the created child process.

[0088] Therefore, when it is detected that the call parameters include the newly created parameters, a preset value can be added to the current namespace identifier to obtain an update identifier. The current namespace identifier can represent the identifier of the namespace created most recently. Accordingly, the current namespace identifier is updated to the update identifier, and when other threads are subsequently created using the specified system call including the newly created parameters, a namespace identifier can be generated for the thread based on the updated update identifier obtained after the update.

[0089] Then the updated identifier is determined as the namespace identifier of the thread. In this way, the accuracy of the namespace identifier can be ensured. At the same time, it can be ensured that the updated namespace identifier is used for the new PID namespace, thereby ensuring the uniqueness of the namespace identifiers of different PID namespaces.

[0090] Of course, when it is detected that the call parameters do not include the newly created parameters, for example, when it is necessary to create a child thread that shares resources with the parent thread, the call parameters do not include the newly created parameters. Accordingly, the current namespace identifier can be directly determined as the namespace identifier of the thread. Among them, the current namespace identifier can be a default namespace identifier. Specifically, the default namespace identifier can be represented by a value of 0, that is, the default namespace identifier can be the value at the time of initialization. All threads created by the specified system calls that do not include the newly created parameters in the call parameters are assigned PIDs under the default namespace. Accordingly, all threads created by the specified system calls that do not include the newly created parameters in the call parameters have the namespace identifiers of 0.

[0091] In the embodiment of the present invention, the namespace identifier may also be referred to as a namespace identifier index, and the namespace identifier may be an integer. The preset value is independent of the original value of the current namespace identifier, and the preset value is a preset fixed value.

[0092] In this embodiment, the preset value can be pre-set by the developer. Exemplarily, the preset value can be 1, that is, each time a new namespace identifier is obtained by adding 1 to the current namespace identifier. Specifically, the current namespace identifier can be an initial value at the beginning, for example, it can be 0. Each time it is detected that the parameters include new parameters, the current namespace identifier is added by 1 to obtain an update identifier. In this way, it can be ensured that different PID namespaces have different namespace identifiers. Exemplarily, assuming that the current namespace identifier is 10, the updated namespace identifier is 11 after the preset value is added. Accordingly, 11 can be determined as the namespace identifier of the thread created this time.

[0093] Accordingly, the step of generating a thread identifier for the thread based on the namespace identifier may specifically include:

[0094] Step 1021: Obtain the thread descriptor PID allocated to the thread, and obtain the namespace identifier of the thread.

[0095] Step 1022: Generate the thread identifier based on the PID and the namespace identifier.

[0096] In an embodiment of the present invention, after determining the update identifier of the thread, the namespace identifier of the thread can be recorded. Accordingly, when generating a thread identifier for the thread, the recorded namespace identifier can be obtained. Further, the PID of the thread can also be obtained, so as to generate the thread identifier of the thread based on the PID of the thread and the namespace identifier of the thread. Specifically, when the thread is created using the clone system call, the operating system kernel can assign a PID to the thread. Accordingly, the PID assigned to the thread by the operating system kernel is obtained. Specifically, the PID of the thread and the namespace identifier of the thread can be connected through a specified character to obtain the thread identifier. The specified character can be pre-set by the developer. For example, the specified character can be "+", and the thread identifier can be expressed as: namespace identifier + PID. Exemplarily, assuming that the namespace identifier of the thread is '4', '9', '4', '9', and the PID of the thread is: 001, then the thread identifier can be obtained: 001+'4', '9', '4', '9'.

[0097] In an embodiment of the present invention, when it is detected that the call parameters of the specified system call include newly created parameters, an updated identifier is obtained as the namespace identifier of the thread by adding a preset value to the current namespace identifier. In this way, the namespace identifier of the thread can be avoided from being repeated with the existing namespace identifier. Accordingly, the thread descriptor PID assigned to the thread is obtained, and the namespace identifier of the thread is obtained. Based on the obtained PID and the namespace identifier, a thread identifier is generated. In this way, the thread identifiers of different threads of the target program can be avoided from being repeated.

[0098] Optionally, in some embodiments of the present invention, after the above step of determining the namespace identifier of the thread created by the specified system call, the following steps may also be included:

[0099] Step S21, obtaining an original thread name set for the thread; the original thread name is used to represent the thread name defined for the specified system call.

[0100] Step S22: compose a target thread name according to the original thread name and the namespace identifier of the thread.

[0101] Step S23: modify the thread name of the thread to the target thread name.

[0102] In an embodiment of the present invention, the original thread name may be the thread name defined in the calling command of the thread. The original thread name may be set by the user in the code as needed, or may be assigned by the system by default for the thread created this time according to the default naming rule. This is not limited in the embodiment of the present invention. Specifically, a system call for obtaining the thread name may be called to obtain the original thread name of the thread. Among them, the system call for obtaining the thread name may be a PR_GET_NAME system call, and PR_GET_NAME may be called using the system call number of the PR_GET_NAME system call and the identifier of the thread. Exemplarily, the thread name returned by PR_GET_NAME may be received to obtain the original thread name.

[0103] After obtaining the original thread name, the original thread name and the namespace identifier of the thread can be combined into a target thread name that carries the original thread name and the namespace identifier of the thread. Further, the thread name of the thread can be modified to the target thread name. That is, in the embodiment of the present invention, the namespace identifier of the thread is recorded in the thread name of the thread.

[0104] Accordingly, the above step of obtaining the namespace identifier of the thread may specifically include: Step 1021a, obtaining the namespace identifier from the thread name.

[0105] Specifically, after the above step S23, when a thread identifier is generated for a thread, a system call for obtaining a thread name can be called to obtain the thread name of the thread. The system call for obtaining the thread name can be a PR_GET_NAME system call, and the system call number of the PR_GET_NAME system call and the thread identifier can be used inside the target program to call PR_GET_NAME to obtain the thread name. Exemplarily, the thread name returned by PR_GET_NAME can be received to obtain the thread name, and the carried namespace identifier can be extracted from the obtained thread name. Specifically, the namespace identifier can be located at a specified position of the thread name, and accordingly, the content at the specified position of the thread name can be extracted to obtain the namespace identifier of the thread. The specified position can be pre-defined according to actual conditions, and the embodiment of the present invention does not limit this.

[0106] In an embodiment of the present invention, after determining the namespace identifier of the thread created by the specified system call, the original thread name set for the thread is obtained, and then a thread name carrying the original thread name and the namespace identifier of the thread is generated as the target thread name. The thread name of the thread is set as the target thread name. In this way, the namespace identifier of the thread is recorded in the form of the thread name of the thread. Accordingly, when a thread identifier is generated for a thread based on the namespace identifier of the thread, the namespace identifier can be obtained from the thread name of the thread. In this way, by recording the namespace identifier of the thread through the thread name of the thread, the required namespace identifier can be easily obtained when the thread identifier is subsequently generated.

[0107] Furthermore, by recording the namespace identifier of the thread in its own thread name, when a thread identifier is subsequently generated for the thread, the namespace identifier is obtained from the thread's own thread name. This can avoid confusion between namespace identifiers of different threads and the problem of inaccurate generated thread identifiers to a certain extent.

[0108] Optionally, the step of composing the target thread name according to the original thread name and the namespace identifier of the thread may specifically include:

[0109] Step S22a: convert the namespace identifier into character form.

[0110] Step S22b: Integrate the namespace identifier in character form with the original thread name according to a preset format to obtain the target thread name.

[0111] In an embodiment of the present invention, the namespace identifier may be converted into a string in ASCII format to obtain a namespace identifier in character form. Specifically, the conversion may be performed based on a preset conversion method. For example, the tens and ones digits of the namespace identifier of the thread may be first separated to obtain two digits in the tens and ones digits. Then, the value of the '0' character in the ASCII code (48) is added to the digit in the tens digit to obtain a digit of the character type corresponding to the tens digit, and the value of the '0' character in the ASCII code (48) is added to the digit in the ones digit to obtain a digit of the character type corresponding to the ones digit. Finally, the digits of the character type corresponding to the tens digit and the digits of the character type corresponding to the ones digit are concatenated into a string to obtain a namespace identifier in character form. In an embodiment of the present invention, converting a namespace identifier in digital form into a namespace identifier in character form can facilitate subsequent electronic devices to record. For example, assuming that the namespace identifier is 11, accordingly, the number "1" in the tens place can be added with the value of the '0' character in the ASCII code (48), and the number "49" of the character type corresponding to the tens place can be obtained, and the number "1" in the ones place can be added with the value of the '0' character in the ASCII code (48), and the number "49" of the character type corresponding to the ones place can be obtained. The final namespace identifier in character form can be: {'4','9','4','9'}.

[0112] Further, the preset format can be predefined according to actual needs. For example, the preset format can be: namespace identifier in character form-original thread name. The namespace identifier in character form can be located at a specified position. The specified position can be predefined according to actual needs. For example, the specified position can be the first M bytes. Accordingly, the namespace identifier in character form can be placed in the first M bytes in the preset format, and then the original thread name can be placed in the subsequent position to obtain the target thread name. In an embodiment of the present invention, the returned thread name can be received based on a pointer. When the pointer for receiving the original thread name is passed, the pointer can be moved back by M bytes to reserve M bytes for the namespace identifier of the thread. Wherein, M is a positive integer. For example, M can be 2. Accordingly, the namespace identifier of the thread can be filled in the reserved 2 bytes respectively to obtain a new thread name with the namespace identifier, that is, the target thread name. Accordingly, when extracting the carried namespace identifier from the target thread name, the carried namespace identifier can be obtained by directly reading the first 2 bytes in the target thread name.

[0113] Further, a system call for setting the thread name can be called to set the thread name of the thread to the target thread name. The system call for setting the thread name can be a PR_SET_NAME system call, and the system call number of the PR_SET_NAME system call, the thread identifier and the target thread name can be used to call PR_SET_NAME to set the thread name of the thread to the target thread name. Exemplarily, assuming that the target thread name is '4', '9', '4', '9'-thread1, the thread identifier is child1, and the system call number of the PR_SET_NAME system call is __NR_prctl-XXX, then __NR_prctl-XXX, '4', '9', '4', '9'-001 and child1 can be used as call parameters to call the PR_SET_NAME system call to set the thread name to '4', '9', '4', '9'-thread 1.

[0114] Figure 2 is a schematic diagram of thread name setting provided by an embodiment of the present invention, such as Figure 2 As shown, you can first determine whether the calling parameters of the specified system call include "CLONE_NEWPID". If it does, you can add 1 to the current namespace identifier to get the namespace identifier of the thread. Get the original thread name, concatenate the original thread name with the namespace identifier of the thread to get the target thread name. Set the thread name of the thread to the target thread name. If "CLONE_NEWPID" is not included, you can end this process. Specifically, you can directly determine the current namespace identifier as the namespace identifier of the thread, and perform subsequent processing.

[0115] In the embodiment of the present invention, the namespace identifier of the thread is first converted into a namespace identifier in character form. Then, the namespace identifier in character form is integrated with the original thread name according to a preset format to obtain the target thread name. In this way, the generation efficiency of the target thread name can be ensured to a certain extent.

[0116] Optionally, the above step of setting the information index item including the thread identifier for the thread may specifically include:

[0117] Step 1031: Obtain thread-related information defined for the thread as target information.

[0118] Step 1033: Generate an information index item including the thread identifier and the target information for the thread.

[0119] In an embodiment of the present invention, an information index item can be set in a preset thread information index table, and the thread related information can be information related to the thread, and the thread related information of the thread can be pre-defined by the user in the code of the target program according to actual needs. Specifically, the thread identifier can be used as the key name (key) of the thread unique identifier, and the target information of the thread can be used as the key value (value) to generate a key-value pair as the information index item of the thread. Accordingly, the information index item includes the thread identifier and the target information of the thread.

[0120] The preset thread information index table can be used to record the information index items of each thread in the target program. A memory area can be allocated to the target program as a preset thread information index table during the initialization phase of the target program. Each time a thread is created subsequently, the information index item of the thread can be recorded in the memory area, thereby continuously setting the information index item for the thread information index table. Accordingly, for the created thread, the thread information index table records the information index item of the thread. Accordingly, the thread information index table can record the PID, namespace identifier and thread-related information of the created thread. Accordingly, when it is necessary to manage the thread-related information of the thread later, the thread-related information corresponding to the thread can be searched from the thread information index table based on the thread identifier of the thread, and then the thread-related information found can be processed accordingly. For example, read the thread-related information found, modify the thread-related information found, delete the thread-related information found, and so on.

[0121] In some application scenarios, thread-related information may include a signal processing function of a thread. Accordingly, the signal processing function defined for a thread in the code of the target program may be used as the target information of the thread. Accordingly, the thread information index table may be a signal processing function table, which may record the PID, namespace identifier, and signal processing function of the thread, thereby facilitating the correct calling of the registered signal processing function. Exemplarily, it is assumed that a stack space child stack is defined for a child thread in the code of the target program.

[0122] Then define two signal processing functions sigusr1_handler and child_sigusr1_handler, which are used to process the SIGUSR1 signal received by the main thread and the child thread respectively. Then, in the main function, set the signal processing function of the SIGUSR1 signal for the main thread. Use the clone system call to create a child thread, and use child_func as the entry function of the child thread. In the child_func function, set the signal processing function of the SIGUSR1 signal of the child thread, and keep the thread running. Accordingly, sending SIGUSR1 signals to the main thread and the child thread can be processed by the signal processing functions of the main thread and the child thread respectively. Then the PID, namespace identifier and signal processing function sigusr1_handler of the main thread can be added to the thread information index table as an information index item. The PID, namespace identifier and signal processing function child_sigusr1_handler of the child thread can be added to the thread information index table as an information index item.

[0123] In this way, the signal processing function sigusr1_handler of the main thread can be accurately found based on the PID+namespace identifier of the main thread (i.e., the thread identifier of the main thread), and the signal processing function child_sigusr1_handler of the child thread can be accurately found based on the PID+namespace identifier of the child thread (i.e., the thread identifier of the child thread). In the embodiment of the present invention, the PID namespace of the thread is marked by the namespace identifier, and indexing is performed based on the PID+namespace identifier, which can avoid the problem of being unable to accurately find thread-related information due to repeated indexes. Assuming that a new PID namespace is created for the child thread, since the two are in different namespaces, the operating system kernel may assign the same PID to the main thread and the child thread, assuming that the PIDs of the main thread and the child thread are both 001. In the related art, an information index item is directly created for the main thread: key=001, value=sigusr1_handler; and an information index item is created for the child thread: key=001, value=child_sigusr1_handler. In this way, the keys of the two information index items are the same. When searching for thread-related information of the child thread, the information index item of the child thread cannot be accurately determined, and therefore, the thread-related information of the child thread cannot be accurately found. In the embodiment of the present invention, the namespace identifier is determined. Assuming that the namespace identifier of the main thread is: '4', '8', '4', '8', and the namespace identifier of the child thread is: '4', '9', '4', '9', the information index item is created for the main thread in the embodiment of the present invention: key = 001 + '4', '8', '4', '8', value = sigusr1_handler; and the information index item is created for the child thread: key = 001 + '4', '9', '4', '9', value = child_sigusr1_handler. The keys of the two information index items are different. When searching for thread-related information of the child thread, the information index item of the child thread can be accurately determined. Therefore, the thread-related information of the child thread can be accurately found.

[0124] For example, when the interception program is running, if you need to find the signal processing function of a certain thread, you can get the PID of the thread, call the PR_GET_NAME system call to get the thread name of the thread, and then extract the namespace identifier of the thread from the obtained thread name. Accordingly, based on the PID+namespace identifier of the thread, you can index the signal processing function table to find the signal processing function corresponding to the thread.

[0125] In an embodiment of the present invention, thread-related information defined for a thread is first obtained as target information. Then, an information index item including a thread identifier and target information is generated for the thread, which can facilitate subsequent searches for the target information of the thread. Further, the information index item is set in a preset thread information index table. In this way, the thread information index table can record the information index items of each thread of the target program, thereby facilitating subsequent searches for thread-related information based on the thread information index table according to the thread identifier of the thread.

[0126] Optionally, the embodiment of the present invention may further include the following steps:

[0127] Step S31: In response to the name acquisition request, the current thread name of the thread is acquired.

[0128] Step S32: Clear the namespace identifier of the thread carried in the current thread name to obtain the original thread name.

[0129] Specifically, step S31 may be executed when the name acquisition request is an external name acquisition request, and the external name acquisition request may be a name acquisition request triggered by other programs other than the target program. Specifically, the name acquisition request inserted in the code of the target program may be referred to as an internal name acquisition request, and other name acquisition requests other than the internal name acquisition request are external name acquisition requests. In an embodiment of the present invention, code for implementing the thread processing method provided in an embodiment of the present invention may be inserted into the code of the target program. The code for implementing the thread processing method provided in an embodiment of the present invention may be referred to as a thread processing code, and accordingly, the thread processing code includes an internal name acquisition request, which may be used to request a call to a system call for obtaining a thread name. When the original thread name needs to be obtained as described above, or when the thread name is required when generating a thread identifier, the internal name acquisition request may be executed to directly call the PR_GET_NAME system call.

[0130] Further, in the case of receiving a name acquisition request, the source of the name acquisition request can be determined. If the name acquisition request belongs to a thread processing code, it can be determined that the name acquisition request is an internal name acquisition request. Otherwise, it can be determined that the name acquisition request is an external name acquisition request. Accordingly, in the case where the name acquisition request is an external name acquisition request, the PR_GET_NAME system call called by the external name acquisition request can be intercepted, and then the signal processing function corresponding to the PR_GET_NAME system call is replaced with a first preset proxy function, and the first preset proxy function is called. In the first preset proxy function, the namespace identifier attached to the currently acquired thread name is cleared to obtain the original thread name, and then the signal processing function corresponding to the PR_GET_NAME system call is called to return the original thread name to the requester of the external name acquisition request. Among them, the requester of the external name acquisition request can be the program that generates the external name acquisition request. Specifically, the first M bytes of the current thread name acquired can be deleted, and then the remaining part can be determined as the original thread name and returned to the requester of the external name acquisition request.

[0131] In the embodiment of the present invention, recording the namespace identifier of the created thread in the thread name is equivalent to modifying the thread name. Accordingly, in response to the external name acquisition request, the current thread name of the thread is acquired. Then, the namespace identifier carried in the current thread name is cleared to obtain the original thread name. The original thread name is returned to the requester of the external name acquisition request. In this way, it is possible to avoid returning the namespace identifier to the external requester, so that the external requester can obtain the unmodified original thread name, and the modification is imperceptible to the external requester, ensuring that the modification operation will not affect the external requester.

[0132] Optionally, the embodiment of the present invention may further include the following steps:

[0133] Step S41: In response to a name modification request, a new thread name carried in the name modification request is obtained.

[0134] Step S42: Integrate the new thread name and the namespace identifier of the thread into the thread name of the thread.

[0135] In an embodiment of the present invention, a name modification request can be used to call a system call for modifying a thread name. Exemplarily, the system call for modifying a thread name can be a PR_SET_NAME system call. Accordingly, in the case of receiving a name modification request, the new thread name carried by the name modification request can be extracted. Among them, the new thread name carried by the name modification request can be pre-set by the user as needed, and the embodiment of the present invention does not limit this. A thread name carrying the new thread name and the namespace identifier of the thread is composed according to the new thread name and the namespace identifier of the thread. Among them, the specific combination method can refer to the above-mentioned implementation method of composing a thread name according to the original thread name and the namespace identifier of the thread, which will not be repeated here.

[0136] Accordingly, the PR_SET_NAME system call called by the name modification request can be intercepted, the signal processing function corresponding to the PR_SET_NAME system call can be replaced with a second preset proxy function, and the second preset proxy function can be called. In the second preset proxy function, the thread name carrying the new thread name and the thread namespace identifier is used as the parameter of the intercepted PR_SET_NAME system call, thereby controlling the thread name to be modified to the thread name carrying the new thread name and the thread namespace identifier.

[0137] In an embodiment of the present invention, in response to a name modification request, a new thread name carried in the name modification request is obtained. The new thread name and the namespace identifier of the thread are integrated into the thread name of the thread. In this way, it can be ensured that when the thread name is modified, the thread name still carries the namespace identifier of the thread, thereby avoiding the problem of namespace identifier loss caused by thread name modification.

[0138] Reference Figure 3 , shows a block diagram of a thread processing device provided by an embodiment of the present invention, such as Figure 3 As shown, the thread processing device may specifically include:

[0139] The determination module 201 is used to determine the namespace identifier of the thread created by any specified system call in response to a call command of the target program; the specified system call is used to create a system call;

[0140] A first generating module 202 is used to generate a thread identifier for the thread based on the namespace identifier; the thread identifier is a globally unique identifier;

[0141] The first setting module 203 is used to set information index items including the thread identifier for the thread, so as to search for the information index items of the thread through the thread identifier, thereby obtaining thread-related information of the thread.

[0142] Optionally, the determining module 201 is specifically configured to:

[0143] In the case where it is detected that the calling parameters of the designated system call include a newly created parameter, a preset value is added to the current namespace identifier to obtain an update identifier; the newly created parameter is used to indicate the creation of a new namespace for the thread;

[0144] Determine the update identifier as the namespace identifier of the thread;

[0145] The first generating module 202 is specifically used for:

[0146] Obtaining a thread descriptor PID assigned to the thread, and obtaining a namespace identifier of the thread;

[0147] The thread identifier is generated based on the PID and the namespace identifier.

[0148] Optionally, the first setting module 203 is specifically configured to:

[0149] Obtain thread related information defined for the thread as target information;

[0150] An information index item including the thread identifier and the target information is generated for the thread.

[0151] Optionally, the device further comprises:

[0152] A first acquisition module is used to acquire an original thread name set for the thread; the original thread name is used to represent the thread name defined for the specified system call;

[0153] A second generation module, used for forming a target thread name according to the original thread name and the namespace identifier of the thread;

[0154] A second setting module, used for modifying the thread name of the thread to the target thread name;

[0155] The first generating module 202 is further specifically configured to obtain the namespace identifier from the thread name.

[0156] Optionally, the second generating module is specifically used to:

[0157] Converting the namespace identifier into character form;

[0158] According to a preset format, the namespace identifier in character form is integrated with the original thread name to obtain the target thread name.

[0159] Optionally, the device further comprises:

[0160] A second acquisition module, configured to acquire the current thread name of the thread in response to the name acquisition request;

[0161] The clearing module is used to clear the namespace identifier of the thread carried in the current thread name to obtain the original thread name.

[0162] Optionally, the device further comprises:

[0163] A third acquisition module, configured to respond to the name modification request and acquire a new thread name carried in the name modification request;

[0164] An integration module is used to integrate the new thread name and the namespace identifier of the thread into the thread name of the thread.

[0165] In summary, in the thread processing device provided by the embodiment of the present invention, in response to the call command of any specified system call in the target program, the namespace identifier of the thread created by the specified system call is determined; the specified system call is used to create the system call. A thread identifier is generated for the thread based on the namespace identifier; the thread identifier is a globally unique identifier. An information index item including the thread identifier is set for the thread, so that the information index item of the thread is searched through the thread identifier, thereby obtaining the thread-related information of the thread. In this way, since the thread identifier is globally unique, that is, the search basis for thread-related information is not repeated, the situation where the search basis for different threads has the same name is avoided, and therefore, the problem of not being able to accurately find the thread-related information can be avoided.

[0166] Reference Figure 4 , is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. Figure 4 As shown, the electronic device includes: a processor, a memory, a communication interface and a communication bus.

[0167] The processor, the memory and the communication interface communicate with each other through the communication bus; the memory is used to store at least one executable instruction, and the executable instruction enables the processor to execute the thread processing method of the above embodiment. The executable instruction can form a program.

[0168] An embodiment of the present invention provides a machine-readable medium having instructions stored thereon, which, when executed by one or more processors, enables the processors to execute the thread processing method of the aforementioned embodiment.

[0169] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.

[0170] It will be appreciated by those skilled in the art that the embodiments of the present invention may be provided as methods, devices, or computer program products. Therefore, the embodiments of the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the embodiments of the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.

[0171] It should be noted that all actions of acquiring signals, information or data in this application are carried out in compliance with the relevant data protection laws and policies of the country where they are located and with the authorization given by the owner of the corresponding device.

[0172] The embodiments of the present invention are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0173] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing terminal device to operate in a predictable manner, so that the instructions stored in the computer readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0174] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable terminal device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0175] Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.

[0176] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are merely used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations.

[0177] Moreover, the terms "include", "comprises" or any other variation thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device that includes a series of elements includes not only those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of more restrictions, an element defined by the sentence "comprises a ..." does not exclude the presence of other identical elements in the process, method, article or terminal device that includes the element.

[0178] The thread processing method, thread processing device, electronic device and one or more readable media provided by the present invention are introduced in detail above. The principle and implementation mode of the present invention are explained by using specific examples in this article. The description of the above embodiments is only used to help understand the method and core idea of ​​the present invention. At the same time, for those skilled in the art, according to the idea of ​​the present invention, there will be changes in the specific implementation mode and application scope. In summary, the content of this specification should not be understood as limiting the present invention.

Claims

1. A thread processing method, characterized in that: The method comprises: In response to a call command of any specified system call in the target program, determining a namespace identifier of a thread created by the specified system call; the specified system call is used to create a system call; Generate a thread identifier for the thread based on the namespace identifier; the thread identifier is a globally unique identifier; An information index item including the thread identifier is set for the thread, so as to search for the information index item of the thread through the thread identifier, thereby obtaining thread-related information of the thread.

2. The method according to claim 1, characterized in that: The determining of the namespace identifier of the thread created by the specified system call includes: In the case where it is detected that the calling parameters of the designated system call include a newly created parameter, a preset value is added to the current namespace identifier to obtain an update identifier; the newly created parameter is used to indicate the creation of a new namespace for the thread; Determine the update identifier as the namespace identifier of the thread; Then, generating a thread identifier for the thread based on the namespace identifier includes: Obtaining a thread descriptor PID assigned to the thread, and obtaining a namespace identifier of the thread; The thread identifier is generated based on the PID and the namespace identifier.

3. The method according to claim 1, characterized in that The step of setting an information index item including the thread identifier for the thread includes: Obtain thread related information defined for the thread as target information; An information index item including the thread identifier and the target information is generated for the thread.

4. The method according to claim 2, characterized in that: The method further comprises: Obtaining an original thread name set for the thread; the original thread name is used to represent the thread name defined for the specified system call; Composing a target thread name according to the original thread name and the namespace identifier of the thread; Modify the thread name of the thread to the target thread name; Then, obtaining the namespace identifier of the thread includes: The namespace identifier is obtained from the thread name.

5. The method according to claim 4, characterized in that The forming of the target thread name according to the original thread name and the namespace identifier of the thread includes: Converting the namespace identifier into character form; According to a preset format, the namespace identifier in character form is integrated with the original thread name to obtain the target thread name.

6. The method according to claim 4, characterized in that The method further comprises: In response to the name acquisition request, acquiring the current thread name of the thread; The namespace identifier of the thread carried in the current thread name is cleared to obtain the original thread name.

7. The method according to claim 4, characterized in that The method further comprises: In response to the name modification request, obtaining a new thread name carried in the name modification request; The new thread name and the namespace identifier of the thread are integrated into the thread name of the thread.

8. A thread processing device, characterized in that: Applied to an electronic device, the electronic device includes an integrated graphics card and a display, the device includes: A determination module, configured to determine, in response to a call command of any specified system call in the target program, a namespace identifier of a thread created by the specified system call; the specified system call is used to create a system call; A first generating module, configured to generate a thread identifier for the thread based on the namespace identifier; the thread identifier is a globally unique identifier; The first setting module is used to set information index items including the thread identifier for the thread, so as to search for the information index items of the thread through the thread identifier, thereby obtaining thread-related information of the thread.

9. An electronic device, characterized in that: include: A processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface communicate with each other via the communication bus; The memory is used to store executable instructions, and the executable instructions enable the processor to execute the method according to any one of claims 1 to 7.

10. One or more machine-readable media, characterized in that Instructions are stored thereon, which, when executed by one or more processors, cause the processors to perform the method according to any one of claims 1 to 7.

Citation Information

Cited By

  • Chain type recording method and device based on thread management and storage medium

    CN120849234A