LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 09-12-2005, 08:28 AM   #1
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
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 ?

Regards,
Murugesan
 
Old 09-12-2005, 11:04 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
Old 09-12-2005, 11:44 PM   #3
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Original Poster
Rep: Reputation: 29
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 ?


-Murugesan
 
Old 09-13-2005, 03:57 AM   #4
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Quote:
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"
 
Old 09-13-2005, 04:29 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
After calling the mychildhandler function does the child goes back to the original location where it received the sigterm?
why don't you experiment and find out?
It's the only way to learn.
 
Old 09-13-2005, 06:35 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
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.
 
Old 09-13-2005, 07:44 AM   #7
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Original Poster
Rep: Reputation: 29
Hi primo,

Quote:
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
This helped me in resolving the issue.

Thanks a lot.


Thanks for others too for posting your comments.

Regards,
Murugesan
 
  


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
Linux vs. Windows: process handling learnfast Linux - Newbie 2 05-27-2005 12:50 PM
I need a a gprs modem to send log files back from a remote location any info paulb2244 Linux - Hardware 1 06-19-2004 02:57 PM
Logout process - can't get back the login screen DarkChief Linux - General 4 04-21-2004 05:45 AM
Zombie Process Handling kghoshal Linux - Newbie 2 07-21-2003 09:10 AM
nagios sigterm dallasnewbie Linux - Networking 1 05-28-2003 10:32 AM

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

All times are GMT -5. The time now is 11:57 AM.

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