A method for protecting inter-thread communication using semaphores in Linux systems

By introducing semaphore protection flags into the Linux system, the problems of synchronization failure and false permissions in inter-thread communication are solved, ensuring the reliability and effectiveness of inter-thread communication. The determination method of semaphore protection flags is adopted.

CN114237920BActive Publication Date: 2026-03-10TONG FANG ELECTRONICS SCI & TECH
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-30
Publication Date
2026-03-10

AI Technical Summary

Technical Problem

In Linux systems, semaphore communication between threads suffers from false permissions, leading to synchronization failures and affecting the effectiveness and reliability of thread execution. Existing technologies lack effective solutions.

Method used

By introducing semaphore protection flags between threads, the determination of whether to activate the thread of the communication object is used to ensure the synchronization and reliability between threads. The sem_init, pthread_create, sem_post, and sem_getvalue functions are used for initialization and operation.

Benefits of technology

It effectively solves the synchronization problem and false permission problem in inter-thread communication, ensuring the timeliness and execution reliability of inter-thread communication.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114237920B_ABST
    Figure CN114237920B_ABST
Patent Text Reader

Abstract

This invention discloses a method for protecting inter-thread communication using semaphores in a Linux system, comprising the following steps: After the Linux system starts, semaphores S1~S2 are created. n Create threads T1~T 2n Create inter-thread semaphore protection flags F1~F n n is a positive integer; the semaphores S1~S2 are implemented using the system-provided sem_init function. n The initialization is performed using the system-provided pthread_create function to implement threads T1~T. 2n Initialization uses the system-provided assignment operator to set the inter-thread semaphore protection flags F1~F1. n Initialized to 0. This invention effectively solves the synchronization problem and false permission problem in inter-thread communication by adding an inter-thread semaphore protection flag and further determining whether it is really necessary to activate the thread of the communication object by judging the value of the inter-thread semaphore protection flag, thus ensuring the effectiveness and reliability of inter-thread communication.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of operating system technology, and more specifically, to a method for protecting inter-thread communication using semaphores in a Linux system. Background Technology

[0002] Linux is an embedded operating system with a wide range of applications, including medical instruments, automotive, industrial control, aerospace, and other military fields. Embedded systems are application-centric and customizable, typically consisting of specific functional modules and a computer control module. Embedded Linux systems offer advantages such as low cost, support for multiple hardware platforms, excellent performance, and good network support.

[0003] A thread is the smallest unit of processing by the operating system. A process often contains multiple threads, and these threads share resources, address space, and data segments. Therefore, the synchronization and mutual exclusion of resource access between threads must be considered.

[0004] A semaphore is a software-level approximation of an interrupt mechanism. In operating systems, semaphores employ P and V operations, essentially acting as non-negative integer calculators to control access to shared resources. A P operation decrements the semaphore `sem` by 1, and a V operation increments it by 1. Threads use the semaphore's value to determine whether they have access to the shared resource. However, externally triggered P operations by threads are random. If the thread function corresponding to the V operation for the semaphore `sem` has not completed its first execution, and more than one new P operation is triggered, the thread corresponding to the V operation may miss the response to the newly triggered P operation, leading to false permissions and causing synchronization failures between threads. This often affects the timeliness and reliability of thread execution.

[0005] There are currently no effective solutions to these problems. Summary of the Invention

[0006] To address the aforementioned technical problems in related technologies, this invention proposes a method for protecting inter-thread communication using semaphores in a Linux system, which overcomes the shortcomings of existing technologies.

[0007] To achieve the above-mentioned technical objectives, the technical solution of the present invention is implemented as follows:

[0008] A method for protecting inter-thread communication using semaphores in a Linux system includes the following steps:

[0009] After the S1 Linux system boots up, the application executes and creates semaphores S1~S1. n Create threads T1~T 2n Create inter-thread semaphore protection flags F1~Fn n is a positive integer;

[0010] S2 uses the system-provided sem_init function to implement semaphores S1~S2. n The initialization is performed using the system-provided pthread_create function to implement threads T1~T. 2n Initialization uses the system-provided assignment operator to set the inter-thread semaphore protection flags F1~F1. n Initialize to 0;

[0011] S3 Thread T 2n It is in a blocked state, waiting to be woken up and activated;

[0012] S4 Thread T 2n-1 China F n When the value is not equal to 0, execute S3; thread T 2n-1 China F n Execute S5 when the value is 0;

