📄 mm1smlb.c
字号:
/* External definitions for single-server queueing system using simlib. */
#include "simlib.h" /* Required for use of simlib.c. */
#define EVENT_ARRIVAL 1 /* Event type for arrival. */
#define EVENT_DEPARTURE 2 /* Event type for departure. */
#define LIST_QUEUE 1 /* List number for queue. */
#define LIST_SERVER 2 /* List number for server. */
#define SAMPST_DELAYS 1 /* sampst variable for delays in queue. */
#define STREAM_INTERARRIVAL 1 /* Random-number stream for interarrivals. */
#define STREAM_SERVICE 2 /* Random-number stream for service times. */
/* Declare non-simlib global variables. */
int num_custs_delayed, num_delays_required;
float mean_interarrival, mean_service;
FILE *infile, *outfile;
/* Declare non-simlib functions. */
void init_model(void);
void arrive(void);
void depart(void);
void report(void);
main() /* Main function. */
{
/* Open input and output files. */
infile = fopen("mm1smlb.in", "r");
outfile = fopen("mm1smlb.out", "w");
/* Read input parameters. */
fscanf(infile, "%f %f %d", &mean_interarrival, &mean_service,
&num_delays_required);
/* Write report heading and input parameters. */
fprintf(outfile, "Single-server queueing system using simlib\n\n");
fprintf(outfile, "Mean interarrival time%11.3f minutes\n\n",
mean_interarrival);
fprintf(outfile, "Mean service time%16.3f minutes\n\n", mean_service);
fprintf(outfile, "Number of customers%14d\n\n\n", num_delays_required);
/* Initialize simlib */
init_simlib();
/* Set maxatr = max(maximum number of attributes per record, 4) */
maxatr = 4; /* NEVER SET maxatr TO BE SMALLER THAN 4. */
/* Initialize the model. */
init_model();
/* Run the simulation while more delays are still needed. */
while (num_custs_delayed < num_delays_required) {
/* Determine the next event. */
timing();
/* Invoke the appropriate event function. */
switch (next_event_type) {
case EVENT_ARRIVAL:
arrive();
break;
case EVENT_DEPARTURE:
depart();
break;
}
}
/* Invoke the report generator and end the simulation. */
report();
fclose(infile);
fclose(outfile);
return 0;
}
void init_model(void) /* Initialization function. */
{
num_custs_delayed = 0;
event_schedule(sim_time + expon(mean_interarrival, STREAM_INTERARRIVAL),
EVENT_ARRIVAL);
}
void arrive(void) /* Arrival event function. */
{
/* Schedule next arrival. */
event_schedule(sim_time + expon(mean_interarrival, STREAM_INTERARRIVAL),
EVENT_ARRIVAL);
/* Check to see whether server is busy (i.e., list SERVER contains a
record). */
if (list_size[LIST_SERVER] == 1) {
/* Server is busy, so store time of arrival of arriving customer at end
of list LIST_QUEUE. */
transfer[1] = sim_time;
list_file(LAST, LIST_QUEUE);
}
else {
/* Server is idle, so start service on arriving customer, who has a
delay of zero. (The following statement IS necessary here.) */
sampst(0.0, SAMPST_DELAYS);
/* Increment the number of customers delayed. */
++num_custs_delayed;
/* Make server busy by filing a dummy record in list LIST_SERVER. */
list_file(FIRST, LIST_SERVER);
/* Schedule a departure (service completion). */
event_schedule(sim_time + expon(mean_service, STREAM_SERVICE),
EVENT_DEPARTURE);
}
}
void depart(void) /* Departure event function. */
{
/* Check to see whether queue is empty. */
if (list_size[LIST_QUEUE] == 0)
/* The queue is empty, so make the server idle and leave the departure
(service completion) event out of the event list. (It is currently
not in the event list, having just been removed by timing before
coming here.) */
list_remove(FIRST, LIST_SERVER);
else {
/* The queue is nonempty, so remove the first customer from the queue,
register delay, increment the number of customers delayed, and
schedule departure. */
list_remove(FIRST, LIST_QUEUE);
sampst(sim_time - transfer[1], SAMPST_DELAYS);
++num_custs_delayed;
event_schedule(sim_time + expon(mean_service, STREAM_SERVICE),
EVENT_DEPARTURE);
}
}
void report(void) /* Report generator function. */
{
/* Get and write out estimates of desired measures of performance. */
fprintf(outfile, "\nDelays in queue, in minutes:\n");
out_sampst(outfile, SAMPST_DELAYS, SAMPST_DELAYS);
fprintf(outfile, "\nQueue length (1) and server utilization (2):\n");
out_filest(outfile, LIST_QUEUE, LIST_SERVER);
fprintf(outfile, "\nTime simulation ended:%12.3f minutes\n", sim_time);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -