Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. #include <sys/mman.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <wait.h>
  9. #include <string.h>
  10. #include <semaphore.h>
  11. #define BUFFER_SIZE 10
  12.  
  13. typedef struct {
  14. int data [BUFFER_SIZE];
  15. int head;
  16. int tail;
  17. } Buffer;
  18.  
  19. int main(void) {
  20. int i, data_size = sizeof (Buffer);
  21.  
  22. shm_unlink("/shmtest_pl4_ex13");
  23.  
  24. // Criar a zona de shared memory
  25. int fd = shm_open("/shmtest_pl4_ex13", O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
  26. ftruncate (fd, data_size);
  27. Buffer * shared_data = (Buffer *) mmap (NULL, data_size,
  28. PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  29.  
  30. // Criação dos semáforos. São criados 3 semáforos.
  31. // - O primeiro garante que apenas um dos processos está a aceder à zona de
  32. // memória partilhada, logo começa a 1. (Exclusão mútua)
  33. // - O segundo garante que o consumidor fica bloqueado quando não há mais nada
  34. // para ler, visto que só passa a ser maior que 0 quando o produtor o incrementa
  35. // - O terceiro garante que só é utilizado o espaço disponível para o buffer,
  36. // começando portanto com o valor do BUFFER_SIZE
  37. const char *array_nome_semaforos[3];
  38. array_nome_semaforos[0] = "sema_pl4_ex13_exclusao_mutua";
  39. array_nome_semaforos[1] = "sema_pl4_ex13_itens";
  40. array_nome_semaforos[2] = "sema_pl4_ex13_espaco_disponivel";
  41. sem_t * array_semaforos [3];
  42. int valor_inicial_semaforo [3] = {1, 0, BUFFER_SIZE};
  43.  
  44. // Delete dos semáforos
  45. for (i = 0; i < 3; i++) {
  46. sem_unlink (array_nome_semaforos[i]);
  47. }
  48.  
  49. for (i = 0; i < 3; i++) {
  50. if ((array_semaforos[i] = sem_open (array_nome_semaforos[i], O_CREAT | O_EXCL, 0644, valor_inicial_semaforo[i])) == SEM_FAILED) {
  51. perror("Error creating the semaphore.");
  52. exit(1);
  53. }
  54. }
  55.  
  56. // Inicialização de head e tail. Começam ambos a 0.
  57. shared_data->head = 0;
  58. shared_data->tail = 0;
  59.  
  60. pid_t p1, p2;
  61. int pids[2];
  62.  
  63. // Criação dos 2 filhos
  64. p1 = fork();
  65. if (p1 > 0) {
  66. pids[0] = p1;
  67. p2 = fork();
  68. pids[1] = p2;
  69. }
  70.  
  71. // array_semaforos[0]: exclusão mútua
  72. // array_semaforos[1]: itens
  73. // array_semaforos[2]: espaço disponível no buffer
  74. if (p1 > 0 && p2 > 0) { // 1 consumidor
  75. for (i = 0; i < 30; i++) {
  76. sem_wait (array_semaforos[1]);
  77. sem_wait (array_semaforos[0]);
  78. printf ("[PAI]\n");
  79. if (shared_data->head > shared_data->tail) {
  80. //printf ("[PAI] i: %d; Li o valor %d\n", i, shared_data->data[shared_data->tail]);
  81. shared_data->tail++;
  82. }
  83. if(shared_data->head == 10 && shared_data->tail == 10){
  84. shared_data->head = 0;
  85. shared_data->tail = 0;
  86. }
  87. sem_post (array_semaforos[0]);
  88. sem_post (array_semaforos[2]);
  89. }
  90. } else { // 2 Produtores
  91. int increasing_value = 1;
  92. for (i = 0; i < 15; i++) {
  93. sem_wait (array_semaforos[2]);
  94. sem_wait (array_semaforos[0]);
  95. printf ("[FILHO]\n");
  96. //printf ("[FILHO] Escrevi o valor %d\n", increasing_value);
  97. shared_data->data[shared_data->head] = increasing_value;
  98. shared_data->head++;
  99. increasing_value++;
  100. sem_post (array_semaforos[0]);
  101. sem_post (array_semaforos[1]);
  102. }
  103. munmap (shared_data, data_size);
  104. close (fd);
  105. exit (1);
  106. }
  107.  
  108. // Pai espera pelos processos filho só no fim para que executem todos
  109. // paralelamente
  110. for (i = 0; i < 2; i++) {
  111. waitpid (pids[i], NULL, 0);
  112. }
  113.  
  114. // Delete dos semáforos
  115. for (i = 0; i < 3; i++) {
  116. sem_unlink (array_nome_semaforos[i]);
  117. }
  118.  
  119. // Close e delete da shared memory
  120. munmap (shared_data, data_size);
  121. close (fd);
  122. shm_unlink("/shmtest_pl4_ex13");
  123.  
  124. printf("Fim do programa.\n");
  125. return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement