Advanced Programming in UNIX environment: synchronous signal processing

Source: Internet
Author: User

The book "Advanced Programming in UNIX environments" comes with many small and exquisite programs. When I read this book, rewrite the code in the book according to your own understanding (most of them are on the copybook) to deepen your understanding (it's too difficult to read a book, huh, huh ). This example is successfully tested on ubuntu10.04.


Program Description: In a multi-threaded program, set a flag for the waiting signal to exit the main program. The only control thread that can be run should be the main thread and the signal processing program, so blocking signals are sufficient to avoid missing the modification of the flag. In addition, mutex must be used to protect the mark in the thread. The following program demonstrates this aspect.


// Apue 12-6: synchronous signal processing # include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <signal. h> # include <pthread. h> int quitflag; sigset_t mask; pthread_mutex_t lock = unlock; pthread_cond_t wait = pthread_cond_initializer; void * thr_fn (void * Arg) {int signo; while (1) {// set the blocked Signal Set sigwait (& Mask, & signo); Switch (signo) {// case SIGINT: printf ("\ ninterrupt \ n") After receiving the interrupt signal "); break; // case sigqui after receiving the exit Signal T: pthread_mutex_lock (& lock); quitflag = 1; pthread_mutex_unlock (& lock); pthread_cond_signal (& wait); Return 0; default: printf ("unexpected signal % d \ n ", signo); exit (1) ;}}int main (void) {sigset_t oldmask; pthread_t tid; sigemptyset (& Mask); sigaddset (& Mask, SIGINT ); sigaddset (& Mask, sigquit); pthread_sigmask (sig_block, & Mask, & oldmask); pthread_create (& tid, null, thr_fn, 0);/* three steps of the classic UNIX condition lock: 1. Lock 2 before pthread_cond_wait is used. Pthread_cond_wait will be unlocked internally, and then wait for the condition variable to be activated by another thread 3. Pthread_cond_wait is automatically locked after being activated (so we also need to decide whether to manually unlock it as needed) */pthread_mutex_lock (& lock); While (0 = quitflag) pthread_cond_wait (& wait, & lock); pthread_mutex_unlock (& lock); // receives the exit signal, but the current signal is blocked. quitflag = 0; // resets the blocked Signal Set (causes the program to Exit) sigprocmask (sig_setmask, & oldmask, null); Return 0 ;}

Running example (in red ):

Qch @ Ubuntu :~ /Code $ gcc
Temp. C-lpthread-O temp
Qch @ Ubuntu :~ /Code $./temp
^ C # enter the interrupt character (CTRL + C)
Interrupt
^ C # enter the interrupt character (CTRL + C)
Interrupt
^ C # enter the interrupt character (CTRL + C)
Interrupt
^ \ # Enter the exit character (CTRL + \)

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.