LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-28-2009, 07:32 AM   #1
nesta
Member
 
Registered: Aug 2006
Posts: 100

Rep: Reputation: 15
changing context thread of sw signal handler


Hi all,
i have a c program that contains a process of 2 threads:
1. main thread.
2. application thread

i have setup a sw signal handler to this process, i write this code in the application thread but i noticed that when the sw signal is sent & the sw signal handler is invoked, it is running in the context of the main thread.

Actually i want the sw signal handler to run in the context of the application thread not the main thread.

i tried to use pthread_sigmask in the main thread & the application thread such that:
1. i block the signal in the main thread
2. unblock the signal in the application thread

but the result is:
i receive no signals @ all.

how can i force the sw signal handler to run in certain thread other than the main thread?

Note that my signal type is SIGUSR1

thanks in advance.
 
Old 05-28-2009, 07:52 PM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Do you intend to send signal SIGUSR1 from the main thread to the application thread, or is signal SIGUSR1 coming from some other source?
 
Old 05-31-2009, 04:12 PM   #3
nesta
Member
 
Registered: Aug 2006
Posts: 100

Original Poster
Rep: Reputation: 15
Code:
Do you intend to send signal SIGUSR1 from the main thread to the application thread, or is signal SIGUSR1 coming from some other source?
the signal SIGUSR1 will be sent from ISR handler not from main thread?
 
Old 05-31-2009, 06:16 PM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Don't use a signal handler, if at all possible.

David R. Butenhof has written an excellent book published by Addison Wesley. The title is Programming with POSIX Threads. In this book is the following paragraph about signals:
Quote:
It is always best to avoid using signals in conjunction with threads. At the same time, it is often not possible or practical to keep them separate. When signals and threads meet, beware. If at all possible, use only pthread_sigmask to mask signals in the main thread, and sigwait to handle signals synchronously within a single thread dedicated to that purpose. If you must use sigaction (or equivalent) to handle synchronous signals (such as SIGSEGV within threads), be especially cautious. Do as little work as possible within the thread-catching function.
So use sigwait() in a separate signal-handling thread.

Another recommendation: the book has many, many things to say that would probably interest you. You could save much time in the long run by buying this book and reading almost the whole thing from cover to cover.

Highly recommended.
 
Old 06-01-2009, 09:44 AM   #5
nesta
Member
 
Registered: Aug 2006
Posts: 100

Original Poster
Rep: Reputation: 15
[QUOTE]Don't use a signal handler, if at all possible./QUOTE]
do u mean that it is preferred not to use one of the following apis:
1.signal
2.sigaction
it looks strange to me why shouldn't i do this?

anyway i use sigwait as u suggested,
here is my code where:
1. i block 3 signals in the main thread.
2. i create another thread whose signals will be inherited from the main thread signals i.e. the 3 signals are blocked in the child thread aslo.
but i have a question here: How can i receive these signals in the child thread, they are already blocked?

3. i call sigwait in the child thread.

My results after executing the following code are:
1. when i pressed CTRL + C, i could successfully receive the SIGINT in the child thread & the SIGINT is printed on the stdout.
2. when i tried to send the same signal inside the ISR of an interrupt using send_sig_info i receive nothing.can anyone tell me why?


[CODE]
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include<fcntl.h>
#include<assert.h>

static sigset_t signal_mask; /* signals to block */
void *signal_thread (void *arg);

int main (int argc, char *argv[])
{
int configfd;
char buf[10]={0};
pthread_t sig_thr_id; /* signal handler thread ID */
int rc; /* return code */



sigemptyset (&signal_mask);
sigaddset (&signal_mask, SIGINT);
sigaddset (&signal_mask, SIGTERM);
sigaddset (&signal_mask, SIGUSR1);



/* kernel needs to know our pid to be able to send us a signal ->
* we use device file for this -> do not forget to insert the module.
*/
configfd = open("/dev/DSP-B_DEVICE", O_WRONLY);
if(configfd < 0)
{
perror("open");
printf("Can't open the DSP-B_Device");
assert(0);
return;
}

sprintf(buf, "%d", getpid());
if (write(configfd, buf, strlen(buf) + 1) < 0)
{
perror("fwrite");
/*return -1;*/
printf("Can't write to the DSP-B_Device");
return;
}



rc = pthread_sigmask (SIG_BLOCK, &signal_mask, NULL);
if (rc != 0)
{
/* handle error */
}
/* any newly created threads inherit the signal mask */


rc = pthread_create (&sig_thr_id, NULL, signal_thread, NULL);
if (rc != 0) {
/* handle error */
printf("cant create thread\n");
}

pause();

}


void *signal_thread (void *arg)
{
int sig_caught; /* signal caught */
int rc; /* returned code */


rc = sigwait (&signal_mask, &sig_caught);
if (rc != 0) {
/* handle error */
}
switch (sig_caught)
{

case SIGUSR1:
printf("SIGUSR1\n");
break;
case SIGINT: /* process SIGINT */
printf("SIGINT\n");
break;
case SIGTERM: /* process SIGTERM */

break;
default: /* should normally not happen */
fprintf (stderr, "\nUnexpected signal %d\n", sig_caught);
break;
}
}/CODE]
 
Old 06-01-2009, 11:01 PM   #6
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by nesta View Post
do u mean that it is preferred not to use one of the following apis:
1.signal
2.sigaction
it looks strange to me why shouldn't i do this?
You shouldn't do this because your pthreads program is unlikely to work correctly if you do.
Quote:
Originally Posted by nesta View Post
How can i receive these signals in the child thread, they are already blocked?
In the child thread, do this:
Code:
sigemptyset (&signal_mask);
sigaddset (&signal_mask, SIGINT);
sigaddset (&signal_mask, SIGTERM);
sigaddset (&signal_mask, SIGUSR1);
rc = pthread_sigmask (SIG_UNBLOCK, &signal_mask, NULL);
if (rc != 0)
{
/* handle error */
}
Oh. And. In your code, your signal handling thread runs everything just once. So if you get one of these signals, it will be handled by your signal handling thread, but if you then get another of the same signal, or any of the other signals, this thread will already have finished. If your intent is simply to finish up the whole application, this is fine. But if you want to process such signals more than once while running this application, you may wish to run this signal handling code in a loop within your signal handling thread.
Quote:
Originally Posted by nesta View Post
2. when i tried to send the same signal inside the ISR of an interrupt using send_sig_info i receive nothing.can anyone tell me why?
I haven't a clue. Sorry.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
user-define signal handler & coredump for signal 11(SIGSEGV) Alexlun Linux - Software 2 05-24-2009 06:37 AM
How to reinstall a signal handler after exec ( Strange behaviour in signal handling ) lali.p Programming 0 09-20-2008 12:11 PM
signal SIGINT handler SOMETIMES doesn't exit Peterius Programming 3 05-25-2008 11:55 PM
signal handler routine vdx Programming 7 09-24-2007 12:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration