Mudanças entre as edições de "AULA 24 - Programação 1 - Engenharia"
Ir para navegação
Ir para pesquisar
(Criou página com '<syntaxhighlight lang=c> #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++;...') |
|||
Linha 1: | Linha 1: | ||
+ | =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= | ||
+ | <syntaxhighlight lang=c> | ||
+ | #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); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Exemplo de aplicação no projeto: | ||
+ | |||
+ | <syntaxhighlight lang=c> | ||
+ | #include <stdio.h> | ||
+ | #include <time.h> | ||
+ | #include <stdlib.h> | ||
+ | |||
+ | |||
+ | typedef struct tipo_sala{ | ||
+ | char salaId[20]; | ||
+ | int hora_ent; | ||
+ | int hora_sai; | ||
+ | } TSala; | ||
+ | |||
+ | typedef struct tipo_aluno{ | ||
+ | char userId[20]; | ||
+ | TSala *pSala; | ||
+ | }TAluno; | ||
+ | |||
+ | main() | ||
+ | { | ||
+ | time_t rawtime; | ||
+ | struct tm *tminfo; | ||
+ | |||
+ | TAluno *pAluno = malloc(sizeof(TAluno)); | ||
+ | printf("Entre com o userId do usuario\n"); | ||
+ | scanf("%s", pAluno->userId ); | ||
+ | |||
+ | pAluno->pSala = malloc(sizeof(TSala)); | ||
+ | |||
+ | printf("Entre com o nome da sala\n"); | ||
+ | scanf("%s", pAluno->pSala->salaId ); | ||
+ | |||
+ | 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 */ | ||
+ | |||
+ | time ( &rawtime ); | ||
+ | tminfo = localtime ( &rawtime ); | ||
+ | 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) { | ||
+ | printf("Abrir porta\n"); | ||
+ | } else { | ||
+ | printf("Não abrir porta\n"); | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | =Execução Periódica de Handlers= | ||
+ | |||
<syntaxhighlight lang=c> | <syntaxhighlight lang=c> | ||
#include <stdio.h> | #include <stdio.h> |
Edição das 09h37min 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{
char salaId[20];
int hora_ent;
int hora_sai;
} TSala;
typedef struct tipo_aluno{
char userId[20];
TSala *pSala;
}TAluno;
main()
{
time_t rawtime;
struct tm *tminfo;
TAluno *pAluno = malloc(sizeof(TAluno));
printf("Entre com o userId do usuario\n");
scanf("%s", pAluno->userId );
pAluno->pSala = malloc(sizeof(TSala));
printf("Entre com o nome da sala\n");
scanf("%s", pAluno->pSala->salaId );
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 */
time ( &rawtime );
tminfo = localtime ( &rawtime );
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) {
printf("Abrir porta\n");
} else {
printf("Não abrir porta\n");
}
}
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);
}
}