Re: Why SIGALRM can't happen in thread on HP-UX? (1216 Views)
Reply
Frequent Advisor
yang shaohua
Posts: 33
Registered: ‎02-22-2011
Message 1 of 17 (1,386 Views)

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!

Occasional Advisor
rchaurasiya
Posts: 6
Registered: ‎07-29-2013
Message 2 of 17 (1,320 Views)

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

[ Edited ]

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

 

Acclaimed Contributor
Dennis Handly
Posts: 25,200
Registered: ‎03-06-2006
Message 3 of 17 (1,301 Views)

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.

Occasional Advisor
rchaurasiya
Posts: 6
Registered: ‎07-29-2013
Message 4 of 17 (1,296 Views)

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

Acclaimed Contributor
Dennis Handly
Posts: 25,200
Registered: ‎03-06-2006
Message 5 of 17 (1,293 Views)

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.

 

 

 

 

 


 

Frequent Advisor
yang shaohua
Posts: 33
Registered: ‎02-22-2011
Message 6 of 17 (1,285 Views)

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!

Occasional Advisor
rchaurasiya
Posts: 6
Registered: ‎07-29-2013
Message 7 of 17 (1,281 Views)

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.

Acclaimed Contributor
Dennis Handly
Posts: 25,200
Registered: ‎03-06-2006
Message 8 of 17 (1,279 Views)

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);

Frequent Advisor
yang shaohua
Posts: 33
Registered: ‎02-22-2011
Message 9 of 17 (1,276 Views)

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.

Frequent Advisor
yang shaohua
Posts: 33
Registered: ‎02-22-2011
Message 10 of 17 (1,274 Views)

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.

Occasional Advisor
rchaurasiya
Posts: 6
Registered: ‎07-29-2013
Message 11 of 17 (1,268 Views)

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

Hi yang,

 

You havent indicated your code donig SIG_UNBLOCK in new thread as Dennis said. Can you confirm that?

Also, pthread_kill() worked because you are forcing signal to be sent to new thread, while alarm will make it to post on process, where kernel will direct it to an appropriate thread.

 

Thanks, Ramesh

Frequent Advisor
yang shaohua
Posts: 33
Registered: ‎02-22-2011
Message 12 of 17 (1,264 Views)

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

Hi Ramesh

 I confirm that I BLOCK the signal in mainthread, and UNBLOCK in new thread, but cann't receive the signal in new thread.

but if I use pthread_kill to sent the alarm signal directly to the new thread, the ALARM signal can be received by the new thread.

 

thanks

yang

Frequent Advisor
yang shaohua
Posts: 33
Registered: ‎02-22-2011
Message 13 of 17 (1,258 Views)

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

In fact, I didn't use SIG_UNBLOCK . I just keep the original setting in old_signal_set, coz by default the ALARM is not blocked, so after block, I reset the original setting for unblock action. please refer to :

 

static sigset_t sig_set;
static sigset_t old_sig_set;

 

void BlockSignalAlarm()
{
  sigemptyset( &sig_set );
  sigaddset( &sig_set,SIGALRM );   
  int err = pthread_sigmask( SIG_BLOCK, &sig_set, &old_sig_set ); 
  if( err != 0 )
    printf("error %d occured when block signal alarm", err );
  else  
    printf("block signal alarm");
}

 

void UnblockSignalAlarm()
{
  int err = pthread_sigmask(SIG_SETMASK,&old_sig_set,NULL);
  if( err != 0 )
    printf("error %d occured when unblock signal alarm", err );
  else
    printf("unblock signal alarm") ;
}

 

after the process started, I call BlockSignalAlarm to keep the old_sig_set status firstly.

 

 

 

Occasional Advisor
rchaurasiya
Posts: 6
Registered: ‎07-29-2013
Message 14 of 17 (1,239 Views)

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

Hi yang,

 

I have left my program running in loop for a while, which uses routines you have shown here and I dont seem to see it failing. I presume those timing windows would have been taken care of.

Are you able to easily reproduce this? Would it be possible to share the full program which failed for you?

 

Thanks, Ramesh

Frequent Advisor
yang shaohua
Posts: 33
Registered: ‎02-22-2011
Message 15 of 17 (1,235 Views)

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

Hi,Ramesh

 It is my fault, I found the code can run correctly on another HP ITA64 machine. but failed on my certain machine although it is the same HP ITA64.

 why?

 

yang

Occasional Advisor
rchaurasiya
Posts: 6
Registered: ‎07-29-2013
Message 16 of 17 (1,228 Views)

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

I have edited my first response to clarify - signal going to process instead of thread was the main reason for original problem you mentioend. If you are facing this issue even after ensuring that signal should come to a particular thread, there should be something worth  taking a closer look. Looking at your program would be helpful yang.

 

Thanks, Ramesh

Acclaimed Contributor
Dennis Handly
Posts: 25,200
Registered: ‎03-06-2006
Message 17 of 17 (1,216 Views)

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

> I just keep the original setting in old_signal_set, coz by default the ALARM is not blocked

 

You might want to try my SIG_UNBLOCK fragment.

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.