Projeto1617_FaseFinal/Makefile
all: simulador monitor
monitor: monitor.o
gcc -Wall -g monitor.o -o monitor -lpthread
monitor.o: monitor.c util.h unix.h
gcc -c monitor.c
simulador: simulador.o
gcc -Wall -g simulador.o -o simulador -lpthread
simulador.o: simulador.c util.h unix.h
gcc -c simulador.c
clean:
rm *.o
Projeto1617_FaseFinal/monitor.c
#include <pthread.h>
#include <string.h>
#include "unix.h"
#include "util.h"
//apontador para o ficheiro onde será escrito o relatório da simulação
FILE *relatorio;
//variáveis globais para a estatística
int num_fila_Piscina=0, num_fila_Toboga=0, num_Total_Clientes_Normais=0, num_Total_cli_Idoso_Crianca = 0, num_cli_normal=0, num_cli_Idoso_Crianca=0, desiste_espera=0, desiste_medo=0, num_utilizacoes_tbg1=0, num_utilizacoes_tbg2=0, num_utilizacoes_tbg3=0, num_utilizacoes_tbg4=0, num_utilizacoes_piscina=0, inicio_simulacao=0, fim_simulacao=0, soma_espera_normal=0, soma_espera_prio = 0, esperou_prio=0, esperou_normal=0;
//variáveis globais para controlo de execução
int corre=0, pausa=0;
void mostra_estatistica()
{
if(corre && !pausa)
printf(" 1. Estado atual: Simulacao a decorrer.\n");
else
{
if(pausa)
printf(" 1. Estado atual: Simulação em pausa.\n");
else
printf(" 1. Estado atual: Simulação terminada.\n");
}
printf(" 2. Tamanho atual da fila de espera Tobogães: %d\n", num_fila_Toboga);
printf(" 2. Tamanho atual da fila de espera Piscina: %d\n", num_fila_Piscina);
//printf(" 3. Tamanho atual da fila de espera para Clientes Prioritários: %d\n", num_fila_crianca_idoso);
//printf(" 4. Número atual de Carros: %d\n", num_carros);
printf(" 5. Clientes Normais: %d\n", num_Total_Clientes_Normais);
printf(" 6. Clientes Idosos/Criança: %d\n", num_Total_cli_Idoso_Crianca);
printf(" 7. Total de Clientes: %d\n", num_Total_Clientes_Normais + num_Total_cli_Idoso_Crianca);
printf(" 8. Desistências na Fila de espera: %d\n", desiste_espera);
printf(" 9. Desistências Por medo: %d\n", desiste_medo);
printf("10. Total de desistências: %d\n", desiste_espera + desiste_medo);
printf("11. Número de Clientes que foram ao Tobogã 1: %d\n", num_utilizacoes_tbg1);
printf("11. Número de Clientes que foram ao Tobogã 2: %d\n", num_utilizacoes_tbg2);
printf("11. Número de Clientes que foram ao Tobogã 3: %d\n", num_utilizacoes_tbg3);
printf("11. Número de Clientes que foram ao Tobogã 4: %d\n", num_utilizacoes_tbg4);
printf("11. Número de Clientes que foram á piscina: %d\n", num_utilizacoes_piscina);
if(esperou_normal)
printf("12. Tempo médio de espera de Clientes Normais: %f minutos\n",(float)soma_espera_normal/(float)esperou_normal);
else
printf("12. Tempo médio de espera de Clientes Normais: 0 minutos\n");
if(esperou_prio)
printf("13. Tempo médio de espera de Clientes Prioritários: %f minutos\n",(float)(soma_espera_prio/esperou_prio));
else
printf("13. Tempo médio de espera de Clientes Prioritários: 0 minutos\n");
}
//função que fica à escuta das mensagens do simulador
void *escuta_.
2. inicio_simulacao=0, fim_simulacao=0, soma_espera_normal=0,
soma_espera_prio = 0, esperou_prio=0, esperou_normal=0;
//variáveis globais para controlo de execução
int corre=0, pausa=0;
void mostra_estatistica()
{
if(corre && !pausa)
printf(" 1. Estado atual: Simulacao a decorrer.n");
else
{
if(pausa)
printf(" 1. Estado atual: Simulação em
pausa.n");
else
printf(" 1. Estado atual: Simulação
terminada.n");
}
printf(" 2. Tamanho atual da fila de espera Tobogães:
%dn", num_fila_Toboga);
printf(" 2. Tamanho atual da fila de espera Piscina: %dn",
num_fila_Piscina);
//printf(" 3. Tamanho atual da fila de espera para Clientes
Prioritários: %dn", num_fila_crianca_idoso);
//printf(" 4. Número atual de Carros: %dn", num_carros);
printf(" 5. Clientes Normais: %dn",
num_Total_Clientes_Normais);
printf(" 6. Clientes Idosos/Criança: %dn",
num_Total_cli_Idoso_Crianca);
printf(" 7. Total de Clientes: %dn",
num_Total_Clientes_Normais + num_Total_cli_Idoso_Crianca);
printf(" 8. Desistências na Fila de espera: %dn",
desiste_espera);
printf(" 9. Desistências Por medo: %dn", desiste_medo);
3. printf("10. Total de desistências: %dn", desiste_espera +
desiste_medo);
printf("11. Número de Clientes que foram ao Tobogã 1:
%dn", num_utilizacoes_tbg1);
printf("11. Número de Clientes que foram ao Tobogã 2:
%dn", num_utilizacoes_tbg2);
printf("11. Número de Clientes que foram ao Tobogã 3:
%dn", num_utilizacoes_tbg3);
printf("11. Número de Clientes que foram ao Tobogã 4:
%dn", num_utilizacoes_tbg4);
printf("11. Número de Clientes que foram á piscina:
%dn", num_utilizacoes_piscina);
if(esperou_normal)
printf("12. Tempo médio de espera de Clientes
Normais: %f
minutosn",(float)soma_espera_normal/(float)esperou_normal);
else
printf("12. Tempo médio de espera de Clientes
Normais: 0 minutosn");
if(esperou_prio)
printf("13. Tempo médio de espera de Clientes
Prioritários: %f
minutosn",(float)(soma_espera_prio/esperou_prio));
else
printf("13. Tempo médio de espera de Clientes
Prioritários: 0 minutosn");
}
//função que fica à escuta das mensagens do simulador
void *escuta_comunicacao(void *arg)
{
int sockfd=*((int *) arg);
int msg, tempo=0, num_comandos, id, tipo_cliente;
char buffer[256],comando[20],
atracao[20];//,utilizador[5],timestamp[11],;
10. NUMERO_TOBOGAS=4
CAPACIDADE_UM_TOBOGA=1
Projeto1617_FaseFinal/simulador.c
/****************************************************
*****************************************************
***
Sistemas Operativos - Projeto Pratico 2016/2017 - Simulacao de
um Parque Aquático
Docentes: Eduardo Marques, Luís Gaspar
Alunos:
*****************************************************
*****************************************************
**/
#include <time.h>
#include <stdlib.h>
#include <semaphore.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include "unix.h"
#include "util.h"
/****************************************************
*****************************************************
***
* variaveis globais
* descricao: podem ser acedidas em qualquer momento e
local durante a execucao do simulador
*****************************************************
11. *****************************************************
**/
sem_t zona_embarqueToboga_prio;
sem_t zona_embarqueToboga;
sem_t lugares_Piscina;
sem_t tobogas_livres;
sem_t sai_atracao;
sem_t filaEsperaPiscina;
sem_t sai_piscina;
sem_t controla_toboga;
sem_t pode_entrar_toboga;
pthread_mutex_t mutex;
int num_cliente, num_toboga_ocupados, fila_prio,
carros_em_servico, num_toboga;
int tempoEntra;
int nr_toboga;
char atracao [20];
time_t start;
struct sockaddr_un serv_addr;
int sockfd, servlen;
//variaveis com informacao da configuracao
int INICIO_FINAL, TEMPO_SIMULACAO,
TEMPO_MEDIO_CHEGADA_CLIENTES,
PROB_DESISTE_ESPERA, PROB_DESISTE_ESPERA_PRIO,
PROB_DESISTE_MEDO, PROB_DESISTE_MEDO_PRIO,
TEMPO_VIAGEM, NUMERO_TOBOGAS,
CAPACIDADE_UM_TOBOGA;
int corre=0,pausa=0,controlo_pausa;
12. char printA[20] = "Toboga 1";
char printB[20] = "Toboga 2";
char printC[20] = "Toboga 3";
char printD[20] = "Toboga 4";
char printE[20] = "Piscina";
/****************************************************
*****************************************************
***
* funcao: tarefa_cliente_IdosoCrianca
* argumentos: ponteiro do tipo void
* devolve: ponteiro do tipo void
* descricao: comportamento de uma thread que representa
um cliente idoso ou criança
*****************************************************
*****************************************************
**/
void *tarefa_cliente_IdosoCrianca(void *ptr)
{
pthread_detach(pthread_self()); //detach para devolver os
recursos ao sistema -- pthread_self() retorna o id da tarefa
int g, entra_espera;
char ImprimeAtracao[20];
time_t temp_i;
int tem_esp = rand()%10; // entre 0 e 10 minutos de espera
char buffer_c[256];
sem_wait(&filaEsperaPiscina); //Se tiver lugar na fila de
espera entra nesta, senão não faz nada
pthread_mutex_lock(&mutex); //só uma tarefa é que pode
entrar aqui, reserva os recuros - mais ngm pode aceder
int num_cli_Idoso_Crianca = num_cliente++;
fila_prio++;
printf("O cliente %d chegou a fila de espera da piscina.n",
num_cli_Idoso_Crianca);
13. sprintf(buffer_c, "%d CHEGADA_PISCINA %d 1n",
(int)time(0), num_cli_Idoso_Crianca);
send(sockfd,buffer_c,sizeof(buffer_c),0);
temp_i = time(0);
if((int)(time(0) - temp_i) > tem_esp)
{
g = rand()%100;
if(g <= PROB_DESISTE_ESPERA_PRIO)
{
if(tem_esp == 1)
printf("O cliente %d desistiu da fila
de espera da piscina ao fim de %d minuto a espera.n",
num_cli_Idoso_Crianca, tem_esp);
else
printf("O cliente %d desistiu da fila de
espera da piscina ao fim de %d minutos a espera.n",
num_cli_Idoso_Crianca, tem_esp);
sprintf(buffer_c, "%d DESISTE_FILA_PISCINA
%d 1n", (int)time(0), num_cli_Idoso_Crianca);
send(sockfd,buffer_c,sizeof(buffer_c),0);
fila_prio--;
sem_post(&filaEsperaPiscina);
pthread_mutex_unlock(&mutex);
return NULL;
}
}
//desistencia
g = rand()%100;
if(g <= PROB_DESISTE_MEDO_PRIO)
{
printf("O cliente %d desistiu porque ficou com
14. medo.n", num_cli_Idoso_Crianca);
sprintf(buffer_c, "%d DESISTE_EMBARQUEn",
(int)time(0));
send(sockfd,buffer_c,sizeof(buffer_c),0);
sem_post(&filaEsperaPiscina);
pthread_mutex_unlock(&mutex);
return NULL;
}
pthread_mutex_unlock(&mutex); //liberta os recursos, o
cliente não desistiu da fila de espera - quer entrar
sem_wait(&lugares_Piscina); //vericar se existe ou não
lugares disponíveis na piscian (50)
sem_post(&filaEsperaPiscina); //se existir sai da fila de
espera da piscina e entra nesta
pthread_mutex_lock(&mutex);
// strcpy(ImprimeAtracao, atracao);
printf("O cliente %d entrou na piscina.n",
num_cli_Idoso_Crianca);
sprintf(buffer_c, "%d EMBARQUE_PISCINA %d %dn",
(int)(time(0)-temp_i), num_cli_Idoso_Crianca);
send(sockfd,buffer_c,sizeof(buffer_c),0);
pthread_mutex_unlock(&mutex);
sem_wait(&sai_piscina); //semáforo para ver se o cliente
quer ou não sair da piscina. Se não quiser fica aqui à espera
pthread_mutex_lock(&mutex);
printf("O cliente %d saiu da piscina.n",
num_cli_Idoso_Crianca);
sprintf(buffer_c, "%d DESEMBARQUE %d %dn",
(int)time(0), num_cli_Idoso_Crianca);
send(sockfd,buffer_c,sizeof(buffer_c),0);
sem_post(&lugares_Piscina);
15. pthread_mutex_unlock(&mutex);
return NULL;
}
/****************************************************
*****************************************************
***
* funcao: tarefa_cliente
* argumentos: ponteiro do tipo void
* devolve: ponteiro do tipo void
* descricao: comportamento de uma thread que representa
um cliente nao prioritario
*****************************************************
*****************************************************
**/
void *tarefa_cliente(void *ptr)
{
pthread_detach(pthread_self());
int g;
char ImprimeAtracao[20];
time_t temp_i;
char buffer_c[256];
int tempo_piscina = rand()%100;
int tem_esp = rand()%10; // entre 0 e 10 minutos de espera
int prob_ir_atracao = rand()%100;
//para tratar da saida do cliente da piscina, assinala que ele
quer sair da piscina
if (tempo_piscina>=1 && tempo_piscina <= 15){
sem_post(&sai_piscina);
}else{
if(prob_ir_atracao<= 80){
16. pthread_mutex_lock(&mutex);
int num_cli = num_cliente++;
printf("O cliente %d chegou a fila de espera dos
tobogãs.n", num_cli);
sprintf(buffer_c, "%d CHEGADA_TOBOGA %d 0n",
(int)time(0), num_cli);
send(sockfd,buffer_c,sizeof(buffer_c),0);
pthread_mutex_unlock(&mutex);
sem_wait(&zona_embarqueToboga);
pthread_mutex_lock(&mutex);
//desistencia por tempo
if((int)(time(0) - temp_i) > tem_esp)
{
g = rand()%100;
if(g <= PROB_DESISTE_ESPERA)
{
if(tem_esp == 1)
printf("O cliente %d desistiu da fila de
espera dos tobogãs ao fim de %d minuto a espera.n", num_cli,
tem_esp);
else
printf("O cliente %d desistiu da fila de
espera dos tobogãs ao fim de %d minutos a espera.n", num_cli,
tem_esp);
sprintf(buffer_c, "%d
DESISTE_FILA_TOBOGA %d 0n", (int)time(0), num_cli);
send(sockfd,buffer_c,sizeof(buffer_c),0);
sem_post(&zona_embarqueToboga);
pthread_mutex_unlock(&mutex);
17. return NULL;
}
}
printf("O cliente %d entrou na zona de embarque dos
tobogãs.n",num_cli);
sprintf(buffer_c, "%d ENTRA_EMBARQUE %d 0n",
time(0), num_cli);
send(sockfd,buffer_c,sizeof(buffer_c),0);
//desistencia por medo
g = rand()%100;
if(g <= PROB_DESISTE_MEDO)
{
printf("O cliente %d desistiu da fila de espera dos
tobogãs porque ficou com medo.n", num_cli);
sprintf(buffer_c, "%d DESISTE_EMBARQUEn",
(int)time(0));
send(sockfd,buffer_c,sizeof(buffer_c),0);
sem_post(&zona_embarqueToboga);
pthread_mutex_unlock(&mutex);
return NULL;
}
pthread_mutex_unlock(&mutex);
sem_wait(&tobogas_livres);
sem_post(&zona_embarqueToboga); //assinala que ficou
um lugar vago na zona de embarque dos tobogas
pthread_mutex_lock(&mutex);
// if(nr_toboga >=0 && nr_toboga<4)
// nr_toboga++;
// strcpy(ImprimeAtracao, atracao);
printf("O cliente %d entrou no tobogã %d.n", num_cli,
18. nr_toboga); //O cliente entrou no tobogã X
sprintf(buffer_c, "%d EMBARQUE %d %dn",
(int)(time(0)-temp_i), num_cli, nr_toboga);
send(sockfd,buffer_c,sizeof(buffer_c),0);
num_toboga_ocupados++;
pthread_mutex_unlock(&mutex);
sem_wait(&sai_atracao); //para já ver se todos sairam
pthread_mutex_lock(&mutex);
printf("O cliente %d saiu da zona de tobogãs.n",
num_cli);
sprintf(buffer_c, "%d DESEMBARQUE %d %dn",
(int)time(0), num_cli, nr_toboga);
send(sockfd,buffer_c,sizeof(buffer_c),0);
pthread_mutex_unlock(&mutex);
}
else{
sem_wait(&filaEsperaPiscina); //Se tiver lugar na fila
de espera entra nesta, senão não faz nada
pthread_mutex_lock(&mutex); //só uma tarefa é que pode
entrar aqui, reserva os recuros - mais ngm pode aceder
int num_cli_Idoso_Crianca = num_cliente++;
fila_prio++;
printf("O cliente %d chegou a fila de espera da piscina.n",
num_cli_Idoso_Crianca);
sprintf(buffer_c, "%d CHEGADA_PISCINA %d 1n",
(int)time(0), num_cli_Idoso_Crianca);
send(sockfd,buffer_c,sizeof(buffer_c),0);
//temp_i = time(0);
if((int)(time(0) - temp_i) > tem_esp)
{
g = rand()%100;
19. if(g <= PROB_DESISTE_ESPERA_PRIO)
{
if(tem_esp == 1)
printf("O cliente %d desistiu da fila
de espera da piscina ao fim de %d minuto a espera.n",
num_cli_Idoso_Crianca, tem_esp);
else
printf("O cliente %d desistiu da fila de
espera da piscina ao fim de %d minutos a espera.n",
num_cli_Idoso_Crianca, tem_esp);
sprintf(buffer_c, "%d DESISTE_FILA_PISCINA
%d 1n", (int)time(0), num_cli_Idoso_Crianca);
send(sockfd,buffer_c,sizeof(buffer_c),0);
fila_prio--;
sem_post(&filaEsperaPiscina);
pthread_mutex_unlock(&mutex);
return NULL;
}
}
//desistencia
g = rand()%100;
if(g <= PROB_DESISTE_MEDO_PRIO)
{
printf("O cliente %d desistiu porque ficou com
medo.n", num_cli_Idoso_Crianca);
sprintf(buffer_c, "%d DESISTE_EMBARQUEn",
(int)time(0));
send(sockfd,buffer_c,sizeof(buffer_c),0);
sem_post(&filaEsperaPiscina);
pthread_mutex_unlock(&mutex);
return NULL;
}
20. pthread_mutex_unlock(&mutex); //liberta os recursos, o
cliente não desistiu da fila de espera - quer entrar
sem_wait(&lugares_Piscina); //vericar se existe ou não
lugares disponíveis na piscian (50)
sem_post(&filaEsperaPiscina); //se existir sai da fila de
espera da piscina e entra nesta
pthread_mutex_lock(&mutex);
// strcpy(ImprimeAtracao, atracao);
printf("O cliente %d entrou na piscina.n",
num_cli_Idoso_Crianca);
sprintf(buffer_c, "%d EMBARQUE_PISCINA %d %dn",
(int)(time(0)-temp_i), num_cli_Idoso_Crianca);
send(sockfd,buffer_c,sizeof(buffer_c),0);
pthread_mutex_unlock(&mutex);
sem_wait(&sai_piscina); //semáforo para ver se o cliente
quer ou não sair da piscina. Se não quiser fica aqui à espera
pthread_mutex_lock(&mutex);
printf("O cliente %d saiu da piscina.n",
num_cli_Idoso_Crianca);
sprintf(buffer_c, "%d DESEMBARQUE %d %dn",
(int)time(0), num_cli_Idoso_Crianca);
send(sockfd,buffer_c,sizeof(buffer_c),0);
sem_post(&lugares_Piscina);
pthread_mutex_unlock(&mutex);
return NULL;
}
}
return NULL;
}
/****************************************************
*****************************************************
***
21. * funcao: tarefa_atracao
* argumentos: ponteiro do tipo void
* devolve: ponteiro do tipo void
* descricao: comportamento de uma thread que representa
um cliente prioritario
*****************************************************
*****************************************************
**/
void *tarefa_atracao(void *ptr)
{
pthread_detach(pthread_self());
int i;
char printAtracao[20];
char buffer_c[256];
time_t hora_partida, hora_i;
char variavel[20] = "atracao";
nr_toboga ++;
void inicia_viagem()
{
printf("O tobogã %d começou.n", nr_toboga);
sprintf(buffer_c, "%d ARRANQUE %dn",
(int)time(0), nr_toboga);
send(sockfd,buffer_c,sizeof(buffer_c),0);
hora_partida = time(0);
pthread_mutex_unlock(&mutex);
while((int)(time(0) - hora_partida) <
TEMPO_VIAGEM); // tempo de viagem
pthread_mutex_lock(&mutex);
printf("O tobogã %d terminou e está disponível aos
22. proximos utilizadores.n", nr_toboga);
sprintf(buffer_c, "%d FINAL_VIAGEM %dn",
(int)time(0), nr_toboga);
send(sockfd,buffer_c,sizeof(buffer_c),0);
sem_post(&sai_atracao); //diz que todos os
clientes sairam (1)
num_toboga_ocupados= 0;
// for(i=0;i<NUMERO_TOBOGAS; i++){
// sem_post(&tobogas_livres); //indica que
os tobogãs vão estar vazios
// }
nr_toboga = 0;
}
while(1)
{
pthread_mutex_lock(&mutex);
printf("O tobogã %d está disponível.n",
nr_toboga);
sprintf(buffer_c, "%d DISPONIVEL %dn",
(int)time(0), nr_toboga);
send(sockfd,buffer_c,sizeof(buffer_c),0);
sem_post(&tobogas_livres);
pthread_mutex_unlock(&mutex);
int i;
//sem_getvalue(&zona_embarqueToboga, &i);
23. hora_i = time(0);
if(time(0) > start + TEMPO_SIMULACAO)
{
usleep(100000);
pthread_mutex_lock(&mutex);
if(num_toboga_ocupados == 0)
{
printf("O parque ja fechou e nao tem
clientes em espera.n");
printf("O tobogã %d vai ser limpo.n",
nr_toboga);
carros_em_servico--;
pthread_mutex_unlock(&mutex);
return NULL;
}
printf("O tobogã %d comecou.n", nr_toboga);
sprintf(buffer_c, "%d ARRANQUE %dn",
(int)time(0), nr_toboga);
send(sockfd,buffer_c,sizeof(buffer_c),0);
hora_partida = time(0);
pthread_mutex_unlock(&mutex);
while((int)(time(0) - hora_partida) <
TEMPO_VIAGEM); // tempo de viagem
pthread_mutex_lock(&mutex);
printf("O tobogã %d fechou.n", nr_toboga);
sprintf(buffer_c, "%d FINAL_VIAGEM %dn",
(int)time(0), nr_toboga);
send(sockfd,buffer_c,sizeof(buffer_c),0);
for(i = 0; i < NUMERO_TOBOGAS; i++)
24. sem_post(&sai_atracao);
}
else
{
while(num_toboga_ocupados <
NUMERO_TOBOGAS && time(0) < start +
TEMPO_SIMULACAO); //enquanto todos os tobogãs não
estiverem ocupados não vai iniciar a sua atividade
//é incrementado na tarefa clienteNormal
pthread_mutex_lock(&mutex);
if(num_toboga_ocupados ==
NUMERO_TOBOGAS) //se todos os tobogãs estiverem
atualmente ocupados, vai iniciar a sua atividade (de todos)
{
pthread_mutex_unlock(&mutex);
usleep(100000);
pthread_mutex_lock(&mutex);
inicia_viagem();
}
else
{
pthread_mutex_unlock(&mutex);
usleep(100000);
pthread_mutex_lock(&mutex);
if(num_toboga_ocupados == 0)
{
printf("O parque ja fechou e nao
tem clientes em espera.n");
printf("O tobogã %d vai ser
arrumado.n", nr_toboga);
carros_em_servico--;
pthread_mutex_unlock(&mutex);
return NULL;
25. }
inicia_viagem();
}
}
pthread_mutex_unlock(&mutex);
}
return NULL;
}
//Funcao que trata dos pedidos vindos do Monitor
void *recebe_comandos_monitor(void *arg)
{
struct sockaddr_un cli_addr;
int done, n, id;
int sockfd=*((int *) arg), clilen=sizeof(cli_addr);
char buffer[256];
//Ciclo que fica a espera dos pedidos dos Monitor, para lhe
dar resposta adequada
while(1)
{
done=0;
if((n=recv(sockfd, buffer, sizeof(buffer), 0)) <= 0)
{
if(n < 0)
perror("recv error");
done=1;
}
buffer[n]='0';
if(!strcmp(buffer, "terminan"))
{
corre=0;
26. exit(1);
}
else
{
if(!strcmp(buffer, "inicion"))
corre = 1;
if(!strcmp(buffer, "pausan"))
pausa = 1;
if(!strcmp(buffer, "retomarn"))
pausa = 0;
}
}
return NULL;
}
int main(int argc, char *argv[])
{
srand(time(NULL));
num_cliente=1;
//atracao = 1;
num_toboga_ocupados = 0;
fila_prio = 0;
if(argc<2)
{
printf("Falta o ficheiro de configuração. Não é
possível continuar.");
return 1;
}
else
{
//interpretacao do ficheiro de configuraçao
27. int *conf = leitura_configuracao(argv[1]);
INICIO_FINAL = conf[0];
TEMPO_SIMULACAO =
conf[1];
TEMPO_MEDIO_CHEGADA_CLIENTES =
conf[2];
PROB_DESISTE_ESPERA =
conf[3];
PROB_DESISTE_ESPERA_PRIO = conf[4];
PROB_DESISTE_MEDO = conf[5];
PROB_DESISTE_MEDO_PRIO =
conf[6];
TEMPO_VIAGEM = conf[7];
NUMERO_TOBOGAS = conf[8];
CAPACIDADE_UM_TOBOGA
= conf[9];
// Inicializar semáforos
sem_init (&zona_embarqueToboga, 0, 10);
sem_init (&pode_entrar_toboga, 0, 0);
sem_init (&lugares_Piscina, 0, 50);
sem_init (&sai_atracao, 0, 0);
sem_init (&tobogas_livres, 0, 0); //semáforo para
indicar se os tobogãs estão (0) ou não vazios (1)
sem_init (&filaEsperaPiscina,0,10);
sem_init (&sai_piscina,0,0);
sem_init (&controla_toboga,0,1);
//criacao do socket e ligação
if((sockfd=socket(AF_UNIX, SOCK_STREAM, 0)) <
0)
perror("Simulador: cant open socket stream");
serv_addr.sun_family=AF_UNIX;
strcpy(serv_addr.sun_path, UNIXSTR_PATH);
28. servlen=strlen(serv_addr.sun_path)+sizeof(serv_addr.sun_f
amily);
if(connect(sockfd, (struct sockaddr *) &serv_addr,
servlen) < 0)
perror("connect error");
//Criacao da tarefa que ira tratar dos pedidos enviados
pelo Monitor
pthread_t thread;
pthread_create(&thread, NULL,
&recebe_comandos_monitor, &sockfd);
while(!corre);
int i, r, h;
char buffer[256];
carros_em_servico = CAPACIDADE_UM_TOBOGA;
start = time(0);
sprintf(buffer, "%d INICIOn", (int)start);
send(sockfd,buffer,sizeof(buffer),0);
TEMPO_SIMULACAO *= 60;
//thread atracao
for(i = 0; i < 4; i++)
{
num_toboga=1;
pthread_create(&thread, NULL,
&tarefa_atracao, &sockfd);
//usleep(10000);
}
while((int)(time(0) - start) < TEMPO_SIMULACAO)
{
29. r = rand()%100;
if (r <= 50)
{
//Criacao da tarefa de um cliente prioritário
pthread_create(&thread, NULL,
&tarefa_cliente_IdosoCrianca, &sockfd);
}
else
{
//Criacao da tarefa de um cliente normal
pthread_create(&thread, NULL,
&tarefa_cliente, &sockfd);
}
r = rand()%1;
h = rand()%3;
h = h * 100000;
if(r == 0)
h = 0 - h;
usleep((TEMPO_MEDIO_CHEGADA_CLIENTES*100000
0) + h);
if(pausa)
{
pthread_mutex_lock(&mutex);
controlo_pausa = 1;
}
while(pausa);
if(controlo_pausa)
{
pthread_mutex_unlock(&mutex);
controlo_pausa = 0;
}
}
30. while(carros_em_servico>0);
printf("nO parque já fechou e não existem mais
clientes em espera.n");
printf("nSimulação terminada.n");
sprintf(buffer,"%d FIM n",(int) time(0));
send(sockfd,buffer,sizeof(buffer),0);
while(corre);
close(sockfd);
}
return 0;
}
Projeto1617_FaseFinal/unix.h
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#define UNIXSTR_PATH "./s.unixstr"
#define UNIXDG_PATH "/tmp/s.unixdgx"
#define UNIXDG_TMP "/tmp/dgXXXXsdsXX"
Projeto1617_FaseFinal/util.h
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
31. #include <time.h>
//#define MAXLINE 512
int *leitura_configuracao(char *file)
{
int *a = (int *)malloc(sizeof(int) * 10);
FILE *fp = fopen (file, "r");
if(fp == NULL )
{
printf("Nao e possivel abrir o ficheiro de
configuracao.n");
abort();
}
int num, i=0;
char name[100], buff[500];
while (fgets( buff, sizeof buff, fp) != NULL)
{
32. if(sscanf(buff, "%[^=]=%d", name, &num) == 2)
a[i++]=num;
}
fclose(fp);
return a;
}
Running head: ATHLETICS SUPREME COMPANY
1
ATHLETICS SUPREME COMPANY
2
Athletics supreme company
Name
Institution
Overview of athletics supreme
Athletics supreme has majored in providing equipments
designed for use by the amateur athlete. The company produces
quality and safe equipments even if the consumers’ are
amatures.this equipments prove to express devolution in the
amateur respective sports which has enabled athletic supreme
33. capture the market share through its way of branding when it
comes to amateur adult team sport. In most cases, a large
market opportunity share is neglected when it comes to
adolescent sport which is the very core of athletic supreme and
ventured into the manufacture of safety helmets for the kids and
other smaller equipments used by kids only which has lead the
company to building of brands which are entertaining while
providing safety to the users (Smith, Watts, & Dagneau, 2015).
For companies like puma and adidas for instance these brands
were sold primarily in departmental stores but now its evident
that outlet malls provide sportswear to companies and even
creation of competition is upcoming and from different
companies and even stores.
Market Research Strategies
When it comes to market, the company needs to analyze and
compare brands selling and products that these companies are
selling. The company also needs to find that consumer needs
that is not met and also identify gaps in order for it to further
understand the market and placement of the pricing strategies. It
also needs to perform qualitative and quantitative examinations
of the target market details.sttrategies needed to pull data from
all available sources to further identify their strong competitors
and also data analysis will help athletic supreme company to
locate new small companies emerging and reviews of their
profit and losses to use the data to the companies advantage to
gain insight not only into market potential but also in terms of
the consumer wants too.unsuccessfull and successfulness trials
in competition allows the athletic supreme to further identify
what they want their sportswear and brands to convey to any
customer keeping in mind that the customer needs and wants are
not necessarily the same (LeRoy, 2015).
Product Strategies
Athletics supreme company being already an established
manufacturer of new sportswear lines needed and safe sport
34. gears to be marketed and create a new link between safety and
attire, advertisement could accomplish this but it seems
complicated since the company is emerging in adolescent sport
arena. Its best to markets these attires which are similar to the
big name brands kids associate and this is done through the
colour and design creation that is appealing to the youthful eye
in the solid market. Mature adult sport simply needs to display
new attires and gears they are already familiar with which will
be able to be used easily with no complications in the event of
use or competition. Safety being a much harder area when it
comes to trust, attire is based on design demand in maintaining
the companies safety quality through attire purchase which
eventually becomes the second habit of the customer (Smith,
Watts, & Dagneau, 2015).
Place Strategies
Being that the athletics supreme company unwillingness to
compromise its core base, the gear and quality of safety remains
in their original state with no interference the company’s
product line should not be located togherether instead they
should be broken down into smaller gear facilities which will be
the premier supplier of attires while the main facility will be
able to accommodate any additional order without
compromising quality through unnecessary shifting from one
facility to the other in search of accommodation location.
Maintaining of staff at the new attire facility will enable a
structure and historical knowledge base. The company needs
additional software components to its ordering system in order
to allow gears and attires to be ordered and shipped on time and
in their complete state (LeRoy, 2015).
Pricing Strategies
In order for athletics supreme company to promote the new
attire lines, it should include sample dialogues in the
equipments orders shipped including giving the customers
discounts on the attires purchased initially even though the
35. equipment prices will need to be left at their original price so as
to reflect the unchanged product (Smith, Watts, & Dagneau,
2015). Customers who are likely to combine products and bring
customers need to be given discounts as a way of appreciation.
Pricing should be competitive with similar companies but
should not exceed the competition price mark set up by the
Deutsche Bahn (DB) caps.
since caps are all money-makers mechanisms, the products
needs to be prised accordingly with no problem since no
companies products wants to be victims when it comes to
prizing. Products cannot be under prized since they have the
potential to make profit to the company. In order to get the
consumers attention, sleeved shirts should be at least or just
below the competitors pricing in order to give athletics supreme
company a slight advantage. Through capitalizing on the
companies sport equipments and its safety in the larger market
like the baseball for instance is the leading market and the
company should gain attention through offering lower priced
shirts and suggest complimenting caps (LeRoy, 2015).
Promotional Strategies
With regards to advancement, the organization needs to utilize a
specialist to decide the necessities of the encompassing markets
and how they can best address these issues with advancement as
a forceful deal limited time procedure. Through people group
mindfulness and writing the organization can run battles on
radios stations to concentrate on the regular games furthermore
take the items to the general population enthusiastically as a
method for advancing the organization and additionally the
client. This ought to be done to illuminate the clients of the new
market items and their constrained ecological effects. Being that
most adolescent are presently via web-based networking media,
the organization can utilize this to advance the organization's
items through posting them via web-based networking media
furthermore commercials through televisions, posters, flyers,
newspapers and magazines.
36. Competitive advantage
Games finest necessities to accumulate knowledge in rivalry
with the goal for them to have the capacity to contend as
required. They initially need to recognize their objective and be
inventive in discovering the opposition sections they are in,
what the clients purchase from the contenders, what advantages
are there in rivalry, and any data they can discover on
evaluating, administrations offered, rivalry, and limited time
procedures. This should effortlessly be possible through going
to the stores or locales, converse with the contenders’ clients
and merchants to get data and utilize them further bolstering
your good fortune (Smith, Watts, and Dagneau, 2015).
Being that games incomparable has an officially settled market
it likewise needs to put measures to guarantee that it remains in
the market and even progress globally. In order to build up its
clothing market all around, the organization needs to
comprehend the market conduct by doing research in the areas
in inquiries and set up work systems which are probably going
to work in those local markets and in addition the worldwide
markets which have a tendency to contrast much of the time.
This could be as far as framework which may influence the
generation of items and shipment which has its focal points and
burdens.
Conclusion
It is important to keep an eye on what the people like to wear or
what keeps the customers comfortable. Being a producer you
need to keep more emphasis on the product you are producing
because the product may be eye catching but to tight to fit the
intended customer or even to big to fit the responsible party’s
when it comes to attire one should now the condition which is
likely to make the customer buy and wear the companies
products. Research is a key factor when it comes to decision
making at the last stages before the production process starts.
37. References
Smith, P. B., Watts, N. A., & Dagneau, F. O. (2015). U.S.
Patent No. 9,174,111. Washington, DC: U.S. Patent and
Trademark Office.
LeRoy, M. H. (2015). Courts and the Future of Athletic Labour
in College Sports. Ariz. L. Rev., 57, 475.