예제 #1
0
	void TestDataSource::initTrain(int train_id, const stop_arr_t& stops)
	{
		if (stops.size() < 1) {
			return;
		}
		for (unsigned int i = 0; i < stops.size() - 1; i++) {
			createTrain(train_id, stops[i].first, stops[i + 1].first, stops[i].second, stops[i + 1].second);
		}
	}
예제 #2
0
int main ( int argc, char *argv[] )
{
  int		trainCount = 0;
  char 		*filename = NULL;
  pthread_t	*tids;
  int		i;


  /* Parse the arguments */
  if ( argc < 2 )
  {
    printf ("Usage: part1 n {filename}\n\t\tn is number of trains\n");
    printf ("\t\tfilename is input file to use (optional)\n");
    exit(0);
  }

  if ( argc >= 2 )
  {
    trainCount = atoi(argv[1]);
  }
  if ( argc == 3 )
  {
    filename = argv[2];
  }	

  initTrain(filename);
  tids = (pthread_t *) malloc(sizeof(pthread_t)*trainCount);

  for (i=0;i<trainCount;i++)
  {
    TrainInfo *info = createTrain();

    if ( pthread_create (&tids[i],0, Train, (void *)info) != 0 )
    {
      printf ("Failed creation of Train.\n");
      exit(0);
    }
  }

  /*
   * This code waits for all train threads to terminate
   */
  for (i=0;i<trainCount;i++)
  {
    pthread_join (tids[i], NULL);
  }

  free(tids);
  free(East);
  free(West);
  return 0;
}
예제 #3
0
int main(int argc, char *argv[])
{
	if(argc < 2)
	{
		perror("Run executable as <executable-path> <probability>\n");
		exit(1);
	}
	srand(time(NULL));
	key_t key;
	if ((key = ftok("manager.c", 'J')) == -1)
	{
        perror("ftok");
        exit(1);
    }
    if ((sid = initsem(key, 6)) == -1)
    {
        perror("initsem");
        exit(1);
    }
    int i, n, j;
    float cur, p;
    p = atof(argv[1]);
    fp1 = fopen("sequence.txt", "r");
    fp2 = fopen("matrix.txt", "w");

    fgets(trains, MAX_TRAINS, fp1);
    n = strlen(trains);
    fprintf(fp2, "\t\t\t\t\tSemaphores\n\t\tNorth\tWest\tSouth\tEast\n");
    for(i=0;i<n;i++)
    	fprintf(fp2, "\t\t0\t0\t0\t0\n\n");
    fclose(fp1);
    fclose(fp2);

    A = (int **)malloc(n*sizeof(int *));
    for(i=0;i<n;i++)
    	A[i] = (int *)malloc(4*sizeof(int));
	
	for(i=0;i<n;)
    {
    	cur = (rand()%101)/100;
    	if(cur > p)
    	{
    		createTrain(i, n);
    		i++;
    	}
    	else
    	{
    		if(checkDeadlock(n)==1)
    		{
    			printf("............................\nSystem Deadlocked\n");
    			for(j=0;j<i;j++)
    				kill(tr_pr[j], SIGKILL);
    			exit(1);
    		}
    	}
    }
    while(1)
    {
    	sleep(1);
    	if(checkDeadlock(n)==1)
		{
			printf("............................\nSystem Deadlocked\n");
			for(j=0;j<i;j++)
				kill(tr_pr[j], SIGKILL);
			exit(1);
		}
    }
    return 0;
}
예제 #4
0
	void TestDataSource::initTrains()
	{
		switch (test) {
		case 1:
			// Tests basic train switch cost logic. Expected result is to ride train 1 all the way to station 600, 
			// even though some of the route appears to be faster if we switch train.
			// run with -t 1 100 10:00:00 600
			initTrain(3, stop_arr_t{
				  makeStop(500, "12:01:00"), makeStop(600, "12:30:00")
		});
			initTrain(1, stop_arr_t{
				makeStop(100, "10:00:00"), makeStop(200, "10:30:00"), makeStop(300, "11:00:00"), makeStop(400, "11:30:00"), makeStop(500, "12:00:00"), makeStop(600, "12:30:00")
			});
			initTrain(2, stop_arr_t{
				makeStop(200, "10:31:00"), makeStop(400, "10:32:00")
			});
			break;
		case 2:
			// Basic shortest-path test, expected result is to ride train 2 from 100 to 400 and then go back to 300 using train 3
			// run with -t 2 100 10:00:00 300
			initTrain(1, stop_arr_t{
				  makeStop(100, "10:00:00"), makeStop(200, "10:30:00"), makeStop(300, "11:00:00"), makeStop(400, "11:30:00")
		});
			initTrain(2, stop_arr_t{
				makeStop(100, "10:00:00"), makeStop(400, "10:30:00")
			});
			initTrain(3, stop_arr_t{
				makeStop(400, "10:30:00"), makeStop(300, "10:40:00")
			});
			break;
		case 3:
			// Test train switch minimization in a more complicated case. Expected result is to only use train 1.
			// run with -t 3 100 10:00:00 400
			initTrain(1, stop_arr_t{
				  makeStop(100, "10:10:00"), makeStop(200, "10:30:00"), makeStop(300, "11:00:00"), makeStop(400, "11:30:00")
		});
			initTrain(2, stop_arr_t{
				makeStop(100, "10:00:00"), makeStop(300, "10:30:00")
			});
			break;
		case 4:
			// Test trains with WAIT_ON_TRAIN and alt-route finding
			// expected result is to use train 2 and then switch to 3
			// run with -t 4 100 10:00 300
			createTrain(1, getStationById(100), getStationById(200), Utils::parseTime("10:00"), Utils::parseTime("10:20"));
			createTrain(1, getStationById(200), getStationById(300), Utils::parseTime("10:30"), Utils::parseTime("11:00"));
			
			createTrain(2, getStationById(100), getStationById(200), Utils::parseTime("10:10"), Utils::parseTime("10:20"));
			
			createTrain(3, getStationById(200), getStationById(300), Utils::parseTime("10:30"), Utils::parseTime("10:40"));
			break;
		case 5:
			// Test trains with WAIT_ON_TRAIN
			// expected result is to use train 1 only in train switching mode, train 2 then 1 in delayed leaving
			// run with -t 5 100 10:00 300
			createTrain(1, getStationById(100), getStationById(200), Utils::parseTime("10:00"), Utils::parseTime("10:20"));
			createTrain(1, getStationById(200), getStationById(300), Utils::parseTime("10:30"), Utils::parseTime("11:00"));

			createTrain(2, getStationById(100), getStationById(200), Utils::parseTime("10:10"), Utils::parseTime("10:20"));

			createTrain(3, getStationById(200), getStationById(300), Utils::parseTime("10:30"), Utils::parseTime("11:00"));
			break;
		default:
			throw HaException("Test case not implemented", HaException::UNIMPLEMENTED_ERROR);
		}
	}
