Example #1
0
void main(int argc, char **argv) {
    struct priority_queue *p;
    int i, tmp1, tmp2;
    
    p = pq_new(20);

    for (i = 0; i < 20; i++) {
        tmp1 = rand() % 100;
        tmp2 = rand() % 100;

        printf("Inserting %d from %d\n",tmp1, tmp2);
        pq_insert(p, tmp1, tmp2);
    }

    for (i = 0; i < 20; i++) {
        pq_extract(p, &tmp1, &tmp2);
        printf("Valor: %d from %d\n", tmp1, tmp2);
    }
    pq_free(p);

    p = pq_new(3);
    pq_insert(p, -13, 0);
    pq_insert(p, -12, 0);
    pq_insert(p, -9, 0);
    pq_extract(p, &tmp1, &tmp2);
    printf("Valor: %d from %d\n", tmp1, tmp2);
    pq_extract(p, &tmp1, &tmp2);
    printf("Valor: %d from %d\n", tmp1, tmp2);
    pq_extract(p, &tmp1, &tmp2);
        printf("Valor: %d from %d\n", tmp1, tmp2);

}
Example #2
0
int test_allocation() {
  process_queue* pq;
  pq_allocate(&pq);
  if (pq == NULL ||
 	pq->head != NULL ||
	pq->tail != NULL)
	return 0;
  pq_free(&pq);
  if (pq != NULL) return 0; //set to NULL after deallocation for scheduler
  return 1;
}
Example #3
0
int main() {
    PQ_PTR pq = pq_create(10, 0);

    pq_insert(pq, 0, 5);
    pq_insert(pq, 1, 10);
    pq_insert(pq, 2, -3);
    pq_insert(pq, 3, 2);
    pq_insert(pq, 4, 20);
    pq_insert(pq, 5, -80);
    pq_insert(pq, 6, -40);
    pq_insert(pq, 7, 33);
    pq_insert(pq, 8, 120);
    pq_insert(pq, 9, 7);

    printf("size of queue: %d\n", pq_size(pq));

    double p;
    int id;

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_remove_by_id(pq, 2);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_delete_top(pq, &id, &p);
    printf("first in queue: %d, %f\n", id, p);

    pq_free(pq);

    return 0;
}
Example #4
0
int main(int argc, char **argv) {
    unsigned int exp_universe, big, exp_n, n, n_1, i, j, m, repeats, alpha;
    struct priority_queue *pq, *pq1, *pq2;
    unsigned int *elems;
    unsigned int idx;
    struct timespec before, after;

    if (argc < 6) {
        printf("Usage : run_once exp_universe exp_n percentage filename repetitions big\n");
        printf("Example : run_once 11 5 16 test_file 2 1 will run a test with:\n \t2^16 integers in the range [0,...2^11 - 1]\n\tOne of the structures will hold approximately 16 percent of the elements; the remaining amount will be in the other.\n\tThe test will be repeated (without any change in the input integers) 2 times.\n\tThe test is big so times will be measured in ms.\n");
        exit(1);
    }

    big = atoi(argv[6]);
    exp_universe = atoi(argv[1]); 
    exp_n = atoi(argv[2]); 
    alpha = atoi(argv[3]); 
    repeats = atoi(argv[5]);
    n = 1 << exp_n;

    n_1 = (alpha * n) / 100; 

    elems = gen_instance(argv[4], n, exp_universe);


    for (m = 0; m < repeats; m++) {

    pq1 = pq_new(n_1, exp_universe);
    pq2 = pq_new(n - n_1, exp_universe);


    for (i = 0; i < n_1; i++) {
        pq_insert(pq1, elems[i]);
    }
    for (; i < n; i++) {
        pq_insert(pq2, elems[i]);
    }

    clock_gettime(CLOCK_MONOTONIC, &before);
    pq = pq_merge(pq1, pq2);
    clock_gettime(CLOCK_MONOTONIC, &after);

    if (big) {
        printf("merge: %lldms\n", timespec_diff_ms(after, before));
    } else {
        printf("merge: %lldns\n", timespec_diff_ns(after, before));
    }

    pq_free(pq);
    }
    free(elems);
}
/**
 * Compute the union of 'number' bitmaps using a heap. This can
 * sometimes be faster than roaring_bitmap_or_many which uses
 * a naive algorithm. Caller is responsible for freeing the
 * result.
 */
