Not able to Display the Catched Signal


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Not able to Display the Catched Signal
# 1  
Old 08-19-2012
Power Not able to Display the Catched Signal

my code here is intended to display the received signal that's it but its not working when i try to send the signal
kill -SIGUSR1 pid
or
kill -10 pid
or any other signals according to my program

Code:
#include"headers.h"
static void sig_usr(int);
main()
{
        if(signal(SIGUSR1,sig_usr)==SIG_ERR)
                printf("cant catch signal sigusr1");
        if(signal(SIGUSR2,sig_usr)==SIG_ERR)
                printf("cant catch signal sigusr2");
        while(1);
        pause();
}
static void sig_usr(int signo)
{
if(signo==SIGUSR1)
        printf("Rx sigusr1");
else if(signo==SIGUSR2)
        printf("Rx sigusr2");
else printf("recieved signal is %d",signo);
return;
}

so i debugged it
debug results:

Code:
Breakpoint 1, main () at catch_signal.c:5
5       if(signal(SIGUSR1,sig_usr)==SIG_ERR)
(gdb) n
7       if(signal(SIGUSR2,sig_usr)==SIG_ERR)
(gdb) n
9       while(1);
(gdb) n

Program received signal SIGUSR1, User defined signal 1.
main () at catch_signal.c:9
9       while(1);
(gdb) n
sig_usr (signo=10) at catch_signal.c:13
13  {
(gdb) n
14  if(signo==SIGUSR1)
(gdb) n
15      printf("Rx sigusr1");
(gdb) n

Program received signal SIGTRAP, Trace/breakpoint trap.
0x08048326 in printf@plt ()
(gdb) n
Single stepping until exit from function printf@plt,
which has no line number information.
0x08048310 in ?? ()
(gdb) n
Cannot find bounds of current function

im supposed to get the result output as Rx signal usr1 but i havnt why soSmilie
# 2  
Old 08-19-2012
Signal 10, as I recall, is bus error, which is no catch but core dump if allowed. Signal 1 is hangup or HUP, very catchable even in scripts. Your man kill, man signal, signal.h will vary:
Code:
   signum   signame   Name            Description
   ___________________________________________________________________________
      0     SIGNULL   Null            Check access to pid
      1     SIGHUP    Hangup          Terminate; can be trapped
      2     SIGINT    Interrupt       Terminate; can be trapped
      3     SIGQUIT   Quit            Terminate with core dump; can be trapped
      9     SIGKILL   Kill            Forced termination; cannot be trapped
     15     SIGTERM   Terminate       Terminate; can be trapped
     24     SIGSTOP   Stop            Pause the process; cannot be trapped
     25     SIGTSTP   Terminal stop   Pause the process; can be trapped
     26     SIGCONT   Continue        Run a stopped process
 
signal.h:
 
/* Signal numbers */
#  define _SIGHUP       1       /* hangup */
#  define SIGINT        2       /* Interrupt */
#  define _SIGQUIT      3       /* quit */
#  define SIGILL        4       /* Illegal instruction (not reset when
                                   caught) */