[0013] S5 will thread T 2n-1 The semaphore protection flag Fn is set to 1, and the semaphore S is protected by the sem_post function provided by the system. n Perform the V operation;

[0014] S6 When thread T 2n-1 S n After the V operation is performed, thread T 2n Activated upon being awakened;

[0015] S7 Thread T 2n The actual content of the function corresponding to the executing thread;

[0016] S8 Thread T 2n The current semaphore S is obtained using the sem_getvalue function provided by the system. n The value is assigned to the semaphore protection flag F. n ;

[0017] S9 If thread T 2n China F n If the value is 0, return to S3 and wait for the thread to be awakened and activated; otherwise, return to S7 and execute thread T. 2n The actual content of its corresponding function.

[0018] Furthermore, n is determined by the actual number of communications between threads.

[0019] Furthermore, the thread T 2n-1 With the thread T 2n Communication between them requires determining the semaphore protection flag F. nIs the value equal to 1?

[0020] Furthermore, the V operation is performed on thread T. 2n-1 superior.

[0021] Furthermore, one semaphore corresponds to two threads.

[0022] The beneficial effects of the present invention are as follows: The communication protection method of the present invention adds a semaphore protection flag between threads. By judging the value of the semaphore protection flag, the threads can further determine whether it is really necessary to activate the thread of the communication object, thereby effectively solving the synchronization problem and false permission problem in inter-thread communication, and ensuring the effectiveness and reliability of inter-thread execution. Attached Figure Description

[0023] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0024] Figure 1 This is a flowchart of a method for protecting inter-thread communication using semaphores in a Linux system according to an embodiment of the present invention. Detailed Implementation

[0025] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. All other embodiments obtained by those skilled in the art based on the embodiments of the present invention are within the scope of protection of the present invention.

[0026] like Figure 1 As shown in the embodiment of the present invention, a method for protecting inter-thread communication using semaphores in a Linux system includes the following steps:

[0027] After the S1 Linux system boots up, the application executes and creates semaphores S1~S1. n Create threads T1~T 2n Create inter-thread semaphore protection flags F1~F n n is a positive integer;

[0028] S2 uses the system-provided sem_init function to implement semaphores S1~S2. n The initialization is performed using the system-provided pthread_create function to implement threads T1~T. 2nInitialization uses the system-provided assignment operator to set the inter-thread semaphore protection flags F1~F1. n Initialize to 0;

[0029] S3 Thread T 2n It is in a blocked state, waiting to be woken up and activated;

[0030] S4 Thread T 2n-1 China F n When the value is not equal to 0, execute S3; thread T 2n-1 China F n Execute S5 when the value is 0;

[0031] S5 will thread T 2n-1 The semaphore protection flag Fn is set to 1, and the semaphore S is protected by the sem_post function provided by the system. n Perform the V operation;

[0032] S6 When thread T 2n-1 S n After the V operation is performed, thread T 2n Activated upon being awakened;

[0033] S7 Thread T 2n The actual content of the function corresponding to the executing thread;

[0034] S8 Thread T 2n The current semaphore S is obtained using the sem_getvalue function provided by the system. n The value is assigned to the semaphore protection flag F. n ;

[0035] S9 If thread T 2n China F n If the value is 0, return to S3 and wait for the thread to be awakened and activated; otherwise, return to S7 and execute thread T. 2n The actual content of its corresponding function.

[0036] In this embodiment, n is determined by the actual number of communications between threads.

[0037] In the embodiment, the thread T 2n-1 With the thread T 2n Communication between them requires determining the semaphore protection flag F. n Is the value equal to 1?

[0038] In the embodiment, the V operation is performed on thread T. 2n-1 superior.

[0039] In this embodiment, one semaphore corresponds to two threads.

[0040] The specific implementation process is as follows:

[0041] (1) After the Linux system boots up, the application is executed, and semaphores S1~S2 are created. n Create threads T1~T 2n Create inter-thread semaphore protection flags F1~F n Where n represents the number, determined by the actual number of inter-thread communications; for ease of description, using logical conventions, let's assume {S1, T1, T2, F1} is the first group of inter-thread communication G1, and {S2, T3, T4, F1} is the second group of inter-thread communication G2. Depending on the value of n, we can represent the communication G between n groups of threads. n , that is, {S n T 2n-1 T 2n F n};

