LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Trap command (http://www.linuxquestions.org/questions/programming-9/trap-command-4175484061/)

waddles 11-09-2013 10:14 PM

Trap command
 
I have created a background process which self destructs after less than a second and returns a SIGALRM to my shell.
While the man page seems to say the response of the trap command is instantaneous I am unsure how that should be interpreted. Does the trap in one function of my program immediately through control from another function in my FG process to the argument of the trap? Then once the function in the argument finishes executing does control revert to where it was at when the trap sprung?
Or does processing have to work through and back to where the trap statement is placed before springing once the shell receives the SIGALRM signal?? I need a reference for a simpleton's tutorial if you aren't unable to address all of the above as I think I have this congratulated with interrupt handling.

Richard Cranium 11-10-2013 03:25 PM

I'm afraid that isn't very clear what you are asking. Perhaps an example would help.

waddles 11-12-2013 12:39 AM

Moving this to a non-*nix forum is NOT helpful!
It is germain to bash shell scripts and Linux processing in this case.
Nevertheless, it sounds like U may not be familiar with interrupts, which usually place their current location within a script on a stack in order to return processing to it following some interrupt code. With a NON-ERRONEOUS signal being emitted from a background script and which kills itself I can presume there is no need to return. Thus my query regarding that phase is understandable as the trap command will just invoke a function and no further action can be taken.
My quandary is: if a non-erroneous signal is emitted from within one function (or background process generated by) of the script and is trapped within a second which causes a third function to process, what action is taken when the third function completes.
I guess that control is returning to the main process from the third function but I can find no definitive documentation.

bigearsbilly 11-13-2013 09:11 AM

Trying to do complicated signal handling in shell scripts is like herding cats.

waddles 11-13-2013 11:51 PM

Am closing this query as it appears the information requested is not available here.

bigearsbilly 11-14-2013 10:27 AM

wise move, lots of idiots here.

ntubski 11-14-2013 01:22 PM

Quote:

Originally Posted by waddles (Post 5061638)
While the man page seems to say the response of the trap command is instantaneous I am unsure how that should be interpreted.

Not always instantaneous:
Quote:

3.7.6 Signals

If Bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes. When Bash is waiting for an asynchronous command via the wait builtin, the reception of a signal for which a trap has been set will cause the wait builtin to return immediately with an exit status greater than 128, immediately after which the trap is executed.
Quote:

Originally Posted by waddles
Does the trap in one function of my program immediately through control from another function in my FG process to the argument of the trap?

Timing is not guaranteed: the trap handler starts executing as soon as the signal is recieved (unless something else is being executed, as mentioned above), but there may be some delay between the time the signal is sent and when it is received.

Quote:

Then once the function in the argument finishes executing does control revert to where it was at when the trap sprung?
Or does processing have to work through and back to where the trap statement is placed before springing once the shell receives the SIGALRM signal??
Definitely the first one. I can't point to an actual sentence in the manual, but you can try it and see.


All times are GMT -5. The time now is 03:47 PM.