Multiple Signals


 
Thread Tools Search this Thread
Top Forums Programming Multiple Signals
# 1  
Old 01-29-2003
Multiple Signals

I want to catch multiple instances of same signals serially ie the signal handler should be invoked recursively for multiple instances of the same signal.How it is possible ?
# 2  
Old 01-29-2003
This is an odd thing to want. This is the way signals used to work and it was nasty.

You can still get the old behavior though. The details depend on which signal interface you are using. Posix is the way to go, so I'll assume that you're using that.

When you install your signal handler with sigaction(), specify the SA_NODEFER flag in the flags argument.

Then if your signal handler is running because you process got a signal, and another occurence of the same signal happens, your signal handler will be interrupted with another call to itself.

If you come to your senses and don't specify SA_NODEFER, then if your signal handler is running because your process got a signal, and another occurence of the same signal happens, the signal is deferred. When your handler returns, it will be called again to process the new signal.
# 3  
Old 01-30-2003
I am writing a Process Manager for an inhouse project. It spawns new process and waits for a SIGCHLD signal to occur,to which it has to do an activity.The problem is that if multiple child gets killed at the same time my module receives only a single instance of SIGCHLD.
As per your post :

"another occurence of the same signal happens, your signal handler will be interrupted with another call to itself."

The following example doesn't vindicates it:

#include <stdio.h>
#include <signal.h>
void callme ( int sign )
{
printf ( "Called \n" ) ;
sleep ( 5 ) ;
}
main ( )
{
sigset_t mask ;
struct sigaction action ;
sigemptyset( &mask ) ;
action.sa_mask = mask ;
action.sa_flags = SA_NODEFER ;
action.sa_handler = callme ;
sigaction ( SIGUSR1 , &action , NULL ) ;
while ( 1 ) ;
}
# 4  
Old 01-30-2003
Quote:
Originally posted by S.P.Prasad


"another occurence of the same signal happens, your signal handler will be interrupted with another call to itself."

The following example doesn't vindicates it:
How could it? You need another printf that says "Finished sleeping" or something after the sleep in your handler. Then you need to send a usr1 signal to the process from another window. After the the "Called" prints out, immediately send another usr1 signal to the process. You then should see:
Called
Called
Finished sleeping
Finished sleeping

And, oh yeah, you need to remember that printf is buffered! If you want to see the characters in real time as the program runs, you gotta unbuffer stdout. Below I have modified your code enough that it can now be used to illustrate that your kernel is behaving correctly...
Code:
#include <stdio.h>
#include <signal.h>
void callme ( int sign ) 
{
       printf ( "Called \n" ) ;
       sleep ( 5 ) ;
       printf( " Finished sleeping \n" ) ;
}
main ( )
{
       sigset_t mask ;
       struct sigaction action ;
       setvbuf(stdout, NULL, _IONBF, 0);
       sigemptyset( &mask ) ;
       action.sa_mask = mask ;
       action.sa_flags = SA_NODEFER ;
       action.sa_handler = callme ;
       sigaction ( SIGUSR1 , &action , NULL ) ;
       while ( 1 ) ;
}

# 5  
Old 01-31-2003
First ,by Default , output to a terminal is line buffered and all other input/output is fully buffered.Hence I think it is not mandatory to call setvbuf ( ).

Secondly, I don't think the statement
printf ( " Finished Sleeping\n" ) was necessary cause the code will definitely print
"Called" as many times as you will send signal externally.

Thirdly If we send signals at intervals definitely the handler will be called that number of times.

I will explictly state my problem definition :
After we compiled and excuted this code , we opened up another teminal window and find out the pid of the executable. Assuming the pid to be 1234 we issued the following command at the prompt :

# kill -USR1 1234 ; kill -USR1 1234 ; kill -USR1 1234 ; kill -USR1 1234 ; kill -USR1 1234 ; kill -USR1 1234 ;kill -USR1 1234 \n

The desired output should be that the handler should be called 7 times but it was called only ONCE ???

Why ?
# 6  
Old 01-31-2003
I tested my code and got the results that I descibed. This was on HP-UX 11.00. Did you even give my code a try? Do exactly what I said and you will see that it works.

My guess is that you are sending the signals too fast. You need to let the printf finish before you send another signal. printf is not re-entrant. By sending the characters to a buffer first, you are exacerbating the timing problem. If you're not going to at least try my advice then I really don't see any point in offering it.
# 7  
Old 02-01-2003
I did tried your code as you have posted.But it did not generate the result as you have stated. The signal handler was called only once inspite of multiple signals instances feeded to it. I have tried your code both on SCO 5.5 and SunOS 5.8.The output is same as I have stated.
The problem is that for the Process Manager there can be a possibility that all the process can get killed at a single moment. In this scenario I will receive a single instance of SIGCHLD. This will be a very major problem with respect to development.
Thanks for all your consideration to my request but if you can provide us a best solution ( as you have already did many times ) it would be very helpful to us.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Help with Signals

Hi All, The problem statement is as below: Problem: A process (exe) is getting executed in background. The output of this process is getting logged in a file. After successfully running for some time the process gets terminated. In the log file following is present: ^M[7m Interrupt ^M[27m... (8 Replies)
Discussion started by: Praty.27
8 Replies

2. Programming

C++ signals Linux

how can do this programs in c++ Program description: Infinite loop in a program starts working with 2 seconds the screen (console) "I 'm trying" to write, but it automatically after 10 seconds, the screen "Close" will terminate the execution of typing. c++ code (3 Replies)
Discussion started by: ss54
3 Replies

3. Programming

Can we debug Signals

Hi, In our program, we are using SIGTERM and i tired to put break point in this function. But my debuger is unable to brake at that point. I am working on Mac X and using XCode. Thanks (1 Reply)
Discussion started by: Saurabh78
1 Replies

4. OS X (Apple)

How to debug signals

Hi, In our program, we are using SIGTERM and i tired to put break point in this function. But my debuger is unable to brake at that point. I am working on Mac X and using XCode. Thanks (0 Replies)
Discussion started by: Saurabh78
0 Replies

5. Programming

threads and signals

can any one give me an example of a concurrency program in threads and signals, i.e how to deliver messages between threads using signals. thanks (2 Replies)
Discussion started by: moe_7
2 Replies

6. UNIX for Dummies Questions & Answers

threads and signals

can any one give me an example of a concurrency program in threads and signals, i.e how to deliver messages between threads using signals. thanks (0 Replies)
Discussion started by: moe_7
0 Replies

7. Programming

Using Signals

How can use signals in a C program If i want a child program to signal it's parent program that it(child) program has completed the task that it was assigned.:confused: (2 Replies)
Discussion started by: kapilv
2 Replies

8. UNIX for Dummies Questions & Answers

Signals...

(posted this in the scripting forum as well, but figured it should go here) So, what's going on is this: For our program, we had to create our own shell, and if the user pressed ctrl-c just at the cmdline, then this signal would be ignored, but if there is a foreground process running, let's... (0 Replies)
Discussion started by: blind melon
0 Replies

9. Programming

Threads Signals

Hi In my process there are few threads. Now, lets say all the threads are blocked for some reason or other.. now i read it somewhere that the kernel in this situation sends in some signal which can be caught. please let me know what signal is it and more details about that.. Thanks in... (1 Reply)
Discussion started by: uday_kumar_spl
1 Replies

10. Programming

Signals In HP-UX

does the way of handling, interrupting signals in HP-UX same as that of solaris. If there is difference than what it is.?:confused: (1 Reply)
Discussion started by: kapilv
1 Replies
Login or Register to Ask a Question