Beispiel #1
0
int main(void)
{
    /* initialise kernel */ 
    si_kernel_init(); 
	
	/* initialise message handling */ 
    si_message_init(); 
	
	/* set up random number generator */
	srand(12345);
    
    /* initialise UI channel */ 
    si_ui_init(); 

    /* set size of GUI window */ 
    si_ui_set_size(670, 700); 

    /* initialise variables */         
    mainlift=lift_create();

    /* create tasks */ 

    si_task_create(lift_task, &Lift_Stack[STACK_SIZE-1], 20); 

    si_task_create(user_task, &User_Stack[STACK_SIZE-1], 15);
 
    /* start the kernel */ 
    si_kernel_start(); 

    /* will never be here! */ 
    return 0; 
}
Beispiel #2
0
int main(int argc, char **argv)
{
  
  //si_ui_init();
	debug_init();
	init_random();
	Lift = lift_create();
	sem_init(&mutex,0,0);
        // Create tasks as appropriate here
	/* create tasks */ 
	//pthread_t user_thread_handle;
	pthread_t lift_thread_handle;
	//pthread_create(&user_thread_handle, NULL, user_thread, 0);
	pthread_create(&lift_thread_handle, NULL, lift_thread, 0);

		
	int i;
	for(i = 0; i < MAX_N_PERSONS; i++){
	  pthread_t handle;
	  pthread_create(&handle, NULL, passenger_thread, (void*) &i);
	  
	  sem_wait(&mutex);
	  
	  // Set the real-time priority of the current thread to 5
	  // If you want to set the priority of another thread, specify the
	  // appropriate pthread_t instead of the pthread_self() function.
	  struct sched_param p;
	  if(i == 2){
	    p.sched_priority = 10;
	  }else{
	    p.sched_priority = 1;
	  }
	  if(pthread_setschedparam(handle, SCHED_RR, &p) != 0){
	    perror("Could not set the thread priority");
	  }
	  
	  
	  pthread_detach(handle); // Ensure resources are reclaimed appropriately
	}
	
	
	pthread_join(lift_thread_handle, NULL);
	//pthread_join(user_thread_handle, NULL);
	
	
	return 0;
}
Beispiel #3
0
int main(int argc, char **argv)
{
	init_random();
	Lift = lift_create();


        // Create tasks as appropriate here
	pthread_t user_thread_handle;
	pthread_t lift_thread_handle;
	
	pthread_create(&user_thread_handle, NULL, user_thread, 0);
	pthread_create(&lift_thread_handle, NULL, lift_thread, 0);
	
	pthread_join(user_thread_handle, NULL);
	pthread_join(lift_thread_handle, NULL);
	

	return 0;
}
Beispiel #4
0
static void lift_process(void) {
    lift_type Lift;
    Lift = lift_create();
    int change_direction, next_floor, tmp_id, tmp_to_floor;
    struct long_time_msg done_m;
    done_m.type = LIFT_TRAVEL_DONE;
    char msgbuf[4096];
    int found = 0;

    int from_floor_list[MAX_N_PERSONS][MAX_TRIP_LEN]; // Contains the from_floors for trips
    int to_floor_list[MAX_N_PERSONS][MAX_TRIP_LEN];   // Contains the to_floors for trips
    int iterations[MAX_N_PERSONS];                    // Which iteration is the persons on
    int trip_len[MAX_N_PERSONS];                      // Trip length for the persons
    struct timeval starttime[MAX_N_PERSONS][MAX_TRIP_LEN];    // Keeps track of timing of each persons trips
    long long int timediff[MAX_N_PERSONS][MAX_TRIP_LEN];
    struct timeval tmp_time;

    while(1) {
        int i;
        int j;
        int id_array[MAX_N_PERSONS * N_FLOORS];
        //struct lift_msg reply;
        struct lift_msg *m;
        struct long_lift_msg *long_m;
        int count, tmp_id;
        message_send((char *) Lift, sizeof(*Lift), QUEUE_UI, 0); // Draw the lift

        int len = message_receive(msgbuf, 4096, QUEUE_LIFT); // Wait for a message
        if(len < sizeof(struct lift_msg)) {
            fprintf(stderr, "Message too short\n");
            continue;
        }

        if (len == sizeof(struct lift_msg)) {
            m = (struct lift_msg *) msgbuf;
            // We now that this is a LIFT_MOVE msg
            /* Check if someone wants to leave at this floor */
            for (i = 0; i < MAX_N_PASSENGERS; i++) {
                if (Lift->passengers_in_lift[i].id != NO_ID &&
                    Lift->passengers_in_lift[i].to_floor == Lift->floor) {
                    tmp_id = Lift->passengers_in_lift[i].id;
                    leave_lift(Lift,
                               tmp_id,
                               Lift->passengers_in_lift[i].to_floor);

                    // Calculate elapsed time
                    gettimeofday(&tmp_time, NULL);
                    timediff[tmp_id][iterations[tmp_id]] =
                        (tmp_time.tv_sec*1000000ULL + tmp_time.tv_usec) -
                        (starttime[tmp_id][iterations[tmp_id]].tv_sec*1000000ULL +
                         starttime[tmp_id][iterations[tmp_id]].tv_usec);

                    //                    printf("Person %d finished an iteration in %lld \n", tmp_id, timediff[tmp_id][iterations[tmp_id]]);

                    //printf("Person %d leave lift.\n", tmp_id);
                    if (iterations[tmp_id] < trip_len[tmp_id] - 1) {
                        iterations[tmp_id]++;
                        // Start timing for that person
                        gettimeofday(&starttime[tmp_id][iterations[tmp_id]], NULL);
                        enter_floor(Lift,
                                    tmp_id,
                                    from_floor_list[tmp_id][iterations[tmp_id]],
                                    to_floor_list[tmp_id][iterations[tmp_id]]);
                        //  printf("Person %d enter floor %d.\n", tmp_id,
                        //from_floor_list[tmp_id][iterations[tmp_id]]);
                    } else {
                        // Send done message with elapsed time info
                        // printf("Sending done message to person %d.\n", tmp_id);
                        //printf("Sent timediff: ");
                        done_m.person_id = tmp_id;

                        // fill message with info, person knows how many iterations he made

                        for (i=0; i < trip_len[tmp_id]; i++) {
                            done_m.timediff[i] = timediff[tmp_id][i];
                            //  printf("%lld, ", done_m.timediff[i]);
                        }
                        //                        printf("\n");
                        message_send((char *) &done_m,
                                     sizeof(done_m),
                                     QUEUE_FIRSTPERSON + tmp_id,
                                     0);
                        //                        printf("Message sent to person %d\n", tmp_id);
                    }
                }
            }

            /* Check if someone at the floor wants to enter the lift */
            for (i = 0; i < MAX_N_PERSONS; i++) {
                tmp_id = Lift->persons_to_enter[Lift->floor][i].id;
                tmp_to_floor = Lift->persons_to_enter[Lift->floor][i].to_floor;
                if (tmp_id != NO_ID) {
                    if (!enter_lift(Lift,
                                    Lift->persons_to_enter[Lift->floor][i].id,
                                    Lift->floor,
                                    Lift->persons_to_enter[Lift->floor][i].to_floor)) {
                        break;
                    }
                }
            }

            change_direction = !Lift->floor || Lift->floor == N_FLOORS-1;
            next_floor = change_direction ^ Lift->up ? Lift->floor-1 : Lift->floor+1;

            Lift->moving = 0;
            /* check if direction shall be changed */
            if (change_direction) {
                Lift->up = !Lift->up;
            }
            Lift->floor = next_floor;
            Lift->moving = 1;
        } else if (len == sizeof(struct long_lift_msg)) {
            // We know that this is a LIFT_TRAVEL message
            // Update the trip list for the person that sent the message.
            long_m = (struct long_lift_msg *) msgbuf;
            tmp_id = long_m->person_id;
            trip_len[tmp_id] = long_m->trip_len;
            //            printf("Received a long message from %d.\n Floors:", tmp_id);
            for (i=0; i < long_m->trip_len; i++) {
                to_floor_list[tmp_id][i] = long_m->to_floor[i];
                from_floor_list[tmp_id][i] = long_m->from_floor[i];
                //                printf("(%lld, %lld), ", from_floor_list[tmp_id][i], to_floor_list[tmp_id][i]);
            }
            //printf("\n");

            iterations[tmp_id] = 0;
            // Start timing for that person
            gettimeofday(&starttime[tmp_id][iterations[tmp_id]], NULL);

            // Make person enter lift
            enter_floor(Lift, tmp_id, from_floor_list[tmp_id][0], to_floor_list[tmp_id][0]);


        }
    }

    return;
}
Beispiel #5
0
static void lift_process(void)
{
        lift_type Lift;
	Lift = lift_create();
	int change_direction, next_floor;
	int temp_id;
	int temp_to_floor;
	int to_floors_all[MAX_N_PERSONS][NUMBER_MESSAGES];
	int from_floors_all[MAX_N_PERSONS][NUMBER_MESSAGES];
	int person_message_iterator[MAX_N_PERSONS];
	char msgbuf[4096];
	while(1){
		int i;
		struct lift_msg reply;
		struct lift_msg *m;
		//message_send((char *) Lift, sizeof(*Lift), QUEUE_UI,0); // Draw the lift
		int len = message_receive(msgbuf, 4096, QUEUE_LIFT); // Wait for a message
		if(len < sizeof(struct lift_msg)){
			fprintf(stderr, "Message too short\n");
			continue;
		}
		
		m = (struct lift_msg *) msgbuf;
		switch(m->type){
		case LIFT_MOVE:
		  
		  for (i = 0; i < MAX_N_PASSENGERS; i ++){
		    if (Lift->passengers_in_lift[i].id != NO_ID){
		      if(Lift->passengers_in_lift[i].to_floor == Lift->floor){
			temp_id = Lift->passengers_in_lift[i].id;
			Lift->passengers_in_lift[i].id = NO_ID;
			Lift->passengers_in_lift[i].to_floor = NO_FLOOR;
			
			if(person_message_iterator[temp_id] < NUMBER_MESSAGES){
			  enter_floor(Lift,temp_id,from_floors_all[temp_id][person_message_iterator[temp_id]],to_floors_all[temp_id][person_message_iterator[temp_id]]);
			  //message_send((char *) Lift, sizeof(*Lift), QUEUE_UI,0); // Draw the lift
			  person_message_iterator[temp_id]++;
			}else{
			  
			  reply.type = LIFT_TRAVEL_DONE;
			  
			  //message_send((char *) Lift, sizeof(*Lift), QUEUE_UI,0); // Draw the lift
			message_send(&reply, sizeof(reply), QUEUE_FIRSTPERSON + temp_id, 0);
			}
		      }
		    }
		  }

		  /* Check if passengers want to enter the lift */
		  for (i = 0; i < MAX_N_PERSONS; i ++){
		      temp_id = Lift->persons_to_enter[Lift->floor][i].id;
		      temp_to_floor = Lift->persons_to_enter[Lift->floor][i].to_floor;
		      if ((temp_id != NO_ID) && n_passengers_in_lift(Lift) < MAX_N_PASSENGERS){
			leave_floor(Lift, temp_id, Lift->floor); 
			enter_lift(Lift, temp_id, temp_to_floor);
			//message_send((char *) Lift, sizeof(*Lift), QUEUE_UI,0); // Draw the lift
		      }
		    }
		  
		  lift_next_floor(Lift, &next_floor, &change_direction);
		  lift_move(Lift, next_floor, change_direction);
		  break;
		case LIFT_TRAVEL:
                  
		  for(i = 0; i < NUMBER_MESSAGES; i++){
		    to_floors_all[m->person_id][i] = m->to_floor[i];
		    from_floors_all[m->person_id][i] = m->from_floor[i];
		  }
		  person_message_iterator[m->person_id] = 1;
		  enter_floor(Lift, m->person_id, m->from_floor[0], m->to_floor[0]);
		  //message_send((char *) Lift, sizeof(*Lift), QUEUE_UI,0); // Draw the lift
		  
		  break;
		}
	}
	return;
}
Beispiel #6
0
static void lift_process(void)
{
  lift_type Lift;
  Lift = lift_create();
  int change_direction, next_floor;	
  char msgbuf[4096];
  struct lift_msg travel_list[MAX_N_PERSONS];

  while(1){
    int i;

    struct lift_msg reply;
    struct lift_msg *m;
    int len = message_receive(msgbuf, 4096, QUEUE_LIFT); // Wait for a message
    if(len < sizeof(struct lift_msg)){
      fprintf(stderr, "Message too short\n");
      continue;
    }
		
    m = (struct lift_msg *) msgbuf;
    
    for(i = 0; i < MAX_N_PASSENGERS; i++){
      if (Lift->passengers_in_lift[i].to_floor == Lift->floor)
	{
	  int id = Lift->passengers_in_lift[i].id;
	  //fprintf(stderr,"ID is: %d\n",id);
	  (travel_list[id].trip)++;
	  //fprintf(stderr,"Trip is: %d\n", travel_list[id].trip);
					//message_send((char *) &reply, sizeof(reply), QUEUE_FIRSTPERSON + Lift->passengers_in_lift[i].id ,0);
					/*for(j = 0; j < MAX_N_PASSENGERS; j++){
						fprintf(stderr, "before removing passengers_in_lift[%d].id = %d\n", j, Lift->passengers_in_lift[j].id);
					}*/
    					Lift->passengers_in_lift[i].id = NO_ID; 
    					Lift->passengers_in_lift[i].to_floor = NO_FLOOR;
					if(travel_list[id].trip == PLANNED_TRIPS){
						message_send((char *) &reply, sizeof(reply), QUEUE_FIRSTPERSON + id ,0);
						//travel_list[id].trip = 0;
						//fprintf(stderr,"inne i skicka meddelande\n");
						/*for(j = 0; j < MAX_N_PASSENGERS; j++){
							fprintf(stderr, "passengers_in_lift[%d].id = %d\n", j, Lift->passengers_in_lift[j].id);
						}*/

					}
					else if(travel_list[id].trip > PLANNED_TRIPS){
						//fprintf(stderr, "IASFHFHKUEFHKSFH\n");
						/*for(j = 0; j < MAX_N_PASSENGERS; j++){
							fprintf(stderr, "passengers_in_lift[%d].id = %d\n", j, Lift->passengers_in_lift[j].id);
						}*/
						exit(1);
					}
					else{
						enter_floor(Lift, id, travel_list[id].from_floor[travel_list[id].trip], travel_list[id].to_floor[travel_list[id].trip]);
						//fprintf(stderr,"next trip here\n");
					}
					
        			}
    			}
			

			//    Check if passengers want to enter elevator
                        //        Remove the passenger from the floor and into the elevator
			for(i = 0; i < MAX_N_PERSONS; i++){
				person_data_type person = Lift->persons_to_enter[Lift->floor][i];
        			if (person.id != NO_ID && (n_passengers_in_lift(Lift) < MAX_N_PASSENGERS))
        			{
					//fprintf(stderr,"Going to enter lift\n");
					leave_floor(Lift, person.id, Lift->floor);
					enter_lift(Lift, person.id, person.to_floor);
        			}
    			}
			//    Move the lift


			lift_next_floor(Lift, &next_floor, &change_direction);
			lift_move(Lift, next_floor, change_direction);

			break;
		case LIFT_TRAVEL:
                        // TODO:
                        //    Update the Lift structure so that the person with the given ID  is now present on the floor
			travel_list[m->person_id]=*m;
			enter_floor(Lift, m->person_id, m->from_floor[0], m->to_floor[0]);

			break;
		default:
			fprintf(stderr, "Error: unkown message type sent!!!!! \n");
		break;
		}
		
	}
Beispiel #7
0
static void lift_process(void) {
    lift_type Lift;
	Lift = lift_create();
	int change_direction, next_floor, tmp_id;
	struct lift_msg done_m;
    done_m.type = LIFT_TRAVEL_DONE;
	char msgbuf[4096];

	while(1) {
		int i;
		struct lift_msg reply;
		struct lift_msg *m;
		message_send((char *) Lift, sizeof(*Lift), QUEUE_UI, 0); // Draw the lift
		int len = message_receive(msgbuf, 4096, QUEUE_LIFT); // Wait for a message
		if(len < sizeof(struct lift_msg)) {
			fprintf(stderr, "Message too short\n");
			continue;
		}
		
		m = (struct lift_msg *) msgbuf;
		switch(m->type){
		case LIFT_MOVE:
			// TODO: 
			//    Check if passengers want to leave elevator
            //        Remove the passenger from the elevator
            //        Send a LIFT_TRAVEL_DONE for each passenger that leaves
            //        the elevator
			//    Check if passengers want to enter elevator
            //        Remove the passenger from the floor and into the elevator
			//    Move the Lift‰
            for (i = 0; i < MAX_N_PASSENGERS; i++) {
                if (Lift->passengers_in_lift[i].id != NO_ID &&
                    Lift->passengers_in_lift[i].to_floor == Lift->floor) {
                    tmp_id = Lift->passengers_in_lift[i].id;
                    leave_lift(Lift,
                               tmp_id,
                               Lift->passengers_in_lift[i].to_floor);

                    message_send((char *) &done_m,
                                 sizeof(done_m),
                                 QUEUE_FIRSTPERSON + tmp_id,
                                 0);
                }
            }

            for (i = 0; i < MAX_N_PERSONS; i++) {
                if (Lift->persons_to_enter[Lift->floor][i].id != NO_ID) {
                    if (!enter_lift(Lift,
                                    Lift->persons_to_enter[Lift->floor][i].id,
                                    Lift->floor,
                                    Lift->persons_to_enter[Lift->floor][i].to_floor)) {
                        break;
                    }
                }
            }

            change_direction = !Lift->floor || Lift->floor == N_FLOORS-1;
            next_floor = change_direction ^ Lift->up ? Lift->floor-1 : Lift->floor+1;
            
            Lift->moving = 0;
            /* check if direction shall be changed */
            if (change_direction) {
                    Lift->up = !Lift->up;
                }            
            Lift->floor = next_floor;
            Lift->moving = 1;
			break;
		case LIFT_TRAVEL:
            // TODO:
            // Update the Lift structure so that the person with the given ID  is now present on the floor
            enter_floor(Lift, m->person_id, m->from_floor, m->to_floor);
			break;
		}
	}
	return;
}