C Multi-threaded programming signal processing

Source: Internet
Author: User

When programming with Linux C, processing signals is unavoidable. In multithreaded programming you need to pay attention to the use of two functions, one is pthread_sigmask (), used to block a signal in the thread, and the other is sigaction (), used in the thread to set the signal processing mode.

void sig_handler1 (int arg)
{
printf ("Thread1 get Signal\n");
Return
}
void sig_handler2 (int arg)
{
printf ("Thread2 get Signal\n");
Return
}

void *thread_fun1 (void *arg)
{
printf ("New Thread 1\n");

struct Sigaction Act;
memset (&act, 0, sizeof (ACT));
Sigaddset (&act.sa_mask, sigquit);
Act.sa_handler = Sig_handler1;
Sigaction (Sigquit, &act, NULL);

Pthread_sigmask (Sig_block, &act.sa_mask, NULL);
Sleep (2);
}


void *thread_fun2 (void *arg)
{
printf ("New Thread 2\n");

struct Sigaction Act;
memset (&act, 0, sizeof (ACT));
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 ()
{
pthread_t Tid1, Tid2;
int err;
int s;

Err = pthread_create (&TID1, NULL, THREAD_FUN1, NULL);
if (err! = 0)
{
printf ("Create new Thread 1 failed\n");
Return
}
Err = pthread_create (&tid2, NULL, thread_fun2, NULL);
if (err! = 0)
{
printf ("Create new Thread 2 failed\n");
Return
}

Sleep (1);

s = Pthread_kill (TID1, sigquit);
if (s! = 0)
{
printf ("Send signal to Thread1 failed\n");
}
s = Pthread_kill (Tid2, sigquit);
if (s! = 0)
{
printf ("Send signal to Thread2 failed\n");
}

Pthread_join (TID1, NULL);
Pthread_join (Tid2, NULL);

return 0;
}

The result will be:

New Thread 2

New Thread 1

Thread1 Get Signal

Or:

New Thread 1

New Thread 2

Thread2 Get Signal

Will find that the Sig_handler printed content with the last registered processing function, which means that thread 1 and thread 2, in thread 1 is shielded sigquit, and Threads 2 does not, in any case, no matter how many times the last processing function to print the content should be thread2 get signal. But it turns out that's not the case, why??

In fact, for a signal processing function, the last time the program executes the processing function to register, if the thread 1 last execution, then the thread 1 registered processing function, the last thread 2 in the processing function is replaced by thread 1. That is, in all threads, the same signal must be processed in any line thread the same signal.

C Multi-threaded programming signal processing

Related Article

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.