- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Why SIGALRM cann't happen in thread on HPUX?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2013 08:34 PM
07-17-2013 08:34 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2013 02:14 AM - edited 07-30-2013 05:14 AM
07-29-2013 02:14 AM - edited 07-30-2013 05:14 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2013 08:10 PM
07-29-2013 08:10 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2013 11:08 PM
07-29-2013 11:08 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2013 11:31 PM
07-29-2013 11:31 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2013 12:49 AM
07-30-2013 12:49 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2013 12:53 AM
07-30-2013 12:53 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2013 12:55 AM
07-30-2013 12:55 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2013 01:13 AM
07-30-2013 01:13 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2013 01:15 AM
07-30-2013 01:15 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2013 01:20 AM
07-30-2013 01:20 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2013 01:27 AM
07-30-2013 01:27 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2013 01:40 AM
07-30-2013 01:40 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2013 03:26 AM
07-30-2013 03:26 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2013 03:33 AM
07-30-2013 03:33 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-30-2013 05:26 AM
07-30-2013 05:26 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2013 12:12 AM
07-31-2013 12:12 AM
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.