LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-17-2012, 06:55 AM   #1
Navjot Arora
LQ Newbie
 
Registered: Apr 2012
Location: Bangalore
Distribution: J ARORA
Posts: 22

Rep: Reputation: Disabled
system function in linux


Hi All,

In the below code , when system function get executed (in LINUX and compiled with g++) , it will send the SIGCHLD signal to process (from which it is called) which doesn't happen in solaris code i mean if i run the same code in solaris, system function doesn't send any signal to process from which it is called.As a result of which my process in Linux got hanged.

#include <iostream>
using namespace std;
#include <cstdlib>
#include <signal.h>
void sigHandler(int sgnNbr)
{
cout <<"Signal " << sgnNbr << "caught" << endl;
system("echo $HOSTNAME");
exit(1);
}
int main()
{
signal(SIGCHLD,sigHandler);
system("echo $HOSTNAME");
return 0;
}

my question is :
1) why the process get hanged in the above case
2) why in solaris , system function doesnt send any signal

and my last question which is related to my real application is :

Actually , in my real application , some other process sending the SIGKILL signal to my process and in signal handler i am killing the one process using system function as a result it got hanged(which is similar to the above scenario). Is there any way to overcome this.

Please let me know if my query is not clear
 
Old 08-17-2012, 07:44 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
No process is able to catch a SIGKILL (9) signal. Can you please elaborate as to what the issue is with your real application?

As for your sample application, why are you calling system() from within the signal handler?? Duh... that is going to generate another SIGCHLD, and... in other words, the exit() function call in the signal handler will never be reached.


P.S. I should have added that a system() call will fork a child-process; hence when that child exits, a SIGCHLD signal is issued.

Last edited by dwhitney67; 08-17-2012 at 08:15 AM.
 
1 members found this post helpful.
Old 08-17-2012, 09:10 AM   #3
Navjot Arora
LQ Newbie
 
Registered: Apr 2012
Location: Bangalore
Distribution: J ARORA
Posts: 22

Original Poster
Rep: Reputation: Disabled
Hi,

Sorry that is SOGABRT not SIGKILL

the above program i wrote just to simulate and explain the behavior which is happening in my application

---------- Post added 08-17-12 at 07:41 PM ----------

read SOGABRT as SIGABRT
 
Old 08-17-2012, 09:15 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
According to
Code:
man 7 signal


       The entries in the "Action" column of the table specify the default action for the signal, as follows:

       Term   Default action is to terminate the process.

       Ign    Default action is to ignore the signal.

       Core   Default action is to terminate the process and dump core.

       Stop   Default action is to stop the process.

       First the signals described in the original POSIX.1 standard.

       Signal     Value     Action   Comment
       -------------------------------------------------------------------------
<...unrelated entries trimmed...>
       SIGCHLD   20,17,18    Ign     Child stopped or terminated
This is described as the POSIX-compliant behavior.

So it seems that for some reason, your program's default signal action has been modified. Since this property is inherited from the parent process, perhaps that is one avenue to explore. Other wise, perhaps the change is an inadvertent side effect of some API that your application is bound to.
It is possible that Solaris does not conform to POSIX standards for this.

The recursive signal handling in your example may be masking or exacerbating the real root of the matter.

--- rod.

Last edited by theNbomr; 08-17-2012 at 09:18 AM.
 
Old 08-18-2012, 03:59 PM   #5
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Having a look with strace it looks like system() includes a waitpid(). As the first waitpid() is never closed, the second waitpid() in the signal handler can’t be created. At this point it hangs.
 
  


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
LXer: Learn how to invoke Linux kernel function system calls LXer Syndicated Linux News 0 04-07-2010 09:10 PM
linux system() function + memory consumption mihirsevak Programming 2 02-17-2007 08:40 AM
How can i use tell function in linux system ? vskgopu Programming 1 07-20-2006 10:53 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM

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

All times are GMT -5. The time now is 01:25 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