User defined signal 1 terminates my POSIX program!
I have a program that runs some pthreads, and in every thread there is a connect()
, recv()
and send()
. The problem is that sometimes it suddendly closes the entire program showing the message "User defined signal 1"
. It runs in linux, using POSIX threads, in C. The code is like that:
pthread_mutex_t cur_lock;
int stop = 0;
void SocketsFunction(){
//..
connect();
while(开发者_JAVA技巧recv()<0)
{
//do stuff
send();
}
close();
return NULL;
}
void job()
{
//..
while (!stop)
{
if (something)
//..
else
{
stop = 1;
break;
}
pthread_mutex_unlock(&cur_lock);
SocketsFunction();
pthread_mutex_lock(&cur_lock);
}
pthread_mutex_unlock(&cur_lock);
return NULL;
}
main(){
//..
pthread_mutex_init(&cur_lock, NULL);
//..
for(i = 0; i < 30; ++i)
pthread_create(&pID, NULL, job, NULL);
//..
}
Something is sending "User defined signal 1" (SIGUSR1
I believe) to your process, and the default action for this signal is to terminate the process. If you want to avoid that you need to either stop whatever is sending the signal, install a signal handler for the signal, or block the signal in all threads (e.g. at the beginning of main before creating any threads) with pthread_sigmask
.
Since the code you included in your question has nothing to do with signal handling or what's sending the signal, I can't really be more specific.
精彩评论