Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.89 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <pthread.h>
  4. #include <semaphore.h>
  5. #include <sys/fcntl.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/wait.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include <assert.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include <errno.h>
  17. #include <sys/msg.h>
  18. #include <signal.h>
  19.  
  20. #define DEBUG
  21. #define MAX 1024
  22. #define MAX2 50
  23. #define PIPE_NAME "input_pipe"
  24.  
  25. /****************************ESTRUTURAS********************/
  26.  
  27. typedef struct Voo_partida *lista_voo_partida;
  28. typedef struct Voo_chegada *lista_voo_chegada;
  29. typedef struct Voo_aux *lista_voos;
  30.  
  31.  
  32. typedef struct Voo{
  33. char nome[MAX2];
  34. int inicial;
  35. }voo;
  36.  
  37. typedef struct Voo_aux{
  38. char nome[MAX2];
  39. int inicial;
  40. lista_voos next;
  41. }voo_aux;
  42.  
  43. typedef struct Mensagem{
  44. long msgtyp;
  45. char nome[MAX2];
  46. int combustivel;
  47. int instante_partida;
  48. int eta;
  49. int init;
  50. int flag_prioridade;
  51. } mensagem;
  52.  
  53. typedef struct Callback_id{
  54. long msgtyp;
  55. int id;
  56. } callback_id;
  57.  
  58. typedef struct Voo_partida{
  59. voo dep;
  60. int descolagem;
  61. lista_voo_partida next;
  62. }voo_partida;
  63.  
  64. typedef struct Voo_chegada{
  65. voo arr;
  66. int eta;
  67. int combustivel;
  68. lista_voo_chegada next;
  69. }voo_chegada;
  70.  
  71. typedef struct Estatistica{
  72. int total_voos_criados;
  73. int total_voos_aterraram;
  74. time_t tempo_medio_espera_aterrar;
  75. int total_voos_descolaram;
  76. time_t tempo_medio_espera_descolar;
  77. int media_manobras_aterragem;
  78. int media_manobras_emergencia;
  79. int voos_redirecionados;
  80. int voos_rejeitados;
  81. }estatistica;
  82.  
  83. /*********************HEADER DE FUNCOES**********************/
  84.  
  85. void criar_processo_torre();
  86. char * horas();
  87. void escreve_log();
  88. void ler_config();
  89. void cria_semaforos();
  90. void cria_pipe();
  91. void cria_mq();
  92. void cria_shm_stats();
  93. void cria_shm_partida();
  94. void cria_shm_chegada();
  95. void *abre_pipe();
  96. void gestor();
  97. void *thread_manage();
  98. void *thread_msq();
  99. void *thread_worker(void* i);
  100. int condition();
  101. void imprime_lista_chegada(lista_voo_chegada head);
  102. lista_voo_partida cria_lista_partida();
  103. lista_voo_chegada cria_lista_chegada();
  104. lista_voos cria_lista_voos();
  105. void insere_lista_chegada(lista_voo_chegada head, char * n, int init, int eta, int fuel);
  106. void insere_fila_chegada(lista_voo_chegada head, char * n, int init, int eta, int fuel);
  107. void insere_lista_partida(lista_voo_partida head, char * n, int init, int desc);
  108. void insere_fila_partida(lista_voo_partida head, char * n, int init, int desc);
  109. void procura_lista_partida(lista_voo_partida lista, int chave, lista_voo_partida *ant, lista_voo_partida *atual);
  110. void procura_fila_partida(lista_voo_partida lista, int chave, lista_voo_partida *ant, lista_voo_partida *atual);
  111. void procura_lista_chegada(lista_voo_chegada lista, int chave, lista_voo_chegada *ant, lista_voo_chegada *atual);
  112. void procura_fila_chegada(lista_voo_chegada lista, int chave, lista_voo_chegada *ant, lista_voo_chegada *atual);
  113. void remove_lista_partida(lista_voo_partida partida, char *nome);
  114. void remove_lista_chegada(lista_voo_chegada chegada, char *nome);
  115. void imprime_lista_partida(lista_voo_partida teste);
  116. void insere_lista_voos(lista_voos head, char * n, int init);
  117. void procura_lista_voos(lista_voos lista, int chave, lista_voos *ant, lista_voos *atual);
  118. int hora_ut();
  119. int checkNome(lista_voos lista,char *nome);
  120. int checkLista(lista_voo_chegada chegadas, lista_voo_partida partidas,char *nome);
  121. double calcula_init(int init);
  122. void ms2ts( struct timespec *ts, unsigned long ms);
  123. void termina_programa(int signum);
  124. void remove_lista_voo(lista_voos voo, char *nome);
  125. /***********************VARIAVEIS GLOBAIS********************/
  126.  
  127. int ut_inicial; //variavel que guarda a hora inicial do sistema em segundos
  128. int ut_atual;
  129. pid_t pid_torre; //pid do processo da Torre de Controle
  130. FILE* flog; //ficheiro de log
  131. sem_t *log_mutex; //mutex do log
  132. int shm_stats_id,shm_partida_id, shm_chegad_id, mq_id,pipe_id; //variaveis de shm
  133. estatistica * stats;
  134. voo_chegada *shm_chegada;
  135. voo_partida *shm_partida;
  136.  
  137. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  138. pthread_mutex_t mutex_thread = PTHREAD_MUTEX_INITIALIZER;
  139.  
  140. int ut,dur_descolagem,dur_aterragem,int_descolagem,int_aterragem, holding_min, holding_max, max_partidas, max_chegadas; //variaveis de leitura da config
  141. pthread_t thread_voo[MAX2],thread_pipe, thread_manage_id,thread_mq_id;
  142.  
  143. lista_voo_chegada fila_espera_chegada;
  144. lista_voo_partida fila_espera_partida;
  145.  
  146.  
  147. lista_voo_chegada lista_chegada;
  148. lista_voo_partida lista_partida;
  149. lista_voos lista_voo_aux;
  150.  
  151. double time_to_wait;
  152. unsigned long value;
  153. struct timespec ts;
  154. int ids = 1;
  155.  
  156. /****************************FUNCOES*****************************/
  157.  
  158. //funcao para debug print
  159. void debug_print(char * aux){
  160. #ifdef DEBUG
  161. printf("[DEBUG] %s\n",aux);
  162. #endif
  163. }
  164.  
  165.  
  166. void criar_processo_torre(){
  167. debug_print("Processo Torre criado");
  168. int i;
  169.  
  170. fila_espera_partida = cria_lista_partida();
  171. fila_espera_chegada = cria_lista_chegada();
  172. printf("TORRE ESPEROU\n");
  173. mensagem msg;
  174. callback_id index_number;
  175.  
  176. // fica à espera de mensagens das threads
  177. while(1){
  178. printf("OLE OLE MISTER\n\n");
  179. if(msgrcv(mq_id, &msg, sizeof(msg)-sizeof(long), -4, 0) != -1){
  180.  
  181. // nº negativo funciona por prioridades -> 1º valor <= msgtype
  182. printf("OLA\n");
  183. if(msg.msgtyp == 3){
  184. printf("--------TORRE DE CONTROLO--------\n\n");
  185.  
  186. printf("NAME: %s \n",msg.nome);
  187. printf("TAKEOFF: %d \n",msg.instante_partida);
  188. printf("----------------\n");
  189. i = 0;
  190. while(strcmp(shm_partida[i].dep.nome,"\0") != 0){
  191. i++;
  192. }
  193.  
  194. printf("%d\n", i);
  195.  
  196. strcpy(shm_partida[i].dep.nome,msg.nome);
  197. shm_partida[i].descolagem = msg.instante_partida;
  198. shm_partida[i].dep.inicial = msg.init;
  199.  
  200. index_number.msgtyp = 4;
  201. index_number.id = i;
  202.  
  203. insere_fila_partida(fila_espera_partida,msg.nome,msg.init,msg.instante_partida);
  204. imprime_lista_partida(fila_espera_partida);
  205. msgsnd(mq_id, &index_number, sizeof(index_number)-sizeof(long), 0);
  206. //signal(SIGINT,termina_programa);
  207.  
  208. //falta o id
  209. }
  210. else if(msg.msgtyp == 4){
  211. i=0;
  212. printf("--------TORRE DE CONTROLO--------\n\n");
  213.  
  214.  
  215. //adiciona o novo elemento à shm memory e envia o id para a thread
  216. while (strcmp(shm_chegada[i].arr.nome,"\0") != 0)
  217. {
  218. i++;
  219. }
  220. strcpy(shm_chegada[i].arr.nome,msg.nome);
  221. shm_chegada[i].arr.inicial = msg.init;
  222. shm_chegada[i].eta = msg.eta;
  223. shm_chegada[i].combustivel = msg.combustivel;
  224.  
  225. index_number.msgtyp = 1;
  226. index_number.id = i;
  227. insere_fila_chegada(fila_espera_chegada,msg.nome,msg.init, msg.eta,msg.combustivel);
  228. //falta o id
  229. imprime_lista_chegada(fila_espera_chegada);
  230. msgsnd(mq_id, &index_number, sizeof(index_number)-sizeof(long), 0);
  231. //adiciona o novo elemento a lista de espera e ordena-o
  232. //signal(SIGINT,termina_programa);
  233.  
  234. }
  235. else if(msg.msgtyp <= 2){
  236. i=0;
  237. printf("--------TORRE DE CONTROLO--------\n\n");
  238.  
  239. printf("---PRIORITY FLIGHT---\n\n");
  240.  
  241. //adiciona o novo elemento à shm memory e envia o id para a thread
  242. while (strcmp(shm_chegada[i].arr.nome,"\0") != 0)
  243. {
  244. i++;
  245. }
  246. strcpy(shm_chegada[i].arr.nome,msg.nome);
  247. shm_chegada[i].arr.inicial = msg.init;
  248. shm_chegada[i].eta = msg.eta;
  249. shm_chegada[i].combustivel = msg.combustivel;
  250.  
  251. index_number.msgtyp = 1;
  252. index_number.id = i;
  253. insere_fila_chegada(fila_espera_chegada,msg.nome,msg.init, msg.eta,msg.combustivel);
  254. //falta o id
  255. imprime_lista_chegada(fila_espera_chegada);
  256. msgsnd(mq_id, &index_number, sizeof(index_number)-sizeof(long), 0);
  257. //adiciona o novo elemento a lista de espera e ordena-o
  258. //signal(SIGINT,termina_programa);
  259.  
  260. }
  261. }
  262. else{
  263. break;
  264. }
  265. }
  266. }
  267. /*if(pthread_create(&thread_mq_id,NULL,thread_msq,NULL)!=0){
  268. perror("ERROR creating the thread pipe!\n");
  269. }
  270. printf("TORRE PASSOU\n");
  271. */
  272. //inicializa as filas de espera a
  273.  
  274.  
  275. void *thread_msq(){
  276.  
  277. int i;
  278. while(1){
  279. // fica à espera de mensagens das threads
  280. mensagem msg;
  281. callback_id index_number;
  282. printf("OLE OLE MISTER\n\n");
  283. msgrcv(mq_id, &msg, sizeof(msg)-sizeof(long), 0, 0);
  284.  
  285. // nº negativo funciona por prioridades -> 1º valor <= msgtype
  286. printf("OLA\n");
  287. if(msg.msgtyp == 3){
  288. printf("--------TORRE DE CONTROLO--------\n\n");
  289.  
  290. printf("NAME: %s \n",msg.nome);
  291. printf("TAKEOFF: %d \n",msg.instante_partida);
  292. printf("----------------\n");
  293. i = 0;
  294. while(strcmp(shm_partida[i].dep.nome,"\0") != 0){
  295. i++;
  296. }
  297.  
  298. printf("%d\n", i);
  299.  
  300. strcpy(shm_partida[i].dep.nome,msg.nome);
  301. shm_partida[i].descolagem = msg.instante_partida;
  302. shm_partida[i].dep.inicial = msg.init;
  303.  
  304. index_number.msgtyp = 4;
  305. index_number.id = i;
  306.  
  307. insere_fila_partida(fila_espera_partida,msg.nome,msg.init,msg.instante_partida);
  308. imprime_lista_partida(fila_espera_partida);
  309. msgsnd(mq_id, &index_number, sizeof(index_number)-sizeof(long), 0);
  310. //signal(SIGINT,termina_programa);
  311.  
  312. //falta o id
  313. }
  314. else if(msg.msgtyp == 4){
  315. i=0;
  316. printf("--------TORRE DE CONTROLO--------\n\n");
  317.  
  318.  
  319. //adiciona o novo elemento à shm memory e envia o id para a thread
  320. while (strcmp(shm_chegada[i].arr.nome,"\0") != 0)
  321. {
  322. i++;
  323. }
  324. strcpy(shm_chegada[i].arr.nome,msg.nome);
  325. shm_chegada[i].arr.inicial = msg.init;
  326. shm_chegada[i].eta = msg.eta;
  327. shm_chegada[i].combustivel = msg.combustivel;
  328.  
  329. index_number.msgtyp = 1;
  330. index_number.id = i;
  331. insere_fila_chegada(fila_espera_chegada,msg.nome,msg.init, msg.eta,msg.combustivel);
  332. //falta o id
  333. imprime_lista_chegada(fila_espera_chegada);
  334. msgsnd(mq_id, &index_number, sizeof(index_number)-sizeof(long), 0);
  335. //adiciona o novo elemento a lista de espera e ordena-o
  336. //signal(SIGINT,termina_programa);
  337.  
  338. }
  339. else if(msg.msgtyp <= 2){
  340. i=0;
  341. printf("--------TORRE DE CONTROLO--------\n\n");
  342.  
  343. printf("---PRIORITY FLIGHT---\n\n");
  344.  
  345. //adiciona o novo elemento à shm memory e envia o id para a thread
  346. while (strcmp(shm_chegada[i].arr.nome,"\0") != 0)
  347. {
  348. i++;
  349. }
  350. strcpy(shm_chegada[i].arr.nome,msg.nome);
  351. shm_chegada[i].arr.inicial = msg.init;
  352. shm_chegada[i].eta = msg.eta;
  353. shm_chegada[i].combustivel = msg.combustivel;
  354.  
  355. index_number.msgtyp = 1;
  356. index_number.id = i;
  357. insere_fila_chegada(fila_espera_chegada,msg.nome,msg.init, msg.eta,msg.combustivel);
  358. //falta o id
  359. imprime_lista_chegada(fila_espera_chegada);
  360. msgsnd(mq_id, &index_number, sizeof(index_number)-sizeof(long), 0);
  361. //adiciona o novo elemento a lista de espera e ordena-o
  362. //signal(SIGINT,termina_programa);
  363.  
  364. }
  365. }
  366.  
  367. }
  368. void termina_programa(int signum){
  369.  
  370. signal(SIGINT, termina_programa);
  371. signal(SIGUSR1,SIG_IGN);
  372.  
  373. int i;
  374. i = 0;
  375. kill(pid_torre,0); //vai esperar pelo Torre de controlo acabar
  376.  
  377. escreve_log("Torre de Controlo eliminada\n");
  378. while (i < max_chegadas){ //Vai enviar sinal aos armazens para morrerem
  379. shmdt(shm_chegada);
  380.  
  381. }
  382. shmctl(shm_chegad_id,IPC_RMID,NULL);
  383.  
  384. i = 0;
  385. while (i < max_partidas){ //Vai enviar sinal aos armazens para morrerem
  386. shmdt(shm_partida);
  387.  
  388. }
  389. shmctl(shm_partida_id,IPC_RMID,NULL);
  390. lista_voo_chegada temp_c;
  391. lista_voo_partida temp_p;
  392. lista_voos temp_a;
  393. while(lista_chegada->next!=NULL){
  394. temp_c=lista_chegada;
  395. lista_chegada=lista_chegada->next;
  396. free(temp_c);
  397. }
  398. while(lista_partida->next!=NULL){
  399. temp_p=lista_partida;
  400. lista_partida=lista_partida->next;
  401. free(temp_p);
  402. }
  403.  
  404. while(lista_voo_aux->next!=NULL){
  405. temp_a=lista_voo_aux;
  406. lista_voo_aux=lista_voo_aux->next;
  407. free(temp_a);
  408. }
  409.  
  410. msgctl(mq_id, IPC_RMID, 0);
  411. escreve_log("Message queue eliminada\n");
  412. pthread_join(thread_voo,NULL);
  413. pthread_join(thread_pipe,NULL);
  414. close(pipe_id);
  415. unlink(PIPE_NAME);
  416.  
  417. pthread_mutex_destroy(&mutex_thread);
  418. pthread_cond_destroy(&cond);
  419.  
  420. exit(0);
  421.  
  422. }
  423.  
  424. //funcao que calcula hora do sistema e returna string no formato 19:35:01
  425. char * horas(){
  426. char * aux;
  427. aux = malloc(sizeof(char*));
  428. time_t atual;
  429. time(&atual);
  430. struct tm *hora_local = localtime(&atual);
  431. sprintf(aux, "%02d:%02d:%02d", hora_local->tm_hour, hora_local->tm_min, hora_local->tm_sec);
  432. return aux;
  433. }
  434.  
  435. //funcao que guarda hora de inicio do programa, em segundos
  436. void hora_inicio(){
  437. int hora_incial,minutos_inicial,segundos_inicial;
  438. time_t atual;
  439. time(&atual);
  440. struct tm *hora_local = localtime(&atual);
  441. hora_incial= hora_local->tm_hour * 60 * 60;
  442. minutos_inicial= hora_local->tm_min * 60;
  443. segundos_inicial= hora_local->tm_sec;
  444. ut_inicial = hora_incial + minutos_inicial + segundos_inicial;
  445. }
  446.  
  447. //funcao que devolve hora atual do programa em ut
  448. int hora_ut(){
  449. double aux,aux2;
  450. time_t atual;
  451. time(&atual);
  452. struct tm *hora_local = localtime(&atual);
  453. aux = (((hora_local->tm_hour * 60 * 60) + (hora_local->tm_min * 60) + hora_local->tm_sec)-ut_inicial);
  454. aux2 = (int) aux*ut/1000;
  455. return(aux2);
  456. }
  457.  
  458. double calcula_init(int init){
  459. double final,aux;
  460. int var;
  461. var = init;
  462. aux = (double) ut;
  463. var = init-hora_ut();
  464. final = (aux/1000) * var;
  465. return final;
  466. }
  467.  
  468.  
  469.  
  470. void imprime_lista_partida(lista_voo_partida head){
  471. lista_voo_partida atual = head->next;
  472. printf("LISTA DE ESPERA - VOOS PARTIDA\n");
  473. while (atual != NULL) {
  474. printf("----------------\n");
  475. printf("NOME: %s \n", atual->dep.nome);
  476. printf("Takeoff : %d\n ", atual->descolagem);
  477. printf("----------------\n");
  478. atual = atual->next;
  479.  
  480. }
  481. }
  482.  
  483. void imprime_lista_voos(lista_voos head){
  484. lista_voos atual = head->next;
  485. printf("LISTA DE VOOS\n");
  486. while (atual != NULL) {
  487. printf("----------------\n");
  488. printf("NAME: %s \n",atual->nome);
  489. printf("INIT: %d \n",atual->inicial);
  490. printf("----------------\n");
  491. printf("\n\n");
  492. atual = atual->next;
  493. }
  494. }
  495.  
  496.  
  497. void imprime_lista_chegada(lista_voo_chegada head){
  498. lista_voo_chegada atual = head->next;
  499. printf("LISTA DE ESPERA - VOOS CHEGADA\n");
  500. while (atual != NULL) {
  501. printf("----------------\n");
  502. printf("NOME:%s\n", atual->arr.nome);
  503. printf("INIT :%d\n", atual->arr.inicial);
  504. printf("FUEL: %d \n",atual->combustivel);
  505. printf("ETA: %d\n",atual->eta);
  506. printf("----------------\n");
  507. atual = atual->next;
  508. }
  509. }
  510.  
  511.  
  512. lista_voos cria_lista_voos(){
  513. lista_voos head = NULL;
  514. head = (lista_voos)malloc(sizeof(voo_aux));
  515. if(head != NULL){
  516. strcpy(head->nome," ");
  517. head->inicial = 0;
  518. head->next = NULL;
  519. }
  520. return(head);
  521. }
  522.  
  523.  
  524. lista_voo_partida cria_lista_partida(){
  525. lista_voo_partida head = NULL;
  526. head = (lista_voo_partida) malloc(sizeof(voo_partida));
  527. if(head != NULL){
  528. strcpy(head->dep.nome," ");
  529. head->dep.inicial = 0;
  530. head->descolagem = 0;
  531. head->next = NULL;
  532. }
  533. return(head);
  534. }
  535.  
  536. lista_voo_chegada cria_lista_chegada(){
  537. lista_voo_chegada head = NULL;
  538. head = (lista_voo_chegada) malloc(sizeof(voo_chegada));
  539. if(head != NULL){
  540. strcpy(head->arr.nome," ");
  541. head->arr.inicial = 0;
  542. head->eta = 0;
  543. head->combustivel = 0;
  544. head->next = NULL;
  545. }
  546. return (head);
  547. }
  548.  
  549.  
  550. void insere_lista_voos(lista_voos head, char * n, int init){
  551. lista_voos atual;
  552. lista_voos ant, inutil;
  553. atual = (lista_voos) malloc (sizeof (voo_aux));
  554. if (atual != NULL)
  555. {
  556. strcpy(atual->nome,n);
  557. atual->inicial = init;
  558.  
  559. procura_lista_voos(head, init, &ant, &inutil);
  560. atual->next = ant->next;
  561. ant->next = atual;
  562. }
  563. }
  564.  
  565. void insere_lista_partida(lista_voo_partida head, char * n, int init, int desc){
  566. lista_voo_partida atual;
  567. lista_voo_partida ant, inutil;
  568. atual = (lista_voo_partida) malloc (sizeof (voo_partida));
  569. if (atual != NULL)
  570. {
  571. strcpy(atual->dep.nome,n);
  572. atual->dep.inicial = init;
  573. atual->descolagem = desc;
  574.  
  575. procura_lista_partida(head, init, &ant, &inutil);
  576. atual->next = ant->next;
  577. ant->next = atual;
  578. }
  579. }
  580.  
  581.  
  582.  
  583. void insere_fila_partida(lista_voo_partida head, char * n, int init, int desc){
  584. lista_voo_partida atual;
  585. lista_voo_partida ant, inutil;
  586. atual = (lista_voo_partida) malloc (sizeof (voo_partida));
  587. if (atual != NULL)
  588. {
  589. strcpy(atual->dep.nome,n);
  590. atual->dep.inicial = init;
  591. atual->descolagem = desc;
  592.  
  593. procura_fila_partida(head, desc, &ant, &inutil);
  594. atual->next = ant->next;
  595. ant->next = atual;
  596. }
  597. }
  598.  
  599.  
  600. int checkNome(lista_voos lista,char *nome){
  601. lista_voos atual;
  602. atual = lista->next;
  603. if(atual == NULL){
  604. return 0;
  605. }
  606. else{
  607. while(atual->next != NULL){
  608. if(strcmp(atual->nome,nome) == 0){
  609. return 1;
  610. }
  611. atual = atual->next;
  612. }
  613. return 0;
  614. }
  615. }
  616.  
  617.  
  618. void procura_lista_voos(lista_voos lista, int chave, lista_voos *ant, lista_voos *atual)
  619. {
  620. *ant = lista;
  621. *atual = lista->next;
  622. while ((*atual) != NULL &&( ((*atual)->inicial) < chave))
  623. {
  624. *ant = *atual;
  625. *atual = (*atual) -> next;
  626. }
  627. }
  628.  
  629.  
  630. void procura_lista_partida(lista_voo_partida lista, int chave, lista_voo_partida *ant, lista_voo_partida *atual)
  631. {
  632. *ant = lista;
  633. *atual = lista->next;
  634. while ((*atual) != NULL &&( ((*atual)->dep.inicial) < chave))
  635. {
  636. *ant = *atual;
  637. *atual = (*atual) -> next;
  638. }
  639. }
  640.  
  641. void procura_fila_partida(lista_voo_partida lista, int chave, lista_voo_partida *ant, lista_voo_partida *atual)
  642. {
  643. *ant = lista;
  644. *atual = lista->next;
  645. while ((*atual) != NULL &&( ((*atual)->descolagem) < chave))
  646. {
  647. *ant = *atual;
  648. *atual = (*atual) -> next;
  649. }
  650. }
  651.  
  652.  
  653. void insere_lista_chegada(lista_voo_chegada head, char * n, int init, int eta, int fuel){
  654. lista_voo_chegada atual;
  655. lista_voo_chegada ant, inutil;
  656. atual = (lista_voo_chegada) malloc (sizeof (voo_chegada));
  657. if (atual != NULL)
  658. {
  659. strcpy(atual->arr.nome,n);
  660. atual->arr.inicial = init;
  661. atual->eta = eta;
  662. atual->combustivel = fuel;
  663. }
  664.  
  665. procura_lista_chegada(head, init, &ant, &inutil);
  666. atual->next = ant->next;
  667. ant->next = atual;
  668. }
  669.  
  670.  
  671. void insere_fila_chegada(lista_voo_chegada head, char * n, int init, int eta, int fuel){
  672. lista_voo_chegada atual;
  673. lista_voo_chegada ant, inutil;
  674. atual = (lista_voo_chegada) malloc (sizeof (voo_chegada));
  675. if (atual != NULL)
  676. {
  677. strcpy(atual->arr.nome,n);
  678. atual->arr.inicial = init;
  679. atual->eta = eta;
  680. atual->combustivel = fuel;
  681. }
  682.  
  683. procura_fila_chegada(head, eta, &ant, &inutil);
  684. atual->next = ant->next;
  685. ant->next = atual;
  686. }
  687.  
  688. void procura_fila_chegada(lista_voo_chegada lista, int chave, lista_voo_chegada *ant, lista_voo_chegada *atual)
  689. {
  690. *ant = lista;
  691. *atual = lista->next;
  692. while ((*atual) != NULL && ((*atual)->eta) < chave)
  693. {
  694. *ant = *atual;
  695. *atual = (*atual) -> next;
  696. }
  697. }
  698.  
  699.  
  700.  
  701. void procura_lista_chegada(lista_voo_chegada lista, int chave, lista_voo_chegada *ant, lista_voo_chegada *atual)
  702. {
  703. *ant = lista;
  704. *atual = lista->next;
  705. while ((*atual) != NULL &&( ((*atual)->arr.inicial) < chave))
  706. {
  707. *ant = *atual;
  708. *atual = (*atual) -> next;
  709. }
  710. }
  711.  
  712.  
  713. //funcao de escrita no log
  714. void escreve_log(char *aux){
  715. sem_wait(log_mutex);
  716. fprintf(flog,"%s %s\n",horas(),aux);
  717. sem_post(log_mutex);
  718. fflush(0);
  719. }
  720.  
  721. //Funcao de leitura da config inicial do programa
  722. void ler_config(){
  723. FILE * fconfig;
  724. int linha=0;
  725. char buffer1[MAX2],buffer2[MAX2];
  726.  
  727. if ((fconfig=fopen("config.txt", "r")) == NULL){
  728. perror("Erro ao abrir o ficheiro config\n");
  729. exit(1);
  730. }else{
  731. while (linha!=6){
  732. linha++;
  733. if(linha==1){
  734. fscanf(fconfig, "%s", buffer1);
  735. ut = atoi(buffer1);
  736. }else if(linha==2){
  737. fscanf(fconfig, "%[^,],%s", buffer1,buffer2);
  738. dur_descolagem = atoi(buffer1);
  739. int_descolagem = atoi(buffer2);
  740. }else if(linha==3){
  741. fscanf(fconfig, "%[^,],%s", buffer1,buffer2);
  742. dur_aterragem = atoi(buffer1);
  743. int_aterragem = atoi(buffer2);
  744. }else if(linha==4){
  745. fscanf(fconfig, "%[^,],%s", buffer1,buffer2);
  746. holding_min = atoi(buffer1);
  747. holding_max = atoi(buffer2);
  748. }else if(linha==5){
  749. fscanf(fconfig, "%s", buffer1);
  750. max_partidas = atoi(buffer1);
  751. }else if(linha==6){
  752. fscanf(fconfig, "%s", buffer1);
  753. max_chegadas = atoi(buffer1);
  754. }
  755. }
  756. fclose(fconfig);
  757. debug_print("Ficheiro cfg lido");
  758. }
  759. }
  760.  
  761. //------------------Shared Memory Estatistica------------------------
  762. void cria_shm_stats(){
  763. if((shm_stats_id = shmget(IPC_PRIVATE,(sizeof(estatistica)),IPC_CREAT | 0766)) < 0){
  764. perror("Erro ao criar a memoria partilhada para as estatisticas ( IPC_CREAT)");
  765. }else{
  766. if(( stats = (estatistica *) shmat(shm_stats_id,NULL,0)) == (estatistica *)-1){
  767. perror("Erro ao criar a memoria partilhada para as estatisticas ( IPC_CREAT)");
  768. }
  769. else{
  770. stats->total_voos_criados = 0;
  771. stats->media_manobras_aterragem = 0;
  772. stats->total_voos_descolaram = 0;
  773. stats->total_voos_aterraram = 0;
  774. stats->media_manobras_emergencia = 0;
  775. stats->voos_redirecionados = 0;
  776. stats->voos_rejeitados = 0;
  777. stats->tempo_medio_espera_aterrar = 0;
  778. stats->tempo_medio_espera_descolar = 0;
  779. debug_print("Memoria partilhada Estatistica criada");
  780. }
  781. }
  782. }
  783.  
  784. //------------------Shared Memory Partidas------------------------
  785.  
  786. void cria_shm_partida(){
  787. if((shm_partida_id = shmget(IPC_PRIVATE,sizeof(voo_partida) * max_partidas,IPC_CREAT | 0766)) <0){
  788. perror("Erro a criar memoria partilhada para os voos de partida ");
  789. }
  790. if (( shm_partida = (voo_partida *) shmat(shm_partida_id,NULL,0)) == (voo_partida *)-1){
  791. perror("Erro a criar memoria partilhada para os voos de partida ");
  792. }
  793. else{
  794. debug_print("Memoria partilhada para os voos de partida criada com sucesso");
  795. int i;
  796. for(i = 0; i<max_partidas; i++){
  797.  
  798. strcpy(shm_partida[i].dep.nome,"\0");
  799. shm_partida[i].dep.inicial = 0;
  800. shm_partida[i].descolagem = 0;
  801. }
  802. }
  803.  
  804. }
  805. //------------------Shared Memory chegadas------------------------
  806. void cria_shm_chegada(){
  807. if((shm_chegad_id = shmget(IPC_PRIVATE,sizeof(voo_chegada) * max_chegadas,IPC_CREAT | 0766)) <0){
  808. perror("Erro a criar memoria partilhada para os voos de chegada ");
  809. }
  810. if (( shm_chegada = (voo_chegada *) shmat(shm_chegad_id,NULL,0)) == (voo_chegada *)-1){
  811. perror("Erro a criar memoria partilhada para os voos de chegada ");
  812. }
  813. else{
  814. debug_print("Memoria partilhada para os voos de chegada criada com sucesso");
  815. int i;
  816. for(i = 0; i<max_chegadas; i++){
  817.  
  818. strcpy(shm_chegada[i].arr.nome,"\0");
  819. shm_chegada[i].arr.inicial = 0;
  820. shm_chegada[i].combustivel = 0;
  821. shm_chegada[i].eta = 0;
  822. }
  823. }
  824. }
  825.  
  826. //funcao que cria pipe
  827. void cria_pipe(){
  828. unlink(PIPE_NAME);
  829. if ((mkfifo(PIPE_NAME, O_CREAT|O_EXCL|0600)<0) && (errno!= EEXIST)){ // Cria o pipe
  830. perror("Erro ao inicializar PIPE");
  831. }else{
  832. if ((pipe_id=open(PIPE_NAME, O_RDWR)) < 0){
  833. perror("Impossivel abrir Pipe para leitura");
  834. exit(0);
  835. }else{
  836. debug_print("Named Pipe criado");
  837. }
  838. }
  839. }
  840.  
  841. void *abre_pipe(){
  842. char aux[MAX],*buffer1;
  843. char auxNome[MAX2];
  844. int a,b,c;
  845. char original[MAX],erro[MAX],correto[MAX];
  846.  
  847. while (1) {
  848.  
  849. read(pipe_id, &aux, sizeof(char[MAX]));
  850. strcpy(original,aux);
  851. sprintf(correto,"%s%s","NEW COMMAND => ",original);
  852. sprintf(erro,"%s%s","WRONG COMMAND => ",original);
  853. buffer1 = strtok(aux," ");
  854.  
  855. if(strcmp(buffer1,"ARRIVAL") == 0){
  856.  
  857. //echo ARRIVAL TP437 init: 100 eta: 100 fuel: 1000 >> "input_pipe"
  858. //guarda nome
  859. buffer1 = strtok(NULL, " ");
  860. strcpy(auxNome, buffer1);
  861. //init:
  862. buffer1 = strtok(NULL, " ");
  863. if(strcmp(buffer1,"init:")!=0){
  864. escreve_log(erro);
  865. continue;
  866. }
  867. //init: 100
  868. buffer1 = strtok(NULL, " ");
  869. a = atoi(buffer1);
  870. if(a==0){
  871. escreve_log(erro);
  872. continue;
  873. }
  874. if(a<hora_ut()){
  875. escreve_log(erro);
  876. continue;
  877. }
  878. //eta:
  879. buffer1 = strtok(NULL, " ");
  880. if(strcmp(buffer1,"eta:")!=0){
  881. escreve_log(erro);
  882. continue;
  883. }
  884. //eta: 120
  885. buffer1 = strtok(NULL, " ");
  886. b = atoi(buffer1);
  887. if(b==0){
  888. escreve_log(erro);
  889. continue;
  890. }
  891. if(b<hora_ut() || b<a){
  892. escreve_log(erro);
  893. continue;
  894. }
  895. //fuel
  896. buffer1 = strtok(NULL, " ");
  897. if(strcmp(buffer1,"fuel:")!=0){
  898. escreve_log(erro);
  899. continue;
  900. }
  901. //fuel: 1000
  902. buffer1 = strtok(NULL, " ");
  903. c = atoi(buffer1);
  904. if(c==0){
  905. escreve_log(erro);
  906. continue;
  907. }
  908. if(c<b){
  909. escreve_log(erro);
  910. continue;
  911. }
  912. //pthread_mutex_unlock(&mutex_thread);
  913. pthread_mutex_lock(&mutex_thread);
  914. if((checkNome(lista_voo_aux,auxNome)== 0)){
  915. printf("O voo %s não existe no sistema, pode prosseguir\n",auxNome);
  916. insere_lista_chegada(lista_chegada,auxNome,a,b,c);
  917. insere_lista_voos(lista_voo_aux,auxNome,a);
  918. pthread_cond_signal(&cond);
  919. }
  920. else{
  921. printf("O voo %s já se encontra no sistema, acesso negado\n",auxNome);
  922. }
  923. pthread_mutex_unlock(&mutex_thread);
  924. escreve_log(correto);
  925.  
  926. }else if(strcmp(buffer1,"DEPARTURE") == 0 ){
  927. int i;
  928. //nome
  929. buffer1 = strtok(NULL, " ");
  930. strcpy(auxNome, buffer1);
  931. //init:
  932. buffer1 = strtok(NULL, " ");
  933. if(strcmp(buffer1,"init:")!=0){
  934. escreve_log(erro);
  935. continue;
  936. }
  937. //init:100
  938. buffer1 = strtok(NULL, " ");
  939. a = atoi(buffer1);
  940. if(a==0){
  941. escreve_log(erro);
  942. continue;
  943. }
  944. if(a<hora_ut()){
  945. escreve_log(erro);
  946. continue;
  947. }
  948. //takeoff:
  949. buffer1 = strtok(NULL, " ");
  950. if(strcmp(buffer1,"takeoff:")!=0){
  951. escreve_log(erro);
  952. continue;
  953. }
  954. //takeoff: 100
  955. buffer1 = strtok(NULL, " ");
  956. b = atoi(buffer1);
  957. if(b==0){
  958. escreve_log(erro);
  959. continue;
  960. }
  961. if(b<hora_ut() || b<a){
  962. escreve_log(erro);
  963. continue;
  964. }
  965. i = 0;
  966. pthread_mutex_unlock(&mutex_thread);
  967. pthread_mutex_lock(&mutex_thread);
  968. if((checkNome(lista_voo_aux,auxNome)== 0)){
  969.  
  970. printf("O voo %s não existe no sistema pode prosseguir\n",auxNome);
  971. //se o i = max_chegadas diz logo que nao dá e sai -> FAZER
  972. insere_lista_partida(lista_partida,auxNome,a,b);
  973. insere_lista_voos(lista_voo_aux,auxNome,a);
  974. pthread_mutex_unlock(&mutex_thread);
  975. pthread_cond_signal(&cond);
  976. escreve_log(correto);
  977.  
  978. }
  979. else{
  980. printf("O voo %s já se encontra no sistema, acesso negado\n",auxNome);
  981. pthread_mutex_unlock(&mutex_thread);
  982. }
  983.  
  984. }else{
  985. escreve_log(erro);
  986. }
  987. }
  988. }
  989.  
  990. //DEPARTURE TP440 init: 0 takeoff: 100 >> "input_pipe"
  991. //echo ARRIVAL TP437 init: 100 eta: 100 fuel: 1000 >> "input_pipe"
  992.  
  993. //mutex para o ficheiro de escrita no log
  994. void cria_semaforos(){
  995. sem_unlink("LOG_MUTEX");
  996. if((log_mutex = sem_open("LOG_MUTEX",O_CREAT|O_EXCL,0766,1))==SEM_FAILED){
  997. perror("Impossivel incializar o Semaforo para o Log File\n");
  998. sem_unlink("LOG_MUTEX");
  999. exit(0);
  1000. }
  1001. else{
  1002. debug_print("Semaforos criados");
  1003. }
  1004. }
  1005.  
  1006. //--------------Message Queue------------------
  1007. void cria_mq(){
  1008. mq_id = msgget(IPC_PRIVATE, IPC_CREAT|0777);
  1009. if (mq_id < 0)
  1010. {
  1011. perror("Erro ao inicializar a message queue");
  1012. }
  1013. else{
  1014. debug_print("Message queue criada");
  1015. }
  1016. }
  1017.  
  1018. //thread voo
  1019. void *thread_worker(void* i){
  1020.  
  1021. int id;
  1022. mensagem msg;
  1023. callback_id call;
  1024.  
  1025. int flag;
  1026. flag = *((int*) i);
  1027. //vai buscar o ultimo elemento da lista de voos para enviar pela mq a info
  1028.  
  1029. //procura na lista de voos de partida
  1030.  
  1031.  
  1032. debug_print("Thread Voo Criada");
  1033.  
  1034.  
  1035. if(flag == 2){
  1036. pthread_mutex_unlock(&mutex_thread);
  1037. pthread_mutex_lock(&mutex_thread);
  1038. strcpy(msg.nome,lista_partida->next->dep.nome);
  1039. msg.eta = 0;
  1040. msg.combustivel = 0;
  1041. msg.flag_prioridade = 0;
  1042. msg.msgtyp = 3;
  1043. msg.instante_partida = lista_partida->next->descolagem;
  1044. msg.init = lista_partida->next->dep.inicial;
  1045. remove_lista_voo(lista_voo_aux,lista_partida->next->dep.nome);
  1046. remove_lista_partida(lista_partida,lista_partida->next->dep.nome);
  1047. msgsnd(mq_id, &msg, sizeof(msg)-sizeof(long), 0);
  1048. while(1){
  1049. msgrcv(mq_id, &call, sizeof(call)-sizeof(long), 0, 0);
  1050. id = call.id;
  1051. printf("ID- %d\n", id);
  1052. if(id>= 0 && id <=max_chegadas){
  1053. break;
  1054. }
  1055. }
  1056. pthread_mutex_unlock(&mutex_thread);
  1057.  
  1058. } //procura na lista de voos de chegada
  1059. else if(flag == 1){
  1060. if(lista_chegada->next->combustivel <= 4 * ut + lista_chegada->next->eta){
  1061. msg.msgtyp = 1;
  1062. printf("PRIORITY\n");
  1063.  
  1064. strcpy(msg.nome,lista_chegada->next->arr.nome);
  1065. msg.eta = lista_chegada->next->eta;
  1066. msg.combustivel = lista_chegada->next->combustivel;
  1067. msg.init = lista_chegada->next->arr.inicial;
  1068. msg.flag_prioridade = 0;
  1069. msg.instante_partida = 0;
  1070. remove_lista_voo(lista_voo_aux,lista_chegada->next->arr.nome);
  1071. remove_lista_chegada(lista_chegada,lista_chegada->next->arr.nome);
  1072. msgsnd(mq_id, &msg, sizeof(msg)-sizeof(long), 0);
  1073.  
  1074. while(1){
  1075. msgrcv(mq_id, &call, sizeof(call)-sizeof(long), 0, 0);
  1076. id = call.id;
  1077. printf("ID- %d\n", id);
  1078. if(id>= 0 && id <=max_chegadas){
  1079. break;
  1080. }
  1081. }
  1082. pthread_mutex_unlock(&mutex_thread);
  1083. }
  1084. else{
  1085. //pthread_mutex_unlock(&mutex_thread);
  1086. //pthread_mutex_lock(&mutex_thread);
  1087. printf("NOT PRIORITY\n");
  1088. msg.msgtyp = 4;
  1089. strcpy(msg.nome,lista_chegada->next->arr.nome);
  1090. msg.eta = lista_chegada->next->eta;
  1091. msg.combustivel = lista_chegada->next->combustivel;
  1092. msg.init = lista_chegada->next->arr.inicial;
  1093. msg.flag_prioridade = 0;
  1094. msg.instante_partida = 0;
  1095.  
  1096. imprime_lista_chegada(lista_chegada);
  1097.  
  1098.  
  1099. msgsnd(mq_id, &msg, sizeof(msg)-sizeof(long), 0);
  1100. remove_lista_voo(lista_voo_aux,lista_chegada->next->arr.nome);
  1101. //pthread_mutex_unlock(&mutex_thread);
  1102. while(1){
  1103. msgrcv(mq_id, &call, sizeof(call)-sizeof(long), 0, 0);
  1104. id = call.id;
  1105. printf("ID- %d\n", id);
  1106. if(id>= 0 && id <=max_chegadas){
  1107. break;
  1108. }
  1109. }
  1110. }
  1111. while (1){
  1112. if(shm_chegada[id].eta <= hora_ut()){
  1113. break;
  1114. }
  1115. }
  1116. }
  1117. }
  1118. //funcao que chama as funcoes todas de criar
  1119. void gestor(){
  1120. cria_semaforos();
  1121. ler_config();
  1122. hora_inicio();
  1123. cria_semaforos();
  1124. cria_shm_stats();
  1125. cria_shm_chegada();
  1126. cria_shm_partida();
  1127. pthread_mutex_init(&mutex_thread,NULL);
  1128. pthread_cond_init(&cond, NULL);
  1129. lista_chegada = cria_lista_chegada();
  1130. lista_partida = cria_lista_partida();
  1131. lista_voo_aux = cria_lista_voos();
  1132. cria_mq();
  1133. cria_pipe();
  1134. }
  1135.  
  1136. void remove_lista_chegada(lista_voo_chegada chegada, char *nome){
  1137. lista_voo_chegada atual,ant;
  1138. atual = chegada->next;
  1139. ant = chegada;
  1140. if(atual!= NULL){
  1141. while (strcmp(atual->arr.nome,nome)!=0)
  1142. {
  1143. atual = atual->next;
  1144. ant = ant->next;
  1145. }
  1146. ant->next = atual->next;
  1147. free(atual);
  1148. }
  1149. }
  1150.  
  1151. void remove_lista_voo(lista_voos chegada, char *nome){
  1152. lista_voos atual,ant;
  1153. atual = chegada->next;
  1154. ant = chegada;
  1155. if(atual!= NULL){
  1156. while (strcmp(atual->nome,nome)!=0)
  1157. {
  1158. atual = atual->next;
  1159. ant = ant->next;
  1160. }
  1161. ant->next = atual->next;
  1162. free(atual);
  1163. }
  1164. }
  1165.  
  1166. void remove_lista_partida(lista_voo_partida partida, char *nome){
  1167. lista_voo_partida atual,ant;
  1168. atual = partida->next;
  1169. ant = partida;
  1170. if(atual!= NULL){
  1171. while (strcmp(atual->dep.nome,nome)!=0)
  1172. {
  1173. atual = atual->next;
  1174. ant = ant->next;
  1175. }
  1176. ant->next = atual->next;
  1177. free(atual);
  1178. }
  1179. }
  1180.  
  1181. int checkLista(lista_voo_chegada chegadas, lista_voo_partida partidas,char *nome){
  1182. lista_voo_partida atual_partida;
  1183. lista_voo_chegada atual_chegada;
  1184. atual_chegada = chegadas;
  1185. atual_partida = partidas;
  1186. if(chegadas->next == NULL){
  1187. while(partidas->next != NULL){
  1188. if(strcmp(partidas->next->dep.nome,nome) == 0){
  1189. return 2;
  1190. }
  1191. else{
  1192. partidas = partidas->next;
  1193. }
  1194. }
  1195. }
  1196. else if(partidas->next == NULL){
  1197. while(chegadas->next != NULL){
  1198. if(strcmp(chegadas->next->arr.nome,nome) == 0){
  1199. return 1;
  1200. }
  1201. else{
  1202. chegadas = chegadas->next;
  1203. }
  1204. }
  1205. }
  1206. else if(partidas->next != NULL && chegadas->next != NULL){
  1207. while(chegadas->next != NULL){
  1208. if(strcmp(chegadas->next->arr.nome,nome) == 0){
  1209. return 1;
  1210. }
  1211. else{
  1212. chegadas = chegadas->next;
  1213. }
  1214. }
  1215. while(partidas->next != NULL){
  1216. if(strcmp(partidas->dep.nome,nome) == 0){
  1217. return 2;
  1218. }
  1219. else{
  1220. partidas = partidas->next;
  1221. }
  1222. }
  1223. }
  1224. }
  1225.  
  1226. int condition(){
  1227.  
  1228. double var;
  1229. var = (hora_ut());
  1230. if(lista_voo_aux->next != NULL){
  1231. if(lista_voo_aux->next->inicial <= hora_ut()){
  1232. return 1;
  1233. }
  1234. else{
  1235. return 0;
  1236. }
  1237. }
  1238. else{
  1239. return 0;
  1240. }
  1241. }
  1242.  
  1243. void *thread_manage(){
  1244.  
  1245. int k;
  1246. k=-1;
  1247.  
  1248. while(1){
  1249. pthread_mutex_lock(&mutex_thread);
  1250. while(condition() == 0){
  1251. if(lista_voo_aux->next != NULL){
  1252. clock_gettime(CLOCK_REALTIME, &ts);
  1253. double var;
  1254. var = calcula_init(lista_voo_aux->next->inicial);
  1255. ts.tv_sec += var;
  1256. pthread_cond_timedwait(&cond,&mutex_thread,&ts);
  1257. }
  1258. else{
  1259. clock_gettime(CLOCK_REALTIME, &ts);
  1260. ts.tv_sec += 5;
  1261. printf("ESPEROU + 5seg\n");
  1262. pthread_cond_wait(&cond,&mutex_thread);
  1263. }
  1264. }
  1265. k++;
  1266. int a = checkLista(lista_chegada,lista_partida,lista_voo_aux->next->nome);
  1267. remove_lista_voo(lista_voo_aux,lista_voo_aux->next->nome);
  1268. pthread_mutex_unlock(&mutex_thread);
  1269.  
  1270. if(pthread_create(&thread_voo[k],NULL,thread_worker, (void*) &a) !=0){
  1271. perror("ERROR creating the thread pipe!\n");
  1272. }
  1273. printf("Asnbasv\n");
  1274. //signal(SIGINT,termina_programa);
  1275. }
  1276.  
  1277. }
  1278.  
  1279. int main(){
  1280.  
  1281. lista_voo_chegada aux_lista_chegada;
  1282. lista_voo_partida aux_lista_partida;
  1283. lista_voos aux_lista_voos;
  1284.  
  1285. aux_lista_partida = lista_partida;
  1286. aux_lista_chegada = lista_chegada;
  1287. aux_lista_voos = lista_voo_aux;
  1288. int k;
  1289. k=-1;
  1290.  
  1291. pthread_mutex_init(&mutex_thread,NULL);
  1292. pthread_cond_init(&cond, NULL);
  1293.  
  1294. debug_print("Programa iniciado");
  1295.  
  1296. flog=fopen("log.txt","w");
  1297.  
  1298. gestor();
  1299.  
  1300. if(pthread_create(&thread_pipe,NULL,abre_pipe,NULL)!=0){
  1301. perror("ERROR creating the thread pipe!\n");
  1302. }
  1303.  
  1304. if ((pid_torre = fork()) == 0) {
  1305. criar_processo_torre();
  1306. }
  1307. else if (pid_torre == -1) {
  1308. perror("Erro a criar o processo torre de controlo");
  1309. }
  1310. else{
  1311.  
  1312. if(pthread_create(&thread_manage_id,NULL,thread_manage,NULL)!=0){
  1313. perror("ERROR creating the thread pipe!\n");
  1314. }
  1315. while(1){
  1316. sleep(10);
  1317. }
  1318. }
  1319. fclose(flog);
  1320. return 0;
  1321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement