Operating System - HP-UX
1756816 Members
2698 Online
108853 Solutions
New Discussion юеВ

Re: C : Using signals with the libpthread library

 
Rick Barlow
New Member

C : Using signals with the libpthread library

Hi, I have run into a problem when porting our application. The app is using a combination of signals and the sleep() system call. We recently had to add the libpthread to the list of libraries to link with, due to upgrading the product (ZCOM) that we are linking to, and this changed the behavior of the application. Here is an explanation. The app sets up a function to call in the sigaction for the SIGALRM signal.
in the code, some logic requires the alarm() call to be invoked, to wake up the process during a sleep() function. The SIGALRM signal handler sets a variable to 1. Below is a trimmed down example.

/* start of x.c */

#include
#include
int got_it = 0;

void H_alarm()
{
got_it = 1;
}

main()
{
struct sigaction act;
act.sa_flags = 0;
act.sa_handler = H_alarm;
sigaction(SIGALRM, &act, NULL);

alarm(5);
sleep(10);
if (got_it) {
printf("WENT INTO ALARM HANDLER\n");
} else {
printf("DID NOT GO INTO ALARM HANDLER\n");
}
}

/* end of x.c */

Two options...
1) cc -o x x.c
2) cc -o x x.c -lpthread

If linked without libpthread.a,
the response is WENT INTO ALARM HANDLER, after
the 5 seconds. If linked WITH libpthread.a,
the process still ends after 5 seconds, but it does not invoke the handler, and will print the second statement.

There are probably other ways to re-design the app, but it is a large app, and I would prefer if someone knew if there was a different compilation option, or additional value to set which would force the behavior to be the same as it is w/o linking to pthread.

Can anyone help? Thanks in advance!!
2 REPLIES 2
Jason Deckard
Occasional Advisor

Re: C : Using signals with the libpthread library

Hi Rick,

sleep() behaves differently in a multi-threaded environment. From the sleep manpage:

If a SIGALRM is generated for a multi-threaded process, it may not be delivered to a thread currently in sleep().

I am not familiar with your application that uses sleeps and alarms, but you could use sigwait() to block your program until it receives a SIGALRM.

Hope that helps,
Jason
[Insert humorous and/or inspirational quote here]
see call 1201019986
Occasional Contributor

Re: C : Using signals with the libpthread library

Hello Rick,
Did you finally find a solution to your problem concerning SIGALRM signal handler when you link your program with libpthread library, or did you have to look for an alternate way to do what you wanted to do ?
I'm having the exact same problem (messages posted on last tuesday and wednesday), but so far could not find anybody to help and am still trying to solve it.
Hope you get this message. Thank you in advance.
Regards