Advertisement
Guest User

Untitled

a guest
May 24th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/wait.h>
  8. #include <unistd.h>
  9. #include <math.h>
  10. #include <signal.h>
  11.  
  12. int count = 0;
  13. int mode = 0;
  14. const uint32_t adress_mask = 1073676288;
  15. const uint32_t host_mask = 16383;
  16.  
  17. uint32_t is_category_b(uint32_t num) {
  18. if (num >> 30 == 2) {
  19. return 1;
  20. }
  21. return 0;
  22. }
  23.  
  24. uint32_t get_adress(uint32_t num) {
  25. return (num & adress_mask) >> 16;
  26. }
  27.  
  28. uint32_t get_host(uint32_t num) {
  29. return num & host_mask;
  30. }
  31.  
  32. void signal_handler(int signo) {
  33. if (count == 4) {
  34. exit(0);
  35. }
  36. if (signo == SIGUSR1) {
  37. mode = 1 - mode;
  38. ++count;
  39. } else if (signo == SIGUSR2) {
  40. uint32_t num;
  41. scanf("%o", &num);
  42. if (is_category_b(num)) {
  43. if (mode == 0) {
  44. printf("%d\n", get_adress(num));
  45. fflush(stdout);
  46. } else if (mode == 1) {
  47. printf("%d\n", get_host(num));
  48. fflush(stdout);
  49. }
  50. } else {
  51. printf("%d\n", 0);
  52. }
  53. }
  54. }
  55.  
  56. int main(int argc, char ** argv) {
  57. struct sigaction sa = {};
  58. sa.sa_handler = signal_handler;
  59. sa.sa_flags = SA_RESTART;
  60. sigaction(SIGUSR1, &sa, NULL);
  61. sigaction(SIGUSR2, &sa, NULL);
  62. printf("%d\n", getpid());
  63. fflush(stdout);
  64. while (42) {
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement