Advertisement
AJBF

Just testing

May 18th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/shm.h>
  6. #include <sys/types.h>
  7. #include <wait.h>
  8. #include <fcntl.h>
  9. #include <sys/ipc.h>
  10. #include <sys/stat.h>
  11.  
  12. void salir_si_error(char *);
  13. void handler_usuario1(int);
  14. void handler_usuario2(int);
  15.  
  16. int maxNum = 0;
  17.  
  18. int main(void)
  19. {
  20. struct sigaction accion;
  21. key_t Clave;
  22. pid_t pid1, pid2;
  23. //int IDM;
  24. //int * Memoria;
  25. int aux = 0;
  26.  
  27. printf("Ingrese un num max: ");
  28. scanf("%d",&maxNum);
  29.  
  30. Clave = ftok ("/bin/ls", 33);
  31. if (Clave == -1)
  32. {
  33. printf("Error, no existe la clave\n");
  34. exit(EXIT_FAILURE);
  35. }
  36.  
  37.  
  38. printf("Fin primera etapa\n");
  39.  
  40. pid1 = fork();
  41.  
  42. if(pid1 == -1)
  43. {
  44. printf("ERROR\n");
  45. exit(EXIT_FAILURE);
  46. }
  47.  
  48. if (pid1 == 0)
  49. {
  50. printf("Comienzo Segunda Etapa\n");
  51. accion.sa_handler = handler_usuario1;
  52. sigemptyset(&accion.sa_mask);
  53. accion.sa_flags = SA_NOCLDSTOP;
  54.  
  55. if((sigaction(SIGUSR1, &accion, NULL)) < 0)
  56. {
  57. salir_si_error("sigaction");
  58. }
  59.  
  60. printf("Fin Segunda Etapa\n");
  61. exit(EXIT_SUCCESS);
  62. }
  63.  
  64. else
  65. {
  66. pid2 = fork();
  67.  
  68. if(pid2 == -1)
  69. {
  70. printf("ERROR\n");
  71. exit(EXIT_FAILURE);
  72. }
  73.  
  74. if (pid2 == 0)
  75. {
  76. printf("Comienzo Tercera Etapa\n");
  77. accion.sa_handler = handler_usuario2;
  78. sigemptyset(&accion.sa_mask);
  79. accion.sa_flags = SA_NOCLDSTOP;
  80.  
  81. if((sigaction(SIGUSR2, &accion, NULL)) < 0)
  82. {
  83. salir_si_error("sigaction");
  84. }
  85.  
  86. printf("Fin Tercera Etapa\n");
  87.  
  88. exit(EXIT_SUCCESS);
  89. }
  90. kill(pid1, SIGUSR1);
  91. /*while (aux <= maxNum)
  92. {
  93. //printf("estoy en el while!\n");
  94. kill(pid1, SIGUSR1);
  95. kill(pid2, SIGUSR2);
  96. } */
  97.  
  98. exit(EXIT_SUCCESS);
  99.  
  100. }
  101.  
  102. }
  103.  
  104. void salir_si_error(char *mensaje)
  105. {
  106. perror(mensaje);
  107. exit(EXIT_FAILURE);
  108. }
  109.  
  110. void handler_usuario1(int signum)
  111. {
  112. int aux =0;
  113.  
  114. if(signum == SIGUSR1)
  115. {
  116. printf("heyyyy bro!");
  117. }
  118. else
  119. {
  120. printf("Interceptada %d\n", signum);
  121. }
  122. }
  123.  
  124. void handler_usuario2(int signum)
  125. {
  126. int aux = 0;
  127.  
  128. if(signum == SIGUSR2)
  129. {
  130. printf("heyyyy bro 2!");
  131.  
  132. }
  133. else
  134. {
  135. printf("Interceptada %d\n", signum);
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement