3 * Copyright (C) 2002 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /****************************************************************
22 * Example of a broken implementation for sleep
24 * Author: Simone Piccardi
27 ****************************************************************/
29 * Include needed headers
32 #include <unistd.h> /* unix standard library */
33 #include <signal.h> /* signal constants, types and functions */
35 void alarm_hand(int sig) {
36 /* check if the signal is the right one */
37 if (sig != SIGALRM) { /* if not exit with error */
38 printf("Something wrong, handler for SIGALRM\n");
40 } else { /* do nothing, just interrupt pause */
44 unsigned int sleep(unsigned int seconds)
46 sighandler_t prev_handler;
47 /* install and check new handler */
48 if ((prev_handler = signal(SIGALRM, alarm_hand)) == SIG_ERR) {
49 printf("Cannot set handler for alarm\n");
52 /* set alarm and go to sleep */
55 /* restore previous signal handler */
56 signal(SIGALRM, prev_handler);
57 /* return remaining time */