Multiple Signals


 
Thread Tools Search this Thread
Top Forums Programming Multiple Signals
# 8  
Old 02-01-2003
Hmmm... there seems to be two very different questions here. For your first question about how to write a signal handler that will interrupt itself, the code that I posted really will work provided that you let the prinf finish prior to sending another signal. If this isn't working for you then you not timing the signals correctly. So let's just automate the procedure with a child process that will send the signals with correct timing:
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 ;
        if(fork()) {
                sigemptyset( &mask ) ;
                setvbuf(stdout, NULL, _IONBF, 0);
                action.sa_mask = mask ;
                action.sa_flags = SA_NODEFER ;
                action.sa_handler = callme ;
                sigaction ( SIGUSR1 , &action , NULL ) ;
                while ( 1 ) ;
        } else {
                sleep(1);
                kill(getppid(), SIGUSR1);
                sleep(1);
                kill(getppid(), SIGUSR1);
                sleep(1);
                kill(getppid(), SIGUSR1);
                sleep(30);
                kill(getppid(), SIGTERM);
                exit(0);
        }
 }

I just can't believe that this will fail on any unix system. You should see:
Called
Called
Called
Finished sleeping
Finished sleeping
Finished sleeping
Terminated

The handler is not simply being called three times. It is really interrupting itself. I don't see how you can prove that it is interrupting itself without a second printf or allowing the first printf to output a few charaters then get interrupted by a second call to printf. printf is not prepared to be interupted by a second call to itself. At best you might see:
CaCaCalled
but I don't see any chance of that unless you completely unbuffer and even then it's dicey. It really depends on how it was written. The best move is to allow printf to finish before sending a second signal.

At some point a second question has been introduced here about sending a lot of signals to a process in a very short time. If a signal is generated more than once before it is delivered to a process, that is too bad. Only one signal will be delivered. Is that why you don't want to defer a signal? Yes, that will help reduce the chance of it happening but there is no complete solution to this problem. Suppose that your process is sitting on a run queue waiting for a cpu. If another process that does have a cpu sends it 5 copies of the same signal, what can the kernel do? When the process runs, it will get one signal.

Since you mention SIGCHLD specificly, this happens to init 100's of times each day. Several children exit, but init only gets one signal. init simply wait()'s in a loop until a wait() fails with ECHILD. If you treat SIGCHLD to mean that zero or more zombies need to reaped, the problem sort of goes away.
# 9  
Old 02-06-2003
If that's what you have to say then any form of Signal I/O is at stake.
External events which generates signals are not to our coding standard. They occur asynchronously at any time. Take an example in a signal driven I/O for Sockets:
When data arrives , the signal is generated. While the signal handler is executing ( reading data ) , two more data packets arrives at same time , causing signal to be generated two more times but as per our discussion the signal handler will be called only once. Hence the third data packet will only be read only when fourth data packet arrives at the port.

I feel that because signals are not queued ( to a process they are bit flags ) that the problem of multiple instances of same signal exists.
# 10  
Old 02-06-2003
Quote:
Originally posted by S.P.Prasad
Take an example in a signal driven I/O for Sockets:
When data arrives , the signal is generated. While the signal handler is executing ( reading data ) , two more data packets arrives at same time , causing signal to be generated two more times but as per our discussion the signal handler will be called only once. Hence the third data packet will only be read only when fourth data packet arrives at the port.
In that case the signal handler should be prepared to read 0 or more packets for each receipt of a SIGIO.

Looking at a few manpages, I see that doing this portably is easier said than done. It looks like you can set the O_NONBLOCK option and then loop doing recv() until you get -1 with errno set to either EAGAIN or EWOULDBLOCK.

But in general, this is the trick. Whatever the signal means, you need to handle it zero or more times. With any signal delivered to a process by the kernel there should be some way to loop attempting what is required and detecting when you've done that thing enough.

If another process is sending you signals with the expectation that your process can reliably count them, then someone made a design error. The processes would need to switch to another form of IPC.
# 11  
Old 02-09-2003
If yours interested to know: it works fine on GNU gcc 3.2 with Linux kernel 2.4.20.

output:

Called
Called
Called
Finished sleeping
Finished sleeping
Finished sleeping
Terminated
# 12  
Old 02-14-2003
The sigaction structure includes the following members:

void (*sa_handler)();
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask;
int sa_flags;

What is the purpose of the third parameter ( void * ) in the member variable void (*sa_sigaction)(int, siginfo_t *, void * ) of sigaction structure.

Thanks in advance
# 13  
Old 02-14-2003
Quote:
Originally posted by S.P.Prasad

What is the purpose of the third parameter ( void * ) in the member variable void (*sa_sigaction)(int, siginfo_t *, void * ) of sigaction structure.
I have never actually used that so my knowledge is limited, but I'll tell you what I know.

When a signal handler is called the kernel may change a few things first. It may block some extra signals. It may install a special stack is sigaltstack() was called. So when the signal handler executes a return statement, it cannot actually return immediately to the interrupted program. Instead, the return statement will jump to a little piece of code that will put things back the way they were prior to the signal handler being called. Then that little piece of code will jump back to where the process was when it was interrupted. Some people call that little piece of code a "trampoline".

The trampoline knows what to do because there is a structure called a "context" which indicates the way things were. The third parameter is a pointer to the saved context structure. The getcontext() and setcontext() system calls also work with pointers to contexts. Those man pages will lead you to more info on the context structure.

I never use setjmp() and longjmp(). I think that sigsetjmp() and siglongjump() will take care of this stuff so that if you use sigsetjmp in a program, you can safely siglongjmp() out of a signal handler. If that thinking is correct, and I'm not sure that it is, then I am having a hard time imagining a way to actually need to modify contexts. The only one I can think of is to setjump in one signal handler and then to longjmp back into that signal handler from a second signal handler. But there is no way that I'm going to try that.
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