Simulation of Networks
Hints for Simulation Task Set 1
Task 3
This requires us to keep track of information associated with each individual packet.
This means that we need to maintain information of the individual packets in the queue,
and one way to do this is via a doubly linked list.
Here are some fragments of C code which may be useful for this:
/* definitions for the queue */
typedef struct queue_info{
int arrtime; /* Time that packet arrived at the queue */
struct queue_info *qnext; /* Pointer to next item in list */
struct queue_info *qprev; /* Pointer to previous item in list */
} QUEUE;
struct queue_info
*qhead,
*qtail;
.
.
.
void add_to_queue(void);
void remove_from_queue(void);
.
.
.
.
/**************************************************************************/
void add_to_queue(void) /* Adds a packet to the queue */
{
struct queue_info
*t;
t = NEW(QUEUE);
t->arrtime = gmt;
t->qnext = qtail;
t->qprev = qtail->qprev;
qtail->qprev->qnext = t;
qtail->qprev = t;
q += 1;
}
/**************************************************************************/
void remove_from_queue(void) /* Removes a packet from the queue */
{
struct queue_info
*x;
x = qhead->qnext; /* Delete packet from linked list */
qhead->qnext = qhead->qnext->qnext;
qhead->qnext->qprev = qhead;
free(x);
q -= 1;
}
/**************************************************************************/
Task 4
We now have more than one queue,
so one way to deal with this is to expand our event list
to keep track of both the type of event, and the node at which it occurs.
Here are some code fragments which may be useful for this:
struct event_info{
int type;
int node;
};
typedef struct schedule_info{
int time; /* Time that event occurs */
int event_type; /* Type of event */
int node; /* Node for event */
struct schedule_info *next; /* Pointer to next item in list */
} EVENTLIST;
struct schedule_info
*head,
*tail;
void act(struct event_info*); /* act() is now called as "act(&nextevent)"
- don't forget that C is call by value */;
.
.
.
.
/***************************************************/
void schedule(int time_interval, int event, int node_num)
/* Schedules an event of type
'event' at time 'time_interval' in the future
at node 'node_num" */
{
int
event_time;
struct schedule_info
*x,
*t;
event_time = gmt + time_interval;
t = NEW(EVENTLIST);
for(x=head ; x->next->timenext!=tail ; x=x->next);
t->time = event_time;
t->event_type = event;
t->node = node_num;
t->next = x->next;
x->next = t;
}
/**************************************************************************/
void act(struct event_info* nextact) /* find the next event and go to it */
{
struct schedule_info
*x;
gmt = head->next->time; /* step time forward to the next event */
nextact->type = head->next->event_type; /* Record type of this next event */
nextact->node = head->next->node;
x = head->next; /* Delete event from linked list */
head->next = head->next->next;
free(x);
}
/*************************************************************************/