Thread and process shielding codes in Linux

Source: Internet
Author: User
Tags sleep function

1. Shielding SIGUSR1 signals in the main thread
2. creat the two subthreads write and read to cancel the blocking of SIGUSR1 in the write thread.

3. The SIGUSR1 signal sent from other processes will be accepted by the write thread.

Sigprocmask is used to shield signals that change the entire process.
Pthread_sigmask only changes the thread's own signal shielding

Why is the segmentation fault error reported when I send the SIGUSR1 signal to the process running the following code through kill-SIGUSR1 4739?

# Include <cstdlib>
# Include <iostream>
# Include <pthread. h>
# Include <signal. h>
# Include <stdio. h>

Int write_should_exit = 0;

Void catch_signal_usr1 (INT sig ){
Printf ("get Sig is % d \ n", sig );
Write_should_exit = 1;
}

Void * Writer (void * AGR ){
Sigset_t set;
Sigemptyset (& set );
Sigaddset (& set, SIGUSR1 );
Sigprocmask (sig_unblock, & set, null );
// Pthread_sigmask (sig_unblock, & set, null );
While (write_should_exit! = 1 ){
Fprintf (stderr, "I am writing my buffers. \ n ");
Sleep (10 );
}
Fprintf (stderr, "writer % d detect variable changed and eixt. \ n", pthread_self ());
Pthread_exit (null );
}

Void * reader (void * Arg ){
While (write_should_exit! = 1 ){
Fprintf (stderr, "I am reading my buffers. \ n ");
Sleep (10 );
}
Fprintf (stderr, "Reader % d detect writer exit \ n", pthread_self ());
Pthread_exit (null );
}

Int main (INT argc, char ** argv ){
Pthread_t w_tid;
Pthread_t r_tid;
Struct sigaction action;
Sigset_t set;
//
Action. sa_handler = catch_signal_usr1;
Sigemptyset (& Action. sa_mask );
Action. sa_flags = 0;
Sigaction (SIGUSR1, & Action, null );

Sigemptyset (& set );
Sigaddset (& set, SIGUSR1 );
Sigprocmask (sig_block, & set, null );
// Pthread_sigmask (sig_block, & set, null );
Fprintf (stderr, "PID is % d \ n", getpid ());
Pthread_create (& w_tid, null, writer, null );
Pthread_create (& r_tid, null, reader, null );
Fprintf (stderr, "the main thread created reader % d and writer % d. \ n", w_tid, r_tid );
Pthread_join (w_tid, null );
Pthread_join (r_tid, null );
Fprintf (stderr, "main thread will eixt ");

Return 0;
}

Running result:
PID is 4739
I am reading my buffers.
The main thread created reader-1219777680 and writer-1228170384.
I am writing my buffers.
I am reading my buffers.
I am writing my buffers.
I am reading my buffers.
I am writing my buffers.
Segmentation fault
Press [enter] to close the terminal...

Send SIGUSR1 signal through another bash
Administrator @ Ubuntu :~ $ Kill-SIGUSR1 4739

There are also sigprocmask (sig_block, & set, null) for the main line; or pthread_sigmask (sig_block, & set, null); there are similarities and differences between the blocked signal SIGUSR1.

**************************************** **************************************** *******************************

1. in a multi-threaded environment, the signal is sent to the whole process. Generally, all threads have the opportunity to receive this signal. The process executes the signal processing function in the context of the thread that receives the signal, it is hard to know which thread to execute.

2. The implementation of the signal function BSD/Linux is not implemented when the signal processing function is called, but the signal is blocked during signal processing until the signal processing function returns. When other implementations call the signal processing function, the processing of the recovery signal is the default method. Therefore, the signal processing function must be rebuilt in the signal processing function to define the processing function, in these systems, a better way is to use sigaction to create a signal processing function.

3. Which thread will receive the signal sent to the process? Apue said that in a multi-threaded program, if special signal blocking is not performed, the system selects a thread to process the signal when sending the signal to the process.

4. If some threads can shield a signal while some threads can process the signal, when we send this signal to a thread that cannot process the signal, the system will deliver the signal to the thread with the smallest process number that can process the signal.

5. If we register a signal processing function and use sigwait to wait for the signal at the same time, who will get the signal? In Linux, sigwait has a high priority.

6 In the POSIX thread model in Linux, a thread has an independent process number. You can use getpid () to obtain the thread number, and the thread number is saved in the value of pthread_t. The process number of the main thread is the process Number of the whole process. Therefore, sending a signal to the main process only sends the signal to the main thread. If the main thread sets signal shielding, the signal will be delivered to a thread that can be processed.

7. When you call the system function to execute shell commands, you can block sigchld with confidence, because the system will handle the problem of sub-Process Termination by itself.

8 when using sleep (), you need to block the sigalrm signal with confidence. Currently, the sleep function does not depend on the sigalrm signal of the alrm function.

LINUX multi-thread signal Summary (2)

1. by default, the signal will be received and processed by the main process, even if the signal processing function is registered by the subthread. each thread has its own signal shielding word. You can use the sigprocmask function to shield a thread from responding to the signal, leaving only the thread that needs to process the signal to process the specified signal.

3. for a signal processing function, the processing function registered at the last time the program is executed shall prevail, that is, in all threads, the processing of the same signal in any thread must be the same 4. you can use pthread_kill to send the apue signal to a specified thread. Each thread has its own signal shielding word, but the signal processing is shared by all threads in the process, this means that although a single thread can block some signals, when the thread modifies the processing behavior related to a signal, all threads share the change of the processing behavior. In this way, if a thread chooses to ignore a signal, and other threads can recover the default processing behavior of the signal, or set a new processing program for the signal, the signal selection of the above thread can be revoked.

The signal in the process is sent to a single thread. If the signal is related to hardware failure or timer timeout, the model is sent to the thread that causes the event, other signals are sent to any thread.

The behavior of sigprocmask is not defined in the multi-thread process. The thread must use pthread_sigmask To summarize: A signal can be processed by any thread without blocking it, however, there is only one processing function shared by multiple threads in a process. ......

LINUX multi-thread signal Summary (III)

1 in LINUX multi-threaded applications, each thread can set the signal mask of this thread by calling pthread_sigmask. In general, the blocked signal cannot interrupt the execution of this thread, unless the signal is generated because the program is running incorrectly, such as SIGSEGV; in addition, the signal sigkill and sigstop that cannot be ignored cannot be blocked.

2. When a thread calls pthread_create () to create a new thread, the signal mask of this thread will be inherited by the newly created thread.

3. It is best to use the sigaction Method for signal installation. sigaction is a stable signal processing designed to replace signal. The use of signal is relatively simple. Signal (signalno, signalproc); tasks that cannot be completed are: 1. do not know the cause of the signal generation; 2. if the processing signal cannot block other signals but signaction, you can set more messages. Especially in the Process of signal processing functions, what kind of processing the signal is accepted.

The sigaction function is used to change the behavior of a process after receiving a specific signal.

The 4 sigprocmask function can only be used in a single thread, and the pthread_sigmask function is used in multiple threads.

5. A signal is a special message sent to a process. Its typical feature is Asynchronization.

6. A signal set represents a set of multiple signals. Its type is sigset_t. 7. Each process has a signal mask (or a signal mask), which defines the signal set that the current process requires blocking.

8. Blocking means that the Linux kernel does not deliver all signals to the process in the mask. Therefore, the process can temporarily block delivery of specific signals by modifying the signal mask. The blocked signal will not affect the process behavior until the signal is actually delivered.

9 The ignore signal is different from the blocking signal. The ignore signal refers to the signal that the Linux kernel has delivered to the application, but the application directly discards the signal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.