Operating System - HP-UX
1753899 Members
7857 Online
108809 Solutions
New Discussion

Problem regarding linking signal handling program with "-lpthread" option on HP-UX 11.0

 
Hao Yu
Occasional Contributor

Problem regarding linking signal handling program with "-lpthread" option on HP-UX 11.0

Problem regarding linking signal handling program with "-lpthread" option on HP-UX 11.0

Howdy!

My program is to catch and handle sigmentation violation triggered
by out of bound array reference. When I link the program without
"-lpthread", my handler is called. When I link the program with
"-lpthread" option, my handler is not called, and the program
gives "Segmentation fault" itself. The code and execution output
is as below (it is very short):


-------Program-------

#include
#include
#include
#include

float a[100];

void sig_handler2( int sig_num ) {

printf( " - handler2: sig_num = %d\n", sig_num ); fflush( stdout );
exit(1);
}

void set_signal( void ) {

if ( signal( SIGSEGV, sig_handler2 ) == SIG_ERR ) {
fprintf( stderr, "can not set handler for SIGSEGV\n" );
exit(1);
}
}

void main() {

int i, ip=0;
int kk = 500000;

set_signal();

printf( " - start\n" );
for (i=0; i a[i] = i*i;
}
}

-------Compiling-------
make native.handle
cc +O2 -c sig.native.c
cc +O2 -o native.handle sig.native.o
make native.wrong
cc +O2 -c sig.native.c
cc +O2 -o native.wrong sig.native.o -lpthread

-------Execution --------
./native.handle
- start
- handler2: sig_num = 11

./native.wrong
- start
Segmentation fault
--------------------------


Also, I changed my signal handling code to use "POSIX sigaction()".
When I link the program without "-lpthread", the handler is called.
When I link the program with "-lpthread", the program is just hanging
there. The code and execution output is as below:


-------Program --------
#include
#include
#include
#include

float hao[100];

void sig_handler2( int sig_num ) {

printf( " - handler2: signo = %d\n", sig_num ); fflush( stdout );
exit(1);
}

void set_signal( void ) {

struct sigaction sact;

sact.sa_handler = sig_handler2;
sact.sa_flags = 0;
sigemptyset( &sact.sa_mask );

if (sigaction( SIGSEGV, &sact, (struct sigaction *) NULL ) < 0) {
fprintf( stderr, "SIGSEGV not set\n" );
exit(1);
}
}

void main() {

int i, ip=0;
int kk = 500000;
set_signal();

printf( " - start\n" );
for (i=0; i hao[i] = i*i;
}
}

-------Compiling --------
make posix.handle
cc +O2 -c sig.posix.c
cc +O2 -o posix.handle sig.posix.o
make posix.wrong
cc +O2 -c sig.posix.c
cc +O2 -o posix.wrong sig.posix.o -lpthread

-------Execution --------
./posix.handle
- start
- handler2: signo = 11

./posix.wrong
- start

--------------------------

Note:

1. machine: hp v-class
os: hp-ux 11.0

2. The reason I encountered this problem is that I was trying to use signal
handler to catch sigmentation violation introduced by out of bound array
reference in parallel loop. Parallel program is always linked with
"-lpthread" option.

student
1 REPLY 1
Mike Stroyan
Honored Contributor

Re: Problem regarding linking signal handling program with "-lpthread" option on HP-UX 11.0

The heart of your problem in the test program is that you are writing all over global data, including pthread synchronization variables.
The signal handler spins on a broken spinlock variable when you call printf or even exit().
You can make your example work by changing the
hao[i] = i*i;
line to a read-
ip += hao[i];
That gets a signal without overwriting all that memory.

You should also be including pthread.h and compiling with -D_POSIX_C_SOURCE=199506L as documented in "man pthread".

You really should not be using printf in a signal handler. The stdio functions are not reentrant. You could get in more trouble when handling a signal raised within a stdio function.