Re: trapping key press using signals

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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" <[email protected]> wrote in message 
news:[email protected]...
> 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 [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[Index of Archives]     [Kernel Newbies]     [Netfilter]     [Bugtraq]     [Photo]     [Stuff]     [Gimp]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Video 4 Linux]     [Linux for the blind]     [Linux Resources]
  Powered by Linux