roaring_bitmap_t *roaring_bitmap_or_many_heap(uint32_t number,
		const roaring_bitmap_t **x) {
	if (number == 0) {
		return roaring_bitmap_create();
	}
	if (number == 1) {
		return roaring_bitmap_copy(x[0]);
	}
	roaring_pq_t *pq = create_pq(x, number);
	while (pq->size > 1) {
		roaring_pq_element_t x1 = pq_poll(pq);
		roaring_pq_element_t x2 = pq_poll(pq);

		if (x1.is_temporary && x2.is_temporary) {
                        roaring_bitmap_t *newb =lazy_or_from_lazy_inputs(x1.bitmap,
					x2.bitmap);
			uint64_t bsize = roaring_bitmap_portable_size_in_bytes(newb);
			roaring_pq_element_t newelement = { .size = bsize, .is_temporary =
					true, .bitmap = newb };
			pq_add(pq, &newelement);
		} else if (x2.is_temporary) {
                        roaring_bitmap_lazy_or_inplace(x2.bitmap, x1.bitmap);
			x2.size = roaring_bitmap_portable_size_in_bytes(x2.bitmap);
			pq_add(pq, &x2);
		} else if (x1.is_temporary) {
			roaring_bitmap_lazy_or_inplace(x1.bitmap, x2.bitmap);
			x1.size = roaring_bitmap_portable_size_in_bytes(x1.bitmap);

			pq_add(pq, &x1);
		} else {
		  roaring_bitmap_t *newb = roaring_bitmap_lazy_or(x1.bitmap,
					x2.bitmap);
			uint64_t bsize = roaring_bitmap_portable_size_in_bytes(newb);
			roaring_pq_element_t newelement = { .size = bsize, .is_temporary =
					true, .bitmap = newb };

			pq_add(pq, &newelement);
		}

	}
	roaring_pq_element_t X = pq_poll(pq);
	roaring_bitmap_t *answer = X.bitmap;
	roaring_bitmap_repair_after_lazy(answer);
	pq_free(pq);
	return answer;
}
Example #6
0
// run the simulation
void run_sim() {
    
    if (FEL == NULL)
        return;
    
    // as long as the priority queue is not empty
    // remove its top element, and execute its callback
    // on the event data
    while (simtime <= 365) {
        SimEvent *se = (SimEvent *) pq_pop(FEL);
        simtime = se->timestamp;
        callback(se->data);
        free(se);
    }
    
    // free the pq
    pq_free(FEL);
    FEL = NULL;
    
}
Example #7
0
File: pqueue.c Project: fdotli/libu
int pq_create (size_t nitems, pq_t **ppq)
{
    pq_t *pq = NULL;

    dbg_return_if (ppq == NULL, ~0);
    dbg_return_if (nitems < 2, ~0); /* Expect at least 2 elements. */

    /* Make room for both the queue head and items' array. */
    dbg_err_sif ((pq = u_zalloc(sizeof *pq)) == NULL);
    dbg_err_sif ((pq->q = u_zalloc((nitems + 1) * sizeof(pq_item_t))) == NULL);

    /* Init the index of last element in array: valid elements are stored 
     * at index'es [1..nitems]. */
    pq->nelems = 0;
    pq->nitems = nitems;

    *ppq = pq;

    return 0;
err:
    pq_free(pq);
    return ~0;
}
Example #8
0
int main(int argc, char **argv) {
	/* SIMULATION VARIABLES */
	int n = 0;      // processes
	int cs_t = 13;  // context switch time (in ms)
	FILE *inf;      // input file (processes.txt)
	FILE *outf;   // output file (simout.txt)

	// recording variables
	float a_cpu_t = 0.0;  // average cpu burst time
	float a_turn_t = 0.0; // average turnaround time
	float a_wait_t = 0.0; // average wait time
	int total_cs = 0;     // total context switches
	int total_burst = 0;  // total cpu bursts
	int total_mem = 256;  // total virtual memory
	int total_t = 0;      // total time
	int total_d = 0;      // total defrag time
	int t_slice = 80;     // round robin time slice
	int t_memmove = 10;   // defragmentation memmove time

	if(argc != 1) {
		printf("There are no arguments associated with this simulation. Input is read from processes.txt.\n");
		return 1;
	}

	// priority queues and process
	priority_queue srtf = pq_new(n), srtn = pq_new(n), srtb = pq_new(n), rrf = pq_new(n), rrn = pq_new(n), rrb = pq_new(n);
	process *p1, *p2, *p3, *p4, *p5, *p6;

	/* read file */
	if((inf = fopen("processes.txt", "r")) == NULL) {
		perror("open() failed");
		return EXIT_FAILURE;
	}

	if((outf = fopen("simout.txt", "w")) == NULL) {
		perror("fopen() failed");
		return EXIT_FAILURE;
	}

	// get current line
	char *line = NULL;
	size_t nbytes = 0;

	while(getline(&line, &nbytes, inf) != -1) {
		// trim the string first
		char *newline;
		newline = trim(line);

		if(*newline != 0) {
			// get values
			char pn;
			int at, bt, bn, it, mem;
			sscanf(newline, "%c|%i|%i|%i|%i|%i", &pn, &at, &bt, &bn, &it, &mem);

			// allocate and init
			p1 = malloc(sizeof(process));
			p2 = malloc(sizeof(process));
			p3 = malloc(sizeof(process));
			p4 = malloc(sizeof(process));
			p5 = malloc(sizeof(process));
			p6 = malloc(sizeof(process));
			*p1 = proc_new(pn, at, bt, bn, it, 0, mem);
			*p2 = *p1;
			*p3 = *p1;
			*p4 = *p1;
			*p5 = *p1;
			*p6 = *p1;

			// calculate burst statistics
			a_cpu_t += bt * bn;
			total_burst += bn;

			// push to queues
			++n;
			pq_push(srtf, p1, at);
			pq_push(srtn, p2, at);
			pq_push(srtb, p3, at);
			pq_push(rrf, p2, at);
			pq_push(rrn, p5, at);
			pq_push(rrb, p6, at);
		}
	}

	// free memory
	free(line);

	// create virtual memory
	v_memory vm_ff = vm_new(total_mem, 'f');
	v_memory vm_nf = vm_new(total_mem, 'n');
	v_memory vm_bf = vm_new(total_mem, 'b');

	/* ===begin simulations=== */
	a_cpu_t /= total_burst;
	s_srt(srtf, vm_ff, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove);
	simout("SRT", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_srt(srtn, vm_nf, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove);
	simout("SRT", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_srt(srtb, vm_bf, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove);
	simout("SRT", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_rr(rrf, vm_ff, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove, t_slice);
	simout("RR", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_rr(rrn, vm_nf, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove, t_slice);
	simout("RR", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);
	printf("\n\n");
	s_rr(rrb, vm_bf, n, cs_t, &a_wait_t, &total_cs, &total_t, &total_d, t_memmove, t_slice);
	simout("RR", "First-Fit", outf, &a_cpu_t, &a_turn_t, &a_wait_t, &total_cs, total_burst, total_t, total_d);

	// close streams
	fclose(inf);
	fclose(outf);

	// free memory and exit gracefully
	vm_free(vm_ff);
	vm_free(vm_nf);
	vm_free(vm_bf);
	pq_free(srtf);
	pq_free(srtn);
	pq_free(srtb);
	pq_free(rrf);
	pq_free(rrn);
	pq_free(rrb);
	free(srtf);
	free(srtn);
	free(srtb);
	free(rrf);
	free(rrn);
	free(rrb);
	return EXIT_SUCCESS;
}