[0042] (2) Use the system-provided sem_init function to initialize the semaphore, use the system-provided pthread_create function to initialize the thread, and use the system-provided assignment operator to set the inter-thread semaphore protection flags F1~F1. n Set all to 0;

[0043] (3) Thread T x (1≤x≤2n, where n is a positive integer and x is an even number), causing it to be in a blocked state, waiting for the thread to be awakened;

[0044] (4) Thread T x (1 ≤ x ≤ 2n, where n is a positive integer and x is an odd number) This needs to be discussed with thread T. x When performing inter-thread communication (where 1 ≤ x ≤ 2n, and n is a positive integer, and x is an even number), determine the semaphore protection flag F. n (1 ≤ x ≤ n);

[0045] (5) When F n If the value of F is not equal to 0, proceed to step 3; when F n When the value is 0, proceed to step 6;

[0046] (6) Set the signal protection flag F n The value is set to 1, and the sem_post function provided by the system is used to control the semote S. x (1≤x≤n) Perform operation V; according to the operating system principle, step 7 must be executed;

[0047] (7) Thread T x (1 ≤ x ≤ 2n, where n is a positive integer and x is an even number) Activation and wake-up;

[0048] (8) Execute the content that the thread needs to complete (such as accessing public resources);

[0049] (9) Obtain the value of semaphore S through the sem_getvalue function provided by the system, and assign it to the semaphore protection flag F. n ;

[0050] (10) If F n If the value is not equal to 0, return to step 8; otherwise, return to step 3 and wait for the thread to be awakened.

[0051] To facilitate understanding of the above technical solutions of the present invention, the following detailed description of the above technical solutions of the present invention will be provided through specific usage methods.

[0052] In practical application, according to the Linux system semaphore inter-thread communication protection method described in this invention, communication between threads is achieved based on the threads and semaphores provided by the Linux system. Threads determine the protection flag F of the protection semaphore. n The value is used to determine whether it is truly necessary to activate the communication object's thread.

[0053] In summary, by using the above-mentioned technical solution of the present invention, by adding a semaphore protection flag between threads, the threads can further determine whether it is really necessary to activate the thread of the communication object by judging the value of the semaphore protection flag, thereby effectively solving the synchronization problem and false permission problem of inter-thread communication, and ensuring the effectiveness and reliability of inter-thread execution.

[0054] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.

Claims

1. A method for protecting communication between threads of a Linux system semaphore, characterized by, Comprising the steps of: After the S1 Linux system is started, an application program is executed, semaphores S1~S n are created, threads T1~T 2n are created, and thread inter-semaphore protection flags F1~F n are created, where n is a positive integer. S2 uses the sem_init function provided by the system to implement the initialization of semaphores S1~S n , uses the pthread_create function provided by the system to implement the initialization of threads T1~T 2n , and uses the assignment operator provided by the system to initialize the thread inter- semaphore protection identifiers F1~F n to 0. S3 thread T 2n In a blocked state, waiting to be awakened and activated; S4 thread T 2n-1 F n S3 when the value of F 2n-1 F n S5 when the value of F S5 set the semaphore protection flag Fn to 1 and provide the sem_post function through the system to the semaphore S 2n-1 S5 set the semaphore protection flag Fn to 1 and provide the sem_post function through the system to the semaphore S n perform the V operation; S6 When thread T 2n-1 S n After the V operation is performed, thread T 2n Activated upon being awakened; S7 thread T 2n executes the thread its corresponding function actual content; S8 Thread T 2n The current semaphore S is obtained using the sem_getvalue function provided by the system. n The value is assigned to the semaphore protection flag F. n ; S9 If the value of the thread T 2n F n is equal to 0, go back to S3, waiting for the thread to be awakened and activated; otherwise go back to S7, executing the function corresponding to the actual content of the thread T 2n .

2. The security method of claim 1, wherein, The n is determined by the actual number of inter-thread communication.

3. The communication protection method according to claim 1, characterized by, The thread T 2n-1 communicates with the thread T 2n between them needs to determine whether the value of the semaphore protection flag F n is equal to 1.

4. The communication protection method according to claim 1, characterized by, The V operation is performed on the thread T 2n-1 above.

5. The communication protection method according to claim 1, characterized by, One semaphore corresponds to two threads.

Citation Information

Patent Citations

  • Processing method and system of timers and asynchronous events

    CN102455940A

  • Systems and Methods for Implementing a Thread Trace Log

    US20190034259A1