Sistemate alcune citazioni, aggiunti alcuni esempi.
[gapil.git] / sources / sleep1.c
diff --git a/sources/sleep1.c b/sources/sleep1.c
new file mode 100644 (file)
index 0000000..224b726
--- /dev/null
@@ -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 <unistd.h>      /* 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;
+    }
+}