int main ( int argc, char *argv[] )
{
	int		trainCount = 0;
	char 		*filename = NULL;
	pthread_t	*tids;
	int		i;

	
	/* Parse the arguments */
	if ( argc < 2 )
	{
		printf ("Usage: part1 n {filename}\n\t\tn is number of trains\n");
		printf ("\t\tfilename is input file to use (optional)\n");
		exit(0);
	}
	
	if ( argc >= 2 )
	{
		trainCount = atoi(argv[1]);
		westQueue.head = malloc(sizeof(TrainInfo*)*trainCount);
		westQueue.size = 0;
		westQueue.consecutiveTrainsCrossed = 0;
		eastQueue.head = malloc(sizeof(TrainInfo*)*trainCount);
		eastQueue.size = 0;
		eastQueue.consecutiveTrainsCrossed = 0;
	}
	if ( argc == 3 )
	{
		filename = argv[2];
	}	

	if (pthread_mutex_init(&queueLock, NULL) != 0)
    {
        printf("\nInit mutex queueLock failed\n");
        return 1;
    }
	if (pthread_mutex_init(&bridgeLock, NULL) != 0)
    {
        printf("\nInit mutex bridgeLock failed\n");
        return 1;
    }
	if (pthread_cond_init(&synchronizeCond, NULL) != 0)
    {
        printf("\nInit condition synchronizeCond failed\n");
        return 1;
    }
	initTrain(filename, trainCount);
	
	/*
	 * Since the number of trains to simulate is specified on the command
	 * line, we need to malloc space to store the thread ids of each train
	 * thread.
	 */
	tids = (pthread_t *) malloc(sizeof(pthread_t)*trainCount);
	
	/*
	 * Create all the train threads pass them the information about
	 * length and direction as a TrainInfo structure
	 */

	for (i=0;i<trainCount;i++)
	{
		TrainInfo *info = createTrain();

		printf ("Train %2d headed %s length is %d\n", info->trainId,
			(info->direction == DIRECTION_WEST ? "West" : "East"),
			info->length );

		if ( pthread_create (&tids[i],0, Train, (void *)info) != 0 )
		{
			printf ("Failed creation of Train.\n");
			exit(0);
		}
	}

	/*
	 * This code waits for all train threads to terminate
	 */
	for (i=0;i<trainCount;i++)
	{
		pthread_join (tids[i], NULL);
	}
	pthread_mutex_destroy(&queueLock);
	pthread_mutex_destroy(&bridgeLock);
	pthread_cond_destroy(&synchronizeCond);

	free(tids);
	return 0;
}