Unix Programming - basic signal problem

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2004 > basic signal problem





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author basic signal problem
amsa

2004-08-22, 6:08 pm

Dear All,

I have a problem in signals.

#include <signal.h>
main()
{
printf("\n use DEL for exiting");
for(;;);
}

when the DEL key is pressed, the kernel sends SIGINT signal to process.

my problems are:
1. here in the above program, the first printf statement doesnt get displayed
at all.
2. and if i press the DEL key, it doesnt terminate. only if i press the
"CTRL \" or "CTRL Z" does it terminate.

another program code
---------------------

#include <signal.h>
void abc();
main()
{
printf("\n press <del> key\n");
signal(SIGINT,abc);
for(;;);
}

void abc() {
printf("\n pressed the <del> key");
}

here also:
1. the del key doesnt work.
2. the function "abc" doesnt get called at all.
3. the printf statements doesnt get executed at all.

whatz happening...can anyone help me.

i have a red hat linux 9.0 with kernel 2.4.20 -8.
Barry Margolin

2004-08-22, 6:08 pm

In article <61302e76.0408202352.13a8f692@posting.google.com>,
amsavarthini@yahoo.com (amsa) wrote:

> Dear All,
>
> I have a problem in signals.
>
> #include <signal.h>
> main()
> {
> printf("\n use DEL for exiting");
> for(;;);
> }
>
> when the DEL key is pressed, the kernel sends SIGINT signal to process.


Are you sure about that? That was the common default setting 20 years
ago, but these days most systems have the interrupt key set to Control-C
by default.

>
> my problems are:
> 1. here in the above program, the first printf statement doesnt get displayed
>
> at all.


By default, stdio uses line buffering on stdout if it's connected to a
terminal. The buffer doesn't get flushed until a newline is printed.
So the first "\n" is probably printed, but the rest of the line is not.
Put:

fflush(stdout);

before the for() loop and you should see the message.

> 2. and if i press the DEL key, it doesnt terminate. only if i press the
> "CTRL \" or "CTRL Z" does it terminate.
>
> another program code
> ---------------------
>
> #include <signal.h>
> void abc();
> main()
> {
> printf("\n press <del> key\n");
> signal(SIGINT,abc);
> for(;;);
> }
>
> void abc() {
> printf("\n pressed the <del> key");
> }
>
> here also:
> 1. the del key doesnt work.


See above.

> 2. the function "abc" doesnt get called at all.


How do you know? As mentioned above, since you don't terminate the
message with a newline, it doesn't get printed immediately.

> 3. the printf statements doesnt get executed at all.


Not even the one in main()?

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Lőrinczy Zsigmond

2004-08-22, 6:08 pm

amsa wrote:
> Dear All,
>
> I have a problem in signals.
>
> #include <signal.h>
> main()
> {
> printf("\n use DEL for exiting");
> for(;;);
> }
>
> when the DEL key is pressed, the kernel sends SIGINT signal to process.
>
> my problems are:
> 1. here in the above program, the first printf statement doesnt get displayed
> at all.
> 2. and if i press the DEL key, it doesnt terminate. only if i press the
> "CTRL \" or "CTRL Z" does it terminate.
>
> another program code
> ---------------------
>
> #include <signal.h>
> void abc();
> main()
> {
> printf("\n press <del> key\n");
> signal(SIGINT,abc);
> for(;;);
> }
>
> void abc() {
> printf("\n pressed the <del> key");
> }
>
> here also:
> 1. the del key doesnt work.
> 2. the function "abc" doesnt get called at all.
> 3. the printf statements doesnt get executed at all.


-call "flush" after every printf
-not "DEL" but "CTRL+C" causes SIGINT
amsa

2004-08-22, 6:08 pm

hi all,

thanks for all your help.
as u said, when i placed a "/n", the print statement was displayed.
secondly, CTRL-C terminated the first program...

#include <signal.h>
main()
{
printf("\n use CTRL-C for exiting\n");
for(;;);
}

but for the 2nd program, again both the printf were displayed....and
the function "abc" got called....but didnt terminate the process.

#include <signal.h>
void abc();
main()
{
printf("\n press CTRL-C key\n");
signal(SIGINT,abc);
for(;;);
}

void abc() {
printf("\n pressed the CTRL-C key\n");
}

whenever i press the CTRL-C, the function abc get called....but i want
the process to be terminated. can it be terminated ?????

av.




Barry Margolin <barmar@alum.mit.edu> wrote in message news:<barmar-FD17C6.04111621082004@comcast.dca.giganews.com>...
> In article <61302e76.0408202352.13a8f692@posting.google.com>,
> amsavarthini@yahoo.com (amsa) wrote:
>
>
> Are you sure about that? That was the common default setting 20 years
> ago, but these days most systems have the interrupt key set to Control-C
> by default.
>
>
> By default, stdio uses line buffering on stdout if it's connected to a
> terminal. The buffer doesn't get flushed until a newline is printed.
> So the first "\n" is probably printed, but the rest of the line is not.
> Put:
>
> fflush(stdout);
>
> before the for() loop and you should see the message.
>
>
> See above.
>
>
> How do you know? As mentioned above, since you don't terminate the
> message with a newline, it doesn't get printed immediately.
>
>
> Not even the one in main()?

Jens.Toerring@physik.fu-berlin.de

2004-08-22, 6:08 pm

amsa <amsavarthini@yahoo.com> wrote:
> #include <signal.h>
> void abc();
> main()
> {
> printf("\n press CTRL-C key\n");
> signal(SIGINT,abc);
> for(;;);
> }
>
> void abc() {
> printf("\n pressed the CTRL-C key\n");
> }


> whenever i press the CTRL-C, the function abc get called....but i want
> the process to be terminated. can it be terminated ?????


You have installed a signal handler that gets called _instead of_
killing the program (which is the default action for SIGINT). If
you want it killed after the signal you need to call exit() in the
signal handler.
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Kurtis D. Rader

2004-08-22, 6:08 pm

On Sat, 21 Aug 2004 12:52:25 -0700, amsa wrote:

> #include <signal.h>
> void abc();
> main()
> {
> printf("\n press CTRL-C key\n");
> signal(SIGINT,abc);
> for(;;);
> }
>
> void abc() {
> printf("\n pressed the CTRL-C key\n");
> }
>
> whenever i press the CTRL-C, the function abc get called....but i want
> the process to be terminated. can it be terminated ?????


If you want the process to be terminated why are you catching the signal?
The default action for SIGINT is to terminate the process. If you're
catching the signal so that you can perform some action before terminating
then you have to explicitly exit from within the signal handler.
amsa

2004-08-22, 6:08 pm

thanks for ur support. i am doing a depth learning of signals and just
getting my basics right. well i have another problem here.

the program is as follows:

main()
{
int i=0,j=50;
signal(SIGILL, sigkey)
j=j/i;
}

void sigkey(signo)
int signo
{
printf("\n process recieved signal :%d",signo);
exit(0);
}

here actually SIGILL (illegal instruction code) should be passed from
the kernel 'cause of DIVIDE-BY-ZERO and such that function sigkey
should be called. but that doesnt happen...it gives the output
"Floating Point Exception".

my 2nd problem:

main()
{
int fd;
signal(SIGSYS,abc);
fd=open("somefile",O_RDONLY);
lseek(fd,0,5);
}

void abc()
{
printf("\n illegal use of system call\n");
exit(0);
}

here the function abc should be called, because lseek cannot have 5 as
the 3rd parameter. so signal SIGSYS should be passed...
but nothing happens...why.

thanks for all ur help.

av.

"Kurtis D. Rader" <krader@skepticism.us> wrote in message news:<pan.2004.08.21.20.13.31.401447@skepticism.us>...
> On Sat, 21 Aug 2004 12:52:25 -0700, amsa wrote:
>
>
> If you want the process to be terminated why are you catching the signal?
> The default action for SIGINT is to terminate the process. If you're
> catching the signal so that you can perform some action before terminating
> then you have to explicitly exit from within the signal handler.

Barry Margolin

2004-08-22, 6:08 pm

In article <61302e76.0408220144.10ee816e@posting.google.com>,
amsavarthini@yahoo.com (amsa) wrote:

> thanks for ur support. i am doing a depth learning of signals and just
> getting my basics right. well i have another problem here.
>
> the program is as follows:
>
> main()
> {
> int i=0,j=50;
> signal(SIGILL, sigkey)
> j=j/i;
> }
>
> void sigkey(signo)
> int signo


Why are you using K&R-style parameter declarations? They've been
obsolete for more than a decade.

> {
> printf("\n process recieved signal :%d",signo);
> exit(0);
> }
>
> here actually SIGILL (illegal instruction code) should be passed from
> the kernel 'cause of DIVIDE-BY-ZERO and such that function sigkey
> should be called. but that doesnt happen...it gives the output
> "Floating Point Exception".


It's signalling SIGFPE, not SIGILL.

>
> my 2nd problem:
>
> main()
> {
> int fd;
> signal(SIGSYS,abc);
> fd=open("somefile",O_RDONLY);
> lseek(fd,0,5);
> }
>
> void abc()
> {
> printf("\n illegal use of system call\n");
> exit(0);
> }
>
> here the function abc should be called, because lseek cannot have 5 as
> the 3rd parameter. so signal SIGSYS should be passed...
> but nothing happens...why.


Read the man page for lseek(). It reports this error by returning -1
and setting errno to EINVAL.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Kurtis D. Rader

2004-08-22, 6:08 pm

On Sun, 22 Aug 2004 02:44:31 -0700, amsa wrote:

> main()
> {
> int i=0,j=50;
> signal(SIGILL, sigkey)
> j=j/i;
> }
>
> void sigkey(signo)
> int signo
> {
> printf("\n process recieved signal :%d",signo); exit(0);
> }
>
> here actually SIGILL (illegal instruction code) should be passed from
> the kernel 'cause of DIVIDE-BY-ZERO and such that function sigkey should
> be called. but that doesnt happen...it gives the output "Floating Point
> Exception".


Why would you expect SIGILL. Assuming your compiler isn't broken your
program won't contain an illegal opcode (i.e., machine instruction). There
isn't a signal for invalid mathematical operations involving integers.
Instead the kernel uses SIGFPE (floating point exception) for invalid
operations that involve either floating point or integer operands.

> my 2nd problem:
>
> main()
> {
> int fd;
> signal(SIGSYS,abc);
> fd=open("somefile",O_RDONLY);
> lseek(fd,0,5);
> }
> }
> void abc()
> {
> printf("\n illegal use of system call\n"); exit(0);
> }
> }
> here the function abc should be called, because lseek cannot have 5 as
> the 3rd parameter. so signal SIGSYS should be passed... but nothing
> happens...why.


Your man page probably says something like "bad argument to routine" as
the reason for SIGSYS. But I've never seen a UNIX implementation which
does that. All the ones I've ever worked with raise SIGSYS only for the
case where it doesn't recognize the system call being made by the process.
Invalid arguments will result in a return code of -1 with errno set to
EINVAL or EFAULT for most syscalls (there are exceptions to this rule so
read the documentation for the syscall you're concerned about).
amsa

2004-08-24, 3:18 am

Hi All,

Thanks for all your help. But I Dont understand one thing in signals.
Signals, like pipes and FIFO's are also used for communication of data
within processes. But learning signals, i feel it is more of error
handling b/w the process-kernel. How is this used for communication
b/w 2 processes (forget the 'kill' system call) ?

'kill' system call is used for communication b/w 2 processes...but why
the name 'kill'...it doesnt represent a meaningful name (i am just
curious abt this system call).

amsa.

"Kurtis D. Rader" <krader@skepticism.us> wrote in message news:<pan.2004.08.22.15.39.55.778962@skepticism.us>...
> On Sun, 22 Aug 2004 02:44:31 -0700, amsa wrote:
>
>
> Why would you expect SIGILL. Assuming your compiler isn't broken your
> program won't contain an illegal opcode (i.e., machine instruction). There
> isn't a signal for invalid mathematical operations involving integers.
> Instead the kernel uses SIGFPE (floating point exception) for invalid
> operations that involve either floating point or integer operands.
>
>
> Your man page probably says something like "bad argument to routine" as
> the reason for SIGSYS. But I've never seen a UNIX implementation which
> does that. All the ones I've ever worked with raise SIGSYS only for the
> case where it doesn't recognize the system call being made by the process.
> Invalid arguments will result in a return code of -1 with errno set to
> EINVAL or EFAULT for most syscalls (there are exceptions to this rule so
> read the documentation for the syscall you're concerned about).

Kurtis D. Rader

2004-08-24, 10:04 pm

On Mon, 23 Aug 2004 21:35:59 -0700, amsa wrote:

> 'kill' system call is used for communication b/w 2 processes...but why
> the name 'kill'...it doesnt represent a meaningful name (i am just
> curious abt this system call).


Probably because the default action for most signals is to terminate, or
kill, the process.
Barry Margolin

2004-08-27, 8:48 pm

In article <61302e76.0408232035.75edb2dc@posting.google.com>,
amsavarthini@yahoo.com (amsa) wrote:

> Thanks for all your help. But I Dont understand one thing in signals.
> Signals, like pipes and FIFO's are also used for communication of data
> within processes. But learning signals, i feel it is more of error
> handling b/w the process-kernel. How is this used for communication
> b/w 2 processes (forget the 'kill' system call) ?


Signals are a general mechanism for notifying processes of unexpected
conditions. Some are used for communication from the hardware (e.g.
SIGFPE, SIGILL, SIGHUP), some are used for communication from the kernel
(e.g. SIGIOT, SIGCHLD), and some are used for communications between
processes (e.g. SIGUSR1, SIGINT).

To communicate between processes you have to use the kill() system call.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com