basic signal problem
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > basic signal problem




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    basic signal problem  
amsa


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11: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 displaye
d
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.





[ Post a follow-up to this message ]



    Re: basic signal problem  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11: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 displa
yed
>
>    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 ***





[ Post a follow-up to this message ]



    Re: basic signal problem  
Lőrinczy Zsigmond


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11: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 displa
yed
>    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





[ Post a follow-up to this message ]



    Re: basic signal problem  
amsa


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11: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@com
cast.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()?





[ Post a follow-up to this message ]



    Re: basic signal problem  
Jens.Toerring@physik.fu-berlin.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11: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





[ Post a follow-up to this message ]



    Re: basic signal problem  
Kurtis D. Rader


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11: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.





[ Post a follow-up to this message ]



    Re: basic signal problem  
amsa


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11: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.4014
47@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.





[ Post a follow-up to this message ]



    Re: basic signal problem  
Barry Margolin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11: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 ***





[ Post a follow-up to this message ]



    Re: basic signal problem  
Kurtis D. Rader


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-22-04 11: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).





[ Post a follow-up to this message ]



    Re: basic signal problem  
amsa


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-24-04 08: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.7789
62@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).





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:27 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register