/** Initalizes the scheduler. Assumptions: - You may assume this will be the first scheduler function called. - You may assume this function will be called only once. - You may assume that cores is a positive, non-zero number. - You may assume that scheme is a valid scheduling scheme. @param num_cores the number of cores that is available by the scheduler. These cores will be known as core(id=0), core(id=1), ..., core(id=cores-1). @param scheme the scheduling scheme that should be used. This value will be one of the six enum values of scheme_t */ void scheduler_start_up(int num_cores, scheme_t scheme) { jobs = (priqueue_t*)malloc(sizeof(priqueue_t)); sch_type = scheme; priqueue_init(jobs,&sch_time); inc_time(0); create_core(&cores,num_cores); }//scheduler_start_up
/** Initalizes the scheduler. Assumptions: - You may assume this will be the first scheduler function called. - You may assume this function will be called once once. - You may assume that cores is a positive, non-zero number. - You may assume that scheme is a valid scheduling scheme. @param cores the number of cores that is available by the scheduler. These cores will be known as core(id=0), core(id=1), ..., core(id=cores-1). @param scheme the scheduling scheme that should be used. This value will be one of the six enum values of scheme_t */ void scheduler_start_up(int cores, scheme_t scheme) { sched = malloc(sizeof(scheduler_t)); sched->scheme = scheme; if(scheme == FCFS || scheme == RR) priqueue_init(&sched->queue, &compare_fcfs); if(scheme == SJF || scheme == PSJF) priqueue_init(&sched->queue, &compare_sjf); if(scheme == PRI || scheme == PPRI) priqueue_init(&sched->queue, &compare_pri); sched->cores = cores; sched->core_status = malloc(sizeof(job_t*) * (sched->cores)); int i; for(i = 0; i < cores; i++) sched->core_status[i] = NULL; finished_jobs = 0; avg_wait = avg_turnaround = avg_response = 0.0f; }
/** Initalizes the scheduler. Assumptions: - You may assume this will be the first scheduler function called. - You may assume this function will be called only once. - You may assume that cores is a positive, non-zero number. - You may assume that scheme is a valid scheduling scheme. @param cores the number of cores that is available by the scheduler. These cores will be known as core(id=0), core(id=1), ..., core(id=cores-1). @param scheme the scheduling scheme that should be used. This value will be one of the six enum values of scheme_t */ void scheduler_start_up(int cores, scheme_t scheme) { ugh = (details_t *) malloc(sizeof(details_t)); ugh->thing = (priqueue_t *) malloc(sizeof(priqueue_t)); ugh->corelist = (int *) malloc(sizeof(int) * (ugh->num_cores = cores)); ugh->total_response_time = ugh->total_turnaround_time = ugh->total_waiting_time = ugh->num_jobs = 0; /** * 0 = FCFS * 1 = SJF * 2 = PSJF * 3 = PRI * 4 = PPRI * 5 = RR */ /** * Different schemes have different notions of priority. The * preemptive versions of schemes are in principle the same. */ switch(scheme) { case FCFS: priqueue_init(ugh->thing, compare0); break; case SJF: case PSJF: priqueue_init(ugh->thing, compare1); break; case PRI: case PPRI: priqueue_init(ugh->thing, compare3); break; case RR: priqueue_init(ugh->thing, compare5); default: break; } int i; for(i = 0; i < ugh->num_cores; i++) ugh->corelist[i] = 0; //Every core is initially idle. ugh->sch = scheme; }
/** Initalizes the scheduler. Assumptions: - You may assume this will be the first scheduler function called. - You may assume this function will be called once once. - You may assume that cores is a positive, non-zero number. - You may assume that scheme is a valid scheduling scheme. @param cores the number of cores that is available by the scheduler. These cores will be known as core(id=0), core(id=1), ..., core(id=cores-1). @param scheme the scheduling scheme that should be used. This value will be one of the six enum values of scheme_t */ void scheduler_start_up(int cores, scheme_t scheme) { num_cores = cores; core_arr = malloc(sizeof(core_t) *cores); int i; for (i = 0; i < cores; i++) { core_arr[i].core_job = NULL; } the_scheme = scheme; if (scheme == FCFS) { priqueue_init(&finished, comp_RR); priqueue_init(&job_queue, comp_FCFS); } if (scheme == RR) { priqueue_init(&finished, comp_RR); priqueue_init(&job_queue, comp_RR); } if (scheme == SJF || scheme == PSJF) { priqueue_init(&finished, comp_RR); priqueue_init(&job_queue, comp_SJF); } if (scheme == PRI || scheme == PPRI) { priqueue_init(&finished, comp_RR); priqueue_init(&job_queue, comp_PRI); } }
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; }
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; }
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; }