signals.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <ctype.h>
  6. #include <signal.h>
  7. #include <unistd.h>
  8. #include "boggle.h"
  9. extern int rounds;
  10. extern int signaled;
  11. extern int end;
  12. void new_round (int sig) {
  13. //system("clear");
  14. //puts("...");
  15. //system("clear");
  16. //rounds++;
  17. //puts("here\n");
  18. signaled = 1;
  19. alarm(TIME_INTERVAL);
  20. }
  21. int create_sigaction_rounds() {
  22. //create sigaction variable
  23. struct sigaction action;
  24. action.sa_handler = new_round;
  25. sigemptyset(&action.sa_mask);
  26. action.sa_flags = 0;
  27. //register new handling behaviour
  28. sigaction(SIGALRM, &action, NULL);
  29. //int i = 0;
  30. return 0;
  31. }
  32. void end_game (int sig) {
  33. end = 1;
  34. }
  35. int create_sigaction_endgame() {
  36. //create sigaction variable
  37. struct sigaction action;
  38. action.sa_handler = end_game;
  39. sigemptyset(&action.sa_mask);
  40. action.sa_flags = 0;
  41. //register new handling behaviour
  42. sigaction(SIGINT, &action, NULL);
  43. //int i = 0;
  44. return 0;
  45. }
  46. /*char *name;
  47. void sing(int code) {
  48. printf("Happy birthday to you,\n");
  49. printf("Happy birthday to you,\n");
  50. sleep(4);
  51. printf("Happy birthday dear %s,\n", name);
  52. printf("Happy birthday to you!\n");
  53. }
  54. int catch_signal(int sig, void (*handler)(int)) {
  55. struct sigaction action;
  56. action.sa_handler = handler;
  57. sigemptyset(&action.sa_mask);
  58. sigaddset (&action.sa_mask, SIGINT);
  59. action.sa_flags = 0;
  60. return sigaction (sig, &action, NULL);
  61. }
  62. int task3_blocking(int argc, char **argv) {
  63. catch_signal(SIGUSR1, sing);
  64. sigset_t block_set;
  65. sigemptyset(&block_set);
  66. sigaddset(&block_set, SIGUSR1);
  67. for (;;) {
  68. printf("Busy studying! Go away.\n");
  69. // Don't be interrupted by SIGUSR1.
  70. // sigprocmask(SIG_BLOCK, &block_set, NULL);
  71. // sleep(30);
  72. //
  73. // printf("Okay I can party now.\n");
  74. // sigprocmask(SIG_UNBLOCK, &block_set, NULL);
  75. // sleep(20);
  76. // }
  77. // return 0;
  78. // }*/