ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
[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?
/* 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 */
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
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.