#  define _SIGTRAP      5       /* trace trap (not reset when caught) */
#  define SIGABRT       6       /* Process abort signal */
#  define _SIGIOT       SIGABRT /* IOT instruction */
#  define _SIGEMT       7       /* EMT instruction */
#  define SIGFPE        8       /* Floating point exception */
#  define _SIGKILL      9       /* kill (cannot be caught of ignored) */
#  define _SIGBUS       10      /* bus error */
#  define SIGSEGV       11      /* Segmentation violation */
#  define _SIGSYS       12      /* bad argument to system call */
#  define _SIGPIPE      13      /* write on a pipe with no one to read it */
#  define _SIGALRM      14      /* alarm clock */
#  define SIGTERM       15      /* Software termination signal from kill */
#  define _SIGUSR1      16      /* user defined signal 1 */
#  define _SIGUSR2      17      /* user defined signal 2 */
#  define _SIGCHLD      18      /* Child process terminated or stopped */
#  define _SIGCLD       _SIGCHLD        /* death of a child */
#  define _SIGPWR       19      /* power state indication */
#  define _SIGVTALRM    20      /* virtual timer alarm */
#  define _SIGPROF      21      /* profiling timer alarm */
#  define _SIGIO        22      /* asynchronous I/O */
#  define _SIGPOLL      _SIGIO  /* for HP-UX hpstreams signal */
#  define _SIGWINCH     23      /* window size change signal */
#  define _SIGWINDOW    _SIGWINCH /* added for compatibility reasons */
#  define _SIGSTOP      24      /* Stop signal (cannot be caught or ignored) */
#  define _SIGTSTP      25      /* Interactive stop signal */
#  define _SIGCONT      26      /* Continue if stopped */
#  define _SIGTTIN      27      /* Read from control terminal attempted by a
                                   member of a background process group */
#  define _SIGTTOU      28      /* Write to control terminal attempted by a
                                   member of a background process group */
#  define _SIGURG       29      /* urgent condition on IO channel */
#  define _SIGLOST      30      /* remote lock lost  (NFS)        */
#  define _SIGRESERVE   31      /* Save for future use */
#  define _SIGDIL       32      /* DIL signal */
#  define _SIGXCPU      33      /* CPU time limit exceeded (setrlimit)  */
#  define _SIGXFSZ      34      /* CPU file size limit exceeded (setrlimit)  */
#  define _SIGCANCEL    35      /* Used for pthread cancellation. */
#  define _SIGGFAULT    36      /* Graphics framebuffer fault */
#  define _SIGRTMIN     37      /* First (highest priority) realtime signal */
#  define _SIGRTMAX     44      /* Last (lowest priority) realtime signal */

# 3  
Old 08-19-2012
Quote:
Originally Posted by shyam.sunder91
im supposed to get the result output as Rx signal usr1 but i havnt why soSmilie
Are you certain that it isn't being printed? Are you running the binary in the background while trying to send signals to it? If so, depending on your terminal settings, an attempt to write to the terminal could stop the process. Also, note that your printf statements do not have a terminating newline. Perhaps it did print but you missed it? Perhaps the output got lost among other output, such as the shell prompt? Depending on how you called it, perhaps its stdout has been redirected?


Quote:
Originally Posted by shyam.sunder91
Code:
        while(1);
        pause();

Your code spins needlessly. while (1); cpu usage will hit 100% and pause() is never reached. You can accomplish your goal without using any cpu between signals: while (1) pause(); or even while (pause());

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 08-19-2012
Bug adding \n solved it

Quote:
Originally Posted by alister
depending on your terminal settings, an attempt to write to the terminal could stop the process. Also, note that your printf statements do not have a terminating newline. Perhaps it did print but you missed it?
this trick done the thing what does actually happened why adding newlines made a change,can u please elucidate,

what '\n' actually does here,also the process exited why so it must have returned and stay

very thanks
# 5  
Old 08-20-2012
\n tells it to flush the buffer.

Until you print a \n, data just stays in a buffer in memory.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 08-20-2012
STDIO streams are either fully buffered (i.e., the buffer is not flushed until the buffer is full), line buffered (i.e., the buffer is flushed when a complete line is in the buffer or the buffer is full), or unbuffered (i.e., the buffer is flushed whenever there is data in the buffer [usually at the end of a call to a stdio function like putc(), printf(), or fwrite()]). By default, stderr is not fully buffered. By default, stdin and stdout cannot be fully buffered unless the system can determine that the stream is not connected to a TTY device file.

So adding a "\n" to your printf() format string will usually initiate a flush of stdout's buffer before printf() returns to the calling program unless the ouput of the program is redirected to a regular file. You can manually flush the buffer anytime you want to with a call to fflush(stdout), and you can use setvbuf() to change the buffering mode for a stream to any of the three forms of buffering for any open stream.
These 4 Users Gave Thanks to Don Cragun For This Post:
# 7  
Old 08-20-2012
Question stream connection to tty device file ?

thanks for ur reply i got an understanding over flushing buffers and their advantage

Quote:
Originally Posted by Don Cragun
By default, stdin and stdout cannot be fully buffered unless the system can determine that the stream is not connected to a TTY device file.
i did not get this line can u elucidate,what is getting connecting to tty device file
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Case signal

Task 1: #!/bin/ksh if then echo "Usage : $0 Signalnumber PID" exit fi case "$1" in 1) echo "Sending SIGHUP signal" kill -SIGHUP $2 ;; 2) echo "Sending SIGINT signal" kill -SIGINT $2 ;; 3) echo "Sending SIGQUIT signal" kill -SIGQUIT $2 (3 Replies)
Discussion started by: Ramesh M
3 Replies

2. Programming

queue a signal

A program have to receive signals and work agreed with it, but the process have to receive more than one signal when it is attending other. Those have to be queued to be attended later recived. how can i do that? thanks. (2 Replies)
Discussion started by: marmaster
2 Replies

3. UNIX for Advanced & Expert Users

DISPLAY=local_host:0.0 ; export DISPLAY

Hi, from my Windows Workstation I can connect with PUTTY to an AIX 6.1 unix server. On AIX via PUTTY I run DBCA which has a grphical interface. Then : #DISPLAY=local_host:0.0 ; export DISPLAY $(hostname) $(whoami):/appli/oracle/product/10.2.0/db_1/bin#dbca _X11TransSocketINETConnect()... (12 Replies)
Discussion started by: big123456
12 Replies

4. UNIX for Dummies Questions & Answers

Trying to block signal

I have this code that doesnt do what it is suppose to do. It should block signal that I send while process is running. I press control+z while this process is running and it should be blocked but it isnt. When i press control+z it gives me this.... + Stopped When I change SIGTSP into SIGINT then... (5 Replies)
Discussion started by: joker40
5 Replies

5. Shell Programming and Scripting

Killed by signal 15.

Hi all I have Master script, Main script ,and 4 Child script. Master.sh #!/bin/bash /export/home/user/Main.shMain.sh #!/bin/bash /export/home/user/Child1.sh & /export/home/user/Child2.sh & /export/home/user/Child3.sh & /export/home/user/Child4.sh &I run only Master.sh script... (1 Reply)
Discussion started by: almanto
1 Replies

6. Programming

Signal Problem

I am using the signal function, and passing it a function named quit procedure...I get the following warning.... passing arg2 of signal from incompatible pointer type... void quit_procedure(void); //this is the way i define my prototype... signal(SIGINT, quit_procedure); Please guide... (5 Replies)
Discussion started by: jacques83
5 Replies

7. Shell Programming and Scripting

Signal question

Who can explain the meaning of the &2 &1 or @, #, etc in the script? Is there any document which can explain the usage of these words in details? for example: ls /etc/sysconfig/network > /dev/null 2>&1 #@ bash, ksh and sh. Thanks in advance for ur advice. (1 Reply)
Discussion started by: GCTEII
1 Replies

8. UNIX for Dummies Questions & Answers

Alarm signal

Hi, when I execute a script on unix AIX, I've got an error message: "Execution: 85328 Signal d'alarme". If I edit this file with "vi", I ve got the same error after a while (about 1 minute). If I try with another user I still have the problem. But if I rename this file, no problem. My... (5 Replies)
Discussion started by: cgsteph
5 Replies

9. Shell Programming and Scripting

thread::signal

Hi,all! Now ,I write perl for windows platform,and will use signal for asynchronous operations ,but I find it could bring some bugs if it is used incorrectly ,pls help!!! :D (1 Reply)
Discussion started by: hhh101
1 Replies
Login or Register to Ask a Question