X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=blobdiff_plain;ds=sidebyside;f=sources%2Fsleep1.c;fp=sources%2Fsleep1.c;h=224b7269db67f63f18a3fb115933f3057ae6a902;hb=86de518c1856b983608c791e0bbbc265aaf8e9b4;hp=0000000000000000000000000000000000000000;hpb=e63a5e53036597d8027e2bf727c606698863a5d6;p=gapil.git diff --git a/sources/sleep1.c b/sources/sleep1.c new file mode 100644 index 0000000..224b726 --- /dev/null +++ b/sources/sleep1.c @@ -0,0 +1,59 @@ +/* sleep1.c + * + * Copyright (C) 2002 Simone Piccardi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/**************************************************************** + * + * Program sleep1.c: + * Example of a broken implementation for sleep + * + * Author: Simone Piccardi + * Sep. 2002 + * + ****************************************************************/ +/* + * Include needed headers + */ +#include /* unix standard library */ + +unsigned int sleep(unsigned int seconds) +{ +/* + * Variables definition + */ + signandler_t prev_handler; + + if ((prev_handler = signal(SIGALRM, alarm_hand)) == SIG_ERR) { + printf("Cannot set handler for alarm\n"); + exit(1); + } + alarm(second); + pause(); + /* restore previous signal handler */ + signal(SIGALRM, prev_handler); + /* remove alarm, return remaining time */ + return alarm(0); +} +void alarm_hand(int sig) { + /* check if the signal is the right one */ + if (sig != SIGALRM) { /* if not exit with error */ + printf("Something wrong, handler for SIGALRM\n"); + exit(1); + } else { /* do nothing, just interrupt pause */ + return; + } +}