C exit from infinite loop on keypress - c

How can I exit from an infinite loop, when a key is pressed?
Currently I'm using getch, but it will start blocking my loop as soon, as there is no more input to read.

If you are using getch() from conio.h anyway, try to use kbhit() instead. Note that both getch() and kbhit() - conio.h, in fact - are not standard C.

The function kbhit() from conio.h returns non-zero value if any key is pressed but it does not block like getch(). Now, this is obviously not standard. But as you are already using getch() from conio.h, I think your compiler has this.
if (kbhit()) {
// keyboard pressed
}
From Wikipedia,
conio.h is a C header file used in old MS-DOS compilers to create text user interfaces. It is not described in The C Programming Language book, and it is not part of the C standard library, ISO C nor is it required by POSIX.
Most C compilers that target DOS, Windows 3.x, Phar Lap, DOSX, OS/2, or Win321 have this header and supply the associated library functions in the default C library. Most C compilers that target UNIX and Linux do not have this header and do not supply the library functions.

I would suggest that you go throgh this article.
Non-blocking user input in loop without ncurses.

If you do not want to use non-standard, non-blocking way and yet graceful exit. Use signals and Ctrl+C with user provided signal handler to clean up. Something like this:
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
/* Signal Handler for SIGINT */
void sigint_handler(int sig_num)
{
/* Reset handler to catch SIGINT next time.
Refer http://en.cppreference.com/w/c/program/signal */
printf("\n User provided signal handler for Ctrl+C \n");
/* Do a graceful cleanup of the program like: free memory/resources/etc and exit */
exit(0);
}
int main ()
{
signal(SIGINT, sigint_handler);
/* Infinite loop */
while(1)
{
printf("Inside program logic loop\n");
}
return 0;
}

// Include stdlib.h to execute exit function
int char ch;
int i;
clrscr();
void main(){
printf("Print 1 to 5 again and again");
while(1){
for(i=1;i<=5;i++)
printf("\n%d",i);
ch=getch();
if(ch=='Q')// Q for Quit
exit(0);
}//while loop ends here
getch();
}

Related

CTRL-C handler not working properly

This program
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <windows.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
void INThandler(int);
int main(void)
{
signal(SIGINT, INThandler);
char data[128];
int n;
while((n=read(0, data, 128)) > 0)
{
if(data[n] = '\n') break;
}
data[n] ='\0';
printf("%s", data);
return 0;
}
void INThandler(int sig)
{
char c;
signal(sig, SIG_IGN);
printf("OUCH, did you hit Ctrl-C?\n"
"Do you really want to quit? [y/n] ");
c = getchar();
if (c == 'y' || c == 'Y')
exit(0);
else
signal(SIGINT, INThandler);
}
Doesn't handle ctrl-c, but terminates at that input.
If I replace everything between the handler install and the return by
while (1)
Sleep(1);
the handler function is called and works but I want to have the read() in there.
EDIT: Looking back at this program, I noticed that I have
if(data[n] = '\n') break;
I wrote '=' instead of '==', but by using the later, it doesn't work properly and I don't understand why. Shouldn't it be a comparison to detect '\n'?
Also, I messed around with the buffer, but I can't make keep the input if I hit CTRL-C.
The example code does not take into accounts two issues:
read() will abort it's work if its process receives a signal (see man 2 read)
It is only guaranteed for a few functions that they can be called savely from a signal handler function (see man 7 signal). printf()and getch() do not not belong to this set of "save" functions.
The first issue could be fixed using a more differentiated way to handle the value returned by read(). I should counts how much data had already been read, together with some smart adjustment of the buffer passed to read().
Regarding the second issue, read() and write() are the functions of choice to perform input/output to/from a signal handler as those are listed to be "save" functions by the man-page mentioned above.
It's because of the implementation of read(2) you use doesn't allow that. Here is what I get when I compile your program, execute it and press ^C:
$ ./program
^COUCH, did you hit Ctrl-C?
Do you really want to quit? [y/n] y
$
or if I answer n and enter some data:
$ ./program
^COUCH, did you hit Ctrl-C?
Do you really want to quit? [y/n] n
test
test
$
Since read(2) is a system call, it depends on the system you use. I use Linux (3.2.0-4-686-pae) and here it's behaving correctly. Maybe you should consider to use scanf(3) or fread(3) with stdio(3) instead, for the platform you're using (which I take to be win32)? Or also use a different implementation, like the ones from Cygwin or MinGW?

Signal issue when enter Ctrl+C

I am a newbie in Linux programming.I copied the code below from a book:
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void ouch (int sig)
{
printf("OUCH! - I got signal %d\n", sig);
(void) signal(SIGINT, SIG_DFL);
}
int main ()
{
(void) signal(SIGINT, ouch);
while(1)
{
printf("Hello World!\n");
sleep(1);
}
}
It was expected to print something when Ctrl+C was entered.But it do nothing but print Hello World!.
EDIT:
I am so sorry that I have binded the Ctrl+C as a short-cut key for copy.
Sorry for trouble caused.
My Suggestion is don't use printf in siginal handler (ouch), it may be undefined behavior. Async-signal-safe functions: The list of safe functions that can be call in signal handler man page.
It is not safe to call all functions, such as printf, from within a signal handler.
A useful technique is to use a signal handler to set a flag and then check that flag
from the main program and print a message if required.
Reference: Beginning Linux Programming, 4th Edition,In this book exactly your code is explained, Chapter 11: Processes and Signals, page 484
An additional helpful link:
Explanation: Use reentrant functions for safer signal handling
Sorry, I can't see a question here... but I can guess what you are interested in.
printf() is a stateful function, thus not reentrant. It uses a FILE structure (variable name is 'stdin') to keep it's state. (It is like calling fprintf(stdin,format,...)).
That means, dependant on implementation and 'luck', calling printf() from a signal handler may print what you expect, but also may print nothing or may even crash or worse, smash your memory! Anything could happen.
So, just don't call functions from within a signal handler that are not explicitely marked 'signal-safe'. You will avoid lot's of headaches in the long term.
Put an fflush(stdout) in your signal handler. It was just buffered, then the second SIGINT exited the program before the buffer could be flushed.

What is the difference between normal function call and signal() system call?

While learning signal() system call, I supposed to come across the following code,
#include <stdio.h>
#include <signal.h>
void sigproc(int);
void quitproc(int);
int main(int argc,char **argv)
{
signal(SIGINT, sigproc); //Is it like a normal Call to signal()?
signal(SIGQUIT, quitproc);// This too?
printf("ctrl- c disabled use ctrl\ to quit \n");
while(1);
return 0;
}
void sigproc(int signo)
{
printf("you have pressed ctrl - c \n");
}
void quitproc(int signo)
{
printf("U cant quit\n");
// exit(0);
}
I am calling the function signal() twice in main(). But its executed only when I'm pressing Ctrl-C and Ctrl-\ keys. I thought its also like normal function call. What is actually happening in the signal handler functions?
The signal function establishes a signal handler. What it means: "When my process receives this signal, run this function instead of doing whatever the default was".
So, in your example the calls to signal don't call the function. To actually see the signals in action, do this:
Start your process in one terminal
From another terminal:
kill -INT `pidof proc`
As a side note, printf and friends aren't async-signal-safe. It might come as a shocker, but it's unsafe to use them in signal handlers.
As a side side note, even if you tagged your question Unix it's important to know that signals (and the signal function) are standard, integral parts of C. Signal handling and the signal function are described in C99 in ยง7.14.1.

Signal Handling in C

