Sponsored Content
Full Discussion: Multiple Signals
Top Forums Programming Multiple Signals Post 34060 by Perderabo on Saturday 1st of February 2003 05:46:20 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
SYSV_SIGNAL(3)						     Linux Programmer's Manual						    SYSV_SIGNAL(3)

NAME
sysv_signal - signal handling with System V semantics SYNOPSIS
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <signal.h> typedef void (*sighandler_t)(int); sighandler_t sysv_signal(int signum, sighandler_t handler); DESCRIPTION
The sysv_signal() function takes the same arguments, and performs the same task, as signal(2). However sysv_signal() provides the System V unreliable signal semantics, that is: a) the disposition of the signal is reset to the default when the handler is invoked; b) delivery of further instances of the signal is not blocked while the signal handler is executing; and c) if the handler interrupts (certain) blocking system calls, then the system call is not automatically restarted. RETURN VALUE
The sysv_signal() function returns the previous value of the signal handler, or SIG_ERR on error. ERRORS
As for signal(2). CONFORMING TO
This function is nonstandard. NOTES
Use of sysv_signal() should be avoided; use sigaction(2) instead. On older Linux systems, sysv_signal() and signal(2) were equivalent. But on newer systems, signal(2) provides reliable signal semantics; see signal(2) for details. The use of sighandler_t is a GNU extension; this type is defined only if the _GNU_SOURCE feature test macro is defined. SEE ALSO
sigaction(2), signal(2), bsd_signal(3), signal(7) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. 2007-05-04 SYSV_SIGNAL(3)
All times are GMT -4. The time now is 11:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy