Advertisement
TblDblKA

ground_button.c

Dec 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include <stdio.h> //puts
  2. #include <signal.h> //signal, kill
  3. #include <unistd.h> //read,
  4. #include <sys/types.h> //kill, getppid
  5.  
  6. pid_t cabin;
  7. static int status = 0; //0 means that button is inactive, 1 - it is activated (or pressed)
  8.  
  9. void
  10. react(int sig);
  11.  
  12. void
  13. press(int sig)
  14. {
  15. signal(SIGUSR1, SIG_IGN);
  16. if (status != 1) {
  17. status = 1;
  18. puts("You've called an elevator from street");
  19. printf("%d\n",cabin);
  20. sleep(4);
  21. kill(cabin, SIGUSR1);
  22. }
  23. signal(SIGUSR1, press);
  24. return;
  25. }
  26.  
  27. void
  28. react(int sig)
  29. {
  30. puts("Ground button is set inactive");
  31. status = 0;
  32. return;
  33. }
  34.  
  35. void
  36. sig_int_handl(int sig)
  37. {
  38. //signal(SIGINT, sig_int_handl);
  39. if (status == 0) {
  40. puts("Button on ground - not pressed");
  41. }
  42. if (status == 1) {
  43. puts("Button on ground is pressed");
  44. }
  45. return;
  46. }
  47.  
  48. int
  49. main(void)
  50. {
  51. //signal(SIGFPE, SIG_IGN);
  52. signal(SIGTERM, SIG_DFL);
  53. pid_t t;
  54. read(0, &t, sizeof(pid_t));
  55. printf("%d BLYA\n", t);
  56. //pid_t ppid = getppid();
  57. //kill(ppid, SIGUSR1); //our process indicates that it is ready to work
  58. signal(SIGUSR1, press); //вызвали лифт с поверхности
  59. signal(SIGTRAP, react); //ответ от двери
  60. signal(SIGINT, sig_int_handl);
  61. while(1){
  62. pause();
  63. }
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement