Advertisement
Guest User

S

a guest
Jun 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. /*****************************************************************
  2. SPECIFICATION TO BE IMPLEMENTED:
  3. Implementare un programma che riceva in input tramite argv[] i pathname
  4. associati ad N file, con N maggiore o uguale ad 1. Per ognuno di questi
  5. file generi un processo che legga tutte le stringhe contenute in quel file
  6. e le scriva in un'area di memoria condivisa con il processo padre. Si
  7. supponga per semplicita' che lo spazio necessario a memorizzare le stringhe
  8. di ognuno di tali file non ecceda 4KB.
  9. Il processo padre dovra' attendere che tutti i figli abbiano scritto in
  10. memoria il file a loro associato, e successivamente dovra' entrare in pausa
  11. indefinita.
  12. D'altro canto, ogni figlio dopo aver scritto il contenuto del file nell'area
  13. di memoria condivisa con il padre entrera' in pausa indefinita.
  14. L'applicazione dovra' gestire il segnale SIGINT (o CTRL_C_EVENT nel caso
  15. WinAPI) in modo tale che quando il processo padre venga colpito da esso dovra'
  16. stampare a terminale il contenuto corrente di tutte le aree di memoria
  17. condivisa anche se queste non sono state completamente popolate dai processi
  18. figli.
  19.  
  20. *****************************************************************/
  21.  
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <signal.h>
  25. #include <pthread.h>
  26. #include <sys/types.h>
  27. #include <sys/ipc.h>
  28. #include <sys/sem.h>
  29. #include <sys/mman.h>
  30. #include <semaphore.h>
  31. #include <fcntl.h>
  32.  
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36.  
  37. #define PAGESIZE (4096)
  38.  
  39.  
  40. int N_FILES;
  41. int ds_sem;
  42.  
  43. char **buffers;
  44.  
  45. struct sembuf oper;
  46.  
  47.  
  48. void error_handler(char *message) {
  49.  
  50. fprintf(stderr, "%s\n", message);
  51. perror("The error was: ");
  52. exit(EXIT_FAILURE);
  53.  
  54. }
  55.  
  56. void read_proc(char *path, int id) {
  57.  
  58. FILE *file = fopen(path, "r");
  59. int ret;
  60.  
  61. if (file == NULL)
  62. error_handler("Opening file failed");
  63.  
  64. char *index = buffers[id];
  65.  
  66. while (fscanf(file, "%s", index) != EOF) {
  67. printf("Reading from %s: %s ...\n", path, index);
  68. index += strlen(index) + 1;
  69. }
  70.  
  71. //signal(S)
  72. oper.sem_num = 0;
  73. oper.sem_op = 1;
  74. oper.sem_flg = 0;
  75.  
  76. while (semop(ds_sem, &oper, 1) == -1)
  77. if (errno != EINTR)
  78. error_handler("Semaphore operation failed");
  79.  
  80.  
  81. return;
  82.  
  83. }
  84.  
  85. void printer(int signo) {
  86.  
  87. printf("\n\nReading from buffers ...\n");
  88.  
  89. for (int i = 0; i < N_FILES; ++i) {
  90.  
  91. printf("FILE %d\n", i + 1);
  92. char *temp = buffers[i];
  93.  
  94. while (strcmp(temp, "\0") != 0) {
  95. printf("%s\n",temp);
  96. temp += strlen(temp) + 1;
  97. }
  98. }
  99. }
  100.  
  101.  
  102. int main(int argc, char **argv) {
  103.  
  104. if (argc < 2) {
  105. fprintf(stderr, "\n\tusage: prog file_1 file_2 ... [file_N]\n\n");
  106. exit(EXIT_FAILURE);
  107. }
  108.  
  109. int ret;
  110. N_FILES = argc - 1;
  111.  
  112. sigset_t set;
  113. struct sigaction act;
  114.  
  115. memset((void *) &act, 0, sizeof(struct sigaction));
  116. memset((void *) &oper, 0, sizeof(struct sembuf));
  117.  
  118. ret = sigfillset(&set);
  119. if (ret == -1)
  120. error_handler("Signal initialization failed");
  121.  
  122. act.sa_mask = set;
  123. act.sa_handler = printer;
  124. act.sa_flags = 0;
  125.  
  126. ret = sigaction(SIGINT, &act, NULL);
  127. if (ret == -1)
  128. error_handler("Signal initialization failed");
  129.  
  130. ds_sem = semget(IPC_PRIVATE, 1, IPC_CREAT | 0660);
  131. if (ds_sem == -1)
  132. error_handler("Semaphore initialization failed");
  133.  
  134.  
  135. ret = semctl(ds_sem, 0, SETVAL, 0);
  136. if (ret == -1)
  137. error_handler("Semaphore setup failed");
  138.  
  139. buffers = (char **)malloc(N_FILES * sizeof(char *));
  140. if (buffers == NULL)
  141. error_handler("Malloc failed");
  142.  
  143. for (int i = 0; i < N_FILES; ++i) {
  144. buffers[i] = mmap(NULL, PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
  145.  
  146. if (buffers[i] == MAP_FAILED)
  147. error_handler("Memory map failed");
  148. }
  149.  
  150. pid_t pids[N_FILES];
  151.  
  152. for (int i = 0; i < N_FILES; ++i) {
  153. pids[i] = fork();
  154.  
  155. if (pids[i] == -1)
  156. error_handler("Fork failed");
  157.  
  158. if (pids[i] == 0) {
  159. signal(SIGINT, SIG_IGN);
  160.  
  161. read_proc(argv[i + 1], i);
  162.  
  163. while (1)
  164. pause();
  165.  
  166. }
  167. }
  168.  
  169. oper.sem_num = 0;
  170. oper.sem_op = - N_FILES;
  171. oper.sem_flg = 0;
  172.  
  173. while (semop(ds_sem, &oper, 1) == -1)
  174. if (errno != EINTR)
  175. error_handler("Semaphore operation failed");
  176.  
  177. while (1)
  178. pause();
  179.  
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement