Operating System - HP-UX
1748145 Members
3520 Online
108758 Solutions
New Discussion

Why SIGALRM cann't happen in thread on HPUX?

 
yang shaohua
Frequent Advisor

Why SIGALRM cann't happen in thread on HPUX?

I have such code as following, the code works and SIGALRM can be captured.

static void sig_alrm(int);

int
main(void)
{
    int     n;
    char    line[MAXLINE];

    if (signal(SIGALRM, sig_alrm) == SIG_ERR) {
        printf("signal(SIGALRM) error\n");
        exit(1);
    }
    siginterrupt( SIGALRM, 1);

    alarm(5);
    if ((n = read(fileno(stdin), line, MAXLINE)) < 0) {
        printf("read error:%s\n", strerror(errno));
        exit(0);
    }
    alarm(0);

    write(fileno(stdout), line, n);
    exit(0);
}

static void sig_alrm(int signo)
{
    printf("alrm\n");
    /* nothing to do, just return to interrupt the read */
}

 

but,if I create a thread, put the READ inside the thread, SIGALRM will not happen anymore.

 

void * thr_fn(void *arg);
static void sig_alrm(int);
pthread_t ntid;

void * thr_fn(void *arg)
{
 int     n;
 char    line[MAXLINE];
 if (signal(SIGALRM, sig_alrm) == SIG_ERR) {
    printf("signal(SIGALRM) error\n");
    exit(1);
 }
 alarm( 2 );
 if ((n = read(fileno(stdin), line, MAXLINE)) < 0) {
    printf("read error%d:%s\n",n, strerror(errno));
    return;
}
 alarm(0);
 write(fileno(stdout), line, n);
}


int
main(void)
{
  int err;
  err =  pthread_create(&ntid, NULL, thr_fn, NULL);
  if( err != 0 )
    printf("cann't create thread: %s\n", strerror( err ));
  sleep( 10 ); 
  exit( 0 );
}

 

Anyone can help me out?

thanks in advance!

16 REPLIES 16
rchaurasiya
Occasional Advisor

Re: Why SIGALRM cann't happen in thread on HPUX?

Hi yang,

 

Sorry for delayed response.

 

alarm(2) will cause SIGALRM to be sent to the process and in a multithreaded scenario, POSIX standard requires signal to be directed to exactly one thread in the process, whoever has not blocked it. Which thread is not specifed. In your example program, signal is getting delivered and handled by the main thread (not by the newly created thread, who is doing read()).

 

If you want read to get interrupted in this program, you may want main thread to block SIGALRM using pthread_sigmask() and it'll work.

 

Please let me know if you need further clarifications on this.

 

Thanks, Ramesh

 

Dennis Handly
Acclaimed Contributor

Re: Why SIGALRM can't happen in thread on HP-UX?

>you may want main thread to block SIGALRM using pthread_sigmask() and it'll work.

 

Looks like you have to block it in main AND unblock in the thread.

rchaurasiya
Occasional Advisor

Re: Why SIGALRM can't happen in thread on HP-UX?

That's right Dennis. If main thread blocks the signal before creating new thread, we'll have to explicitly unblock it in new thread (as thread's signal mask gets inherited from the creator). Or we may let only main thread block the signal after new thread is created, where new thread can wait for good to go message from main thread using some sort of synchronization mechanism between two and main thread after blocking the signal allows new thread to proceed.

 

Thanks, Ramesh

Dennis Handly
Acclaimed Contributor

Re: Why SIGALRM can't happen in thread on HP-UX?

>Or we may let only main thread block the signal after new thread is created

 

I tried just adjusting the sleep and alarm times and I wasn't successful.  So block and an explicit unblock is easier.

 

 

 

 

 


 

yang shaohua
Frequent Advisor

Re: Why SIGALRM cann't happen in thread on HPUX?

thanks for helps from all above

Actually, I did BLOCK signal alarm in main thread, and then use pthread_sigmask to unblock signal alarm in threads, unfornately, the ALARM still cann't be received.

So, I found out a workround, I use  pthread_kill( iThreadID, SIGALRM ) actively, in this way, the working thread can received the alarm signal.

what confused me is WHY, on  HPUX platform, the alarm signal cann't be accepted in thread?

 

again, thanks all guys response me  for your efforts!

rchaurasiya
Occasional Advisor

Re: Why SIGALRM can't happen in thread on HP-UX?

Adjusting sleep and alrm timing might not work always, as it all depends on when the main thread in this case really gets chance to execute and blocks the signal. One of the deterministic ways here could be new thread waits indefinitely for main thread to update some global variable before calling alarm()/read() and main thread updates this global variable only after it has blocked signal. Either ways it'll will work, whichever one finds suitable.

 

And for yang, we also need to ensure that alarm time doesnt get over before new thread is already in read() system call, some sort of synchronization is required there as well.

Dennis Handly
Acclaimed Contributor

Re: Why SIGALRM can't happen in thread on HP-UX?

>I did BLOCK signal alarm in main thread, and then used pthread_sigmask to unblock signal alarm in threads, unfortunately, the ALARM still can't be received.

 

This worked fine for me:

  int err;
  sigset_t set_alrm;
  err = sigemptyset(&set_alrm);
  err = sigaddset(&set_alrm, SIGALRM);
  err = pthread_sigmask(SIG_UNBLOCK, &set_alrm, NULL);

yang shaohua
Frequent Advisor

Re: Why SIGALRM can't happen in thread on HP-UX?

my code like :

  sigemptyset( &sig_set );
  sigaddset( &sig_set,SIGALRM );   
  int err = pthread_sigmask( SIG_BLOCK, &sig_set, &old_sig_set ); 

 

Doesn't work.... :-(

anyway, the workround is working well.

yang shaohua
Frequent Advisor

Re: Why SIGALRM can't happen in thread on HP-UX?

Sure, I can make sure that alarm time doesnt get over before new thread is already in read() system call.