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.
ClassA::start() {
while(true) {
// something got done in here
}
}
Goal: I want to print out the "Got SIGUSR1 handled" line when executed command "kill -SIGUSR1 <pid>" without terminating my application (which I expect what it should be.)
Problem: The program printed out the line, but it's also killed (which I don't want to). Anyone see what I am doing wrong here? How to fix it please.
Firstly, when posting code, please enclose it in code tags:
Code:
[ code ] like this, but without spaces [ /code ]
Secondly, I've tested your program, with "// something got done in here" replaced by "while (true) sleep(1);". I then went on to do "while true; do killall -USR1 a.out; done", and it happily printed out a message ad infinitum.
That leads me to believe that the error is a sympton of whatever it is you have replaced by "//something got done in here". Please post the code here, or--if it's more than, say, 20 lines--in a pastebin or on your website, so we can have a look at it.
Firstly, when posting code, please enclose it in code tags:
Code:
[ code ] like this, but without spaces [ /code ]
Secondly, I've tested your program, with "// something got done in here" replaced by "while (true) sleep(1);". I then went on to do "while true; do killall -USR1 a.out; done", and it happily printed out a message ad infinitum.
That leads me to believe that the error is a sympton of whatever it is you have replaced by "//something got done in here". Please post the code here, or--if it's more than, say, 20 lines--in a pastebin or on your website, so we can have a look at it.
Thanks for your respond. So your replacement would be like this:
Code:
while(true) {
while(true)
sleep(1);
}
Due to the complication of the project policies, I can't post the whole code. But it would do something like this:
Code:
while(true) {
int iResult = select(maxFd+1, readFd, NULL, NULL, NULL); // where maxFd and readFd are ClassA variables
if (iResult == -1) {
printf("Error at select statement.\n");
exit(1);
}
if (FD_ISSET(fd1, &readFd)) {
// do something
}
if (FD_ISSET(fd2, &readFd)) {
// do other thing
}
}
Never mind. I got it resolved. Just in case someone has the same problem. Here is the solution. Since the select statement got the errno = 4 (EINTR), I need to handle that error case. Instead of exiting I have to reset the fd set and continue to loop back to call the select statement.
Code:
while(true) {
setFds(); // function to set fds
int iResult = select(maxFd+1, readFd, NULL, NULL, NULL); // where maxFd and readFd are ClassA variables
if (iResult == -1) {
printf("Error at select statement.\n");
if (errno != EINTR) {
exit(1);
}
continue;
}
if (FD_ISSET(fd1, &readFd)) {
// do something
}
if (FD_ISSET(fd2, &readFd)) {
// do other thing
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.