1
#include "apue.h"
#include <pthread.h>

int     quitflag;
sigset_t    mask;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t wait  = PTHREAD_COND_INITIALIZER;

void *
thr_fn(void *arg)
{
    int err, signo;

    for ( ; ; ) {
    err = sigwait(&mask, &signo);
    if (err != 0)
        err_exit(err, "sigwait failed");
    switch (signo) {
        case SIGINT:
        printf("\ninterrupt\n");
        break;

        case SIGQUIT:
        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)
{
    int     err;
    sigset_t    oldmask;
    pthread_t   tid;

    sigemptyset(&mask);
    sigaddset(&mask, SIGINT);
    sigaddset(&mask, SIGQUIT);
    if ((err =  pthread_sigmask(SIG_BLOCK, &mask, &oldmask)) != 0)
        err_exit(err, "SIG_BLOCK error");

    err = pthread_create(&tid, NULL, thr_fn, 0);
    if (err != 0)
        err_exit(err, "can not create thread");

    **sleep(20);**  /* added code here */
    pthread_mutex_lock(&lock);
    while (quitflag == 0)
        pthread_cond_wait(&wait, &lock); /* first unlock, then suspend */
    pthread_mutex_unlock(&lock);

    quitflag = 0;

    if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
        err_sys("SIG_SETMASK error");

    exit(0);
}    

This is the code of apue that examine "sigwait", i insert "sleep(20)" before pthread_cond_wait is invoked, start the program with ./a.out, then thread thr_fn start, before "20 seconds", i send "signal quit" when ctrl+\, then thread exited, then after a few time, the main process exit automatically, i don't know why? could somebody explain this? my platform is centos, thanks

alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    You send SIGQUIT and the thread unlocks the mutex & signals (`pthread_cond_signal`), thus the loop in the main thread break and it exits. Everything seems as expected to me. What did you expect and why? – P.P Sep 27 '17 at 09:33
  • What are you actually wondering about ? Are you wondering why sleep(20) did not wait a full 20 seconds ? Are you wondering why you have to press ctrl+\ a few times instead of just 1 time ? Are you wondering why main quits after a few times instead of exiting immediately ? Are you wondering about something else that you can specifically tell us ? – nos Sep 27 '17 at 10:22
  • @usr thanks,but this code can due to signal lost,right? – wislinon chen Sep 27 '17 at 10:22
  • @nos i have understand by @usr,so thanks,only for examining "signal lost ", my first purpose~ – wislinon chen Sep 27 '17 at 10:28
  • Ok. So the sleep(20) was not important at all to your question ? (because if you do have a sleep(20), the 1. ctrl+\ could interrupt sleep() and make it return). – nos Sep 27 '17 at 10:30
  • @nos yes,only for testing "signal lost" because of child thread start before main thread(cond_singal before cond_wait),but i now think,although main don't invoke pthread_cond_wait,but "signal quit" still catch by child thread,so there is no "sinal lost",only pthread_cond_wait not be invoked~ – wislinon chen Sep 27 '17 at 10:45

0 Answers0