Linux多线程(十二)线程和信号量

1.进程中每个线程都有自己的信号屏蔽字和信号未决字

2.信号的处理方式是进程中所有线程共享的

3.进程中的信号是递送到单个线程的

4.定时器是进程资源,进程中所有的线程共享共同的定时器

例1:

      下面这个例子通过一个子线程调用了alarm函数产生alarm信号,由于信号的处理方式就是进程中所有线程共享的,这个时候也就是说主线程和该子线程都有可能捕获该信号,通过下面的程序可以看出,是主程序捕获了信号。

       由于alarm是单次触发,所以在信号处理函数中,又需要重新调用alarm函数,继续触发。

实验源码如下:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void sig_handler(int signo)
{  
   printf("pthread id in the sig_handler: %lx\n",pthread_self());
   if(signo==SIGALRM)
   {  
      printf("timeout...\n");
   }
   alarm(2);
}
void * th_fn(void *arg)
{  
   if(signal(SIGALRM,sig_handler)==SIG_ERR)
   {  
      perror("signal sigalrm error");
   }
   alarm(2);
   int i;
   for(i=1;i<=100;i++)
   {  
      printf("(%lx) i: %d\n",pthread_self(),i);
      sleep(1);
   }
   return (void*)0;

}

int main(void)
{
   int err;
   pthread_t rabbit;
   pthread_attr_t attr;
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
   if((err=pthread_create(&rabbit,&attr,th_fn,(void *)0))!=0)
   {
      perror("pthread_create error");
   }
   while(1)
   {
       printf("control thread id: %lx\n",pthread_self());
       sleep(10);
   }
   return 0;

}

主程序里设定了主线程睡10s,但是主线程收到了alarm信号,因此就会被唤醒,所以过2s就会继续运行

control thread id: 7f8fff10f700 is running
(7f8ffe90c700) i: 1
(7f8ffe90c700) i: 2
pthread id in the sig_handler: 7f8fff10f700 
timeout...
control thread id: 7f8fff10f700 is running
(7f8ffe90c700) i: 3
(7f8ffe90c700) i: 4
pthread id in the sig_handler: 7f8fff10f700 
timeout...
control thread id: 7f8fff10f700 is running
(7f8ffe90c700) i: 5
(7f8ffe90c700) i: 6
pthread id in the sig_handler: 7f8fff10f700 
timeout...
control thread id: 7f8fff10f700 is running
(7f8ffe90c700) i: 7
(7f8ffe90c700) i: 8
pthread id in the sig_handler: 7f8fff10f700 
timeout...
control thread id: 7f8fff10f700 is running
(7f8ffe90c700) i: 9
(7f8ffe90c700) i: 10
pthread id in the sig_handler: 7f8fff10f700 
timeout...
control thread id: 7f8fff10f700 is running
(7f8ffe90c700) i: 11
(7f8ffe90c700) i: 12
pthread id in the sig_handler: 7f8fff10f700 
timeout...
control thread id: 7f8fff10f700 is running
(7f8ffe90c700) i: 13
(7f8ffe90c700) i: 14
pthread id in the sig_handler: 7f8fff10f700 
timeout...
control thread id: 7f8fff10f700 is running
(7f8ffe90c700) i: 15
(7f8ffe90c700) i: 16

^C

######################################

如果需要主线程不需要捕获信号,可以在主线程里屏蔽即可:

在上述程序中,while(1)语句前,加入以下代码:

sigset_t set;
sigemptyset(&set);
sigaddset(&set,SIGALRM);

pthread_sigmask(SIG_SETMASK,&set,NULL);

其运行结果如下:

control thread id: 7fda2a07d700 is running
(7fda2987a700) i: 1
(7fda2987a700) i: 2
pthread id in the sig_handler: 7fda2987a700
timeout...
(7fda2987a700) i: 3
(7fda2987a700) i: 4
pthread id in the sig_handler: 7fda2987a700
timeout...
(7fda2987a700) i: 5
(7fda2987a700) i: 6
pthread id in the sig_handler: 7fda2987a700
timeout...
(7fda2987a700) i: 7
(7fda2987a700) i: 8
pthread id in the sig_handler: 7fda2987a700
timeout...
(7fda2987a700) i: 9
(7fda2987a700) i: 10
control thread id: 7fda2a07d700 is running
pthread id in the sig_handler: 7fda2987a700
timeout...
(7fda2987a700) i: 11
(7fda2987a700) i: 12
pthread id in the sig_handler: 7fda2987a700
timeout...
(7fda2987a700) i: 13
(7fda2987a700) i: 14
pthread id in the sig_handler: 7fda2987a700
timeout...
(7fda2987a700) i: 15
(7fda2987a700) i: 16
pthread id in the sig_handler: 7fda2987a700
timeout...
(7fda2987a700) i: 17
(7fda2987a700) i: 18

^C

3.如何在一个线程中结束另一个线程,传入另一个线程的标识符,其源码如下:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void sig_handler(int signo)
{  
   printf("pthread id in the sig_handler: %lx\n",pthread_self());
   if(signo==SIGALRM)
   {  
      printf("timeout...\n");
   }
   alarm(2);
}
void * th_fn(void *arg)
{  
   if(signal(SIGALRM,sig_handler)==SIG_ERR)
   {  
      perror("signal sigalrm error");
   }
   alarm(2);
   int i;
   for(i=1;i<=100;i++)
   {  
      printf("1:(%lx) i: %d\n",pthread_self(),i);
      sleep(1);
   }
   return (void*)0;

}

void *th_fn2(void *arg)
{  
   pthread_t th=(pthread_t) arg;
   int i;
   for(i=1;i<=100;i++)
   {
      if(i==5)
      {
         pthread_cancel(th);
         alarm(0);
      }
      printf("2:(%lx) i: %d\n",pthread_self(),i);
      sleep(1);
   }

}

int main(void)
{
   int err;
   pthread_t rabbit,turtle;
   pthread_attr_t attr;
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
   if((err=pthread_create(&rabbit,&attr,th_fn,(void *)0))!=0)
   {
      perror("pthread_create error");
   }
   if((err=pthread_create(&turtle,&attr,th_fn2,(void *)rabbit))!=0)
   {
      perror("pthread_create error");
   }


   sigset_t set;
   sigemptyset(&set);
   sigaddset(&set,SIGALRM);
   pthread_sigmask(SIG_SETMASK,&set,NULL);
   while(1)
   {
       printf("control thread id: %lx is running\n",pthread_self());
       sleep(10);
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38211852/article/details/80412847
今日推荐