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.
In following program i registered signal SIGINT. When executing this program if SIGINT is sent while fgets is waiting for input, i want to do something in signal handler so that fgets should be unblocked and further processing should happen.
#include <stdio.h>
#include <signal.h>
void SignalHandler(int Signum)
{
//
// Signal received.
//
}
You can do stuff in the signal handler. If you want your main function to continue executing, I'm afraid you need to use lower level read function (at which point you'll have to take care of buffering and finding line ends) which returns -1 with errno == EINTR when signal is returned.
If the above works, it's only by sheer luck. For one, there is a race condition between when signal handler is set up and when jmp_buf is initialized. Furthermore, this will accumulate stuff on your stack. And lastly, you leave the standard library in God knows hat state when you abruptly interrupt fgets. It's not that it's “ugly”, it's plane broken.
If the above works, it's only by sheer luck. For one, there is a race condition between when signal handler is set up and when jmp_buf is initialized. Furthermore, this will accumulate stuff on your stack. And lastly, you leave the standard library in God knows hat state when you abruptly interrupt fgets. It's not that it's “ugly”, it's plane broken.
The race condition condition is there, true, but that's easily fixed by setting the handler after setjmp.
The standard library affected? How? Longjmp can be safely used from a signal handler, fgets can be intermixed with other buffered io so there can't be an impact on other calls. The contents of the buffer will be lost but fgets will continue to function. Broken? Maybe but the hyperbole is a little over the top.
OP's case is reading lines. My example shows reading lines. They do not differ.
You can't say the same thing about gets. There is no safe way to use gets. Longjmp can be safely used and it can be safely used in signal handlers. You just need to be aware that the state of the data being read at the time of the interrupt is indeterminate.
So far you've made no argument to support your position.
I should add that I don't think it is a good idea to do this in anything but trivial code where I don't care about the state of the input. There are too many other solutions such as setting a variable in the signal handler and responding to it after returning from the fgets however that's not what the OP asked for.
Longjmp can be safely used and it can be safely used in signal handlers.
I'm not saying longjmp cannot be used from signal handlers. I'm saying your solution to OP's problem is incorrect and provided code that demonstrates that.
Quote:
Originally Posted by SoftSprocket
however that's not what the OP asked for.
I also don't think OP asked for a solution where data can be dropped.
Thanks @SoftSprocket for your response but this is working for one time means second time on words sigint_handler is not getting called.
For first time program runs and when i press ctrl+c "Hello, sigint!" is printed on stout after that program is waiting for user to give input but second time on words message is not displayed and program is waiting for input.
I visited this link http://www.csl.mtu.edu/cs4411.ck/www...oto/sig-1.html here also i am facing same problem signal handler is executed only on pressing ctrl+c for first time.
Last edited by user@123; 12-08-2014 at 03:33 AM.
Reason: Added references.
Note: using setjmp/longjmp is essencially the same thing as ON ERROR GOTO in BASIC, exceptions in C++/Java/Ada, FCB.EXLST in System/360 Assembly. I wouldn't use any of them, if I could avoid.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.