Signals for communication between Linux threads (18)

1. Signals for communication between Linux threads

Under Linux, each process has its own signal mask. This signal mask specifies which signal is blocked and which signal will not be blocked. It is usually handled by calling sigmask. At the same time, each process also has its own signal action. This set of actions specifies how the signal should be handled. Sigaction is usually called to handle it.

2. Signal installation function

	int sigaction(int signum,const struct sigaction *act ,struct sigaction *oldact);

Function:
Specify the signal number to set the processing function of the signal.

Parameters:
signum: signal number.
act: Pointer to an instance of the structure sigaction.
oldact: used to save the original signal processing, can be NULL.

Return value:
Success: 0.
Failure: -1.

3. Add all signals to the signal set function

	int sigfillset(sigset_t * set);

Function:
sigfillset() is used to initialize the parameter set signal set, and then add all signals to this signal set.

Parameters:
set: The address identified by the signal set. To operate this signal set in the future, just operate on the set.

Return value:
Success: 0.
Failure: -1.

4. Add a signal to the signal set function

	int sigaddset(sigset_t *set, int signum);

Function:
Add a signal to this signal set.

Parameters:
set: The address of the signal set identifier. To operate this signal set in the future, just operate the set.
signum: signal number.

Return value:
Success: 0.
Failure: -1.

5. Initialize the signal set function

	int sigemptyset(sigset_t *set);

Function:
Initialize signal set.

Parameters:
set: The address identified by the signal set. To operate this signal set in the future, just operate on the set.

Return value:
Success: 0.
Failure: -1.

6. Send signal function to thread

	int pthread_kill(thread_t tid, int sig);

Function:
Send signal to thread.

Parameters:
tid: thread ID.
sig: signal number.

Return value:
Success: 0.
Failure: -1.

7. Change or check the signal mask function of the calling thread

	int pthread_sigmask(int how, const sigset_t *act, sigset_t *oldact);

Function:
Change or check the signal mask of the calling thread.

Parameters:
how: add or delete or replace set to the current signal mask.
act: signal shielding word.
oldact: If it is NULL, it is fine.

How description:
SIG_BLOCK: The result set is the union of the current set parameter set (add the signal in the parameter set to the signal mask word) SIG_UNBLOCK: The result set
is the difference set of the current set parameter set (set the signal mask word to the parameter set signal in)
SIG_SETMASK: The result set is the set pointed to by the parameter set (remove the signal in the parameter set from the signal mask word)

Return value:
Success: 0.
Failure: -1.

8. Reference code:

//=============================================================================
// File Name    : thread_signal.c
// Author       : FengQQ
//
// Description  : 信号
// Annotation   : 
//
//	int pthread_kill(pthread_t thread,int sig);
//	向线程thread发送sig信号,成功返回0,失败返回错误码
//
//	int sigaction(int signum,const truct sigaction *act,struct sigaction *oldact);
//	为信号signum设置处理函数,处理函数在sigaction中指定
//	act.sa_mask 信号屏蔽字
//	act.sa_handler 信号集处理程序	
//
//	int pthread_sigmask(int how,const sigset_t *set,sigset_t *oldset);
//	多线程信号屏蔽函数
//	how = SIG_BLOCK:向当前的信号掩码中添加set,其中set表示要取消阻塞的信号组。
//	SIG_UNBLOCK: 向当前的信号掩码中删除set,其中set表示要取消阻塞的信号组。
//	SIG_SETMASK: 将当前的信号掩码替换为set,其中set表示新的信号掩码。
//	在多线程中,新线程的当前信号掩码会继承创造它的线程的信号掩码
//
// Created by FengQQ. 2020-10-05
//=============================================================================
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <errno.h>

//-------------信号回调函数1-----------------
void sig_handler1(int arg)
{
    
    
	printf("pthread1 get signal\r\n");
	return;
}
//-------------信号回调函数2-----------------
void sig_handler2(int arg)
{
    
    
	printf("pthread2 get signal\r\n");
	return;
}
//---------------线程函数1------------------
void *pthread1_callback(void *arg)
{
    
    
	struct sigaction act;
	
	printf("new pthread 1\r\n");
	
	memset(&act,0,sizeof(act));						//将act的内存空间填入0
	sigaddset(&act.sa_mask,SIGQUIT);				//添加一个信号至信号集
	act.sa_handler = sig_handler1;
	sigaction(SIGQUIT,&act,NULL);					//查询或设置信号处理方式
	pthread_sigmask(SIG_BLOCK,&act.sa_mask,NULL);
	sleep(2);
}
//---------------线程函数2------------------
void *pthread2_callback(void *arg)
{
    
           
	struct sigaction act;
	
	printf("new pthread 2\r\n");      

	memset(&act,0,sizeof(act));					//将act的内存空间填入0	
	sigaddset(&act.sa_mask,SIGQUIT);			//添加一个信号至信号集
	act.sa_handler = sig_handler2;	
	sigaction(SIGQUIT,&act,NULL);				//查询或设置信号处理方式
	
	//pthread_sigmask(SIG_BLOCK,&act.sa_mask,NULL);
	sleep(2);
}

int main(int argc,char *argv[])
{
    
    
	int ret;
	int signal_value;
	pthread_t ptid1,ptid2;
	
	ret = pthread_create(&ptid1,NULL,pthread1_callback,NULL);
	if(ret != 0)
	{
    
    
		printf("create new pthread1 failed...\r\n");
		return -1;
	}	
	ret = pthread_create(&ptid2,NULL,pthread2_callback,NULL);
	if(ret != 0)
	{
    
    
		printf("create new pthread2 failed...\r\n");
		return -1;
	}
	
	sleep(1);
	
	signal_value = pthread_kill(ptid1,SIGQUIT);
	if(signal_value != 0)
	{
    
    
		printf("send signal to thread1 failed...\r\n");
	}
	
	signal_value = pthread_kill(ptid2,SIGQUIT);
	if(signal_value != 0)
	{
    
    
		printf("send signal to thread2 failed...\r\n");
	}	
	
	ret  = pthread_join(ptid1,NULL);
	if(ret != 0)
	{
    
    
		printf("pthread1 join failed...\r\n");
	}	
	ret  = pthread_join(ptid2,NULL);
	if(ret != 0)
	{
    
    
		printf("pthread2 join failed...\r\n");
	}
	
	return 0;
}

Semaphores for communication between Linux threads (19)

Link: link .(https://blog.csdn.net/qq_39721016/article/details/120604654?spm=1001.2014.3001.5501)

Guess you like

Origin blog.csdn.net/qq_39721016/article/details/120478269