Home
       bsleep.c - bsleep - breakable sleep
  HTML git clone git://kroovy.de/bsleep
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       bsleep.c (612B)
       ---
            1 #define _POSIX_SOURCE
            2 #include <signal.h>
            3 #include <stdio.h>
            4 #include <stdlib.h>
            5 #include <sys/mman.h>
            6 #include <unistd.h>
            7 
            8 int
            9 main(void)
           10 {
           11         int i;
           12         char *shmem = mmap(NULL, sizeof(char), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
           13         pid_t pid = fork();
           14                 
           15         if (pid == 0) {
           16                 /* CHILD */
           17                 for (i=1;;i++) {
           18                         printf("\r[ press 'b' to interrupt: %ds ] [ '%c' was pressed ] ", i, *shmem); fflush(stdout);
           19                         sleep(1);
           20                 }
           21         } else {
           22                 /* PARENT */
           23                 system("/bin/stty raw -echo");
           24                 while ((*shmem = getchar()) != 'b');
           25                 kill(pid, SIGKILL);
           26                 system("/bin/stty cooked echo");
           27                 printf("\n");
           28         }
           29 }