How can I implement signal Handling for Ctrl-C and Ctrl-D in C....So If Ctrl-C is pressed then the program will ignore and try to get the input from the user again...If Ctrl-D is pressed then the program will terminate...
My program follows:
int main(){
char msg[400];
while(1){
printf("Enter: ");
fgets(msg,400,stdin);
printf("%s\n",msg);
}
}
Thanks,
Dave
When dealing with POSIX signals, you have two means at your disposal. First, the easy (but discouraged) way, signal(). Second, the more elegant, current but complex way, sigaction(). Please use sigaction() unless you find that it isn't available on some platform that you need to work on.
This chapter of the glibc manual explains differences between the two and gives good example code on how to use both. It also lists the signals that can be handled, recommends how they should be handled and goes more in depth on how to tell how any given signal is (or is not) currently being handled. That's way more code than I'd want to paste into an answer here, hence the links.
It really is worth the hour or two it would take you to read the links and work through the examples. Signal handling (especially in programs that daemonize) is extremely important. A good program should handle all fatal signals that can be handled (i.e. SIGHUP) and explicitly ignore signals that it might not be using (i.e. SIGUSR1 / SIGUSR2).
It also won't hurt to study the difference between normal and real time signals, at least up to the understanding of how the kernel merges the prior and not the latter.
Once you work through it, you'll probably feel inclined to write up an easy to modify set of functions to handle your signals and re-use that code over and over again.
Sorry for not giving a quick and dirty code snippet to show you how to solve your immediate need, but this isn't a quick and dirty topic :)
Firstly, Ctrl+D is an EOF indicator which you cannot trap, when a program is waiting for input, hitting Ctrl+D signifies end of file and to expect no more input. On the other hand, using Ctrl+C to terminate a program - that is SIGINT, which can be trapped by doing this:
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <stdarg.h>
static void signal_handler(int);
static void cleanup(void);
void init_signals(void);
void panic(const char *, ...);
struct sigaction sigact;
char *progname;
int main(int argc, char **argv){
char *s;
progname = *(argv);
atexit(cleanup);
init_signals();
// do the work
exit(0);
}
void init_signals(void){
sigact.sa_handler = signal_handler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(SIGINT, &sigact, (struct sigaction *)NULL);
}
static void signal_handler(int sig){
if (sig == SIGINT) panic("Caught signal for Ctrl+C\n");
}
void panic(const char *fmt, ...){
char buf[50];
va_list argptr;
va_start(argptr, fmt);
vsprintf(buf, fmt, argptr);
va_end(argptr);
fprintf(stderr, buf);
exit(-1);
}
void cleanup(void){
sigemptyset(&sigact.sa_mask);
/* Do any cleaning up chores here */
}
In your example it seems you don't need CTRL-C handlind at all. A "signal(SIGINT,SIG_IGN)" seems enough for you, unless your application must handle a SIGINT coming from some other source.
CTRL-D doesn't usually generates signals, it simply communicates the EOF condition.
You can in general control the behavior of your terminal (we are talking about console input, it isn't?) by using the termios library (also here). You can enable, redefine or disable the "interrupt" character (CTRL-C), the EOF one and many other ones (XON, XOFF, modem control...)
Regards
This is a program for handling signal when pressed Ctrl+c
The syntax for signal function is : signal(signal name, function name);
#include<stdio.h>
#include<signal.h> // for handling signal
void signal_handler()
{
printf("Signal Handled here\n");
}
main()
{
printf("In main function..\n");
// SIGINT is signal name create when Ctrl+c will pressed
signal(SIGINT,signal_handler);
sleep(15);
printf("In main after called from signal_handle \n");
}
#include<signal.h>
#include<unistd.h>
#include<stdio.h>
void signal_catch()
{
printf("hi,Your signal catched Here");
}
int main()
{
signal(SIGINT,signal_catch);
//press ctrl+c
sleep(10);
return 0;
}//end main
//if you want to simply ignore ctrl+c interrupt use following code in main
int main()
{
signal(SIGINT,SIG_IGN);
sleep(100);
return 0;
}//end main
//this program wont accept ctrl+c interrupt for 100 seconds.

Continue running program until user type Ctrl + a

I'm practicing C programming for Linux for an exam.
I don't know how to exit the program when user press Ctrl + a ( not Ctrl+c )
For example, looping something until user press Ctrl+a
Could anyone tell me how to check Ctrl+a input?
Notes: I'm using 'gcc' and run output with './a.out'
Thanks in advance for everyone!
Turbo C and other implementations of C for Windows had a function call getch() which would read single characters from the keyboard; those would have done what you want.
In POSIX environments, such as are implemented by gcc-compiled programs under Unix/Linux, that functionality isn't directly there.
There's a library called curses which allows C programs to do full-screen output processing, and there is also getch() functionality in curses. This may end up being the simplest answer to your problem. You'll need to read the documentation on curses and link the header files and library into your program.
There is special support for Ctrl-C, which is translated into a signal by the system. If you want your program to stop as soon as another specific key combination is used, it will be much harder.
you will need to check the standard input of your program, and you will need to set the standard input so the inputs are not buffered (otherwise you won't see any input until it is validated by the user pressing "return"). The latter part would be done with a ioctl() call and would not be portable;
you will need either threads or polling, none of which is very palatable in C.
There are more interesting things to practice in C than these.
catching ctrl-c event
This post answers your question
did you looking for something like this ???
this program won't be stopped since you hit ctrl+A and Enter.
#include <stdio.h>
int main() {
char a;
while( a!=1 ) //Ascii code for ctrl + A == 1
{
a=getchar();
printf("still looping ...\n");
}
printf( "HA! You pressed CTRL+A\n" );
return 0;
}
But if you wanna terminate your program just after pressing ctrl+A (without hitting enter after that), here you are:
#include <stdio.h>
#include <ncurses.h>
int main() {
char a;
initscr();
raw();
while( a!=1 )
a=getch();
endwin();
return 0;
}
for compiling second code using GCC, try this command:
gcc -o program.o -lncurses program.cpp
This will do what you want:
stty intr ^a
./a.out
For extra credit, do the equivalent of "stty intr ^a" using the appropriate library function, e.g. "man termios".
Someone posted first with following code which works for me but I don't know why he deleted his answer.
Original Link : http://www.c.happycodings.com/Gnu-Linux/code18.html
#include <stdio.h>
#include <unistd.h> /* sleep(1) */
#include <signal.h>
void ex_program(int sig);
int main(void) {
(void) signal(SIGINT, ex_program);
while(1)
printf("sleeping .. ZZZzzzz ....\n"), sleep(1);
return 0;
}
void ex_program(int sig) {
printf("Wake up call ... !!! - Catched signal: %d ... !!\n", sig);
(void) signal(SIGINT, SIG_DFL);
}
This is how to set CTRL-A as interrupt to break the process. Notice too that CTRL-C doesn't break it after tcsetattr is called.
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
int main(int argc, char* argv[]) {
struct termios termios;
if ( tcgetattr(0, &termios) != -1 ) {
termios.c_cc[VINTR] = '\x01'; /* CTRL-A */
if ( tcsetattr(0, 0, &termios) != -1 ) {
printf("Ready. Press CTRL-A to break this program\n");
while ( 1 ) {
printf("*\n");
sleep(1);
}
}
}
}

Resources