Advertisement
AJBF

Colamensaje

May 3rd, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. #include <sys/shm.h>
  2. #include <unistd.h>
  3. #include <sys/stat.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <wait.h>
  8. #include <fcntl.h>
  9. #include<sys/ipc.h>
  10.  
  11. struct mensaje
  12. {
  13. long Tipo_Mensaje;
  14. char texto_mensaje[512];
  15. } mensaje;
  16.  
  17. int main ()
  18. {
  19. key_t Clave;
  20. int Id_Cola_Mensajes;
  21. int aux;
  22. int maxNum = 10;
  23. int pid1, pid2;
  24. int val;
  25. //mensaje Un_Mensaje;
  26.  
  27. Clave = ftok ("/bin/ls", 33);
  28. if (Clave == -1)
  29. {
  30. printf("Error, no se consigue la clave\n");
  31. exit(EXIT_FAILURE);
  32. }
  33.  
  34. Id_Cola_Mensajes = msgget (Clave, 0777 | IPC_CREAT);
  35.  
  36. if (Id_Cola_Mensajes == -1)
  37. {
  38. printf("Error no se consigue ID\n");
  39. exit(EXIT_FAILURE);
  40. }
  41.  
  42. pnt(mensaje.texto_mensaje, "0");
  43. val = atoi(mensaje.texto_mensaje);
  44. //mensaje.Tipo_Mensaje = 1;
  45. //mensaje.texto_mensaje = 0;
  46. printf("fin primera etapa\n");
  47.  
  48. //struct mensaje buffer_mensaje;
  49. */
  50. pid1 = fork();
  51.  
  52. if(pid1 == -1)
  53. {
  54. printf("ERROR\n");
  55. exit(EXIT_FAILURE);
  56. }
  57.  
  58. if (pid1 == 0)
  59. {
  60. msgsnd (Id_Cola_Mensajes, (struct msgbuf *)&mensaje, 512, IPC_NOWAIT);
  61. msgrcv (Id_Cola_Mensajes, (struct msgbuf *)&mensaje, 512, 0, 0);
  62.  
  63. //printf("HOLA ESTOY CORRIENDO EN HIJO UNOO\n");
  64. while (mensaje.texto_mensaje <= maxNum)
  65. {
  66. aux=mensaje.texto_mensaje;
  67. if ((aux%2) == 0)
  68. {
  69. aux++;
  70. if (aux <= maxNum)
  71. printf("%d\n", aux);
  72. mensaje.texto_mensaje = aux;
  73. }
  74. }
  75.  
  76. exit(EXIT_SUCCESS);
  77. }
  78. else
  79. {
  80. pid2 = fork();
  81. if(pid2 == -1)
  82. {
  83. printf("ERROR\n");
  84. exit(EXIT_FAILURE);
  85. }
  86. if (pid2 == 0)
  87. {
  88. msgsnd (Id_Cola_Mensajes, (struct msgbuf *)&mensaje, sizeof(mensaje.texto_mensaje)+sizeof(mensaje.mensaje), IPC_NOWAIT);
  89. msgrcv (Id_Cola_Mensajes, (struct msgbuf *)&mensaje, sizeof(mensaje.texto_mensaje) + sizeof(mensaje.mensaje), 2, 0);
  90. // printf("HOLA ESTOY CORRIENDO EN HIJO DOS\n");
  91. while (*Memoria <= maxNum)
  92. {
  93. aux = mensaje.texto_mensaje;
  94. if ((aux%2) == 1)
  95. {
  96. aux++;
  97. if (aux <= maxNum)
  98. printf("%d\n", aux);
  99. mensaje.texto_mensaje = aux;
  100. }
  101. }
  102. exit(EXIT_SUCCESS);
  103. }
  104. //printf("HOLA ESTOY CORRIENDO ALFINAL\n");
  105.  
  106. msgctl (Id_Cola_Mensajes, IPC_RMID, (struct msqid_ds *)NULL);
  107. exit(EXIT_SUCCESS);
  108.  
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement