Advertisement
jaimolias

Untitled

Apr 10th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<sys/mman.h>
  4. #include<sys/stat.h>
  5. #include<fcntl.h>
  6. #include<unistd.h>
  7. #include<sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/wait.h>
  10. #include <time.h>
  11. #include <math.h>
  12. #include <string.h>
  13. #include <mqueue.h>
  14. #include <semaphore.h>
  15.  
  16. #define SEM2 "/sem2"
  17. #define MAXTAM 2096
  18.  
  19. typedef struct {
  20. int valor;
  21. char aviso[80];
  22. } Mensaje;
  23.  
  24. int main(int argc, char** argv) {
  25.  
  26.  
  27. struct mq_attr attributes = {
  28. .mq_flags = 0,
  29. .mq_maxmsg = 10,
  30. .mq_curmsgs = 0,
  31. .mq_msgsize = sizeof (Mensaje)
  32. };
  33.  
  34. char nombreColaRec[80];
  35. char nombreColaEnv[80];
  36. sem_t *sem2;
  37.  
  38. if ((sem2 = sem_open(SEM2, O_EXCL)) == SEM_FAILED) {
  39. perror("sem_open");
  40. exit(EXIT_FAILURE);
  41. }
  42.  
  43.  
  44. strcpy(nombreColaRec, argv[1]);
  45. strcpy(nombreColaEnv, argv[2]);
  46.  
  47. Mensaje msg;
  48. int i = 0;
  49.  
  50.  
  51. mqd_t queue = mq_open(nombreColaRec, O_CREAT | O_RDONLY, S_IRUSR | S_IWUSR, &attributes);
  52. if (queue == (mqd_t) - 1) {
  53. fprintf(stderr, "Error opening the queue\n");
  54. return EXIT_FAILURE;
  55. }
  56.  
  57.  
  58. if (mq_receive(queue, (char *) &msg, sizeof (msg), NULL) == -1) {
  59. fprintf(stderr, "Error sending message\n");
  60. return EXIT_FAILURE;
  61. }
  62.  
  63. printf("Cadena a convertir: %s\n", msg.aviso);
  64. while (msg.aviso[i] != '\0') {
  65.  
  66. if (msg.aviso[i] != ' ') {
  67. msg.aviso[i]++;
  68. }
  69. i++;
  70. }
  71.  
  72. mqd_t queue2 = mq_open(nombreColaEnv, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR, &attributes);
  73. if (queue == (mqd_t) - 1) {
  74. fprintf(stderr, "Error opening the queue\n");
  75. return EXIT_FAILURE;
  76. }
  77.  
  78. if (mq_send(queue2, (char *) &msg, sizeof (msg), 1) == -1) {
  79. fprintf(stderr, "Error sending message\n");
  80. return EXIT_FAILURE;
  81. }
  82.  
  83. sem_post(sem2);
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement