1

In the Chapter 10 Signals of the APUE book, there is a sample code:

#include <signal.h>
#include <unistd.h>

static void sig_alrm(int signo) {
    /* nothing to do, just return to wake up the pause */
}

unsigned int sleep1(unsigned int seconds) {
   if (signal(SIGALRM, sig_alrm) == SIG_ERR)
       return(seconds);

    alarm(seconds);     /* start the timer */
    pause();            /* next caught signal wakes us up */
    return(alarm(0));   /* turn off timer, return unslept time */
}

int main() {
    sleep1(1);
    return 0;
}

which simply implement an "imcomplete" sleep().

The book says that "This function looks like the sleep function, but this simple implementation has three problems."

  1. If the caller already has an alarm set, that alarm is erased by the first call to alarm. We can correct this by looking at alarm’s return value. If the number of seconds until some previously set alarm is less than the argument, then we should wait only until the existing alarm expires. If the previously set alarm will go off after ours, then before returning we should reset this alarm to occur at its designated time in the future.

  2. We have modified the disposition for SIGALRM. If we’re writing a function for others to call, we should save the disposition when our function is called and restore it when we’re done. We can correct this by saving the return value from signal and resetting the disposition before our function returns.

  3. There is a race condition between the first call to alarm and the call to pause. On a busy system, it’s possible for the alarm to go off and the signal handler to be called before we call pause. If that happens, the caller is suspended forever in the call to pause (assuming that some other signal isn’t caught).

I have some doubt about the above 3 statements:

  1. Can somebody provide some example code for the statement1?

  2. I don't know what the statement2 says, can someone give me robust explanation?

  3. I don't know why statement3 will cause a race condition.

Thanks a lot!

7
  • 1
    the posted code does not cleanly compile; The message from the compiler: "warning: unused parameter 'signo' [-Wunused-parameter]" This can be fixed by adding the statement: (void)signo; in the sig_alrm() function Dec 26, 2016 at 17:50
  • 1
    the function: sleep1() returns a value. however, the function: main() fails to make use of that value. Dec 26, 2016 at 17:52
  • 1
    It's surely can be compiled in my Mac.
    – walkcc
    Dec 26, 2016 at 17:53
  • 1
    It compiles on a Mac (or a Linux machine) when you don't ask for warnings. If you ask for warnings, as you should, you get warnings such as the unused parameter warning. I'd also have to make the functions static and use int main(void) to get your code past my default warning options (gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -c program.c). It is worth compiling with warning flags — they head off problems. Dec 26, 2016 at 17:59
  • 1
    when compiling, always enable all the warnings (for gcc, at a minimum use: -Wall -Wextra -pedantic I also use: -Wconversion -std=gnu99 ) Dec 26, 2016 at 18:01

2 Answers 2

2

to answer your question in the title.

The returned value is the number of seconds left in the prior call to alarm()

1

Your Q1:

int r1 = alarm(20);
printf("r1 = %d\n", r1);
int r2 = alarm(10);
printf("r2 = %d\n", r2);

This will usually print r1 = 0 and r2 = 20 because there was no alarm set at the first call and there were still 20 seconds left on the previous call at the second. At that point, there's 10 seconds left until the alarm goes off. There is only ever one alarm signal scheduled. You can't set alarms for 10 and 20 and 53 seconds hence; you can at most have one of these pending.

Your Q2:

  • If the code calling sleep1() already had a signal handler set to handle SIGALRM, you've thrown that information away. You should capture the return value from signal(SIGALRM, sig_alrm) and restore that when sleep1() is done. (It isn't clear what you should do if it was set to SIG_IGN.)

Your Q3:

  • Scheduling is not determinate. Your process could call alarm(1), and then be pre-empted. It could be more than a second before your program is rescheduled, and it will find that the alarm has gone off and handle the alarm before returning to executed pause(). Your call to pause() then will hang the program indefinitely because there is no alarm pending to wake it. You'll have to send a signal somehow.
2
  • For Q1: where to put the printf()? I put in the main, the result is r1 = 0 and r2 = 20, I still can't know how this work.
    – walkcc
    Dec 26, 2016 at 18:11
  • 1
    @walkcc read the manual : The return value of alarm() is the amount of time left on the timer from a previous call to alarm(). If no alarm is currently set, the return value is 0. Dec 26, 2016 at 19:42

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.