/* There shall be one task, called lift_task, for the lift. */ void lift_task(void)//TODO { int next; int dirchange; int send_task_id; int length;//DUMMY message_data_type msg; draw_lift(mainlift); while (1) { si_message_receive((char *) &msg, &length, &send_task_id); if(msg.type == MOVE_MESSAGE) { lift_next_floor(mainlift, &next, &dirchange); lift_move(mainlift, next, dirchange); lift_has_arrived(mainlift); } else if(msg.type == TRAVEL_MESSAGE) { /* a travel message is sent to the lift task when a person would like to make a lift travel */ enter_floor(mainlift, msg.id, msg.from_floor, msg.to_floor); } draw_lift(mainlift); } }
/* MONITOR function lift_travel: performs a journey with the lift starting at from_floor, and ending at to_floor */ void lift_travel(lift_type lift, int id, int from_floor, int to_floor) { pthread_mutex_lock(&lift->mutex); enter_floor(lift, id, from_floor); // vänta på att lift has arrived cv while(1) { pthread_cond_wait(&lift->change, &lift->mutex); if(lift->floor == from_floor) { // försök att gå in i liften if (enter_lift(lift, id, from_floor, to_floor)) { draw_lift(lift); pthread_cond_broadcast(&lift->change); break; } } } // vänta på att liften har nått rätt våning while(1) { pthread_cond_wait(&lift->change, &lift->mutex); if(lift->floor == to_floor) { if (leave_lift(lift, id, to_floor)) { draw_lift(lift); pthread_cond_broadcast(&lift->change); break; } } } pthread_mutex_unlock(&lift->mutex); }
void lift_move(lift_type lift, int next_floor, int change_direction) { /* reserve lift */ pthread_mutex_lock(&lift->mutex); /* the lift is moving */ lift->moving = 1; /* release lift */ pthread_mutex_unlock(&lift->mutex); /* it takes two seconds to move to the next floor */ usleep(1000000); /* reserve lift */ pthread_mutex_lock(&lift->mutex); /* the lift is not moving */ lift->moving = 0; /* the lift has arrived at next_floor */ lift->floor = next_floor; /* check if direction shall be changed */ if (change_direction) { lift->up = !lift->up; } /* draw, since a change has occurred */ draw_lift(lift); /* release lift */ pthread_mutex_unlock(&lift->mutex); }
// This process is responsible for drawing the lift. Receives lift_type structures // as messages. void uidraw_process(void) { char msg[1024]; si_ui_set_size(670, 700); while(1) { message_receive(msg, 1024, QUEUE_UI); lift_type Lift = (lift_type) &msg[0]; draw_lift(Lift); } }
/* There shall be one task, called lift_task, for the lift. */ void lift_task(void) { int next; int dirchange; draw_lift(mainlift); while (1) { lift_next_floor(mainlift, &next, &dirchange); lift_move(mainlift, next, dirchange); lift_has_arrived(mainlift); /* För debug lift_next_floor(mainlift, &next, &dirchange); printf("|%d %d %d |", mainlift->floor, next, dirchange); lift_move(mainlift, next, dirchange); lift_next_floor(mainlift, &next, &dirchange); printf("|%d %d %d |", mainlift->floor, next, dirchange); lift_move(mainlift, next, dirchange); lift_next_floor(mainlift, &next, &dirchange); printf("|%d %d %d |", mainlift->floor, next, dirchange); lift_move(mainlift, next, dirchange); lift_next_floor(mainlift, &next, &dirchange); printf("|%d %d %d |", mainlift->floor, next, dirchange); lift_move(mainlift, next, dirchange); lift_next_floor(mainlift, &next, &dirchange); printf("|%d %d %d |", mainlift->floor, next, dirchange); lift_move(mainlift, next, dirchange); lift_next_floor(mainlift, &next, &dirchange); printf("|%d %d %d |", mainlift->floor, next, dirchange); lift_move(mainlift, next, dirchange); lift_next_floor(mainlift, &next, &dirchange); printf("|%d %d %d |", mainlift->floor, next, dirchange); lift_move(mainlift, next, dirchange); lift_next_floor(mainlift, &next, &dirchange); printf("|%d %d %d |", mainlift->floor, next, dirchange); lift_move(mainlift, next, dirchange); lift_next_floor(mainlift, &next, &dirchange); printf("|%d %d %d |", mainlift->floor, next, dirchange); lift_move(mainlift, next, dirchange); lift_next_floor(mainlift, &next, &dirchange); printf("|%d %d %d |", mainlift->floor, next, dirchange); lift_move(mainlift, next, dirchange); lift_next_floor(mainlift, &next, &dirchange); printf("|%d %d %d |", mainlift->floor, next, dirchange); lift_move(mainlift, next, dirchange); lift_has_arrived(mainlift); printf("| %d %d |", next, dirchange); exit(mainlift->floor); */ } }