Sponsored Content
Full Discussion: Signal handling
Top Forums Programming Signal handling Post 302320138 by #moveon on Wednesday 27th of May 2009 06:54:15 AM
Old 05-27-2009
Signal handling

I am trying to write a small program where I can send signals and then ask for an action to be triggered if that signal is received. For example, here is an example where I am trying to write a programme that will say you pressed ctrl*c when someone presses ctrl+c. My questions are what you would expect from a beginner so bear with me. First, how can I get ro test the functionality of my program? My udnerstanding is you get the process ID and then through kill -signalnumber PID you will be able to send the signal? For example kill -2 2345 to send teh SIGINT to PID 2345. But mine doesn't work. First I opened a new shell then used shellname & on the terminal to get the PID, then on the terminal wrote kill -2 2345 if the PID was that for example. But I always recieve '... No such process'. Where I am getting it wrong?
Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <signal.h>
    void signalhandler(int sig);

    int main(void)
    {
    char s[200];
    
        (void) signal (SIGINT, signalhandler);
    
                
        if (signal(SIGINT, signalhandler) == SIG_ERR) {
            perror("signal");
            exit(1);
        }
            return 0;
    }
    
            void signalhandler(int sig)
    {
        printf("You pressed CTRL+C!\n");
    }

 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Handling SIGUSR2 signal

HI, I need to handle SIGUSR2 signal in my application to change the state of the application dynamically. I have implemented the signal handler. However the application is able to catch only one SIGUSR2 signal. The second SIGUSR2 signal causes the application to crash. This is happning only with... (3 Replies)
Discussion started by: diganta
3 Replies

2. UNIX for Advanced & Expert Users

signal handling in shell script

Hi can any please tell me is it possible to catch the signal in a shell script like we do in C. if yes please give me some idea or a link. (4 Replies)
Discussion started by: Raom
4 Replies

3. Programming

Signal Handling

Hi folks I'm trying to write a signal handler (in c on HPUX) that will catch the child process launched by execl when it's finished so that I can check a compliance file. The signal handler appears to catch the child process terminating however when the signal handler completes the parent... (3 Replies)
Discussion started by: themezzaman
3 Replies

4. Shell Programming and Scripting

Signal handling in Perl

Guys, I'm doing signal handling in Perl. I'm trying to catch ^C signal inside the script. There two scripts : one shell script and one perl script. The shell script calls the perl script. For e.g. shell script a.sh and perl scipt sig.pl. Shell script a.sh looks something like this :... (6 Replies)
Discussion started by: obelix
6 Replies

5. Programming

signal handling question

Hello all, I am starting to learn signal handling in Linux and have been trying out some simple codes to deal with SIGALRM. The code shown below sets a timer to count down. When the timer is finished a SIGALRM is produced. The handler for the signal just increments a variable called count. This... (7 Replies)
Discussion started by: fox_hound_33
7 Replies

6. Programming

signal handling while in a function other than main

Hi, I have a main loop which calls a sub loop, which finally returns to the main loop itself. The main loop runs when a flag is set. Now, I have a signal handler for SIGINT, which resets the flag and thus stops the main loop. Suppose I send SIGINT while the program is in subloop, I get an error... (1 Reply)
Discussion started by: Theju
1 Replies

7. Programming

Signal Handling and Context Switches

Hi guys, this is my first posting, so at first hi to everyone! ;) I have a problem with ucontext_t in connection with signal handling. I want to simulate a preemptive scheduler. I am using the iTimer with ITIMER_PROF, to schedule the interrupts. You find the code below: #include <stdio.h>... (18 Replies)
Discussion started by: XComp
18 Replies

8. Programming

problem in SIGSEGV signal handling

i wrote handler for sigsegv such that i can allocate memory for a variable to which sigsegv generated for illlegal acces of memory. my code is #include <signal.h> #include<stdio.h> #include<stdlib.h> #include<string.h> char *j; void segv_handler(int dummy) { j=(char *)malloc(10); ... (4 Replies)
Discussion started by: pavan6754
4 Replies

9. UNIX and Linux Applications

SIGSEGV Signal handling

Hello, Can anybody tell me how can i handle segmentation fault signal, in C code? (2 Replies)
Discussion started by: mustus
2 Replies

10. Programming

problem in reforking and signal handling

hi friends i have a problem in signal handling ... let me explain my problem clearly.. i have four process .. main process forks two child process and each child process again forks another new process respectively... the problem is whenever i kill the child process it is reforking and the... (2 Replies)
Discussion started by: senvenugopal
2 Replies
SIGQUEUE(2)						     Linux Programmer's Manual						       SIGQUEUE(2)

NAME
sigqueue, rt_sigqueueinfo - queue a signal and data to a process SYNOPSIS
#include <signal.h> int sigqueue(pid_t pid, int sig, const union sigval value); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): sigqueue(): _POSIX_C_SOURCE >= 199309L DESCRIPTION
sigqueue() sends the signal specified in sig to the process whose PID is given in pid. The permissions required to send a signal are the same as for kill(2). As with kill(2), the null signal (0) can be used to check if a process with a given PID exists. The value argument is used to specify an accompanying item of data (either an integer or a pointer value) to be sent with the signal, and has the following type: union sigval { int sival_int; void *sival_ptr; }; If the receiving process has installed a handler for this signal using the SA_SIGINFO flag to sigaction(2), then it can obtain this data via the si_value field of the siginfo_t structure passed as the second argument to the handler. Furthermore, the si_code field of that structure will be set to SI_QUEUE. RETURN VALUE
On success, sigqueue() returns 0, indicating that the signal was successfully queued to the receiving process. Otherwise -1 is returned and errno is set to indicate the error. ERRORS
EAGAIN The limit of signals which may be queued has been reached. (See signal(7) for further information.) EINVAL sig was invalid. EPERM The process does not have permission to send the signal to the receiving process. For the required permissions, see kill(2). ESRCH No process has a PID matching pid. VERSIONS
This system call first appeared in Linux 2.2. CONFORMING TO
POSIX.1-2001. NOTES
If this function results in the sending of a signal to the process that invoked it, and that signal was not blocked by the calling thread, and no other threads were willing to handle this signal (either by having it unblocked, or by waiting for it using sigwait(3)), then at least some signal must be delivered to this thread before this function returns. On Linux, the underlying system call is actually named rt_sigqueueinfo(), and differs in its third argument, which is the siginfo_t struc- ture that will be supplied to the receiving process's signal handler or returned by the receiving process's sigtimedwait(2) call. Inside the glibc sigqueue() wrapper, this argument, info, is initialized as follows: info.si_signo = sig; /* argument supplied to sigqueue() */ info.si_code = SI_QUEUE; info.si_pid = getpid(); /* Process ID of sender */ info.si_uid = getuid(); /* Real UID of sender */ info.si_value = val; /* argument supplied to sigqueue() */ SEE ALSO
kill(2), sigaction(2), signal(2), sigwait(3), signal(7) COLOPHON
This page is part of release 3.27 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/. Linux 2007-07-26 SIGQUEUE(2)
All times are GMT -4. The time now is 08:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy