Sponsored Content
Top Forums Programming problem in SIGSEGV signal handling Post 302331417 by pavan6754 on Monday 6th of July 2009 06:34:04 AM
Old 07-06-2009
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

Code:
#include <signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *j;
void segv_handler(int dummy)
{
  j=(char *)malloc(10);
  strcpy(j,"hello");
  printf("segmentation fault mine j=%s\n",j);
}
main()
{
  signal(SIGSEGV, segv_handler);
  strcpy(j,"hello\n");
  printf("hai\n");
  for (;;)
  {
  }
}

output is :

Code:
segmentation fault mine j=hello
segmentation fault mine j=hello
segmentation fault mine j=hello
segmentation fault mine j=hello
segmentation fault mine j=hello
segmentation fault mine j=hello
segmentation fault mine j=hello
.......infinitely repeating above o/p

can i know why my program is behaving like this..
thanks in advance.....
 

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

3. Programming

Program received signal SIGSEGV, Segmentation fault.

Dear all, I used debugger from C++ and these are the message I got: Program received signal SIGSEGV, Segmentation fault. 0x00323fc0 in free () from /lib/tls/libc.so.6 (gdb) info s #0 0x00323fc0 in free () from /lib/tls/libc.so.6 #1 0x00794fa1 in operator delete () from... (5 Replies)
Discussion started by: napapanbkk
5 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

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... (1 Reply)
Discussion started by: #moveon
1 Replies

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

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

10. AIX

Received signal #11, SIGSEGV [default] on AIX 6.1

Hello, One of our customer is getting segmentation fault when he runs his shell script which invokes our executable on AIX 6.1. On AIX 5.3, there were no issues. Here is the truss output. 811242: __loadx(0x0A040000, 0xF0D3A26C, 0x00000000, 0x00000009, 0x00000000) = 0xF026E884... (0 Replies)
Discussion started by: erra_krishna
0 Replies
SIGNAL(2)						     Linux Programmer's Manual							 SIGNAL(2)

NAME
signal - ANSI C signal handling SYNOPSIS
#include <signal.h> typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); DESCRIPTION
The signal() system call installs a new signal handler for the signal with number signum. The signal handler is set to sighandler which may be a user specified function, or either SIG_IGN or SIG_DFL. Upon arrival of a signal with number signum the following happens. If the corresponding handler is set to SIG_IGN, then the signal is ignored. If the handler is set to SIG_DFL, then the default action associated to the signal (see signal(7)) occurs. Finally, if the han- dler is set to a function sighandler then first either the handler is reset to SIG_DFL or an implementation-dependent blocking of the sig- nal is performed and next sighandler is called with argument signum. Using a signal handler function for a signal is called "catching the signal". The signals SIGKILL and SIGSTOP cannot be caught or ignored. RETURN VALUE
The signal() function returns the previous value of the signal handler, or SIG_ERR on error. PORTABILITY
The original Unix signal() would reset the handler to SIG_DFL, and System V (and the Linux kernel and libc4,5) does the same. On the other hand, BSD does not reset the handler, but blocks new instances of this signal from occurring during a call of the handler. The glibc2 library follows the BSD behaviour. If one on a libc5 system includes <bsd/signal.h> instead of <signal.h> then signal is redefined as __bsd_signal and signal has the BSD semantics. This is not recommended. If one on a glibc2 system defines a feature test macro such as _XOPEN_SOURCE or uses a separate sysv_signal function, one obtains classical behaviour. This is not recommended. Trying to change the semantics of this call using defines and includes is not a good idea. It is better to avoid signal altogether, and use sigaction(2) instead. NOTES
According to POSIX, the behaviour of a process is undefined after it ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by the kill(2) or the raise(3) functions. Integer division by zero has undefined result. On some architectures it will generate a SIGFPE signal. (Also dividing the most negative integer by -1 may generate SIGFPE.) Ignoring this signal might lead to an endless loop. According to POSIX (3.3.1.3) it is unspecified what happens when SIGCHLD is set to SIG_IGN. Here the BSD and SYSV behaviours differ, caus- ing BSD software that sets the action for SIGCHLD to SIG_IGN to fail on Linux. The use of sighandler_t is a GNU extension. Various versions of libc predefine this type; libc4 and libc5 define SignalHandler, glibc defines sig_t and, when _GNU_SOURCE is defined, also sighandler_t. CONFORMING TO
ANSI C SEE ALSO
kill(1), kill(2), killpg(2), pause(2), raise(3), sigaction(2), signal(7), sigsetops(3), sigvec(2), alarm(2) Linux 2.2 2000-04-28 SIGNAL(2)
All times are GMT -4. The time now is 06:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy