Does a process goes back to same location after handling SIGTERM ?
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.
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Rep:
Does a process goes back to same location after handling SIGTERM ?
Hi all,
I have a function like
maincode:
Code:
struct sigaction act;
act.sa_handler = SIGACTION_SIG_CAST (mychildhandler);
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
if (sigaction(SIGTERM, &act, (struct sigaction *)0) == -1)
cout << "FAILED :( \n";
// Do the Same for myPARENThandler
.
fork()
.
.
// MAKE parent to run in an infinite loop and make child to execute some task and quit.
The signal handlers are :
Code:
void mychildhandler(int signalnum )
{
cout<< "Entering into mychildhandler \n";
if (signalnum != SIGTERM )
return;
myfun();
cout<< "Finished with mychildhandler \n";
return;
}
void myPARENThandler(int signalnum )
{
cout<< "Entering into mychildhandler \n";
if (signalnum != SIGTERM )
return;
myfun();
cout<< "Entering into mychildhandler \n";
exit(1);
}
When the parent or child receives SIGTERM, myPARENThandler/mychildhandler gets executed. After the execution of "exit(1);" the parent gets exited.
The child has to do some task. In the mid of the task when the child receives SIGTERM, the above function (mychildhandler) gets executed. I am getting the output "Finished with mychildhandler ". After this statement nothing is getting printed (remember that the child is doing some task and is printing a line after its execution gets completed.)
Now my question is
After calling the mychildhandler function does the child goes back to the original location where it received the sigterm?
If it goes back how can I make sure that it goes back ?
If it does not goes back how can I make it to go back to the location where it left ?
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Original Poster
Rep:
Hi Hko,
Thanks for the reply. The mychildhandler function is getting called in my case. As I said my questions are :
After calling the mychildhandler function does the child goes back to the original location where it received the sigterm?
If it goes back how can I make sure that it goes back ?
If it does not goes back how can I make it to go back to the location where it left ?
Originally posted by Hko
Signal handler functions do not survive a fork(). So you need to call fork() before sigaction() if you want the child process to handle the signal.
Signal handlers DO survive fork()'s. To quote Richard Stevens:
Quote:
When a process calls fork the child inherits the parent's signal dispositions. Here, since the child starts off with a copy of the parent's memory image, the address of a signal-catching function has meaning
in the child.
But:
Quote:
When a program is execed the status of all signals is either default or ignore. Normally all signals are set to their default action, unless the process that calls exec is ignoring the signal. Specifically, the exec functions change the disposition of any signal that are being caught to their default action and leave the status of all other signals alone. (Naturally a signal that is being caught by a process that calls exec cannot be caught in the new program, since the address of the signal-catching function in the caller probabllly has no meaning in the new program file that is execed.)
Also, in some cases it's best to install signals handlers before a fork() because of the tiny window or race between execution of any of the processes involved and the installation of the signal handler itself: a signal could be delivered in between and will cause its default action (with SIGTERM it would be to terminate the process). The parent and the child may share a signal handler. In this case it would be easy with a global pid_t variable-
Quote:
Originally posted by murugesan
After calling the mychildhandler function does the child goes back to the original location where it received the sigterm?
It goes back to the original location. The following guidelines are a must:
1- Save errno and restore it later. Use something like "int save_errno = errno;" and "errno = save_errno;" in the beginning and the end of the signal handler, respectively.
2- Some system calls may return EINTR if they were interrupted by a catched / ignored signal, or you may use SA_RESTART in act.sa_flags
3- Ignore all signals on critical sections of code with setprocmask(2)
Note that both handlers are saying: "Entering into mychildhandler \n"
Originally posted by primo
Signal handlers DO survive fork()'s. To quote Richard Stevens:
"man fork" (on Debian sarge) says:
Quote:
fork creates a child process that differs from the parent process only in its PID and PPID, and in the fact that resource utilizations are set to 0. File locks and pending signals are not inherited.
You're right.
I've misinterpreted the bold sentence of the man page. Sorry.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.