Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.03 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. #include "header.h"
  6.  
  7.  
  8.  
  9. /**Catarina Moreira
  10.  
  11. Catarina Proença**/
  12.  
  13.  
  14.  
  15. //timelog function
  16.  
  17. char *timelog() {
  18. t = time(NULL);
  19. Time = *localtime(&t);
  20.  
  21. snprintf(timenow, sizeof(timenow), "\n%d:%d:%d ",Time.tm_hour, Time.tm_min, Time.tm_sec);
  22. return timenow;
  23. }
  24.  
  25.  
  26.  
  27. //code - init time - takeoff time
  28.  
  29. int create_departure(char *c, int i, int t) {
  30.  
  31.  
  32.  
  33. Lista_departure novo;
  34.  
  35. novo = (Lista_departure) malloc(sizeof(node_struct_departure));
  36.  
  37. novo->departure = malloc(sizeof(departure_struct));
  38.  
  39.  
  40.  
  41. if(novo!=NULL){
  42.  
  43. strcpy(novo->departure->flight_code, c);
  44.  
  45. novo->departure->initial_time = i;
  46.  
  47. novo->departure->takeoff_time = t;
  48.  
  49. }
  50.  
  51.  
  52.  
  53. sem_wait(&semafores->insert_queue);
  54.  
  55. departures = insert_departure(departures,novo);
  56.  
  57. sem_post(&semafores->insert_queue);
  58.  
  59.  
  60.  
  61. write_log_file("Departure introduzida na fila com sucesso!");
  62.  
  63. return 0;
  64.  
  65. }
  66.  
  67.  
  68.  
  69. //code - init time - time to runway - init fuel
  70.  
  71. int create_arrival(char *c, int i, int t, int f) {
  72.  
  73.  
  74.  
  75. Lista_arrival novo;
  76.  
  77. novo = (Lista_arrival) malloc(sizeof(node_struct_arrival));
  78.  
  79. novo->arrival = malloc(sizeof(arrival_struct));
  80.  
  81.  
  82.  
  83. if(novo!=NULL){
  84.  
  85. strcpy(novo->arrival->flight_code, c);
  86.  
  87. novo->arrival->initial_time = i;
  88.  
  89. novo->arrival->time_runway = t;
  90.  
  91. novo->arrival->initial_fuel = f;
  92.  
  93. }
  94.  
  95. sem_wait(&semafores->insert_queue);
  96.  
  97. arrivals = insert_arrival(arrivals, novo);
  98.  
  99. sem_post(&semafores->insert_queue);
  100.  
  101.  
  102.  
  103. write_log_file("Arrival introduzida na fila com sucesso!");
  104.  
  105. return 0;
  106.  
  107. }
  108.  
  109.  
  110.  
  111. Lista_departure insert_departure(Lista_departure departures, Lista_departure novo){
  112.  
  113. Lista_departure ant, act;
  114.  
  115.  
  116.  
  117. if(departures == NULL){
  118.  
  119. departures = novo;
  120.  
  121. novo->next = NULL;
  122.  
  123. return departures;
  124.  
  125. }
  126.  
  127.  
  128.  
  129. //(departures->departure->initial_time)>=(novo->departure->initial_time)
  130.  
  131. act = departures->next;
  132.  
  133. ant = departures;
  134.  
  135.  
  136.  
  137. while((act->next!=NULL) && (act->departure->initial_time)<=(novo->departure->initial_time)){
  138.  
  139. ant = ant->next;
  140.  
  141. act = act->next;
  142.  
  143. }
  144.  
  145.  
  146.  
  147. novo->next = act;
  148.  
  149. ant->next = novo;
  150.  
  151. return departures;
  152.  
  153. }
  154.  
  155.  
  156.  
  157. Lista_arrival insert_arrival(Lista_arrival arrivals, Lista_arrival novo){
  158.  
  159. Lista_arrival ant, act;
  160.  
  161.  
  162.  
  163. if(arrivals == NULL){
  164.  
  165. arrivals = novo;
  166.  
  167. novo->next = NULL;
  168.  
  169. return arrivals;
  170.  
  171. }
  172.  
  173.  
  174.  
  175. act = arrivals->next;
  176.  
  177. ant = arrivals;
  178.  
  179.  
  180.  
  181. while((act->next!=NULL) && (act->arrival->initial_time)<=(novo->arrival->initial_time)){
  182.  
  183. ant = ant->next;
  184.  
  185. act = act->next;
  186.  
  187. }
  188.  
  189.  
  190.  
  191. novo->next = act;
  192.  
  193. ant->next = novo;
  194.  
  195. return arrivals;
  196.  
  197. }
  198.  
  199.  
  200.  
  201. Lista_departure remove_departure(){
  202.  
  203. Lista_departure no;
  204.  
  205. Lista_departure removido;
  206.  
  207.  
  208.  
  209. removido = (Lista_departure) malloc(sizeof(node_struct_departure));
  210.  
  211. removido->departure = malloc(sizeof(departure_struct));
  212.  
  213.  
  214.  
  215. if(departures == NULL){
  216.  
  217. printf("Fila vazia.");
  218.  
  219. }else if(departures->next == NULL){
  220.  
  221. strcpy(removido->departure->flight_code,departures->departure->flight_code);
  222.  
  223. removido->departure->initial_time = departures->departure->initial_time;
  224.  
  225. removido->departure->takeoff_time = departures->departure->takeoff_time;
  226.  
  227. free(departures);
  228.  
  229. departures = NULL;
  230.  
  231. }else{
  232.  
  233. no = departures->next;
  234.  
  235. strcpy(removido->departure->flight_code,departures->departure->flight_code);
  236.  
  237. removido->departure->initial_time = departures->departure->initial_time;
  238.  
  239. removido->departure->takeoff_time = departures->departure->takeoff_time;
  240.  
  241. free(departures);
  242.  
  243. departures=no;
  244.  
  245. }
  246.  
  247. return removido;
  248.  
  249. }
  250.  
  251.  
  252.  
  253. Lista_arrival remove_arrival(){
  254.  
  255. Lista_arrival no;
  256.  
  257. Lista_arrival removido;
  258.  
  259.  
  260.  
  261. removido = (Lista_arrival) malloc(sizeof(node_struct_arrival));
  262.  
  263. removido->arrival = malloc(sizeof(arrival_struct));
  264.  
  265.  
  266.  
  267. if(arrivals == NULL){
  268.  
  269. printf("Fila vazia.");
  270.  
  271. }
  272.  
  273. else if(arrivals->next == NULL){
  274.  
  275. strcpy(removido->arrival->flight_code,arrivals->arrival->flight_code);
  276.  
  277. removido->arrival->initial_time = arrivals->arrival->initial_time;
  278.  
  279. removido->arrival->time_runway = arrivals->arrival->time_runway;
  280.  
  281. removido->arrival->initial_fuel = arrivals->arrival->initial_fuel;
  282.  
  283. free(arrivals);
  284.  
  285. arrivals = NULL;
  286.  
  287. }
  288.  
  289. else{
  290.  
  291. no = arrivals->next;
  292.  
  293. strcpy(removido->arrival->flight_code,arrivals->arrival->flight_code);
  294.  
  295. removido->arrival->initial_time = arrivals->arrival->initial_time;
  296.  
  297. removido->arrival->time_runway = arrivals->arrival->time_runway;
  298.  
  299. removido->arrival->initial_fuel = arrivals->arrival->initial_fuel;
  300.  
  301. free(arrivals);
  302.  
  303. arrivals=no;
  304.  
  305. }
  306.  
  307. return removido;
  308.  
  309. }
  310.  
  311.  
  312.  
  313. void create_pipe(){
  314.  
  315. unlink(PIPE_NAME);
  316.  
  317. if ((mkfifo(PIPE_NAME, O_CREAT|O_EXCL|0600)<0) && (errno!=EEXIST)){
  318.  
  319. perror("Cannot create pipe. ");
  320.  
  321. exit(0);
  322.  
  323. }
  324.  
  325. write_log_file("Pipe created.");
  326.  
  327. }
  328.  
  329.  
  330.  
  331.  
  332.  
  333. void *workerthread_d(){
  334.  
  335. printf("A iniciar departure\n");
  336.  
  337. sem_wait(&semafores->statistics);
  338.  
  339. stats->n_voos_criados++;
  340.  
  341. sem_post(&semafores->statistics);
  342.  
  343.  
  344.  
  345. Lista_departure no = remove_departure();
  346.  
  347. message_struct_d message_d;
  348.  
  349.  
  350.  
  351. message_d.mtype = 3;
  352.  
  353. message_d.takeoff_time = no->departure->takeoff_time;
  354.  
  355. message_d.slot = 0;
  356.  
  357.  
  358.  
  359. if(msgsnd(mq_id,&message_d,sizeof(message_struct_d)-sizeof(long),0)!=0){
  360.  
  361. perror("Error - msgsnd() of mq");
  362.  
  363. }else{
  364.  
  365. printf("Enviado para message queue - Torre de controlo\n");
  366.  
  367. }
  368.  
  369.  
  370.  
  371. usleep(1000*5000);
  372.  
  373.  
  374.  
  375. printf("A terminar departure...\n");
  376.  
  377. pthread_exit(NULL);
  378.  
  379. }
  380.  
  381.  
  382.  
  383. void *workerthread_a(){
  384.  
  385. printf("A iniciar arrival\n");
  386.  
  387.  
  388.  
  389. sem_wait(&semafores->statistics);
  390.  
  391. stats->n_voos_criados++;
  392.  
  393. sem_post(&semafores->statistics);
  394.  
  395.  
  396.  
  397. Lista_arrival no = remove_arrival();
  398.  
  399. message_struct_a message_a;
  400.  
  401.  
  402.  
  403. if(no->arrival->initial_fuel <= 4+no->arrival->time_runway+running_config->L){
  404.  
  405. char aux[INPUT_BUFFER_SIZE];
  406.  
  407.  
  408.  
  409. message_a.mtype =1;
  410.  
  411.  
  412.  
  413. sprintf(aux, "%s EMERGENCY LANDING REQUESTED", no->arrival->flight_code);
  414.  
  415. write_log_file(aux);
  416.  
  417. }
  418.  
  419.  
  420.  
  421. else{
  422.  
  423. message_a.mtype = 2;
  424.  
  425.  
  426.  
  427. }
  428.  
  429.  
  430.  
  431. message_a.initial_fuel = no->arrival->initial_fuel;
  432.  
  433. message_a.time_runway = no->arrival->time_runway;
  434.  
  435.  
  436.  
  437. message_a.slot = 0;
  438.  
  439.  
  440.  
  441. if(msgsnd(mq_id,&message_a,sizeof(message_struct_a)-sizeof(long),0)!=0){
  442.  
  443. perror("Error - msgsnd() of mq");
  444.  
  445. }else{
  446.  
  447. printf("Enviado para message queue - Torre de Controlo\n");
  448.  
  449. }
  450.  
  451.  
  452.  
  453. if(msgrcv(mq_id, &message_a, sizeof(message_struct_a)-sizeof(long), 4, 0) == -1){
  454.  
  455. }
  456.  
  457. else if(errno == ENOMSG){
  458.  
  459. perror("Erro - ENONMSG msgrcv()");
  460.  
  461. }
  462.  
  463. else if(errno == EAGAIN){
  464.  
  465. perror("Erro - EAGAIN msgrcv()");
  466.  
  467. }
  468.  
  469. else{
  470.  
  471. //funcao
  472.  
  473. printf("O meu slot e = %ld\n", message_a.slot);
  474.  
  475. }
  476.  
  477.  
  478.  
  479. usleep(1000*5000);
  480.  
  481. printf("A terminar arrival...\n");
  482.  
  483. pthread_exit(NULL);
  484.  
  485. }
  486.  
  487.  
  488.  
  489. void decrementa(){
  490.  
  491. Departures_ord aux_d = d_ord;
  492.  
  493. Arrivals_ord aux_a = a_ord;
  494.  
  495.  
  496.  
  497. if(aux_a!=NULL){
  498.  
  499. printf("olaaaaaa %d\n",aux_a->arrival->time_runway);
  500.  
  501. }
  502.  
  503.  
  504.  
  505. while(aux_a!=NULL){
  506.  
  507. aux_a->arrival->time_runway -= 1;
  508.  
  509. aux_a->arrival->initial_fuel -= 1;
  510.  
  511. printf("Combustivel = %d", aux_a->arrival->initial_fuel);
  512.  
  513. aux_a = aux_a->next;
  514.  
  515. }
  516.  
  517. while(aux_d!=NULL){
  518.  
  519. aux_d->departure->takeoff_time -= 1;
  520.  
  521. aux_d = aux_d->next;
  522.  
  523. }
  524.  
  525. }
  526.  
  527.  
  528.  
  529. void *time_counter(){
  530.  
  531. while(1){
  532.  
  533. if((departures != NULL) && (departures->departure->initial_time) == mem->t){
  534.  
  535. pthread_create(&id_thread,NULL,workerthread_d,NULL);
  536.  
  537. }
  538.  
  539. if((arrivals != NULL) && (arrivals->arrival->initial_time) == mem->t){
  540.  
  541. pthread_create(&id_thread,NULL,workerthread_a,NULL);
  542.  
  543. }
  544.  
  545.  
  546.  
  547. usleep(1000*running_config->ut);
  548.  
  549. sem_wait(&semafores->mem);
  550.  
  551. mem->t++;
  552.  
  553. decrementa();
  554.  
  555. pthread_cond_broadcast(&cond);
  556.  
  557. sem_post(&semafores->mem);
  558.  
  559.  
  560.  
  561. printf("Tempo %d\n",mem->t);
  562.  
  563.  
  564.  
  565. }
  566.  
  567. }
  568.  
  569.  
  570.  
  571. int init_shm_mem(){
  572.  
  573. if((mem_shmid= shmget(IPC_PRIVATE, sizeof(mem_struct*), IPC_CREAT|0700))<0){
  574.  
  575. perror("Error - shmget() of mem struct");
  576.  
  577. return -1;
  578.  
  579. }
  580.  
  581. if((mem = (mem_struct*) shmat(mem_shmid, NULL, 0)) == (mem_struct*)-1){
  582.  
  583. perror("Error - shmat() of mem struct");
  584.  
  585. return -1;
  586.  
  587. }
  588.  
  589. mem->t = 0;
  590.  
  591. return 0;
  592.  
  593. }
  594.  
  595.  
  596.  
  597. int init_shm_stats(){
  598.  
  599. if((stats_shmid = shmget(IPC_PRIVATE, sizeof(stats_struct*), IPC_CREAT|0700))<0){
  600.  
  601. perror("Error - shmget() of statistics struct");
  602.  
  603. return -1;
  604.  
  605. }
  606.  
  607. if((stats = (stats_struct*) shmat(stats_shmid, NULL, 0)) == (stats_struct*)-1){
  608.  
  609. perror("Error - shmat() of statistics struct");
  610.  
  611. return -1;
  612.  
  613. }
  614.  
  615.  
  616.  
  617. stats->n_voos_criados=0;
  618.  
  619. stats->n_voos_aterrados=0;
  620.  
  621. stats->t_aterrar=0.0;
  622.  
  623. stats->n_voos_descolados=0;
  624.  
  625. stats->t_descolados=0.0;
  626.  
  627. stats->n_holding_aterragem=0.0;
  628.  
  629. stats->n_holding_urgencia=0.0;
  630.  
  631. stats->n_voos_redirecionados=0;
  632.  
  633. stats->voos_rejeitados=0;
  634.  
  635.  
  636.  
  637. return 0;
  638.  
  639. }
  640.  
  641.  
  642.  
  643. //Create departure queue
  644.  
  645. int create_queue(){
  646.  
  647. if((mq_id = msgget(IPC_PRIVATE, IPC_CREAT|0700)) < 0) {
  648.  
  649. perror("Error - msgget()");
  650.  
  651. return -1;
  652.  
  653. }
  654.  
  655. else {
  656.  
  657. write_log_file("Message Queue created.");
  658.  
  659. }
  660.  
  661. return 0;
  662.  
  663. }
  664.  
  665.  
  666.  
  667. void close_named_pipe(){
  668.  
  669. close(fd);
  670.  
  671. unlink(PIPE_NAME);
  672.  
  673. }
  674.  
  675.  
  676.  
  677. //Init queues
  678.  
  679. int init_queues() {
  680.  
  681. departures = NULL;
  682.  
  683. arrivals = NULL;
  684.  
  685.  
  686.  
  687. d_ord = NULL;
  688.  
  689. a_ord = NULL;
  690.  
  691.  
  692.  
  693. write_log_file("Queues ready.");
  694.  
  695. return 0;
  696.  
  697. }
  698.  
  699.  
  700.  
  701. //Ficheiro LOG
  702.  
  703. int init_log_file() {
  704.  
  705. printf("Creating \"log.txt\"...\n");
  706.  
  707.  
  708.  
  709. if((flog = fopen(LOG_FILE_NAME, "a+")) == NULL){
  710.  
  711. perror("Error - fopen(\"log.txt\", \"a\")");
  712.  
  713. return -1;
  714.  
  715. }
  716.  
  717. write_log_file("File \"log.txt\" created successfully.");
  718.  
  719.  
  720.  
  721. return 0;
  722.  
  723. }
  724.  
  725.  
  726.  
  727. // Inicializar semáforos em memória partilhada
  728.  
  729. // Todos os semáforos declaram-se na struct respectiva e inicializam-se aqui
  730.  
  731. int init_shm_semafores() {
  732.  
  733. if((sem_shmid = shmget(IPC_PRIVATE, sizeof(sem_struct*), IPC_CREAT|0700))<0){
  734.  
  735. perror("Error - shmget() of semafores struct");
  736.  
  737. return -1;
  738.  
  739. }
  740.  
  741.  
  742.  
  743. if((semafores = (sem_struct*) shmat(sem_shmid, NULL, 0)) == (sem_struct*)-1){
  744.  
  745. perror("Error - shmat() of semafores struct");
  746.  
  747. return -1;
  748.  
  749. }
  750.  
  751.  
  752.  
  753. sem_init(&semafores->write_log, 1, 1);
  754.  
  755. sem_init(&semafores->insert_queue, 1, 1);
  756.  
  757. sem_init(&semafores->remove_queue, 1, 1);
  758.  
  759. sem_init(&semafores->statistics, 1, 1);
  760.  
  761. sem_init(&semafores->mem,1,1);
  762.  
  763.  
  764.  
  765. return 0;
  766.  
  767. }
  768.  
  769.  
  770.  
  771.  
  772.  
  773. // Inicializar estutura de configuração de acordo com o ficheiro respectivo
  774.  
  775. int read_config(){
  776.  
  777.  
  778.  
  779. FILE *fp;
  780.  
  781. if(!(fp = fopen("config.txt","r"))) {
  782.  
  783. printf("Erro ao abrir o ficheiro config.txt!\n");
  784.  
  785. exit(0);
  786.  
  787. }
  788.  
  789.  
  790.  
  791. running_config = (config_struct*) malloc(sizeof(config_struct));
  792.  
  793.  
  794.  
  795. fscanf(fp,"%d",&running_config->ut);//unidade de tempo
  796.  
  797. fscanf(fp,"%d, %d",&running_config->T,&running_config->dt);//duracao descolagem, intervalo entre descolagem
  798.  
  799. fscanf(fp,"%d, %d",&running_config->L,&running_config->dl);//duracao aterragem, intervalo entre aterragens
  800.  
  801. fscanf(fp,"%d, %d",&running_config->Min,&running_config->Max); //holding duracao minima, holding duracao maxima
  802.  
  803. fscanf(fp,"%d",&running_config->D); //quantidade maxima partidas
  804.  
  805. fscanf(fp,"%d",&running_config->A); //quantidade maxima chegadas
  806.  
  807.  
  808.  
  809. //printf("%d\n",running_config->ut);
  810.  
  811. //printf("%d, %d\n",running_config->T,running_config->dt);
  812.  
  813. //printf("%d, %d\n",running_config->L,running_config->dl);
  814.  
  815. //printf("%d, %d\n",running_config->Min,running_config->Max);
  816.  
  817. //printf("%d\n",running_config->D);
  818.  
  819. //printf("%d\n",running_config->A);
  820.  
  821.  
  822.  
  823. write_log_file("Configuration file \"config.txt\" successfully loaded.");
  824.  
  825.  
  826.  
  827. return 0;
  828.  
  829. }
  830.  
  831.  
  832.  
  833. void le_pipe(){
  834.  
  835. if((fd = open(PIPE_NAME, O_RDWR)) < 0) {
  836.  
  837. perror("Cannot open pipe for reading:\n");
  838.  
  839. exit(0);
  840.  
  841. }
  842.  
  843. else{
  844.  
  845. write_log_file("Open pipe for reading.");
  846.  
  847. }
  848.  
  849.  
  850.  
  851. char aux[BUFFER_SIZE];
  852.  
  853. char str[BUFFER_SIZE];
  854.  
  855. char all_str[50];
  856.  
  857. char flight_code[50];
  858.  
  859. int initial_time,takeoff_time, eta, fuel;
  860.  
  861.  
  862.  
  863. while(1){
  864.  
  865.  
  866.  
  867. read(fd, str, BUFFER_SIZE);
  868.  
  869. strcpy(all_str, str);
  870.  
  871. token = strtok(str," ");
  872.  
  873. if(strcmp(token,"DEPARTURE")==0){
  874.  
  875. token = strtok(NULL," ");
  876.  
  877. if(verifica_codigo(token)==0){
  878.  
  879. sprintf(aux, "WRONG COMMAND => %s", all_str);
  880.  
  881. write_log_file(aux);
  882.  
  883. strncpy(aux, "", sizeof(aux));
  884.  
  885. continue;
  886.  
  887. }
  888.  
  889. strcpy(flight_code, token);
  890.  
  891.  
  892.  
  893. token = strtok(NULL," ");
  894.  
  895. token = strtok(NULL," ");
  896.  
  897. if(verifica_inteiro(token)==0){
  898.  
  899. sprintf(aux, "WRONG COMMAND => %s", all_str);
  900.  
  901. write_log_file(aux);
  902.  
  903. strncpy(aux, "", sizeof(aux));
  904.  
  905. continue;
  906.  
  907. }
  908.  
  909. initial_time = atoi(token);
  910.  
  911.  
  912.  
  913. token = strtok(NULL," ");
  914.  
  915. token = strtok(NULL," ");
  916.  
  917. if(verifica_inteiro(token)==0){
  918.  
  919. sprintf(aux, "WRONG COMMAND => %s", all_str);
  920.  
  921. write_log_file(aux);
  922.  
  923. strncpy(aux, "", sizeof(aux));
  924.  
  925. continue;
  926.  
  927. }
  928.  
  929. takeoff_time = atoi(token);
  930.  
  931. sprintf(aux, "NEW COMMAND => %s", all_str);
  932.  
  933.  
  934.  
  935. write_log_file(aux);
  936.  
  937. strncpy(aux, "", sizeof(aux));
  938.  
  939.  
  940.  
  941. create_departure(flight_code, initial_time, takeoff_time);
  942.  
  943.  
  944.  
  945. }
  946.  
  947.  
  948.  
  949. else if(strcmp(token,"ARRIVAL")==0){
  950.  
  951. token = strtok(NULL," ");
  952.  
  953. if(verifica_codigo(token)==0){
  954.  
  955. sprintf(aux, "WRONG COMMAND => %s", all_str);
  956.  
  957. write_log_file(aux);
  958.  
  959. strncpy(aux, "", sizeof(aux));
  960.  
  961. continue;
  962.  
  963. }
  964.  
  965. strcpy(flight_code, token);
  966.  
  967.  
  968.  
  969. token = strtok(NULL," ");
  970.  
  971. token = strtok(NULL," ");
  972.  
  973. if(verifica_inteiro(token)==0){
  974.  
  975. sprintf(aux, "WRONG COMMAND => %s", all_str);
  976.  
  977. write_log_file(aux);
  978.  
  979. strncpy(aux, "", sizeof(aux));
  980.  
  981. continue;
  982.  
  983. }
  984.  
  985. initial_time = atoi(token);
  986.  
  987.  
  988.  
  989. token = strtok(NULL," ");
  990.  
  991. token = strtok(NULL," ");
  992.  
  993. if(verifica_inteiro(token)==0){
  994.  
  995. sprintf(aux, "WRONG COMMAND => %s", all_str);
  996.  
  997. write_log_file(aux);
  998.  
  999. strncpy(aux, "", sizeof(aux));
  1000.  
  1001. continue;
  1002.  
  1003. }
  1004.  
  1005. eta = atoi(token);
  1006.  
  1007.  
  1008.  
  1009. token = strtok(NULL," ");
  1010.  
  1011. token = strtok(NULL," ");
  1012.  
  1013. if(verifica_inteiro(token)==0){
  1014.  
  1015. sprintf(aux, "WRONG COMMAND => %s", all_str);
  1016.  
  1017. write_log_file(aux);
  1018.  
  1019. strncpy(aux, "", sizeof(aux));
  1020.  
  1021. continue;
  1022.  
  1023. }
  1024.  
  1025. fuel = atoi(token); //FUEL <eta nao da wrong command
  1026.  
  1027. if(fuel<eta){
  1028.  
  1029. sprintf(aux, "WRONG COMMAND => %s", all_str);
  1030.  
  1031. write_log_file(aux);
  1032.  
  1033. strncpy(aux, "", sizeof(aux));
  1034.  
  1035. continue;
  1036.  
  1037. }
  1038.  
  1039.  
  1040.  
  1041. sprintf(aux, "NEW COMMAND => %s", all_str);
  1042.  
  1043. write_log_file(aux);
  1044.  
  1045. strncpy(aux, "", sizeof(aux));
  1046.  
  1047.  
  1048.  
  1049. create_arrival(flight_code, initial_time, eta,fuel);
  1050.  
  1051.  
  1052.  
  1053. }
  1054.  
  1055.  
  1056.  
  1057. else{
  1058.  
  1059. sprintf(aux, "WRONG COMMAND => %s", all_str);
  1060.  
  1061. write_log_file(aux);
  1062.  
  1063. strncpy(aux, "", sizeof(aux));
  1064.  
  1065. }
  1066.  
  1067. }
  1068.  
  1069. }
  1070.  
  1071.  
  1072.  
  1073. int verifica_codigo(char *token1){
  1074.  
  1075. char token[50];
  1076.  
  1077. strcpy(token, token1);
  1078.  
  1079.  
  1080.  
  1081. if(token[0]=='T' && token[1]=='P'){
  1082.  
  1083. for(int i=2; i<strlen(token);i++){
  1084.  
  1085. if(!(isdigit(token[i])==0))
  1086.  
  1087. return 1;
  1088.  
  1089. }
  1090.  
  1091. }
  1092.  
  1093. return 0;
  1094.  
  1095. }
  1096.  
  1097.  
  1098.  
  1099. int verifica_inteiro(char* token1){
  1100.  
  1101. char token[50];
  1102.  
  1103. strcpy(token, token1);
  1104.  
  1105.  
  1106.  
  1107. for(int i=0; i<strlen(token);i++){
  1108.  
  1109. if(isdigit(token[i])==0)
  1110.  
  1111. return 0;
  1112.  
  1113. }
  1114.  
  1115. return 1;
  1116.  
  1117. }
  1118.  
  1119.  
  1120.  
  1121. void init(){
  1122.  
  1123. init_shm_semafores();
  1124.  
  1125. init_log_file();
  1126.  
  1127. read_config();
  1128.  
  1129. init_shm_stats();
  1130.  
  1131. init_shm_mem();
  1132.  
  1133. create_pipe();
  1134.  
  1135. create_queue();
  1136.  
  1137. init_queues();
  1138.  
  1139.  
  1140.  
  1141. write_log_file("PROGRAM STARTED.");
  1142.  
  1143. fflush(stdout);
  1144.  
  1145. }
  1146.  
  1147.  
  1148.  
  1149. void stats_shm_shutdown(){
  1150.  
  1151. if (shmdt(stats)==-1){ // detaching da memoria partilhada
  1152.  
  1153. perror("Error at detaching");
  1154.  
  1155. }
  1156.  
  1157. if (shmctl(stats_shmid, IPC_RMID, NULL)==-1) { //limpar memoria partilhada
  1158.  
  1159. perror("Error unmapping shared memory\n");
  1160.  
  1161. }
  1162.  
  1163. write_log_file("Shared memory for statistics cleaned");
  1164.  
  1165. }
  1166.  
  1167.  
  1168.  
  1169. void mq_shutdown(){
  1170.  
  1171. if(msgctl(mq_id, IPC_RMID, NULL)==-1) {
  1172.  
  1173. perror("Error destroying message queue\n");
  1174.  
  1175. }
  1176.  
  1177. write_log_file("Message queue for departure cleaned");
  1178.  
  1179. }
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185. void mem_shm_shutdown(){
  1186.  
  1187. if (shmdt(mem)==-1){ // detaching da memoria partilhada
  1188.  
  1189. perror("Error at detaching");
  1190.  
  1191. }
  1192.  
  1193. if (shmctl(mem_shmid, IPC_RMID, NULL)==-1) { //limpar memoria partilhada
  1194.  
  1195. perror("Error unmapping shared memory\n");
  1196.  
  1197. }
  1198.  
  1199. write_log_file("Shared memory for mem cleaned");
  1200.  
  1201. }
  1202.  
  1203.  
  1204.  
  1205. void mutexes_shutdown(){
  1206.  
  1207. sem_destroy(&semafores->write_log);
  1208.  
  1209. sem_destroy(&semafores->insert_queue);
  1210.  
  1211. sem_destroy(&semafores->remove_queue);
  1212.  
  1213. sem_destroy(&semafores->statistics);
  1214.  
  1215. sem_destroy(&semafores->mem);
  1216.  
  1217.  
  1218.  
  1219. write_log_file("Semafores cleaned");
  1220.  
  1221. }
  1222.  
  1223.  
  1224.  
  1225. void show_stats(){
  1226.  
  1227. //Escrever estatisticas - sigusr1
  1228.  
  1229. printf("\n\n-----|| ESTATISTICAS ||-----\n");
  1230.  
  1231. printf("Numero total de voos criados - %d\n", stats->n_voos_criados);
  1232.  
  1233. printf("Numero total de voos que aterraram - %d\n", stats->n_voos_aterrados);
  1234.  
  1235. printf("Tempo medio de espera para aterrar - %f\n", stats->t_aterrar);
  1236.  
  1237. printf("Numero total de voos que descolaram - %d\n", stats->n_voos_descolados);
  1238.  
  1239. printf("Tempo medio de espera para descolar - %f\n", stats->t_descolados);
  1240.  
  1241. printf("Numero medio de manobras de holding por voo de aterragem - %f\n", stats->n_holding_aterragem);
  1242.  
  1243. printf("Numero medio de manobras de holding por voo em estado de urgencia - %f\n", stats->n_holding_urgencia);
  1244.  
  1245. printf("Numero de voos redirecionados para outro aeroporto - %d\n", stats->n_voos_redirecionados);
  1246.  
  1247. printf("Voos rejeitados pela Torre de Controlo - %d\n", stats->voos_rejeitados);
  1248.  
  1249. }
  1250.  
  1251.  
  1252.  
  1253. void shutdown(int sig){
  1254.  
  1255. pid_t p;
  1256.  
  1257. int status;
  1258.  
  1259.  
  1260.  
  1261. while((p=wait(&status))>0){
  1262.  
  1263. if(WIFEXITED(status)){
  1264.  
  1265. kill(child_pid,SIGUSR1);
  1266.  
  1267.  
  1268.  
  1269. stats_shm_shutdown();
  1270.  
  1271. mem_shm_shutdown();
  1272.  
  1273. mq_shutdown();
  1274.  
  1275. close_named_pipe();
  1276.  
  1277.  
  1278.  
  1279. free(running_config);
  1280.  
  1281. fclose(flog);
  1282.  
  1283.  
  1284.  
  1285. mutexes_shutdown();
  1286.  
  1287. write_log_file("PROGRAM ENDED.");
  1288.  
  1289. }
  1290.  
  1291. fflush(stdout);
  1292.  
  1293. }
  1294.  
  1295.  
  1296.  
  1297. printf("\n\n");
  1298.  
  1299. exit(0);
  1300.  
  1301. }
  1302.  
  1303.  
  1304.  
  1305. void write_log_file(char* str){
  1306.  
  1307. printf("%s%s",timelog(), str);
  1308.  
  1309. fflush(stdout);
  1310.  
  1311.  
  1312.  
  1313. sem_wait(&semafores->write_log);
  1314.  
  1315. fprintf(flog,"%s%s",timelog(), str);
  1316.  
  1317. fflush(flog);
  1318.  
  1319. sem_post(&semafores->write_log);
  1320.  
  1321. }
  1322.  
  1323.  
  1324.  
  1325. Departures_ord insert_departure_ord(message_struct_d departure){
  1326.  
  1327. Departures_ord novo;
  1328.  
  1329. novo = (Departures_ord) malloc(sizeof(node_struct_departure_ord));
  1330.  
  1331. novo->departure = malloc(sizeof(message_struct_d));
  1332.  
  1333.  
  1334.  
  1335. novo->departure->takeoff_time = departure.takeoff_time;
  1336.  
  1337.  
  1338.  
  1339. Departures_ord ant, act;
  1340.  
  1341.  
  1342.  
  1343. if(d_ord == NULL){
  1344.  
  1345. d_ord = novo;
  1346.  
  1347. novo->next = NULL;
  1348.  
  1349. return d_ord;
  1350.  
  1351. }
  1352.  
  1353.  
  1354.  
  1355. act = d_ord->next;
  1356.  
  1357. ant = d_ord;
  1358.  
  1359.  
  1360.  
  1361. while((act->next!=NULL) && (act->departure->takeoff_time)<=(novo->departure->takeoff_time)){
  1362.  
  1363. ant = ant->next;
  1364.  
  1365. act = act->next;
  1366.  
  1367. }
  1368.  
  1369.  
  1370.  
  1371. novo->next = act;
  1372.  
  1373. ant->next = novo;
  1374.  
  1375. return d_ord;
  1376.  
  1377. }
  1378.  
  1379.  
  1380.  
  1381. Arrivals_ord insert_arrival_ord(message_struct_a arrival){
  1382.  
  1383. Arrivals_ord novo;
  1384.  
  1385. novo = (Arrivals_ord) malloc(sizeof(node_struct_arrival_ord));
  1386.  
  1387. novo->arrival = malloc(sizeof(message_struct_a));
  1388.  
  1389.  
  1390.  
  1391. novo->arrival->mtype = arrival.mtype;
  1392.  
  1393. novo->arrival->time_runway = arrival.time_runway;
  1394.  
  1395. novo->arrival->initial_fuel = arrival.initial_fuel;
  1396.  
  1397.  
  1398.  
  1399. Arrivals_ord ant, act;
  1400.  
  1401.  
  1402.  
  1403. if(a_ord == NULL){
  1404.  
  1405. a_ord = novo;
  1406.  
  1407. novo->next = NULL;
  1408.  
  1409. return a_ord;
  1410.  
  1411. }
  1412.  
  1413.  
  1414.  
  1415. act = a_ord->next;
  1416.  
  1417. ant = a_ord;
  1418.  
  1419.  
  1420.  
  1421. if(novo->arrival->mtype == 1){
  1422.  
  1423. if(ant->arrival->mtype == 2){
  1424.  
  1425. novo->next = ant;
  1426.  
  1427. a_ord = novo;
  1428.  
  1429. return a_ord;
  1430.  
  1431. }
  1432.  
  1433.  
  1434.  
  1435. while(act->next != NULL && act->next->arrival->mtype!=2 && (4+act->arrival->time_runway+running_config->L)<=(4+novo->arrival->time_runway+running_config->L)){
  1436.  
  1437. ant = ant->next;
  1438.  
  1439. act = act->next;
  1440.  
  1441. }
  1442.  
  1443. }
  1444.  
  1445. else{
  1446.  
  1447. while((act->next!=NULL) && (act->arrival->mtype==1)){
  1448.  
  1449. ant = ant->next;
  1450.  
  1451. act = act->next;
  1452.  
  1453. }
  1454.  
  1455.  
  1456.  
  1457. while((act->next!=NULL) && (act->arrival->time_runway)<=(novo->arrival->time_runway)){
  1458.  
  1459. ant = ant->next;
  1460.  
  1461. act = act->next;
  1462.  
  1463. }
  1464.  
  1465. }
  1466.  
  1467.  
  1468.  
  1469. novo->next = act;
  1470.  
  1471. ant->next = novo;
  1472.  
  1473.  
  1474.  
  1475. return a_ord;
  1476.  
  1477. }
  1478.  
  1479.  
  1480.  
  1481. void *waiting_message_d(){
  1482.  
  1483. message_struct_d message_d;
  1484.  
  1485.  
  1486.  
  1487. while(1){
  1488.  
  1489. if(msgrcv(mq_id, &message_d, sizeof(message_struct_d)-sizeof(long), 3, 0) == -1){
  1490.  
  1491. }
  1492.  
  1493. else if(errno == ENOMSG){
  1494.  
  1495. perror("Erro - ENONMSG msgrcv()");
  1496.  
  1497. }
  1498.  
  1499. else if(errno == EAGAIN){
  1500.  
  1501. perror("Erro - EAGAIN msgrcv()");
  1502.  
  1503. }
  1504.  
  1505. else{
  1506.  
  1507. //funcao
  1508.  
  1509. printf("Recebida departure com takeoff = %d\n", message_d.takeoff_time);
  1510.  
  1511. d_ord = insert_departure_ord(message_d);
  1512.  
  1513.  
  1514.  
  1515. nr_slot++;
  1516.  
  1517. message_d.slot = nr_slot;
  1518.  
  1519.  
  1520.  
  1521. if(msgsnd(mq_id,&message_d,sizeof(message_struct_d)-sizeof(long),0)!=0){
  1522.  
  1523. perror("Error - msgsnd() of mq");
  1524.  
  1525. }else{
  1526.  
  1527. printf("Enviado para message queue - thread\n");
  1528.  
  1529. }
  1530.  
  1531. }
  1532.  
  1533. }
  1534.  
  1535. }
  1536.  
  1537.  
  1538.  
  1539. void *waiting_message_priority(){
  1540.  
  1541. message_struct_a message_a;
  1542.  
  1543.  
  1544.  
  1545. while(1){
  1546.  
  1547. if(msgrcv(mq_id, &message_a, sizeof(message_struct_a)-sizeof(long),1, 0) == -1){
  1548.  
  1549.  
  1550.  
  1551. }
  1552.  
  1553. else if(errno == ENOMSG){
  1554.  
  1555. perror("Erro - ENONMSG msgrcv()");
  1556.  
  1557. }
  1558.  
  1559. else if(errno == EAGAIN){
  1560.  
  1561. perror("Erro - EAGAIN msgrcv()");
  1562.  
  1563. }
  1564.  
  1565. else{
  1566.  
  1567. //funcao
  1568.  
  1569. printf("Recebida arrival prioritária com eta = %d e fuel = %d\n", message_a.time_runway, message_a.initial_fuel);
  1570.  
  1571. a_ord = insert_arrival_ord(message_a);
  1572.  
  1573.  
  1574.  
  1575. nr_slot++;
  1576.  
  1577. message_a.slot = nr_slot;
  1578.  
  1579. message_a.mtype = 4;
  1580.  
  1581.  
  1582.  
  1583. if(msgsnd(mq_id,&message_a,sizeof(message_struct_a)-sizeof(long),0)!=0){
  1584.  
  1585. perror("Error - msgsnd() of mq");
  1586.  
  1587. }else{
  1588.  
  1589. printf("Enviado para message queue - thread\n");
  1590.  
  1591. }
  1592.  
  1593. }
  1594.  
  1595. }
  1596.  
  1597. }
  1598.  
  1599.  
  1600.  
  1601.  
  1602.  
  1603. void *waiting_message_a(){
  1604.  
  1605. message_struct_a message_a;
  1606.  
  1607.  
  1608.  
  1609. while(1){
  1610.  
  1611. if(msgrcv(mq_id, &message_a, sizeof(message_struct_a)-sizeof(long),2, 0) == -1){
  1612.  
  1613.  
  1614.  
  1615. }
  1616.  
  1617. else if(errno == ENOMSG){
  1618.  
  1619. perror("Erro - ENONMSG msgrcv()");
  1620.  
  1621. }
  1622.  
  1623. else if(errno == EAGAIN){
  1624.  
  1625. perror("Erro - EAGAIN msgrcv()");
  1626.  
  1627. }
  1628.  
  1629. else{
  1630.  
  1631. //funcao
  1632.  
  1633. printf("Recebida arrival com eta = %d e fuel = %d\n", message_a.time_runway, message_a.initial_fuel);
  1634.  
  1635. a_ord = insert_arrival_ord(message_a);
  1636.  
  1637.  
  1638.  
  1639. nr_slot++;
  1640.  
  1641. message_a.slot = nr_slot;
  1642.  
  1643. message_a.mtype = 4;
  1644.  
  1645.  
  1646.  
  1647. if(msgsnd(mq_id,&message_a,sizeof(message_struct_a)-sizeof(long),0)!=0){
  1648.  
  1649. perror("Error - msgsnd() of mq");
  1650.  
  1651. }else{
  1652.  
  1653. printf("Enviado para message queue - thread\n");
  1654.  
  1655. }
  1656.  
  1657. }
  1658.  
  1659. }
  1660.  
  1661. }
  1662.  
  1663.  
  1664.  
  1665. int get_size_arrivals(){
  1666.  
  1667. Arrivals_ord act = a_ord;
  1668.  
  1669. int count = 0;
  1670.  
  1671.  
  1672.  
  1673. while(act != NULL){
  1674.  
  1675. count++;
  1676.  
  1677. act= act->next;
  1678.  
  1679. }
  1680.  
  1681. return count;
  1682.  
  1683. }
  1684.  
  1685. int generate_time_holding(){
  1686.  
  1687. int random;
  1688.  
  1689. srand(time(NULL));
  1690.  
  1691. random = (rand() % running_config->Max+1)+ running_config->Min+1;
  1692.  
  1693.  
  1694.  
  1695. return random;
  1696.  
  1697. }
  1698.  
  1699.  
  1700.  
  1701. void holding(){
  1702.  
  1703. int size = get_size_arrivals();
  1704.  
  1705. int count = 1;
  1706.  
  1707. Arrivals_ord act = a_ord;
  1708.  
  1709.  
  1710.  
  1711. if(size>5){
  1712.  
  1713. while(count<=5){
  1714.  
  1715. act = act->next;
  1716.  
  1717. count++;
  1718.  
  1719. }
  1720.  
  1721. //messagem
  1722.  
  1723. //generate_time_holding();
  1724.  
  1725. }
  1726.  
  1727. }
  1728.  
  1729.  
  1730.  
  1731. void torre_de_controlo(){
  1732.  
  1733. char res[100];
  1734.  
  1735.  
  1736.  
  1737. sprintf(res,"[%d] Torre de controlo ativa.",getpid());
  1738.  
  1739. write_log_file(res);
  1740.  
  1741.  
  1742.  
  1743. pthread_create(&thread_receive_d,NULL,waiting_message_d,NULL);
  1744.  
  1745. pthread_create(&thread_receive_a,NULL,waiting_message_a,NULL);
  1746.  
  1747. pthread_create(&thread_receive_priority,NULL,waiting_message_priority,NULL);
  1748.  
  1749.  
  1750.  
  1751. pthread_join(thread_receive_d,NULL);
  1752.  
  1753. pthread_join(thread_receive_a,NULL);
  1754.  
  1755. pthread_join(thread_receive_priority,NULL);
  1756.  
  1757. }
  1758.  
  1759.  
  1760.  
  1761. int main(){
  1762.  
  1763. signal(SIGINT, shutdown);
  1764.  
  1765. signal(SIGUSR1, show_stats);
  1766.  
  1767.  
  1768.  
  1769. signal(SIGALRM, SIG_IGN);
  1770.  
  1771. signal(SIGBUS, SIG_IGN);
  1772.  
  1773. signal(SIGTSTP, SIG_IGN);
  1774.  
  1775.  
  1776.  
  1777. init();
  1778.  
  1779.  
  1780.  
  1781. pid_t id = fork();
  1782.  
  1783. if(id == 0){
  1784.  
  1785. child_pid = id;
  1786.  
  1787. torre_de_controlo();
  1788.  
  1789. exit(0);
  1790.  
  1791. }
  1792.  
  1793. else if(id == -1){
  1794.  
  1795. write_log_file("Erro na criacao da torre de controlo.");
  1796.  
  1797. exit(0);
  1798.  
  1799. }
  1800.  
  1801. else{
  1802.  
  1803. pthread_create(&thread_time,NULL,time_counter,&id);
  1804.  
  1805. le_pipe();
  1806.  
  1807.  
  1808.  
  1809.  
  1810.  
  1811. //pthread_join(thread_time,NULL);
  1812.  
  1813. wait(NULL);
  1814.  
  1815. exit(0);
  1816.  
  1817. }
  1818.  
  1819.  
  1820.  
  1821.  
  1822.  
  1823. return 0;
  1824.  
  1825. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement