Signal function


 
Thread Tools Search this Thread
Top Forums Programming Signal function
# 1  
Old 11-23-2012
Linux Signal function

Hello I want to know how can i use signal function in c for keyboard interrupt handling. what i exactly want is : my program is processing and if i press any key while processing , the program should call the interrupt and displays/prints that key and now goes back to processing.

I added the <signal.h> and used signint as a keyboard interrupt handler but it only captures CTRL+C only. I want to capture every key.

Please help me in this thanks.
# 2  
Old 11-23-2012
You need to do two things:

1. set tty to raw mode otherwise it will buffer input and you will not be able to catch key events unless tty flushes the buffer (after pressing return by default)
2. attaching async signal (SIGIO) to your STDIN.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <signal.h>
#include <termios.h>

void sig_handler(int signo) {
    char key;
    
    read(STDIN_FILENO, &key, 1);
    printf("Key %c\n", key);
}

int main(int argc, char** argv) {
    int y = 1;
    struct termios raw;
    
    if (tcgetattr(STDIN_FILENO, &raw) < 0)
        return 1;
    
    /* raw input */
    raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
    
    /* 8bits per char */
    raw.c_cflag |= (CS8);
    
    /* turn off echoing */
    raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
    
    if (tcsetattr(STDIN_FILENO , TCSAFLUSH, &raw) < 0)
        return 1;
    
    
    /* async IO handling for stdin */
    if (ioctl(STDIN_FILENO, FIOASYNC, &y) == -1)
        return 1;
    
    if (signal(SIGIO, sig_handler) == SIG_ERR)
        return 1;
    
    while(1)
        sleep(1);

    return (EXIT_SUCCESS);
}

These 3 Users Gave Thanks to expl For This Post:
# 3  
Old 11-23-2012
The printf() function is not async-signal-safe and should never be called in a signal handler.
This User Gave Thanks to achenle For This Post:
# 4  
Old 11-23-2012
Quote:
Originally Posted by achenle
The printf() function is not async-signal-safe and should never be called in a signal handler.
Good eye! I haven't really thought it to much through, just posted a quick example.
This User Gave Thanks to expl For This Post:
# 5  
Old 11-23-2012
You learn that one the hard way. :-)
These 2 Users Gave Thanks to achenle For This Post:
# 6  
Old 11-23-2012
Thank you very much every one for quick response. It solved my problem. Specially thanks to "expl" for posting the code.

---------- Post updated at 03:34 PM ---------- Previous update was at 03:04 PM ----------

Sir ! now keys are printing correctly but i need to add the other part of code say e.g " Printf(" Other Processing\n"); " ... i want this statement to print continuously and if any key is pressed it prints that key and back to printing "other processing " continuously . I want this to happen until i press CTRL+C to break it. Thanks in advance.

---------- Post updated at 03:39 PM ---------- Previous update was at 03:34 PM ----------

I think it should be in while(1) loop like this :

Code:
  while(1)
    {
        printf("Other program Processing Stuff Here. \n");
            sleep(1);
    }

But its not printing the statement continuously, however keys interrupts are working correctly. Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Need help on awk for printing the function name inside each function

Hi, I am having script which contains many functions. Need to print each function name at the starting of the function. Like below, functionname() { echo "functionname" commands.... } I've tried like below, func=`grep "()" scriptname | cut -d "(" -f1` for i in $func do nawk -v... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

4. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

5. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

6. Shell Programming and Scripting

Record the Signal Type or Number in Bash Trap function

In my Bash script I have an exit/cleanup function in a trap statement like: trap exitCleanup 1 2 3 6 15 25 Is there anyway to capture which signal # has occurred to record in a log file. Please note I am trying to avoid something like: trap 'mySignal=1; exitCleanup' 1 trap... (1 Reply)
Discussion started by: ckmehta
1 Replies

7. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

8. Programming

Handling a signal with a class member function

Hello, i am using the sigaction function to handle the SIGCHLD signal.Is it possible to use a class member function as the handler function (the sa_handler member of the sigaction structure)? The function's signature is: void (*sa_handler)(int);so i don't think i can use a static member function... (2 Replies)
Discussion started by: Zipi
2 Replies

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

10. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies
Login or Register to Ask a Question