lkml.org 
[lkml]   [2006]   [May]   [26]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRe: trapping key press using signals
Date

Hi,

With reference to your question : How to catch signals generated by
key press using signals i would like to share a piece of code which will
help you understand the concept more easily.

There are basically 3 ways to send singnals to Processes:
1.Using the keyboard : Like Ctrl-C , this causes the system to send an INT
signal (SIGINT) to the running process by default it causes the process to
immediately terminate. Another one is Ctrl-Z (SIGTSTP) which causes the
process to suspend execution and Ctrl-\ (SIGABRT) which does the same as
Ctrl-C but gives us some better flexibility.
Here is the piece of code snippet that causes the progeam to print the
string "Testing Code to understand" when a user presses Ctrl-C:

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<signal.h>
void catch_int(sig_num)
{
/* re-set the signal handler again to catch_int, for next time*/
signal(SIGINT,catch_int);
printf("Testing Code to understand\n");
fflush(stdout);
}

int main(int argc,char* argv[])
{
/*set the INT (Ctrl-C) signal handler to 'catch_int' */
signal(SIGINT,catch_int);
/*now,lets get into an infinite loop of doing nothing.*/
for(;;)
pause();
}

The pause() system call causes the process to halt execution, until a signal
is received. It is suerly better than a 'busy wait' infinite loop.


2. Sending signals from the command Line: Like Kill
kill-<signal><PID>
If no signal name or number is specified, by default TERM signal is sent to
the process.
3. Using Susyem Calls: This way of generating signals is acheived by using
Kill system call (normal of sending a signal from one process to another).

Bye,
Bharti.

"pdn" <preetham@sasken.com> wrote in message
news:ds7ggl$jc4$1@ncc-m.sasken.com...
> Hi,
> How do i catch the signals generated by keypress using signals.
>
>



"SASKEN RATED Among THE Top 3 BEST COMPANIES TO WORK FOR IN INDIA - SURVEY 2005 conducted by the BUSINESS TODAY - Mercer - TNS India"

SASKEN BUSINESS DISCLAIMER
This message may contain confidential, proprietary or legally Privileged information. In case you are not the original intended Recipient of the message, you must not, directly or indirectly, use, Disclose, distribute, print, or copy any part of this message and you are requested to delete it and inform the sender. Any views expressed in this message are those of the individual sender unless otherwise stated. Nothing contained in this message shall be construed as an offer or acceptance of any offer by Sasken Communication Technologies Limited ("Sasken") unless sent with that express intent and with due authority of Sasken. Sasken has taken enough precautions to prevent the spread of viruses. However the company accepts no liability for any damage caused by any virus transmitted by this email
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2006-05-26 06:53    [W:0.037 / U:2.436 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site