0

Our current project is based on extending more by including scroll. In order to do this, a timer interval has to be set for a certain period. The part I'm not sure about is where the loop for the alarm signal should be. All the examples I've seen have the timer values in the main, then explicitly call the signal handler via pause() in an infinite while loop.

My code is a bit different, since the functionality requirements go like

print first screen of text after getting terminal dimensions
print prompt
   if prompt = space, print another screen of text //WORKS
   if prompe = q, restore original terminal settings & quit program //WORKS
   if prompt = ENTER, initialize scroll at 1 line every 2 seconds //DOESN'T WORK
   if prompt == f/s, increase/decrease scroll speed by 20%  //DOESN'T WORK

The read in buffer, file pointer and itimerval struct are all global variables to avoid passing as arguments through a chain of functions.

The main function of the program is

void processInput(FILE *fp){
  void printLine(int); //prints a single line of text
  signal(SIGPROF, printLine);
  int c;

  //print first screen of text, check for more text to display

  info(); //print prompt at bottom of screen
  FILE *fterm= fopen("/dev/tty", "r");

  while ((c=getc(fterm)) != EOF){
    if (c== '\n'){
      setTimer(2);

     //four more conditionals like this in basic similarity

  }
}

My setTimer function has a base interval of 2 seconds, and changes that by plus/minus 20% based on f/s input from the user.

void setTimer(int direction){
  int speed=2000000; //2 seconds
  int change= 400000; //400 milliseconds, 20% of 2 seconds
  if (direction == 1) //slow down by 20%
    speed+= change;
  if (direction == 0)
    speed -= change;

  timer.it_value.tv_sec=2;
  timer.it_value.tv_usec=0;
  timer.it_interval.tv_sec=0;
  timer.it_interval.tv_usec= speed;

  setitimer(ITIMER_PROF, &timer, NULL);

}

First question: should I use SIGALRM vs SIGPROF, and alter the ITIMER_XXXX variable accordingly?

Second, where should I put in the loop to trigger the signal? I tried

while(1)
  pause();

in several of the conditionals, but it had the effect of stopping the execution and ignoring any input.

2 Answers 2

1

Without knowing the details of your requirements, couldn't you do this more easily using select()?

Set your initial select timeout to 2 seconds and adjust according to f/s input, meanwhile if there is any standard input before the timeout you process it.

More or less valid general outline:

   int retval;
fd_set rfds;

int input = fileno(fterm);

struct timeval tv, delay;

delay.tv_sec  = 2;
delay.tv_usec = 0;

while (true)
{
    FD_ZERO(&rfds);
    FD_SET(input, &rfds);

    tv.tv_sec  = delay.tv_sec;
    tv.tv_usec = delay.tv_usec;

    retval = select(input + 1, &rfds, NULL, NULL, &tv);

    if (retval == -1)
        perror("select()");
    else
        if (retval)
        {
            if (FD_ISSET(input, &rfds))
            {
                command = readInput(...);

                switch(command)
                {
                    case 'q' ... cleanup & quit
                    case 's' ... calc 20% off delay values
                    case etc ...
                    case default...error handling
                }
            }
        }
        else //timeout
            printLine();
}
1

Working with pause() is dangerous because it's not an atomic operation ... your program could be interrupted by the OS causing you to "lose" the arrival of a signal. Additionally, when pause() itself returns because of the arrival of a signal, it will simply call pause() again. That means you're going to have to-do all your work inside of a signal handler, which may not be the best thing, i.e., if you're inside the signal handler when the next signal goes off, you can end up with some unpredictable behavior if you haven't planned for that sort of event.

A better approach would be to-do the following:

1) Setup a signal mask that blocks SIGPROF at the start of your program.
2) Rather than using a signal handler to-do your heavy lifting, use sigwait(), and set it up with a sigset_t that contains a mask for SIGPROF.
3) Setup the main flow of your program the following way:

sigset_t sigset;
sigemptyset(&sigset);
sigaddset(&sigset, SIGPROF);
sigprocmask(SIG_BLOCK, &sigset, NULL); //block the SIGPROF signal

//process your input

//if you need to, initialize your timer and set it to go off

while(SOME_FLAG_IS_TRUE) //maybe this loops forever ... depends on what you want?
{
    sigwait(&sigset, &signal_num);

    if (signal_num != SIGPROF)
        continue;

    //process input ... 

    //... setup new interval timer with correct timeout ...

    //... repeat loop and/or exit loop or set flag to exit loop
}

That should always catch the signal from the interval timer since sigwait() will properly return after waiting for a signal to arrive to your process, and the SIGPROF signal is always blocked, meaning you can't "lose" signals ... instead at least one of them will be queued up and waiting for the next call to sigwait() to be detected just in case one arrives while you're processing something in your while-loop.

Hope this helps,

Jason

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.