void readMetroLineFromFile(FILE *f, Graph graph) { String temp; String tmpList[SIZE]; String *list; int i; int n; rewind(f); while(fgets(temp.content, SIZE, f) != NULL) { if(strstr(temp.content, "[LINES]") != NULL) { break; } } while(fscanf(f, "%[^=]=", temp.content) != EOF) { fscanf(f, "%[^\n]\n", temp.content); if((n = split(temp.content, ' ', tmpList)) == -1) { return; } if((list = myMalloc(sizeof(String), n)) == NULL) { return; } strcpy(list[0].content, tmpList[0].content); for(i = 1; i < n; i++) { strcpy(list[i].content, tmpList[i].content); addEdge(graph, new_jval_v(&list[i - 1]), new_jval_v(&list[i]), stringCompare); printf("%s - %s\n", list[i - 1].content, list[i].content); } } rewind(f); }
PhysicalFrame* findFreePhysicalPage(MMUSim* sim){ Dllist freeFrames; freeFrames = sim->freePhysicalFrames; Dllist usedFrames; usedFrames = sim->usedPhysicalFrames; JRB tree; tree = sim->frameTree; Dllist nil; nil = dll_nil(freeFrames); Dllist ffp; ffp = dll_first(freeFrames); if(ffp != nil){ PhysicalFrame* freeFrame; freeFrame = ffp->val.v; dll_delete_node(ffp); dll_append(usedFrames, new_jval_v(freeFrame)); if(sim->rep == 2){ // LRU, sort by time jrb_insert_int(tree,freeFrame->lastUsed,new_jval_v(freeFrame)); } else if(sim->rep == 3){ // LFU, sort by lowCount jrb_insert_int(tree,freeFrame->useCount,new_jval_v(freeFrame)); } else if (sim->rep == 4){ // MFU, sort by highCount jrb_insert_int(tree,freeFrame->useCount,new_jval_v(freeFrame)); } return freeFrame; } else { return -1; } }
int readStationListFromFile(FILE *f, JRB stationList) { String temp; StationInfo *station; int n = 0; printf("Station list: \n"); rewind(f); fgets(temp.content, SIZE, f); while(fscanf(f, "%[^=]=", temp.content) != EOF) { if(strstr(temp.content, "[LINES]") != NULL) { break; } if((station = myMalloc(sizeof(StationInfo), 1)) == NULL) { return -1; } strcpy(station->code.content, temp.content); fgets(temp.content, SIZE, f); temp.content[strlen(temp.content) - 1] = '\0'; strcpy(station->name.content, temp.content); jrb_insert_gen(stationList, new_jval_v(&station->name), new_jval_v(&station->code), stringCompare); printf("Station name: \"%s\" - \"%s\"\n", station->name.content, station->code.content); } return n; }
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++; } }
MMUProcess* getProcess(MMUSim* sim, int* pid){ Dllist processList; processList = sim->processes; Dllist nil; nil = dll_nil(processList); Dllist s; s = dll_first(processList); while(s != nil){ MMUProcess* proc; proc = s->val.v; if(proc->pid == pid){ return proc; } s = s->flink; } MMUProcess* ps; ps = malloc(sizeof(MMUProcess)); ps->pid = pid; ps->stats.pid = pid; dll_append(processList, new_jval_v(ps)); PageTable* pageTable = malloc(sizeof(PageTable)); PageTableEntry* pageArray = malloc(sim->pageEntries * sizeof(PageTableEntry)); pageTable->table = pageArray; pageTable->size = sim->pageEntries; ps->pgtbl = pageTable; return ps; }
void initialize_user_process(char **kos_argv) { // zero out the memory bzero(main_memory, MemorySize); PCB *pcb = (PCB *) malloc(sizeof(PCB)); pcb_init(pcb); printf("filename: %s\n", kos_argv[0]); if (load_user_program(kos_argv[0]) < 0) { fprintf(stderr,"Can't load program.\n"); exit(1); } // set up the program counters and the stack register pcb->registers[PCReg] = 0; pcb->registers[NextPCReg] = 4; /* need to back off from top of memory */ /* 12 for argc, argv, envp */ /* 12 for stack frame */ pcb->registers[StackReg] = MemorySize - 24; dll_append(readyq, new_jval_v((void *) pcb)); printf("Running user code.\n"); kt_exit(); }
/* addCounterExample * Add a counterxample to the Scheduler list of counterexamples */ int addCounterExample(int *g) { Jval ce; ce = new_jval_v(g); /* Append counterexample to list */ dll_append(_Scheduler->counterExamples, ce); _Scheduler->listSize++; }
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; }
void BlockKThread(K_t kt, int key) { kt->state = BLOCKED; kt->blocked_list = ktBlocked; kt->blocked_list_ptr = jrb_insert_int(ktBlocked,key,new_jval_v(kt)); return; }
void kt_yield() { InitKThreadSystem(); ktRunning->state = RUNNABLE; dll_append(ktRunnable,new_jval_v(ktRunning)); KtSched(); return; }
void SleepKThread(K_t kt, int until) { kt->state = SLEEPING; kt->blocked_list = ktSleeping; kt->wake_time = until; kt->blocked_list_ptr = jrb_insert_int(ktSleeping,until,new_jval_v(kt)); return; }
void process_files(Dllist dir_list, JRB inode, JRB paths, char *name, JRB links){ int dup_inode, dup_path; char buf[PATH_MAX+1]; tar *t; dup_inode = 1; dup_path = 1; t = (tar*) malloc(sizeof(tar)); strcpy(t->name,realpath(name, buf)); if(lstat(t->name, &t->istat) < 0){ fprintf(stderr,"ERROR: %s No such file or directory\n",t->name); exit(1); } if(jrb_find_str(paths,realpath(t->name,buf)) != NULL){ //my problem could be here dup_path = 0; jrb_insert_str(links,strdup(name), new_jval_v(jrb_find_str(paths,realpath(t->name,buf)))); // printf("Ignoring Duplicate %s\n",t->name); } if(jrb_find_int(inode, t->istat.st_ino) != NULL){ dup_inode = 0; // jrb_insert_str(links,strdup(t->name), // new_jval_v(jrb_find_str(paths,realpath(t->name,buf)))); // printf("Ignoring Duplicate %s\n",t->name); } if(dup_path && dup_inode) { jrb_insert_str(paths,strdup(realpath(t->name,buf)),new_jval_v(t)); jrb_insert_int(inode,(int)t->istat.st_ino,new_jval_v(t)); } if(S_ISDIR(t->istat.st_mode)){ trav_dir(dir_list, inode, paths, name); } }
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; } }
/* * insert bid in descending cost */ void insert_bid(char *msg) { char *p; Bid *b, *tb; int i, id; Dllist ptr; // create a new bid b = (Bid *) malloc(sizeof(Bid)); p = strstr(msg, "B"); p++; b->cost = atoi(p); p = strstr(p, "$"); p++; b->numrobot = atoi(p); b->coalition = new_dllist(); for (i = 0; i < b->numrobot; i++) { p = strstr(p, "$"); p++; id = atoi(p); if (dll_empty(b->coalition)) b->leaderID = id; dll_append(b->coalition, new_jval_i(id)); } printf("insert bid: %s\n", msg); // insert the bid to bidList, increasing cost if (dll_empty(bidList)) { dll_append(bidList, new_jval_v(b)); } else { dll_traverse(ptr, bidList) { tb = (Bid *) jval_v(dll_val(ptr)); if (tb->cost > b->cost) { dll_insert_b(ptr, new_jval_v(b)); break; } if (ptr == dll_last(bidList)) {// append it to the last dll_append(bidList, new_jval_v(b)); break; } } }
/* add person to list * notify the elevator * wait for elevator too pick up */ void wait_for_elevator(Person *p){ pthread_mutex_lock(lock); dll_append(people, new_jval_v((void *) p)); pthread_cond_signal(cond); pthread_mutex_unlock(lock); /* wait to be notified by elevator */ pthread_mutex_lock(p -> lock); do{ pthread_cond_wait(p -> cond, p -> lock); } while(p -> e == NULL); pthread_mutex_unlock(p -> lock); }
Jval cloneNode(Jval item) { String *newItem; String *temp; if((newItem = myMalloc(sizeof(String), 1)) == NULL) { printf("Cannot clone node!\n"); exit(1); } temp = jval_v(item); strcpy(newItem->content, temp->content); return new_jval_v(newItem); }
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); }
int syscall_return(PCB *pcb, int returnVal) { // printf("Goes in syscall\n"); // printf("This is the user_base and user_limit of the curProc in syscall: %i %i\n",curProc->user_base, curProc->user_limit); //Set PCReg in the saved registers to NextPCReg. This pcb is not the global one in KOS() pcb->registers[PCReg] = pcb->registers[NextPCReg]; pcb->registers[2] = returnVal; //Put the PCB onto the ready queue. Jval value = new_jval_v((void *)pcb); //value.v = pcb; //places it at the end of the dllist dll_append(readyq,value); kt_exit(); }
main() { JRB b; JRB bn; IS is; is = new_inputstruct(NULL); b = make_jrb(); while (get_line(is) >= 0) { (void) jrb_insert_str(b, strdup(is->text1), new_jval_v(NULL)); } jrb_traverse(bn, b) { printf("%s", jval_s(bn->key)); }
void set_file(char *name) { JRB entry; if (!*name) name = "<stdin>"; entry = jrb_find_str(file_names,name); if (entry) name = jval_v(jrb_val(entry)); else { name = stralloc(name); jrb_insert_str(file_names,name,new_jval_v(name)); } current_loc.file = name; current_loc.line = 1; }
void wait_for_elevator(Person *p) { // this is a critical section pthread_mutex_lock(p->es->lock); //append the list ((Queue*)p->es->v)->count++; dll_append(((Queue*)p->es->v)->passengers, new_jval_v(p)); //signal the elevators that some one is infact there; pthread_cond_signal(((Queue*)p->es->v)->cond); // remove the critical section pthread_mutex_unlock(p->es->lock); // block on the persons varible pthread_mutex_lock(p->lock); pthread_cond_wait(p->cond, p->lock); pthread_mutex_unlock(p->lock); }
Elevator *get_elevator(int id, JRB elevators) { Elevator *e; JRB tmp; tmp = jrb_find_int(elevators, id); if (tmp != NULL) return (Elevator *) tmp->val.v; e = talloc(Elevator, 1); e->time = 0; e->id = id; e->door = 0; e->floor = 1; e->state = 'R'; jrb_insert_int(elevators, id, new_jval_v((void *) e)); return e; }
void WakeKThread(K_t kt) { /* * look through the various blocked lists and try to wake the * specified thread */ if (kt->state == RUNNING || kt->state == RUNNABLE || kt->state == DEAD) return; jrb_delete_node(kt->blocked_list_ptr); kt->state = RUNNABLE; kt->blocked_list = NULL; kt->blocked_list_ptr = NULL; dll_append(ktRunnable,new_jval_v(kt)); return; }
int syscall_exec(PCB *pcb, int returnVal) { // printf("Goes into syscall_return exec\n"); //Set PCReg in the saved registers to NextPCReg. This pcb is not the global one in KOS() pcb->registers[PCReg] = 0; pcb->registers[NextPCReg] = 4; // pcb->registers[NextPCReg]+=4; //Put the return value into register 2. pcb->registers[2] = returnVal; pcb->registers[2] = returnVal; //Put the PCB onto the ready queue. Jval value = new_jval_v((void *)pcb); //value.v = pcb; //printf("This is in the syscall_return pcb that gets placed in the value to be placed in readyq: %s\n", value.v); //places it at the end of the dllist dll_append(readyq,value); //Call kt_exit kt_exit(); }
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); }
/** * Create metadata. */ int exnodeCreateMetadata(ExnodeMetadata **md) { ExnodeMetadata *temp; /* for memory leaking */ /*temp=(ExnodeMetadata *)malloc(sizeof(*temp));*/ temp=(ExnodeMetadata *)calloc(1,sizeof(*temp)); memset(temp, 0x54, sizeof(ExnodeMetadata)); if(temp==NULL) { return(EXNODE_NOMEM); } /*fprintf(stderr, "CreateMetadata: 0x%x\n", temp);*/ temp->type=NONE; temp->name=NULL; temp->val=new_jval_v(NULL); *md=temp; return(EXNODE_SUCCESS); }
/* * fork a thread and make it runnable */ void * kt_fork(void *(*func)(void *), void *arg) { K_t kt; InitKThreadSystem(); kt = InitKThread(KT_STACK_SIZE,func,arg); if(kt == NULL) { if(Debug & KT_DEBUG) { fprintf(stderr,"kt_fork: couldn't make thread\n"); fflush(stderr); } return(NULL); } kt->state = STARTING; dll_append(ktRunnable,new_jval_v(kt)); return((void *) (kt->tid)); }
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(); }
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++; }
void *initialize_user_process(void *arg) { PCB *start = (PCB *) malloc(sizeof(PCB)); start->registers = (int *) malloc(NumTotalRegs * sizeof(int)); int i; for (i = 0; i < NumTotalRegs; i++) { start->registers[i] = 0; } int argc = 0; while (kos_argv[argc] != NULL) { argc++; } int tempCount = argc; int fIndex = 0; int ptr[argc]; int topOfStack = MemorySize - 20; while (tempCount != 0) { tempCount--; topOfStack -= strlen(kos_argv[fIndex]) + 1; ptr[tempCount] = topOfStack; strcpy(main_memory + topOfStack, kos_argv[fIndex]); fIndex++; } while (topOfStack % 4) { topOfStack--; } topOfStack -= 4; int machineAddress = WordToMachine(0); memcpy(main_memory + topOfStack, &machineAddress, 4); fIndex = 0; while (fIndex < argc) { topOfStack -= 4; machineAddress = WordToMachine(ptr[fIndex]); memcpy(main_memory + topOfStack, &machineAddress, 4); fIndex++; } int argv = topOfStack; // envp topOfStack -= 4; machineAddress = WordToMachine(0); memcpy(main_memory + topOfStack, &machineAddress, 4); // argv topOfStack -= 4; machineAddress = WordToMachine(argv); memcpy(main_memory + topOfStack, &machineAddress, 4); // argc topOfStack -= 4; machineAddress = WordToMachine(argc); memcpy(main_memory + topOfStack, &machineAddress, 4); if (load_user_program(kos_argv[0]) < 0) { fprintf(stderr, "Error: Unable to load user program.\n"); exit(1); } start->registers[StackReg] = topOfStack - 12; start->registers[PCReg] = 0; start->registers[NextPCReg] = 4; Jval j = new_jval_v((void *)start); dll_append(queue, j); kt_exit(); }