예제 #1
0
/**
  Free any memory associated with your scheduler.
 
  Assumptions:
    - This function will be the last function called in your library.
*/
void scheduler_clean_up()
{
	

	int i;

	for (i = 0; i < num_cores; i++) {

		if (core_arr[i].core_job != NULL) {

			free(core_arr[i].core_job);
		}

	}

	free(core_arr);

	pri_node_t *curr = job_queue.head;

	while (curr != NULL) {

		if (curr->data != NULL) {

			free(curr->data);

		}

		curr = curr->next;

	}

	curr = finished.head;

	while (curr != NULL) {

		if (curr->data != NULL) {
			
			free(curr->data);

		}

		curr = curr->next;
		
	}

	priqueue_destroy(&job_queue);
		
	priqueue_destroy(&finished);	


}
예제 #2
0
/**
  Free any memory associated with your scheduler.
 
  Assumptions:
    - This function will be the last function called in your library.
*/
void scheduler_clean_up()
{
    priqueue_destroy(ugh->thing);
    free(ugh->thing);
    free(ugh->corelist);
    free(ugh);
}
예제 #3
0
/**
	Free any memory associated with your scheduler.
 
	Assumptions:
	- This function will be the last function called in your library.
*/
void scheduler_clean_up()
{
	void* p = NULL;
	
	while( (p = priqueue_poll(jobs)) != NULL )
		free_job((job_t*)p);
	
	priqueue_destroy(jobs);
	free(jobs);
	free_core(&cores);
}//scheduler_clean_up
예제 #4
0
int main(int argc, char *argv[]){
  int port;
  int serversock;
  int i = 0;
  int res = 0;
  int acceptsock = -1;
  int num_requests = 0;
  int* x = NULL;
  queue_item_t* tmp = NULL;
  struct sigaction sa;
  pthread_t threads[MAX_THREADS];
  priqueue_t queue, jobs;
  tdata_t data;
  pthread_attr_t attr;
  sem_t semaphore;

  /* Signal handling */
  sa.sa_handler = sigINT_handler;
  sa.sa_flags   = 0;
  sigemptyset(&sa.sa_mask);
  sigaction(SIGINT, &sa, NULL);
  sigaction(SIGPIPE, &sa, NULL);

  /* Basic error-checking */
  if(argc < 2) {
    fprintf(stderr, "Error: No port specified.\n\tUsage: ./proxy <port>\n");
    return 1;
  }

  port = atoi(argv[1]);

  if(port < 1000 || port > 30000) {
    fprintf(stderr, "Error: Port number %d out of range. Please use a port between 1000 and 30000\n", port);
    return 1;
  }

  /* Server setup */
  serversock = newServerSocket(argv[1]);

  if(serversock == -1) {
    fprintf(stderr, "Error: Could not bind/listen to port %d\n", port);
    return 1;
  }

  /* Attributes */
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

  /* Setup synchronization mutex */
  pthread_mutex_init(&mutex, NULL);
  pthread_mutex_init(&hostcheck, NULL);
  sem_init(&semaphore, 0, 0);

  /* Implement queue for caching & jobs */
  priqueue_init(&queue, MAX_CACHE, comparer, match);
  priqueue_init(&jobs, MAX_PENDING, compare_jobs, match_jobs); /* Up to 500 waiting jobs */

  /* init threads */
  data.queue = &queue;
  data.jobs  = &jobs;
  data.sem   = &semaphore;

  for(i = 0 ; i < MAX_THREADS ; ++i)
    pthread_create(&threads[i], &attr, connection_handler, (void*)&data);

  /* The server loop */
  while(!done) {
    x = (int*)malloc(sizeof(int));
    
    if((acceptsock = acceptSocket(serversock)) == -1) { /* If an error occurs or interrupted */
      if(!done)
        fprintf(stderr, "Error: Failed to connect client.\n");
      free(x);
      continue;
    }
    
    *x = acceptsock;

    pthread_mutex_lock(&job_mutex);
    res = priqueue_insert(&jobs, (void*)x);
    pthread_mutex_unlock(&job_mutex);
    if(res == -1) {
      closeSocket(acceptsock);
      free(x); /* Lost (rejected) request */
    } else {
      sem_post(&semaphore);
    }

    acceptsock = -1;
    num_requests++;
  }
  closeSocket(serversock);

  for(i = 0 ; i < MAX_THREADS ; ++i) /* Make sure everything gets through */
    sem_post(&semaphore);

  for(i = 0 ; i < MAX_THREADS ; ++i)
    pthread_join(threads[i], NULL);

  printf("%10d total HTTP requests served.\n", num_requests);

  /* Cleanup queue elements */
  for(i = 0 ; i < MAX_CACHE ; ++i) {
    tmp = (queue_item_t*)priqueue_delete_min(&queue);
    if(tmp == NULL)
      break;
    destroyQueueItem(&tmp);
  }

  for(i = 0 ; i < MAX_PENDING ; ++i) {
    x = (int*)priqueue_delete_min(&jobs);
    if(x == NULL)
      break;
    free(x);
  }

  sem_destroy(&semaphore);
  pthread_mutex_destroy(&mutex);
  pthread_mutex_destroy(&job_mutex);
  pthread_mutex_destroy(&hostcheck);
  pthread_attr_destroy(&attr);
  priqueue_destroy(&queue);
  priqueue_destroy(&jobs);
	return 0;
}
예제 #5
0
int main()
{
	priqueue_t q, q2;

	priqueue_init(&q, compare1);
	priqueue_init(&q2, compare2);

	/* Pupulate some data... */
	int *values = malloc(100 * sizeof(int));

	int i;
	for (i = 0; i < 100; i++)
		values[i] = i;

	/* Add 5 values, 3 unique. */
	priqueue_offer(&q, &values[12]);
	priqueue_offer(&q, &values[13]);
	priqueue_offer(&q, &values[14]);
	priqueue_offer(&q, &values[12]);
	priqueue_offer(&q, &values[12]);
	printf("Total elements: %d (expected 5).\n", priqueue_size(&q));
	priqueue_print(&q);

	int val_removed = *((int *)priqueue_remove_at(&q, 4));
	printf("Element removed: %d (expected 14).\n", val_removed);
	printf("Total elements: %d (expected 4).\n", priqueue_size(&q));

	int val = *((int *)priqueue_poll(&q));
	printf("Top element: %d (expected 12).\n", val);
	printf("Total elements: %d (expected 3).\n", priqueue_size(&q));
	
	int vals_removed = priqueue_remove(&q, &values[12]);
	printf("Elements removed: %d (expected 2).\n", vals_removed);
	printf("Total elements: %d (expected 1).\n", priqueue_size(&q));
	priqueue_print(&q);

	priqueue_offer(&q, &values[10]);
	priqueue_offer(&q, &values[30]);
	priqueue_offer(&q, &values[20]);
	printf("Total elements: %d (expected 4).\n", priqueue_size(&q));

	priqueue_offer(&q2, &values[10]);
	priqueue_offer(&q2, &values[30]);
	priqueue_offer(&q2, &values[20]);
	printf("Total elements: %d (expected 3).\n", priqueue_size(&q2));

	printf("Elements in order queue (expected 10 13 20 30): ");
	for (i = 0; i < priqueue_size(&q); i++)
		printf("%d ", *((int *)priqueue_at(&q, i)) );
	printf("\n");


	printf("Elements in reverse order queue (expected 30 20 10): ");
	for (i = 0; i < priqueue_size(&q2); i++)
		printf("%d ", *((int *)priqueue_at(&q2, i)) );
	printf("\n");

	priqueue_destroy(&q2);
	priqueue_destroy(&q);
	printf("Total elements q: %d (expected 0).\n", priqueue_size(&q));
	printf("Total elements q2: %d (expected 0).\n", priqueue_size(&q2));

	free(values);

	return 0;
}
예제 #6
0
int main(int argc, char **argv)
{
    int 		i, maxnum;
    unsigned long long 	t1, t2;
    priqueue_t		*pq, *pqb;
    data_t		*d;

    if (argc < 2)
        fatal_error("Usage: %s <num elements to insert>\n", argv[0]);

    maxnum = atol(argv[1]);
    if (maxnum <= 0)
        fatal_error("Usage: %s <num elements to insert> <output file>\n", argv[0]);

    // Allocate data array
    data = (data_t*) malloc(sizeof(data_t)*maxnum);
    if (data == NULL)
        fatal_error("Unable to allocate memory\n");
    
    // Generate keys
    for (i = 0; i < maxnum; i++)
	data[i].key = i;
    
    // Create new queue
    pq = priqueue_new((cmpfunc_t)cmpfunc);
    if (pq == NULL)
	fatal_error("Unable to create new priqueue\n");
    
    // Test insert
    t1 = gettime();
    for (i = 0; i < maxnum; i++) {
	priqueue_insert(pq, &data[i].key, &data[i]);
    }
    t2 = gettime();
    printf("%d microseconds to insert %d elements\n", (int)(t2-t1), maxnum);

    // Test delmax
    t1 = gettime();
    for (i = 0; i < maxnum; i++) {
	d = priqueue_delmax(pq);
    }
    t2 = gettime();
    printf("%d microseconds to remove %d elements\n", (int)(t2-t1), maxnum);

    
    // Test join
    pqb = priqueue_new((cmpfunc_t)cmpfunc);
    if (pqb == NULL)
	fatal_error("Unable to create new priqueue\n");
    for (i = 0; i < maxnum/2; i++)
	priqueue_insert(pq, &data[i].key, &data[i]);
    for (i = maxnum/2; i < maxnum; i++)
	priqueue_insert(pqb, &data[i].key, &data[i]);
    
    t1 = gettime();
    pq = priqueue_join(pq, pqb);
    t2 = gettime();
    printf("%d microseconds to join two queues with %d elements\n", (int)(t2-t1), maxnum);
    
    
    priqueue_destroy(pq);
    printf("Priority queue destroyed\n");
    
    return 0;
}
예제 #7
0
int main()
{
	priqueue_t q, q2;

	priqueue_init(&q, compare1);
	priqueue_init(&q2, compare2);

	/* Pupulate some data... */
	int *values = malloc(100 * sizeof(int));

	int i;
	for (i = 0; i < 100; i++)
		values[i] = i;

	/* Add 5 values, 3 unique. */
	priqueue_offer(&q, &values[12]);
  	priqueue_offer(&q, &values[13]);
	priqueue_offer(&q, &values[14]);
	priqueue_offer(&q, &values[12]);
	priqueue_offer(&q, &values[12]);
        //priqueue_offer(&q, &values[15]);
	printf("Total elements: %d (expected 5).\n", priqueue_size(&q));

     	int val = *((int *)priqueue_poll(&q));
	printf("Top element: %d (expected 12).\n", val);
	printf("Total elements: %d (expected 4).\n", priqueue_size(&q));

	int val2 = *((int *)priqueue_poll(&q));
	printf("Top element: %d (expected 12).\n", val2);
	printf("Total elements: %d (expected 3).\n", priqueue_size(&q));

    int val3 = *((int *)priqueue_poll(&q));
	printf("Top element: %d (expected 12).\n", val3);
	printf("Total elements: %d (expected 2).\n", priqueue_size(&q));

	priqueue_offer(&q, &values[10]);
	priqueue_offer(&q, &values[30]);
	priqueue_offer(&q, &values[20]);
       /* priqueue_offer(&q, &values[15]);
        priqueue_offer(&q, &values[23]);
        priqueue_offer(&q, &values[31]);
	*/priqueue_offer(&q2, &values[10]);
	priqueue_offer(&q2, &values[30]);
	priqueue_offer(&q2, &values[20]);


	printf("Elements in order queue (expected 10 13 14 20 30): ");
	 while ( priqueue_size(&q) )
		printf("%d ", *((int *)priqueue_poll(&q)) );
	printf("\n");
       
          printf("Elements in order queue (expected 30 20 10): ");
          while ( priqueue_size(&q2) )
                  printf("%d ", *((int *)priqueue_poll(&q2)) );
          printf("\n");

	priqueue_destroy(&q2);
	priqueue_destroy(&q);

	free(values);
    
	return 0;
}