next up previous contents
Next: Bibliography Up: LYDIAN: User's Guide Previous: Entry Points   Contents

The source code of algorithm Broadcast with ACK

/*******************************************************************
brdcast_ack.c: Source code of algorithm Broadcast with ACK

Design: 3 states {SLEEPING, WAITING, DONE} and
2 messages {BRD, ACK}
Network topology: connected graph with only one initiator
******************************************************************/
typedef struct {
int parent;
LIST ack;
}REGISTERS;

REGISTERS *REG;

reg_alloc() {
REG = (REGISTERS*)malloc(processes*sizeof(REGISTERS));
}

init() {
int i;

for( i=0; i<processes; i++) {
REG[i].parent = -1;
init_list(REG[i].ack);
}
}

sleeping_brd() {
MESSAGE *mess;
int i;

if( PCB[me].adjacents == 1) {
mess = create_message();
mess->kind = ACK;
send_to( mess, CURMESS->port);
new_state = DONE;
}
else {
REG[me].parent = CURMESS->port;
for( i=0; i<PCB[me].adjacents; i++) {
if( i != REG[me].parent) {
insert_list( i, REG[me].ack);
mess = create_message();
mess->kind = BRD;
send_to( mess, i);
}
}
new_state = WAITING;
}
}

waiting_brd() { /* the same for done_brd()*/
MESSAGE *mess;

mess = create_message();
mess->kind = ACK;
send_to( mess, CURMESS->port);
}

waiting_ack() {
MESSAGE *mess;

delete_list( CURMESS->port, REG[me].ack);
if( empty_list( REG[me].ack) == TRUE) {
if( REG[me].parent == -1) { /*initiator*/
new_state = DONE;
simul_end();
return;
}
else { /*send ACK to its parent */
mess = create_message();
mess->kind = ACK;
send_to( mess, REG[me].parent);
new_state = DONE;
}
}
}

start() {
MESSAGE *mess;
int i;

if( PCB[me].adjacents == 0) {
simul_end();
return;
}
for( i=0; i<PCB[me].adjacents; i++) {
insert_list( i, REG[me].ack);
mess = create_message();
mess->kind = BRD;
send_to( mess, i);
}
new_state = WAITING;
}



Ha Hoai Phuong
2002-11-11