Mudanças entre as edições de "AULA 24 - Programação 1 - Engenharia"

De MediaWiki do Campus São José
Ir para navegação Ir para pesquisar
Linha 26: Linha 26:
 
#include <time.h>
 
#include <time.h>
 
#include <stdlib.h>
 
#include <stdlib.h>
 
+
 
+
 
typedef struct tipo_sala{
 
typedef struct tipo_sala{
   char salaId[20];
+
   int  salaId;
 
   int  hora_ent;
 
   int  hora_ent;
 
   int  hora_sai;
 
   int  hora_sai;
 
} TSala;
 
} TSala;
 
+
typedef struct tipo_aluno{
+
    char userId[20];
 
    TSala *pSala;
 
}TAluno;
 
 
 
 
main()
 
main()
 
{
 
{
 
   time_t rawtime;  
 
   time_t rawtime;  
 
   struct tm *tminfo;  
 
   struct tm *tminfo;  
    
+
   TSala Lab1;
  TAluno *pAluno = malloc(sizeof(TAluno));
+
  printf("Entre com o userId do usuario\n");
+
   TSala *pSala;
   scanf("%s", pAluno->userId );
+
 
+
   pSala = malloc(sizeof(TSala));
   pAluno->pSala = malloc(sizeof(TSala));
+
 
+
   pSala->salaId = 5;
   printf("Entre com o nome da sala\n");
+
   pSala->hora_ent = 10;   
  scanf("%s", pAluno->pSala->salaId );  
+
   pSala->hora_sai = 12;  
   
+
   printf("Entre com a hora de entrada\n");
+
  scanf("%d", &pAluno->pSala->hora_ent );   
 
 
 
  printf("Entre com a hora de saída\n");
 
   scanf("%d", &pAluno->pSala->hora_sai );  
 
 
 
 
 
 
   /* Ler o tempo real no momento */
 
   /* Ler o tempo real no momento */
       
+
 
   time ( &rawtime );  
 
   time ( &rawtime );  
 
   tminfo = localtime ( &rawtime );  
 
   tminfo = localtime ( &rawtime );  
 
   printf ( "hora: %d minuto: %d segundo: %d \n", tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec);  
 
   printf ( "hora: %d minuto: %d segundo: %d \n", tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec);  
 
+
   if(pAluno->pSala->hora_ent <= tminfo->tm_hour && pAluno->pSala->hora_sai >= tminfo->tm_hour) {
+
   if(pSala->hora_ent <= tminfo->tm_hour && pSala->hora_sai >= tminfo->tm_hour) {
       printf("Abrir porta\n");
+
       printf("Tempo atual dentro da faixa de restrição\n");
 
   } else {
 
   } else {
       printf("Não abrir porta\n");
+
       printf("Tempo atual FORA da faixa de restrição \n");
 
   }
 
   }
 +
  free(pSala);
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Edição das 10h02min de 20 de novembro de 2014

Objetivos

  • Representando o tempo em uma forma apropriada para comparação de hora, minuto e segundo.
  • Uso de signal para execução periódica de handlers

Representando o tempo em uma forma apropriada para comparação de hora, minuto e segundo

#include <stdio.h>
#include <time.h>

main()
{

   time_t rawtime; 
	struct tm *tminfo; 

   time ( &rawtime ); 
   tminfo = localtime ( &rawtime ); 
   printf ( "hora: %d minuto: %d segundo: %d \n", tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec); 
}

Exemplo de aplicação no projeto:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
 
 
typedef struct tipo_sala{
  int  salaId;
  int  hora_ent;
  int  hora_sai;
} TSala;
 
 
main()
{
   time_t rawtime; 
   struct tm *tminfo; 
   TSala Lab1;
 
   TSala *pSala;
 
   pSala = malloc(sizeof(TSala));
 
   pSala->salaId = 5;
   pSala->hora_ent = 10;   
   pSala->hora_sai = 12; 
 
 
   /* Ler o tempo real no momento */
 
   time ( &rawtime ); 
   tminfo = localtime ( &rawtime ); 
   printf ( "hora: %d minuto: %d segundo: %d \n", tminfo->tm_hour, tminfo->tm_min, tminfo->tm_sec); 
 
   if(pSala->hora_ent <= tminfo->tm_hour && pSala->hora_sai >= tminfo->tm_hour) {
       printf("Tempo atual dentro da faixa de restrição\n");
   } else {
       printf("Tempo atual FORA da faixa de restrição \n");
   }
   free(pSala);
}


Execução Periódica de Handlers

#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <unistd.h>

static signal_recv_count;

void sigalrm_handler(int signum)
{
  
  signal_recv_count++;
}

void init_timer(int tempo)
{
  struct itimerval timer={0};
  char a[200];
  /* Initial timeout value */
  timer.it_value.tv_sec = tempo;

  /* We want a repetitive timer */
  timer.it_interval.tv_sec = tempo;

  /* Register Signal handler
   * And register for periodic timer with Kernel*/
  signal(SIGALRM, &sigalrm_handler);
  setitimer(ITIMER_REAL, &timer, NULL);
}

int main()
{
  init_timer(10);

  while(1) {
  		printf("imprimindo  :%d\n", signal_recv_count);
  		sleep(1);
  }
}