歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 線程控制

線程控制

日期:2017/3/3 12:55:07   编辑:Linux技術
首先看這篇文章學習線程和信號:http://www.cnblogs.com/clover-toeic/p/4126594.html
然後我寫了一個小程序
#include <stdio.h>
#include <pthread.h>
#include <signal.h>

/*
	測試與線程有關的信號函數	
 
 */

sigset_t new_mask , old_mask;
void *thread_func( void *arg );

int main(int argc , char *argv[]){
	pthread_t thrd;

	pr_mask( " main begin: ");

	//set signal set
	sigemptyset( &new_mask );
	sigaddset( &new_mask , SIGUSR1 );
	sigaddset( &new_mask , SIGINT);

  	/* 
         Block SIGINT; other threads created by main() will inherit
         a copy of the signal mask. 
       */
	//first mask some signals 
	if( pthread_sigmask( SIG_BLOCK , &new_mask , &old_mask)
			!= 0 )
		oops( "thread mask:");

	//創建線程
	if( pthread_create( &thrd , NULL , thread_func , NULL)
			!= 0)
		oops( "thread create: ");

	//等待子線程結束
	pthread_join( thrd , NULL );

	pr_mask( " main exit: ");

	return 0;
}

void *thread_func( void *arg ){
	int err ,signop;
	int count ;
	
	count = 0;
	pr_mask( " in thread: " );
	while( count++ < 5 ){
		 err = sigwait( &new_mask ,&signop );
		 if( err != 0 )
			 oops( " sigwait: ");
		 switch( signop ){
			case SIGINT:
				printf( " catch sigint.\n ");
				break;
			case SIGUSR1:
				printf( "catch sigusr1.\n " );
				break;
			default:
				printf( "unkown signal.\n");
				break;
		 }
	}
	//reset mask
	if( pthread_sigmask( SIG_SETMASK , &old_mask , NULL) != 0 )
		oops( " thread mask :" );
	pr_mask( " after set mask :");
}

#include <stdio.h>
#include <errno.h>
#include <signal.h>

void pr_mask( const char *str ){
	sigset_t set;
	int errno_save;			//get the pre errno

	errno_save = errno;

	if( sigprocmask( 0, NULL , &set ) == -1 )
		oops( " sigmask" );
	else{
		printf( "%s" , str );
		if( sigismember( &set , SIGQUIT ) )
			printf( " SIGQUIT" );
		if( sigismember( &set , SIGINT ) )
			printf( " SIGINT" );
		if( sigismember( &set , SIGUSR1 ) )
			printf( " SIGUSR1" );
		if( sigismember( &set , SIGALRM ) )
			printf( " SIGALRM" );
		printf("\n");
	}
	errno = errno_save ;
}
#include <stdio.h>
#include <stdlib.h>

void oops(void *msg){
	perror(msg);
	exit(1);
}

然後執行:我發送 kill -SIGINT 5736 和 kill -SIGUSR1 5736 都能得到正確結果,但是我發送 kill -SIGALRM 5736 程序就終止了。後來查資料得到了解釋:
在linux系統上默認情況下,信號將由主進程接收處理,就算信號處理函數是由子線程注冊的, 在Linux中的posix線程模型中,線程擁有獨立的進程號,可以通過getpid()得到線程的進程號,而線程號保存在pthread_t的值中。而主線程的進程號就是整個進程的進程號,因此向主進程發送信號只會將信號發送到主線程中去。如果主線程設置了信號屏蔽,則信號會投遞到一個可以處理的線程中去(這解釋了man中的示例程序行為)。
其他可以參考:
http://bbs.chinaunix.net/thread-4088635-1-1.html http://blog.csdn.net/yusiguyuan/article/details/14230719 http://blog.csdn.net/yusiguyuan/article/details/14237277
Copyright © Linux教程網 All Rights Reserved