Ctrl c linux signal

List of terminal generated signals (eg Ctrl-C -> SIGINT)

The «keyboard» doesn’t send any signals, the line discipline does. Find out what a line discipline is, then read man 1 stty .

@Gilles Not really. The keyboard driver deals with scancodes and the like. The line discipline is a sort of higher level glue that gives the application an unified view over many other things, like modem lines, USB-to-serial adapters, HID devices, etc.

@SatoKatsura It’s a part of the operating system that sits beween the application and the keyboard. In other words, it’s part of the keyboard driver. With a hardware terminal (like a vt100), it’s the only transformation of keyboard input that’s done by the computer as opposed to the terminal itself.

2 Answers 2

The Linux N_TTY line discipline only sends three different signals: SIGINT, SIGQUIT, and SIGTSTP. By default the following control characters produce the signals:

No signals are involved when you press Ctrl-S or Ctrl-Q. They just throttle the flow of characters, preventing them from reaching the consuming process. Eventually, when buffers fill up, the producing process will block in the write system call until Ctrl-Q unthrottles the flow. Note that it is normal for the call to write can block even without Ctrl-S: if the receiving process is slow to consume the data sent to it, e.g. because it is spending most of its time processing already received data instead of reading new data.

man stty | grep -C1 signal is one source for these three being the only signals generated by the terminal.

Читайте также:  Mysql linux посмотреть базы данных

@TomHale . Except «modem control signals» are electrical signals. 🙂 Really, SIGINT , SIGQUIT and SIGTSTP are the only «usual» signals sent by the line discipline. On BSD you also have things like SIGINFO , but that’s not standard.

You can use stty to check or change the characters that generate signals.

$ stty -a | grep -Ewoe '(intr|quit|susp) = [^;]+' intr = ^C quit = ^\ susp = ^Z 

intr (interrupt) generates SIGINT , quit generates SIGQUIT , susp (suspend) generates SIGTSTP . stty -a will also show things like start = ^Q; stop = ^S; and erase = ^? (backspace), which don’t send signals but affect the terminal layer otherwise.

Plain stty will show the non-default settings and e.g. stty intr ^Q would change the interrupt character to ^Q instead of ^C .

I think ^L (form feed, new page) is not a terminal feature, but a character often used by applications to ask for a redraw the view, rechecking the window size at the same time.

Источник

Ctrl + C interrupt event handling in Linux

I am developing an application that uses C++ and compiles using Linux GNU C Compiler. I want to invoke a function as the user interrupts the script using Ctrl + C keys. What should I do? Any answers would be much appreciated.

@mozcelikors There’s no such thing as «libraries recognized by GCC». If there’s a library written in standard C, any sane C compiler should be able to compile it.

Grijesh, could you please give me an example? I only need to define a kill event, but I don’t have any idea.

3 Answers 3

When you press Ctr + C , the operating system sends a signal to the process. There are many signals and one of them is SIGINT. The SIGINT («program interrupt») is one of the Termination Signals.

Читайте также:  Install play on linux debian

There are a few more kinds of Termination Signals, but the interesting thing about SIGINT is that it can be handled (caught) by your program. The default action of SIGINT is program termination. That is, if your program doesn’t specifically handle this signal, when you press Ctr + C your program terminates as the default action.

To change the default action of a signal you have to register the signal to be caught. To register a signal in a C program (at least under POSIX systems) there are two functions

These functions require the header signal.h to be included in your C code. I have provide a simple example of the signal function below with comments.

#include #include #include // our new library volatile sig_atomic_t flag = 0; void my_function(int sig) < // can be called asynchronously flag = 1; // set flag >int main() < // Register signals signal(SIGINT, my_function); // ^ ^ // Which-Signal |-- which user defined function registered while(1) if(flag)< // my action when signal set it 1 printf("\n Signal caught!\n"); printf("\n default action it not termination!\n"); flag = 0; >return 0; > 

You can compile this code with gcc and execute it from the shell. There is an infinite loop in the code and it will run until you send a SIGINT signal by pressing Ctr + C .

Источник

Оцените статью
Adblock
detector