0

Just learned about sigacation, also implemented another timer to make it more interesting.

#include <stdio.h>
#include <signal.h> 
#include <sys/time.h>

volatile sig_atomic_t gotsignal;


void handler(){

    gotsignal = 1;

}

int main(){

struct sigaction sig;
struct itimerval timer;

timer.it_value.tv_sec = 5;
timer.it_value.tv_usec = 0;
timer.it_interval = timer.it_value;

sig.sa_handler = handler; 
sig.sa_flags = 0; 
sigemptyset(&sig.sa_mask); 

setitimer(ITIMER_REAL, &timer, 0);
sigaction(SIGALRM, &sig, NULL);


int value, y = 100, x=0;

while(!gotsignal && x < y){
    printf("Insert a value: \n");
    scanf("%d", &value);
    x+=value;
    printf("Current x value: %d\n", x);
}
}

What i don't understand is, when it is waiting for user input and i write 5, but not press enter. He still reads it? Shouldn't he clear it off? The output it gave me:

Insert a value: 
1
Current x value: 1
Insert a value: 
2
Current x value: 3
Insert a value: 
5Current x value: 5

What I would want would be like:

Insert a value: 
1
Current x value: 1
Insert a value: 
2
Current x value: 3
Insert a value: 
5(Wthout pressing enter!)Current x value: 3 (He would forget about 5 no?)

1 Answer 1

3

A (pedantically) correct signal handler can do very few things: notably setting a volatile sig_atomic_t variable (this is some integer type), and perhaps calling siglongjmp [I'm not even sure for siglongjmp].

So declare first

volatile sig_atomic_t gotsignal;

then your signal handler is simply

void handler (void)
{
  gotsignal = 1;
}

and your loop is

while(!gotsignal && x < y){
    printf("Insert a value: \n");
    scanf("%d", &value);
    x+=value;
}

Don't forget that asynchronous signals happen at any time (any machine instruction!!!), including inside malloc or printf. Never call these functions from inside a handler.

Bugs related to bad signal handling are hard to debug: they are not reproducible!

Consider perhaps using sigaction.

5
  • Hmmm. That makes sense. But what if I want the program to stop waiting for the scanf after the signal. For example, as soon as he gets the signal he would just forget about the scanf that he is waiting for and restart the loop normally. Is that possible?
    – JPS
    Nov 26, 2011 at 21:55
  • I think that the SIGALRM signal will stop the scanf, since the read syscall (called by scanf) would get an EINTR error. You might want to learn also about the select, pselect or poll (or ppoll) system calls. You can test if scanf failed by its returned value. Nov 26, 2011 at 21:57
  • +1 for also suggesting sigaction, which unlike signal, is fully POSIX compliant.
    – jweyrich
    Nov 26, 2011 at 22:22
  • @BasileStarynkevitch Just edited the starting post, do you think you can still help me please?
    – JPS
    Nov 27, 2011 at 13:02
  • 1
    Reading from the terminal is complex in the details, since it is a tty See linusakesson.net/programming/tty/index.php & en.wikipedia.org/wiki/Pseudo_terminal so you might want to use readline or ncurses libraries. Nov 27, 2011 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.