When you install any signal handler, they are just installed and not executed untill the signal is received by them.
Now from your code its clear that you are not creating any child inside the main(). However, the SIGHUP is handled, what does it mean is that untill the calling shell program exits; this process is not going to get SIGHUP rather it runs in the while(1) only.
Now when you exit the bash (or whatever shell you use) itself , the SIGHUP handler gets called (you might not been able to see this after you exited) and here a child gets created which inturn calls execl() to replace the child process memory area with a new binary. In this process, only the PCB is kept by the scheduler but with the EIP reset as well as other execution stack pointers along with the binary text. Hence whatever signals this new binary code has handled only works and not the earlier haldlers hold good.
It means the parent now exits after a sleep of 60 seconds and the child is all togather a new executable with a overridden signal handlers.
Now lets examine a few more cases:
1) This new binary has not handled SIGHUP -means the default disposition would apply, and you won't see your sighandler executing.
2) This new binary has a different handler for SIGHUP -it'll execute only that and not the above signalhandler which you wrote.
3) This new binary is the same your executable -It should go in the execution scenario which you are expecting, however there is a default timeout is associated with the signal handlers when the Linux kernel executes them asychronously. Hence the fork() and the sleep of 60 seconds inside the handler is not advicable; instead use vfork() and a sleep of not more than a few hundred miliseconds.
I feel you should use a different terminal to see the results or call bash on your default the login shell prompt before starting your program in the background (& option) and then exit bash to see the results.
Last edited by Praveen_218; 10-15-2010 at 03:44 PM..
Reason: Typo error correction.
This User Gave Thanks to Praveen_218 For This Post:
Hello!
I am working on a server where I should have 4 (resident)processes, one of them being "the father" of the others, so I do 3 forks. The problem that I have is that I do an accept (for sockets) in the "father" process and I want to transmit the job to one of the processes "child" with... (3 Replies)
Hello,
I would like to ask you for a little help with program I'm working on. I have problems with signals and synchronizing processes (I'm quite new to this part of programming).
Process "parent" creates new child process "child1" and this process creates new child process "child2". The... (2 Replies)
Hi!
Can someone explain me exactly this technique? Why a process (PARENT) creates a copy of itself with FORK (CHILD)? What's the reason of this behaviour? Sorry, but I cannot understand the logic behind it.
Thanks. (4 Replies)
On my *nix box, I have a telegram program.
When I go like
tel person "la\nla\nla\n"
the person sees
"la\nla\nla\n"
However, when I have a program that forks and execs tel like:
pid = fork();
if (pid < 0) {
perror("fork failed");
exit(EXIT_FAILURE);
}
if (pid == 0) {... (7 Replies)
Hi,
I need to find/implement an application that FTPs (puts) all new files in a certain directory to an external storage unit. This application should check for new files every 10 seconds (leaving the FTP connection open in between the 10 seconds).
The easiest way would be if there are... (2 Replies)
I need to ssh to a remote server and run my script there. This is my script.
$ssh = "ssh username@host";
$cmd = "$ssh 'cd <my dir> && < sudo Run_exe>'";
my $pid = fork;
if ($pid == 0){
exec $cmd;
}
When I run this I get:
pccons_getchar: got r == 0 (1 Reply)
Hello World!
I am writing code in C++ which have to launch another application X using exec().
I would like to set some limits on it using setrlimit etc...
My problem is that i don't know how to forbid using fork() and strlimit by application X.
How can i do it? (3 Replies)
Hello everybody.I want to make clear that i am not going to ask from anybody to build my asignement but i have a big problem. I can't seem to find anywhere ONE good example on C about what i am trying to do:wall:.I think it is simple. All i ask is one example, even a link is fine.
So, i want to... (1 Reply)
Hello... And thanks in advance for any help anyone can offer me on my question! I've been doing a lot of reading to try and find my answer... But I haven't had any luck
What I'm trying to understand is where a child process inherits global environment variables from? I understand the exec()... (2 Replies)