int traverseBFS(Graph graph, int start, Dllist close) { Dllist node, queue; JRB visited; int *output; int temp; int i, n, counter = 0; visited = make_jrb(); queue = new_dllist(); dll_append(queue, new_jval_i(start)); while(!dll_empty(queue)) { node = dll_first(queue); temp = jval_i(node->val); dll_delete_node(node); if(jrb_find_int(visited, temp) == NULL) { counter++; // reportFunc(temp); jrb_insert_int(visited, temp, new_jval_i(temp)); n = outdegree(graph, temp, output); for(i = 0; i < n; i++) { if(jrb_find_int(visited, output[i]) == NULL) { dll_append(queue, new_jval_i(output[i])); } } } } return counter; }
KOS() { /* Semaphores */ writeOK = make_kt_sem(0); writers = make_kt_sem(1); readers = make_kt_sem(1); nElem = make_kt_sem(0); consoleWait = make_kt_sem(0); /* Generics */ sys_stop_read = 0; current_pid = 0; console_size = 256; buffer_head, buffer_tail; // Zero out memory bzero(main_memory, MemorySize); bzero(memory_space_array, 8); /* Initializers */ currentProcess = (PCB *) malloc(sizeof(PCB)); initialize_console_buffer(&buffer_head, &buffer_tail); readyQ = new_dllist(); found_node = make_jrb(); pid_tree = make_jrb(); kt_fork(initialize_user_process, (void *)kos_argv); kt_fork(console_buf_read, (void *)kos_argv[0]); kt_joinall(); start_timer(10); scheduler(); }
void create_trees (JRB hosts) { int i = 0; JRB node; Host *host; nstable = 0; nnon_stable = 0; stable = make_jrb (); non_stable = make_jrb (); jrb_traverse (node, hosts) { host = (Host *) node->val.v; if (i++ % NON_STABLE == 0) { jrb_insert_str (non_stable, host->hnp, new_jval_v (host)); nnon_stable++; } else { jrb_insert_str (stable, host->hnp, new_jval_v (host)); nstable++; } }
/* * for self-test */ void PermuteVertices(int *g, int size, int v1, int v2) { JRB v1_a; JRB v2_b; JRB rb; int i; int j; v1_a = make_jrb(); v2_b = make_jrb(); for(i=0; i < size; i++) { for(j=i+1; j < size; j++) { if(i == v1) { rb = jrb_find_int(v1_a,j); if(rb == NULL) { jrb_insert_int(v1_a,j,(Jval)g[i*size+j]); } } if(j == v1) { rb = jrb_find_int(v1_a,i); if(rb == NULL) { jrb_insert_int(v1_a,i,(Jval)g[i*size+j]); } } if(i == v2) { rb = jrb_find_int(v2_b,j); if(rb == NULL) { jrb_insert_int(v2_b,j,(Jval)g[i*size+j]); } } if(j == v2) { rb = jrb_find_int(v2_b,i); if(rb == NULL) { jrb_insert_int(v2_b,i,(Jval)g[i*size+j]); } } } } jrb_traverse(rb,v1_a) { j = rb->key.i; /* * leave edges between swapped nodes alone */ if(j > v2) { g[v2*size+j] = rb->val.i; } else if(j < v2) { g[j*size+v2] = rb->val.i; } }
/* return jrb holding frequency of characters */ JRB makeStatsTree(char *buffer, int size) { // make jrb to count characters JRB stats; JRB found; // result of searching int val; int i; stats = make_jrb(); for(i = 0; i < size; i++){ found = jrb_find_int(stats, buffer[i]); if(!found){ /* if not found, insert buffer[i] to the tree with val as 1 */ jrb_insert_int(stats, buffer[i], ji(1)); }else{ /* if found, increase val of that node by 1 */ val = jval_i(found->val) + 1; found->val = ji(val); } //end if else }// end for return stats; }
void InitKThreadSystem() { int ok; if(KtInit_d) { return; } ok = 0; #ifdef SOLARIS ok++; #endif #ifdef LINUX ok++; #endif if (ok == 0) { fprintf(stderr, "KThread error: No architecture specified %s\n", "at compile time"); exit(1); } if (ok > 1) { fprintf(stderr, "KThread error: Multiple architectures %s\n", "specified at compile time"); exit(1); } ktActive = make_jrb(); ktRunnable = new_dllist(); ktFree_me = new_dllist(); ktBlocked = make_jrb(); ktSleeping = make_jrb(); ktThread_count = 0; ktTidCounter = 1; ktSidCounter = -1; ktOriginal = InitKThread(0,NULL,NULL); ktRunning = ktOriginal; KtInit_d = 1; return; }
/* make Prior Queue from JRB. stats jrb contain: key: character val: frequency => prior Queue will have form: key: frequency val: character */ JRB makePriorQueue(JRB stats) { JRB tmp; JRB priorQ = make_jrb(); jrb_traverse(tmp, stats){ jrb_insert_int(priorQ, jval_i(tmp->val), tmp->key); }
//static char *Argv[5] = {"argtest","Rex", "my", "man",NULL}; KOS() { //exitSignal = 0; // printf("Starting kos\n"); bzero(main_memory, MemorySize); //make a PCB curProc = (PCB *) malloc(sizeof(PCB)); //make a dllist(readyq) which holds user processes to be run readyq = new_dllist(); //circular console size, head, and tail cbSize = 256; //Step 16 cbHead = 0; cbTail = 0; //step 14: should be initialized to 1 writers = make_kt_sem(1); readers = make_kt_sem(1); writeok = make_kt_sem(0); //step 17: consoleWait= make_kt_sem(0); nelem = make_kt_sem(0); nslots = make_kt_sem(256); //printf("Before Forking\n"); //Step 10: pids curpid = 0; rbtree = make_jrb(); foundPid = make_jrb(); //has to be its own mini tree //Lab4 globalFD = 0; //Step 12: Memory splitting 8 bzero(mem8,8); //fork to the file with args provided, just loading it kt_fork(initialize_user_process, kos_argv); //Step 4: fork to the file with args provided, now reading it, start from the very beginning kos_argv[0] kt_fork(console_buffer, kos_argv[0]); //now join all the threads kt_joinall(); //call the scheduler // start_timer(10); //scheduler(); scheduler(); }
void compress(int argc, char **argv, int verb) { int i, size; IS is; Dllist dir_list, DLLnode; JRB inode, JRBnode, paths, links; void *buf; tar *t; links = make_jrb(); dir_list = new_dllist(); inode = make_jrb(); JRBnode = make_jrb(); paths = make_jrb(); for(i = 2; i < argc; i++){ process_files(dir_list, inode, paths, argv[i], links); } dll_traverse(DLLnode, dir_list){ process_files(dir_list, inode, paths, jval_s(DLLnode->val),links); }
int main() { JRB myJ = make_jrb(); JRB nod; Jval tmp = new_jval_i(5); char *key = "luong"; jrb_insert_str(myJ, key, tmp); tmp = new_jval_i(4); jrb_insert_str(myJ, key, tmp); jrb_traverse(nod, myJ){ printf("%d ", jval_i(nod->val)); }
main() { JRB level_1, level_2; JRB bn, bn2; IS is; is = new_inputstruct(NULL); level_1 = make_jrb(); while (get_line(is) >= 0) { bn = jrb_find_int(level_1, atoi(is->text1)); if (bn == NULL) { bn = jrb_insert_int(level_1, atoi(is->text1), new_jval_v((void *)make_jrb())); } level_2 = (JRB ) jval_v(bn->val); jrb_insert_str(level_2, strdup(is->text1), new_jval_v(NULL)); } jrb_traverse(bn, level_1) { level_2 = (JRB ) jval_v(bn->val); jrb_traverse(bn2, level_2) { printf("%s", bn2->key.s); }
main() { JRB b; JRB bn; IS is; is = new_inputstruct(NULL); b = make_jrb(); while (get_line(is) >= 0) { (void) jrb_insert_int(b, atoi(is->text1), new_jval_s(strdup(is->text1))); } jrb_traverse(bn, b) { printf("%s", jval_s(bn->val)); }
int solve(FILE *f) { Graph g; JRB stationList; String startName, stopName; JRB startCode, stopCode, node; int n; Jval output[SIZE]; g = createGraph(); stationList = make_jrb(); if(readFile(f, g, stationList) == -1) { return 1; } printf("Please enter the start station name:\n"); fscanf(stdin, "%[^\n]", startName.content); while(getchar() != '\n'); printf("Please enter the stop station name:\n"); fscanf(stdin, "%[^\n]", stopName.content); while(getchar() != '\n'); if((startCode = jrb_find_gen(stationList, new_jval_v(&startName), stringCompare)) == NULL) { return 1; } if((stopCode = jrb_find_gen(stationList, new_jval_v(&stopName), stringCompare)) == NULL) { return 1; } printf("Movement between station \"%s\" to station \"%s\"\n", startName.content, stopName.content); n = UShortestPath(g, startCode->val, stopCode->val, cloneNode, stringCompare, myPrint); if(n == 0) { printf("Cannot move from station \"%s\" to station \"%s\"\n", startName.content, stopName.content); } else { printf("Path length between station \"%s\" to station \"%s\": %d\n", startName.content, stopName.content, n); } freeGraph(g); jrb_free_tree(stationList); return 0; }
JRB read_hosts (char *fn, int *nhosts) { FILE *fp; char s[256]; char hn[128]; int port; Key key; JRB hosts; Host *host; char keyinput[64]; fp = fopen (fn, "r"); if (fp == NULL) { perror (fn); exit (1); } hosts = make_jrb (); *nhosts = 0; while (fgets (s, 256, fp) != NULL) { sscanf (s, "%s %d %s", hn, &port, keyinput); host = (Host *) malloc (sizeof (Host)); host->name = strdup (hn); host->port = port; str_to_key (keyinput, &key); key_assign (&(host->key), key); sprintf (s, "%s:%d", hn, port); host->hnp = strdup (s); host->pid = 0; jrb_insert_str (hosts, strdup (s), new_jval_v (host)); *nhosts++; } fclose (fp); return (hosts); }
int UShortestPath(Graph graph, Jval start, Jval stop, Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) { Dllist node, queue, stackVisit; JRB visited; Jval *output; Jval temp, tmp; int i, n; visited = make_jrb(); queue = new_dllist(); stackVisit = new_dllist(); dll_append(queue, start); if((output = myMalloc(sizeof(Jval), 100)) == NULL) { return 0; } while(!dll_empty(queue)) { node = dll_first(queue); temp = cloneFunc(node->val); dll_delete_node(node); if(jrb_find_gen(visited, temp, compare) == NULL) { jrb_insert_gen(visited, temp, temp, compare); dll_prepend(stackVisit, temp); if(compare(temp, stop) == 0) { return solution(graph, start, stop, stackVisit, cloneFunc, compare, reportFunc); } n = getAdjacentVertices(graph, temp, output, compare); for(i = 0; i < n; i++) { if(jrb_find_gen(visited, output[i], compare) == NULL) { dll_append(queue, output[i]); } } } } return -1; }
int deepFirstSearch(Graph graph, int start, int stop, Dllist close) { Dllist node, stack; JRB visited; int output[100]; int temp; int i, n; visited = make_jrb(); stack = new_dllist(); dll_prepend(stack, new_jval_i(start)); while(!dll_empty(stack)) { node = dll_first(stack); temp = jval_i(node->val); dll_delete_node(node); if(jrb_find_int(visited, temp) == NULL) { // reportFunc(temp); dll_append(close, new_jval_i(temp)); jrb_insert_int(visited, temp, new_jval_i(temp)); if(compare(temp, stop) == 0) { jrb_free_tree(visited); free_dllist(stack); return 1; } n = outdegree(graph, temp, output); for(i = 0; i < n; i++) { if(jrb_find_int(visited, output[i]) == NULL) { dll_prepend(stack, new_jval_i(output[i])); } } } } jrb_free_tree(visited); free_dllist(stack); return 0; }
void DFS(Graph graph, Jval start, Jval stop, Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) { Dllist node, stack; JRB visited; Jval *output; Jval temp, tmp; int i, n; visited = make_jrb(); stack = new_dllist(); dll_prepend(stack, start); if((output = myMalloc(sizeof(Jval), 100)) == NULL) { return; } while(!dll_empty(stack)) { node = dll_first(stack); temp = cloneFunc(node->val); dll_delete_node(node); if(jrb_find_gen(visited, temp, compare) == NULL) { reportFunc(temp); jrb_insert_gen(visited, temp, temp, compare); if(compare(temp, stop) == 0) { jrb_free_tree(visited); free_dllist(stack); free(output); return; } n = getAdjacentVertices(graph, temp, output, compare); for(i = 0; i < n; i++) { if(jrb_find_gen(visited, output[i], compare) == NULL) { dll_prepend(stack, output[i]); } } } } }
int insertEdge(Graph g, Jval v1, Jval v2, int (*cmp)(Jval, Jval)) { Graph found; Jval j_adj_list; Jval j = new_jval_v(NULL); found = jrb_find_gen(g, v1, cmp); if(found == NULL){ /* if not found create a new adjacent list which v2 is added to then insert the adjacent list to the tree with the key is v1 */ // create a new adj_list JRB adj_list = make_jrb(); jrb_insert_gen(adj_list, v2, j, cmp); j_adj_list = new_jval_v(adj_list); // add adjacent list to the tree jrb_insert_gen(g, v1, j_adj_list, cmp); return 0; }else{ /* if found check if v2 is in the adj_list of v1 if not, add v2 to adj_list of v1 else return status code 1 */ JRB adj_list = (JRB) jval_v(found->val); found = jrb_find_gen(adj_list, v2, cmp); if(found == NULL){ jrb_insert_gen(adj_list, v2, j, cmp); return 0; }else return 1; } }
SOS() { readyq = new_dllist(); mt_init(); writeok = mt_sem_create(0); writers = mt_sem_create(1); readers = mt_sem_create(1); nelem = mt_sem_create(0); consoleWait = mt_sem_create(0); wr_iobuf = make_io_buffer(1); cr_iobuf = make_io_buffer(256); crb_no_chars = 0; crb_end = 0; crb_begin = 0; curpid = -1; // pids = make_rb(); init_partitions(); DEBUG('e', "pagesize: %d\n", PageSize); jrbTree = make_jrb(); // Step 20 init = new_pcb(); // Step 22 init->pid = get_new_pid(); // Step 22 cread_vnode = new_vnode(); cread_vnode->iobuf =cr_iobuf; cr_iobuf->nwriters = 1; cwrite_vnode = new_vnode(); cwrite_vnode->iobuf = wr_iobuf; wr_iobuf->nreaders = 1; start_timer(10); bzero(main_memory, MemorySize); mt_create(read_console_io, (void *)cr_iobuf); mt_create(write_console_io, (void *)wr_iobuf); //mt_create(read_console, NULL); mt_create(initialize_user_process, (void *)Argv); schedule(); }
int BFStraverse(Graph graph, Jval start, Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) { Dllist node, queue; JRB visited; Jval *output; Jval temp, tmp; int i, n, counter = 0; visited = make_jrb(); queue = new_dllist(); dll_append(queue, start); if((output = myMalloc(sizeof(Jval), 100)) == NULL) { return counter; } while(!dll_empty(queue)) { node = dll_first(queue); temp = cloneFunc(node->val); dll_delete_node(node); if(jrb_find_gen(visited, temp, compare) == NULL) { counter++; reportFunc(temp); jrb_insert_gen(visited, temp, temp, compare); n = getAdjacentVertices(graph, temp, output, compare); for(i = 0; i < n; i++) { if(jrb_find_gen(visited, output[i], compare) == NULL) { dll_append(queue, output[i]); } } } } return counter; }
void *finesleep_initialize(int cheat) { Finesleep *fs; struct timeval tv; struct timezone tz; fs = talloc(Finesleep, 1); fs->cheat = cheat; fs->tree = make_jrb(); fs->lock = talloc(pthread_mutex_t, 1); pthread_mutex_init(fs->lock, NULL); pipe(fs->fd); if (cheat) { fs->stime = 0; } else { gettimeofday(&tv, &tz); fs->stime = tv.tv_usec; fs->stime /= 1000000.0; fs->stime += tv.tv_sec; } return (void *) fs; }
main() { IS is; JRB t, tmp; int i; int j; int x; j = 0; t = make_jrb(); is = new_inputstruct(NULL); while (get_line(is) >= 0) { for (i = 0; i < is->NF; i++) { jrb_insert_str(t, strdup(is->fields[i]), new_jval_i(j)); j++; } } jrb_traverse(tmp, t) { x = strlen(tmp->key.s); printf("%c", tmp->key.s[tmp->val.i%x]); }
int UShortestPath(Graph graph, int start, int stop, Dllist close) { Dllist node, queue, stackVisit; JRB visited; int output[100]; int temp; int i, n; visited = make_jrb(); queue = new_dllist(); stackVisit = new_dllist(); dll_append(queue, new_jval_i(start)); while(!dll_empty(queue)) { node = dll_first(queue); temp = jval_i(node->val); dll_delete_node(node); if(jrb_find_int(visited, temp) == NULL) { jrb_insert_int(visited, temp, new_jval_i(temp)); dll_prepend(stackVisit, new_jval_i(temp)); if(temp == stop) { return solution(graph, start, stop, stackVisit, close); } n = outdegree(graph, temp, output); for(i = 0; i < n; i++) { if(jrb_find_int(visited, output[i]) == NULL) { dll_append(queue, new_jval_i(output[i])); } } } } return -1; }
main(int argc, char **argv) { IS is; JRB people, tmp; JRB elevators; double t; Elevator *e; char name[100]; Person *p; elevators = make_jrb(); people = make_jrb(); is = new_inputstruct(NULL); while (get_line(is) > 0) { sscanf(is->fields[0], "%lf", &t); if (strcmp(is->fields[1], "Elevator") == 0) { e = get_elevator(atoi(is->fields[2]), elevators); if (strcmp(is->fields[3], "opening") == 0) { if (e->door != 0) { printf("Line %d: Elevator %d opening a door that's already open\n", is->line, e->id); exit(1); } if (e->state == 'O') { printf("Line %d: Elevator %d opening a door twice\n", is->line, e->id); exit(1); } if (e->state == 'C') { printf("Line %d: Elevator %d opening a door that is closing\n", is->line, e->id); exit(1); } e->state = 'O'; e->time = t; } else if (strcmp(is->fields[3], "closing") == 0) { if (e->door != 1) { printf("Line %d: Elevator %d closing a door that's already closed\n", is->line, e->id); exit(1); } if (e->state == 'C') { printf("Line %d: Elevator %d closing a door twice\n", is->line, e->id); exit(1); } if (e->state == 'O') { printf("Line %d: Elevator %d closing a door that is opening\n", is->line, e->id); exit(1); } e->state = 'C'; e->time = t; } else if (strcmp(is->fields[5], "closed.") == 0) { if (e->state != 'C') { printf("Line %d: Elevator %d closed a door that was not closing.\n", is->line, e->id); exit(1); } e->state = 'R'; e->door = 0; e->time = t; } else if (strcmp(is->fields[5], "open.") == 0) { if (e->state != 'O') { printf("Line %d: Elevator %d opened a door that was not opening.\n", is->line, e->id); exit(1); } e->state = 'R'; e->door = 1; e->time = t; } else if (strcmp(is->fields[3], "moving") == 0) { if (e->state != 'R') { printf("Line %d: Elevator %d moving from a non-rest state.\n", is->line, e->id); exit(1); } if (e->door != 0) { printf("Line %d: Elevator %d moving when the door is open.\n", is->line, e->id); exit(1); } if (atoi(is->fields[6]) != e->floor) { printf("Line %d: Elevator %d moving from a bad floor.\n", is->line, e->id); exit(1); } e->floor = atoi(is->fields[9]); e->state = 'M'; e->time = t; } else if (strcmp(is->fields[3], "arrives") == 0) { if (e->state != 'M') { printf("Line %d: Elevator %d arriving from a non-moving state.\n", is->line, e->id); exit(1); } if (e->door != 0) { printf("Line %d: Elevator %d arriving when the door is open.\n", is->line, e->id); exit(1); } if (atoi(is->fields[6]) != e->floor) { printf("Line %d: Elevator %d arriving at the wrong floor (%d).\n", is->line, e->id, e->floor); exit(1); } e->state = 'R'; e->time = t; } } else if (strcmp(is->fields[1], "Simulation") == 0) { exit(0); } else { sprintf(name, "%s %s", is->fields[1], is->fields[2]); if (strcmp(is->fields[3], "arrives") == 0) { if (jrb_find_str(people, name) != NULL) { printf("%d: Duplicate person %s\n", is->line, name); exit(1); } p = talloc(Person, 1); p->name = strdup(name); jrb_insert_str(people, p->name, new_jval_v((void *) p)); p->from = atoi(is->fields[6]); p->to = atoi(is->fields[12]); p->state = 'A'; } else { tmp = jrb_find_str(people, name); if (tmp == NULL) { printf("Line %d: Person %s doesn't exist\n", is->line, name); exit(1); } p = (Person *) tmp->val.v; if (strcmp(is->fields[4], "on") == 0) { if (p->state != 'A') { printf("Line %d: Person %s not in arriving state when getting on an elevator\n", is->line, p->name); exit(1); } if (atoi(is->fields[9]) != p->from) { printf("Line %d: Person %s not getting on the proper floor\n", is->line, p->name); exit(1); } e = get_elevator(atoi(is->fields[6]), elevators); if (e->floor != p->from) { printf("Line %d: Person %s getting on an elevator not on the right floor\n", is->line, p->name); exit(1); } if (e->door != 1) { printf("Line %d: Person %s getting on an elevator whose door isn't open.\n", is->line, p->name); exit(1); } if (e->state != 'R') { printf("Line %d: Person %s getting on an elevator who is not at rest.\n", is->line, p->name); exit(1); } p->state = 'O'; p->e = e; } else if (strcmp(is->fields[4], "off") == 0) { if (p->state != 'O') { printf("Line %d: Person %s not on the elevator when getting off\n", is->line, p->name); exit(1); } if (atoi(is->fields[9]) != p->to) { printf("Line %d: Person %s not getting off on the proper floor\n", is->line, p->name); exit(1); } e = get_elevator(atoi(is->fields[6]), elevators); if (e != p->e) { printf("Line %d: Person %s getting off the wrong elevator\n", is->line, p->name); exit(1); } if (e->floor != p->to) { printf("Line %d: Person %s getting off an elevator not on the right floor\n", is->line, p->name); exit(1); } if (e->door != 1) { printf("Line %d: Person %s getting off an elevator whose door isn't open.\n", is->line, p->name); exit(1); } if (e->state != 'R') { printf("Line %d: Person %s getting off an elevator who is not at rest.\n", is->line, p->name); exit(1); } p->state = 'F'; } else if (strcmp(is->fields[4], "done.") == 0) { if (p->state != 'F') { printf("Line %d: Person %s done before getting off the elevator\n", is->line, p->name); exit(1); } jrb_delete_node(tmp); free(p->name); free(p); } } } } exit(0); }
void setupSim(Config* conf, MMUSim* sim){ // Page Bits int physSize = conf->pms; int frameSize = conf->frame; long addressSpaceSize = pow(2,32); int pages = addressSpaceSize / frameSize; int pageBits = log10(pages)/log10(2); printf("Page bits: %d\n", pageBits); sim->pageBits = pageBits; // Page offset int offsetBits = 32 - pageBits; printf("Offset bits: %d\n", offsetBits); sim->offsetBits = offsetBits; // TLB Size int tlbSize = conf->tlbSz; printf("TLB size: %d\n", tlbSize); sim->tlbSz = tlbSize; // TLB Latency int tlbLatency = conf->tlbLat; float tlbLatInMs = tlbLatency * pow(10, -6); printf("TLB latency (milliseconds): %f\n", tlbLatInMs); sim->tlbLat = tlbLatInMs; // Physical Memory Size printf("Physical memory (bytes): %d\n", physSize); sim->physMemSz = physSize; // Physical Frame Size printf("Physical frame size (bytes): %d\n", frameSize); sim->physFrameSz = frameSize; // Number of Physical Frames int numFrames = ceil(((float) physSize) / ((float)frameSize)); printf("Number of physical frames: %d\n", numFrames); sim->numFrames = numFrames; // Memory Latency int memLatency = conf->memLat; float memLatInMs = memLatency * pow(10, -6); printf("Memory latency (milliseconds): %f\n", memLatInMs); sim->memLat = memLatInMs; // Page Table Entries int pageTableEntries = pow(2, 32 - offsetBits); printf("Number of page table entries: %d\n", pageTableEntries); sim->pageEntries = pageTableEntries; // Page Table Replacement Strategy int rep = conf->rep; printf("Page replacement strategy: "); if(rep == 1) {printf("FIFO");} else if(rep == 2) {printf("LRU");} else if(rep == 3) {printf("LFU");} else if(rep == 4) {printf("MFU");} else if(rep == 5) {printf("RANDOM");} printf("\n"); sim->rep = rep; // Disk latency int diskLat = conf->diskLat; printf("Disk latency (milliseconds): %d\n", diskLat); sim->diskLat = diskLat; // Logging int logging = conf->log; printf("Logging: "); if(logging == 0) {printf("off");} else if(logging == 1) {printf("on");} printf("\n\n"); sim->log = logging; // Setup process list sim->processes = new_dllist(); // Setup TLB and Page table TLB* tlb = malloc(sizeof(TLB)); TLB_Entry* tlbArray = malloc(sim->tlbSz * sizeof(TLB_Entry)); tlb->cache = tlbArray; tlb->size = sim->tlbSz; sim->tlb = tlb; // Setup phyiscal frames sim->frameTree = make_jrb(); sim->usedPhysicalFrames = new_dllist(); sim->freePhysicalFrames = new_dllist(); int frameCount = sim->numFrames; sim->physicalFrames = malloc(sizeof(PhysicalFrame) * frameCount); int i; for (i = 0; i < frameCount; i++){ PhysicalFrame* pf = &sim->physicalFrames[i]; pf->num = i; dll_append(sim->freePhysicalFrames, new_jval_v(pf)); Dllist listItem = dll_last(sim->freePhysicalFrames); pf->listRef = listItem; } }
void *initialize_user_process(void *arg) { // printf("Enters initalize_user_process\n"); int i; char **filename = (char **)arg; int argc = 0; while (filename[argc]!=NULL) { printf("This is filename[%i]: %s\n", argc, filename[argc]); argc++; } // printf("This is argc in init: %i\n", argc); //Step 19: putting in argc //k = WordToMachine(argc); //memcpy(main_memory+MemorySize-40+12, &k, 4); //L3 Step 18: start first process, is this init? init=(PCB *)malloc(sizeof(PCB)); init->registers = (int *)malloc(NumTotalRegs*sizeof(int)); for (i=0; i < NumTotalRegs; i++) { init->registers[i] = 0; } init->pid = (int *)0; // printf("This is init's pid: %i\n", (int)init->pid); //L3 Step 19: init->waiter_sem = make_kt_sem(0); init->waiters = new_dllist(); init->children = make_jrb(); //Allocate a new PCB PCB *temp=(PCB *)malloc(sizeof(PCB)); temp->registers = (int *)malloc(NumTotalRegs*sizeof(int)); temp->user_base = 0; //printf("Initial user_base: %i\n", temp->user_base); //Changed Step 12: temp->user_limit = MemorySize-2048; temp->user_limit = MemorySize/8; //printf("Initial user_limt: %i\n", temp->user_limit); //L3 Step 18: temp->parent = init; //L3 Step 19: temp->waiter_sem = make_kt_sem(0); temp->waiters = new_dllist(); //L3 Step 21: make rb tree for children temp->children = make_jrb(); //Changed at Step 12: User_Base = temp->user_base; User_Base = memory8(); User_Limit = temp->user_limit; //printf("This is User_Base in initialize: %i\n", User_Base); //printf("This is User_Limit in initialize: %i\n", User_Limit); //set the regs of the //printf("Setting all the registers to 0 in initalize_user_process\n"); for (i=0; i < NumTotalRegs; i++) temp->registers[i] = 0; //printf("Setting pid in init\n"); temp->pid = (int *)get_new_pid(); printf("First Pid: %i and its parent's should be 0 init: %i\n", temp->pid, temp->parent->pid ); /* set up the program counters and the stack register */ temp->registers[PCReg] = 0; temp->registers[NextPCReg] = 4; //insert the first process as init's child; WOW you can use this function! Jval tempN = new_jval_v((void*)temp); //can only insert Jvals jrb_insert_int(init->children, (int)temp->pid, tempN); //JRB tree, int ikey, Jval val //JRB ptr; // jrb_traverse(ptr, init->children) //{ //printf("This is child pid %i of init %i\n", ptr->key, (int)init->pid); //} //perform_execve(job, fn, argv) // where job is the new PCB, fn is the name of your initial executable (at this point, mine is a.out), //and argv is the argv of this initial job. //returns-> 0 success, errno if error //PrintStack(temp->registers[StackReg], temp->user_base); int errno = perform_execve(temp, filename[0],filename); // printf("This is perform_execve: %i\n", errno); //returns to initialize_user_process(), it either exits because there was an error, or it puts the new job onto the ready queue and calls kt_exit() PrintStack(temp->registers[StackReg], temp->user_base); if (errno!=0) { printf("Perform_execve returned unsucessful\n"); kt_exit(); } //printf("Placing jval into queue\n"); Jval value = new_jval_v((void *)temp); //value.v = temp; //printf("This is the value being placed into the readyq in the initalize_user_process: %s\n",value.v ); dll_append(readyq, value); // printf("Program %s loaded\n", kos_argv[0]); kt_exit(); }
// void randomString(int size, char* output) // { // char bangchucai[] = "abcdefghijklmnopqrstuvwxyz"; // size = rand()%size +3; // int i; // for (i = 0; i < size; ++i) // output[i] = bangchucai[rand() % (sizeof(bangchucai) - 1)]; // output[size] = 0; // } main() { //start = time(NULL); FILE *p,*q,*r,*f1,*f2,*f3; int i,n; JRB a,b,c; a = make_jrb(); char ch[100]; // printf("Nhap so tu: "); // scanf("%d", &n); // while(getchar()!='\n'); // q = fopen("file1.txt", "wt"); // p = fopen("file2.txt", "wt"); // //fputs("//Nghia Tieng Viet\n", p); // //fputs("//Tu Tieng Anh\n", q); // //clock_gettime(CLOCK_REALTIME, &start); // for (i=0; i<n; i++) // { // randomString(100, ch); // c = jrb_find_str(a, ch); // if(c == NULL) // { // fputs(ch,p); // fputs("\n",p); // jrb_insert_str(a,strdup(ch) ,new_jval_i(NULL)); // } // else // i--; // } // jrb_traverse(b,a) // { // fputs(jval_s(b->key),q); // fputs("\n",q); // } // fclose(q); // fclose(p); char ta[20],tv[40], x[50]; f1 = fopen("file11.txt","rt"); f2 = fopen("file22.txt","rt"); f3 = fopen("sample1.dic","wt"); fprintf(f3,"%s","["); fprintf(f3, "%s","\n"); while((!feof(f1))) { fscanf(f1,"%s\n",ta); fprintf(f3,"%s","{"); fprintf(f3,"%s",ta); fprintf(f3,"%s","}"); fprintf(f3,"%s",":"); fscanf(f2,"%s\n",tv); fprintf(f3,"%s","{"); fprintf(f3,"%s",tv); fprintf(f3,"%s","}"); if((!feof(f1))) fputs(",\n", f3); } fprintf(f3, "%s","\n"); fprintf(f3,"%s","]"); fclose(f1); fclose(f2); fclose(f3); // clock_gettime(CLOCK_REALTIME, &end); //printf("time for this algorithm = %u nanoseconds\n", (unsigned int)((end.tv_sec - end.tv_sec) * 1000000000 + (end.tv_nsec - start.tv_nsec))); //end = time(NULL); //printf("Thoi gian chay: %f giay", difftime(end,start)); /*btinit(); FILE *f = fopen("AnhViet.Dic","r"); char key[40], w[50], s[50]; char d; BTA *b1, *b2; b1 = btcrt("word.dic", 0, 0); b2 = btcrt("soundex.dic", 0, 0); char data[50000]; fgetc(f); while (!feof(f)) { fscanf(f,"%s\n", s); strcpy(key, s); n=0; while( (!feof(f)) && ((d=fgetc(f)) !='@') && (d!=EOF)) data[n++]=d; data[n]='\0'; //printf("%s\n",data); btins(b1, key, data, strlen(data)+1); SoundEx(w, key, 4, 1); btins(b2, key, w, strlen(w)+1); } btcls(b1); btcls(b2); return 0;*/ }
Depot *lbone_sortByBandwidth(Depot *depots, int timeout) { int i, count; double *bandwidth; int retval; Depot *new_list; Info **info; Global g; pthread_t tid; JRB sorted, node; pthread_attr_t attr; struct timeval sleeper; g.working = 0; pthread_mutex_init(&g.lock, NULL); /* fork off a detached thread */ retval = pthread_attr_init(&attr); if (retval != 0) { perror("lbone_client_lib: pthread_attr_init failed"); exit(1); } retval = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (retval != 0) { perror("lbone_client_lib: pthread_setdetachstate failed"); exit(1); } count = 0; while (depots[count] != NULL) count++; bandwidth = (double *) malloc(sizeof(double) * (count)); memset(bandwidth, 0, sizeof(double) * count); info = (Info **) malloc(sizeof(Info) * (count)); for (i=0; i < count; i++) { pthread_mutex_lock(&g.lock); g.working++; pthread_mutex_unlock(&g.lock); info[i] = (Info *) malloc(sizeof(Info)); info[i]->id = i; info[i]->bandwidth = &bandwidth[i]; info[i]->timeout = timeout; info[i]->ibp = depots[i]; info[i]->g = &g; retval = pthread_create(&tid, &attr, lbone_pingDepot, (void *) info[i]); if (retval != 0) perror("lbone_checkDepots: pthread_create failed"); } memset(&sleeper, 0, sizeof(struct timeval)); sleeper.tv_usec = 100000; while (g.working > 0) select(0, NULL, NULL, NULL, &sleeper); sorted = make_jrb(); for (i=0; i < count; i++) { jrb_insert_dbl(sorted, bandwidth[i], new_jval_v((void*) depots[i])); free(info[i]); } free(bandwidth); free(info); new_list = (Depot *) malloc(sizeof(Depot) * (count + 1)); memset(new_list, 0, sizeof(Depot) * (count + 1)); i = 0; jrb_rtraverse(node, sorted) { new_list[i] = (Depot) malloc(sizeof(struct ibp_depot)); memset(new_list[i], 0, sizeof(struct ibp_depot)); memcpy(new_list[i], jval_v(node->val), sizeof(struct ibp_depot)); i++; }
Graph createGraph() { return make_jrb(); }
void error_init(void) { file_names = make_jrb(); }