#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_QUEUE_SIZE 10
#define TRUE 1
#define FALSE 0
typedef struct {
int id;
int arrival_time;
int service_time;
} element;
typedef struct {
element queue[MAX_QUEUE_SIZE];
int front, rear;
} QueueType;
QueueType queue;
// 0¿¡¼ 1»çÀÌÀÇ ½Ç¼ö ³¼ö »ý¼º ÇÔ¼ö
double random() {
return rand()/(double)RAND_MAX;
}
// ½Ã¹Ä·¹À̼ǿ¡ ÇÊ¿äÇÑ ¿©·¯°¡Áö »óÅ º¯¼ö
int duration=10; // ½Ã¹°·¹ÀÌ¼Ç ½Ã°£
double arrival_prob=0.7; // ÇϳªÀÇ ½Ã°£ ´ÜÀ§¿¡ µµÂøÇÏ´Â Æò±Õ °í°´ÀÇ ¼ö
int max_serv_time=5; // ÇϳªÀÇ °í°´¿¡ ´ëÇÑ ÃÖ´ë ¼ºñ½º ½Ã°£
int clock;
// ½Ã¹Ä·¹À̼ÇÀÇ °á°ú
int cashier;
int customers; // Àüü°í°´¼ö
int served_customers; // ¼ºñ½º¹ÞÀº °í°´¼ö
int waited_time; // °í°´µéÀÌ ±â´Ù¸° ½Ã°£
// ·£´ý ¼ýÀÚ¸¦ »ý¼ºÇÏ¿© °í°´ÀÌ µµÂøÇß´ÂÁö µµÂøÇÏÁö ¾Ê¾Ò´ÂÁö¸¦ ÆÇ´Ü
int is_customer_arrived() {
if( random() < arrival_prob )
return TRUE;
else return FALSE;
}
// °ø¹é »óÅ °ËÃâ ÇÔ¼ö
int is_empty(QueueType *q)
{
return (q->front == q->rear);
}
// Æ÷È »óÅ °ËÃâ ÇÔ¼ö
int is_full(QueueType *q)
{
return ((q->rear+1)%MAX_QUEUE_SIZE == q->front);
}
// »ðÀÔ ÇÔ¼ö
void enqueue(QueueType *q, element item)
{
if( is_full(q) )
fprintf(stderr,"Å¥°¡ Æ÷È»óÅÂÀÔ´Ï´Ù\n");
q->rear = (q->rear+1) % MAX_QUEUE_SIZE;
q->queue[q->rear] = item;
}
// »èÁ¦ ÇÔ¼ö
element dequeue(QueueType *q)
{
if( is_empty(q) )
fprintf(stderr,"Å¥°¡ °ø¹é»óÅÂÀÔ´Ï´Ù\n");
q->front = (q->front+1) % MAX_QUEUE_SIZE;
return q->queue[q->front];
}
// »õ·Î µµÂøÇÑ °í°´À» Å¥¿¡ »ðÀÔ
void insert_customer(int arrival_time) {
element customer;
customer.id = customers++;
customer.arrival_time = arrival_time;
customer.service_time=(int)(max_serv_time*random()) + 1;
enqueue(&queue, customer);
printf("[°í°´ %d] %dºÐ¿¡ µµÂø ¼ºñ½º½Ã°£Àº %dºÐ\n",
customer.id, customer.arrival_time, customer.service_time);
}
int remove_customer(int cashier) {
element customer;
int service_time=0;
//int servise_timw2=0;
if (is_empty(&queue)) return 0;
customer = dequeue(&queue);
service_time = customer.service_time-1;
served_customers++;
waited_time += clock - customer.arrival_time;
printf("\t[°í°´ %d] (ÀºÇà¿ø%d) %dºÐ¿¡ ¼ºñ½º ½ÃÀÛ ´ë±â½Ã°£Àº %dºÐ\n"
,customer.id, cashier, clock, clock - customer.arrival_time);
return service_time;
}
// Åë°èÄ¡¸¦ Ãâ·ÂÇÑ´Ù.
void print_stat() {
printf("----------------------------------------\n");
printf("¼ºñ½º¹ÞÀº °í°´¼ö = %d\n",
served_customers);
printf("Àüü ´ë±â ½Ã°£ = %dºÐ\n", waited_time);
printf("1Àδç Æò±º ´ë±â ½Ã°£ = %fºÐ\n",
(double)waited_time/served_customers);
printf("¾ÆÁ÷ ´ë±âÁßÀÎ °í°´¼ö = %d\n",
customers-served_customers);
printf("----------------------------------------\n");
}
// ½Ã¹Ä·¹ÀÌ¼Ç ÇÁ·Î±×·¥
void main()
{
int service_time1=0;
int service_time2=0;
int i;
printf("----------------------------------------\n");
printf("//1ºÐ´ç °í°´ µµÂøÀ² : %f\n",arrival_prob);
printf("//1ÀÎ ÃÖ´ë ¼ºñ½º½Ã°£ : %d\n",max_serv_time);
printf("----------------------------------------\n");
clock=0;
while(clock < duration){
clock++;
printf("(t=%d) ",clock);
if (is_customer_arrived()) {
insert_customer(clock);
}
if (service_time1 > 0)
service_time1--;
else {
service_time1 = remove_customer(1);
}
if (service_time2 >0)
service_time2--;
else
service_time2 = remove_customer(2);
}
print_stat();
}