int stringCompare(Jval a, Jval b) { String *x, *y; x = jval_v(a); y = jval_v(b); return strcmp(x->content, y->content); }
void scheduler() { // Init no longer has children. Needs to close. if (jrb_empty(init->children)) { SYSHalt(); } if(dll_empty(readyQ)) { noop_flag = 1; noop(); } else { PCB *process; Dllist node; Jval registers; noop_flag = 0; process = (PCB *) malloc(sizeof(PCB)); node = dll_first(readyQ); // Get next node in queue dll_delete_node(node); // Node in memory. Delete off readyQ // Get registers from node registers = dll_val(node); process = (PCB *)jval_v(registers); // Update current process currentProcess = process; User_Base = process->base; User_Limit = process->limit; run_user_code(process->registers); } }
void scheduler() { //readyq = new_dllist(); if(dll_empty(readyq)) { //printf("empty ready q\n"); current = NULL; noop(); } //takes the first PCB off the readyq and calls run_user_code() on its registers else { // pop off and delete from readyq printf("non-empty q\n"); current = (PCB *) jval_v(dll_val(dll_first(readyq))); printf("non-empty q\n"); dll_delete_node(dll_first(readyq)); printf("non-empty q\n"); run_user_code(current->registers); } }
void myPrint(Jval item) { String *temp; temp = jval_v(item); printf("Station %s\n", temp->content); }
void error_cleanup(void) { JRB entry; jrb_traverse(entry,file_names) free(jval_v(jrb_val(entry))); jrb_free_tree(file_names); }
JRB getAdjVertices(Graph g, Jval v1, int (*cmp)(Jval, Jval)) { JRB tmp, found; found = jrb_find_gen(g, v1, cmp); if(found == NULL) return NULL; else return (JRB) jval_v(found->val); }
/** * Destroy metadata. */ int exnodeDestroyMetadata(ExnodeMetadata *md) { JRB children,cur; if(!md) { return(EXNODE_BADPTR); } /*fprintf(stderr, "DestroyMetadata: 0x%x Type: %d Name: %s\n",*/ /*md, md->type, (md->name!= NULL? md->name: "NullName" ));*/ if(md->type == META) { children=(JRB)jval_v(md->val); /*fprintf(stderr, "\tChildren: 0x%x\n", children);*/ jrb_traverse(cur,children) { exnodeDestroyMetadata((ExnodeMetadata *)jval_v(cur->val)); cur->val.v = NULL; }
int adjacent(Graph g, Jval v1, Jval v2, int (*cmp)(Jval, Jval)) { JRB found = jrb_find_gen(g, v1, cmp); if(found == NULL){ return 0; }else{ found = jrb_find_gen((JRB) jval_v(found->val), v2, cmp); if(found == NULL) return 0; else return 1; } }
//Each elevator is a while loop. //Check the global list and if it’s empty, block on the condition variable for blocking elevators. //When the elevator gets a person to service, it moves to the appropriate flo0or and opens its door. //It puts itself into the person’s e field, then signals the person and blocks until the person wakes it up. //When it wakes up, it goes to the person’s destination floor, opens its door, signals the person and blocks. //When the person wakes it up, it closes its door and re-executes its while loop. //• Your elevator’s should call open door(), close door() from move to floor() appropriately. //All sleeping and printing should be done in elevator skeleton.c. //Thus, your final program that you hand in should not do any sleeping or any printing. void *elevator(void *arg) { Person *person_in_transit; for(;;) { pthread_mutex_lock(((Elevator*)arg)->es->lock); while (!((Queue*)((Elevator*)arg)->es->v)->count) pthread_cond_wait(((Queue*)((Elevator*)arg)->es->v)->cond, ((Elevator*)arg)->es->lock); if(!dll_empty(((Queue*)((Elevator*)arg)->es->v)->passengers)){ person_in_transit = (Person*)jval_v(dll_val(dll_first(((Queue*)((Elevator*)arg)->es->v)->passengers))); dll_delete_node(dll_first(((Queue*)((Elevator*)arg)->es->v)->passengers)); } // unlock the critial section. pthread_mutex_unlock(((Elevator*)arg)->es->lock); //move the elevator. --((Queue*)((Elevator*)arg)->es->v)->count; person_in_transit->from == ((Elevator*)arg)->onfloor?:move_to_floor(((Elevator*)arg), person_in_transit->from); //open the door open_door(((Elevator*)arg)); //add the people to the elevator person_in_transit->e = ((Elevator*)arg); //signal the person pthread_mutex_lock(person_in_transit->lock); pthread_cond_signal(person_in_transit->cond); pthread_mutex_unlock(person_in_transit->lock); // have the elevator wait pthread_mutex_lock(((Elevator*)arg)->lock); pthread_cond_wait(((Elevator*)arg)->cond, ((Elevator*)arg)->lock); pthread_mutex_unlock(((Elevator*)arg)->lock); // close door close_door(((Elevator*)arg)); // elevator moves move_to_floor(person_in_transit->e,person_in_transit->to); // open the door once move has completed. open_door(((Elevator*)arg)); // signal the person pthread_mutex_lock(person_in_transit->lock); pthread_cond_signal(person_in_transit->cond); pthread_mutex_unlock(person_in_transit->lock); // block the elevator pthread_mutex_lock(((Elevator*)arg)->lock); pthread_cond_wait(((Elevator*)arg)->cond, ((Elevator*)arg)->lock); pthread_mutex_unlock(((Elevator*)arg)->lock); // close the door close_door(((Elevator*)arg)); // restart. } }
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); }
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); }
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 V_kt_sem(kt_sem iks) { Ksem ks = (Ksem)iks; K_t wake_kt; InitKThreadSystem(); ks->val++; if(ks->val <= 0) { wake_kt = jval_v(jrb_val(jrb_find_int(ktBlocked,ks->sid))); WakeKThread(wake_kt); } return; }
static int sendHint(int newsockfd, int workingSize) { /* Initialize hint */ char* hintGraph = NULL; char *hintGraphSize = NULL; char hintMessage[READBUFFERSIZE]; char cliqueCount[BUFSIZ]; /* Hint message structure: [result flag]:[matrixsize]:[cliquecount]:[graphmatrix] */ /* Decide which hint to send */ if(workingSize <= _Scheduler->currCEsize+1) { /* Send counterexample */ asprintf(&hintGraphSize, "%d", _Scheduler->currCEsize); /* Get next counterexample from list */ int *key_g; key_g = (int *)jval_v(dll_val(_Scheduler->currPtr)); /* Update pointer */ _Scheduler->currPtr = _Scheduler->currPtr->flink; hintGraph = GraphtoChar(key_g, _Scheduler->currCEsize); sprintf(cliqueCount, "%d", 0); } /* Finish building message */ hintMessage[0] = RESULT; strcat(hintMessage, ":"); strcat(hintMessage, hintGraphSize); strcat(hintMessage, ":"); strcat(hintMessage, cliqueCount); strcat(hintMessage, ":"); strcat(hintMessage, hintGraph); /* just checking */ if(hintGraph == NULL) { fprintf(stderr,"Error: failed to build hint message.\n"); return -1; } int n = write(newsockfd, hintMessage, strlen(hintMessage)); if (n < 0) { fprintf(stderr,"Error: failed to write to socket\n"); return -1; } /* Free memory */ free(hintGraph); }
int getAdjVerticesI(Graph g, Jval v, int* output, int (*cmp)(Jval, Jval)) { // find note v JRB found = jrb_find_gen(g, v, cmp); JRB adj_list; JRB tmp; int count = 0; if(found == NULL){ printf("ERROR: Node %d does not exist\n", jval_i(v)); return 0; }else{ adj_list = (JRB) jval_v(found->val); jrb_traverse(tmp, adj_list){ output[count] = jval_i(tmp->key); count++; } return count; }
/* * 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; } } }
void BFS_Visit(Graph_Struct Graph,char start[]) { Graph_Symbol_Table *Table; char *u,*v; JRB node_rbt,tree,ele; Dllist node_queue,queue = new_dllist(); printf("%s ",start); u = malloc(MAX_LENGTH_STRING); v = malloc(MAX_LENGTH_STRING); Table = Search_On_Table(Graph,start); Table->colour = 'g'; dll_append(queue, new_jval_s(start)); while(!dll_empty(queue)) { node_queue = dll_first(queue); u = jval_s(node_queue->val); dll_delete_node(node_queue); node_rbt = jrb_find_str(Graph.Edges,u); if(node_rbt != NULL) { tree = (JRB)jval_v(node_rbt->val); jrb_traverse(ele,tree) { v = jval_s(ele->key); Table = Search_On_Table(Graph,v); if(Table->colour == 'w') { printf("%s ",v); Table->colour = 'g'; if(Table->previous == NULL) { Table->previous = malloc(MAX_LENGTH_STRING); } strcpy(Table->previous,u); dll_append(queue, new_jval_s(v)); } } }
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; } }
void KtSched() { K_t kt; JRB jb; unsigned int sp; unsigned int now; JRB tmp; Dllist dtmp; /* * start by recording the current stack contents in case * I'm descheduled * * this is where I return to when I'm rescheduled */ if(setjmp(ktRunning->jmpbuf) != 0) { FreeFinishedThreads(); /* * if we are being killed by another thread, jump through * the exitbuf */ if(ktRunning->die_now) { /* Jim: This used to longjmp to the exitbuf, but I changed it for two reasons: 1. It wasn't being removed from ktActive 2. Hell will be paid if it is ktOriginal. I believe kt_exit() is cleaner. I have not tested it. I should. */ kt_exit(); /* not reached */ } return; } start: if (!jrb_empty(ktSleeping)) { now = time(0); while(!jrb_empty(ktSleeping)) { kt = (K_t) jval_v(jrb_val(jrb_first(ktSleeping))); if(kt->wake_time > now) { break; } WakeKThread(kt); } } /* * if there is nothing left to run, exit. However, if there * are sleepers or a joinall, deal with them appropriately */ if(dll_empty(ktRunnable)) { /* * first, check for sleepers and deal with them */ if(!jrb_empty(ktSleeping)) { kt = jval_v(jrb_val(jrb_first(ktSleeping))); sleep(kt->wake_time - now); goto start; } /* * next, see if there is a joinall thread waiting */ jb = jrb_find_int(ktBlocked,0); if(jb != NULL) { WakeKThread((K_t)jval_v(jrb_val(jb))); goto start; } if(!jrb_empty(ktBlocked)) { if(Debug & KT_DEBUG) { fprintf(stderr, "All processes blocked, exiting\n"); fflush(stderr); } exit(1); } else { if(Debug & KT_DEBUG) { fprintf(stderr, "No runnable threads, exiting\n"); fflush(stderr); } exit(0); } fprintf(stderr, "We shouldn't get here\n"); exit(1); } /* Grab the first job of the ready queue */ dtmp = dll_first(ktRunnable); kt = (K_t) dtmp->val.v; dll_delete_node(dtmp); /* If it is runnable, run it */ if(kt->state == RUNNABLE) { ktRunning = kt; ktRunning->state = RUNNING; longjmp(ktRunning->jmpbuf,1); /* This doesn't return */ } /* * if we have never run before, set up initial stack and go */ if(kt->state == STARTING) { if(setjmp(kt->jmpbuf) == 0) { /* * get double word aligned SP -- stacks grow from high * to low */ sp = (unsigned int)&((kt->stack[kt->stack_size-1])); while((sp % 8) != 0) sp--; #ifdef LINUX /* * keep double word aligned but put in enough * space to handle local variables for KtSched */ kt->jmpbuf->__jmpbuf[JB_BP] = (int)sp; kt->jmpbuf->__jmpbuf[JB_SP] = (int)sp-1024; PTR_MANGLE(kt->jmpbuf->__jmpbuf[JB_SP]); #endif #ifdef SOLARIS /* * keep double word aligned but put in enough * space to handle local variables for KtSched */ kt->jmpbuf[JB_FP] = (int)sp; kt->jmpbuf[JB_SP] = (int)sp-1024; #endif /* * set ktRunning while we still have local variables */ kt->state = RUNNING; ktRunning = kt; /* * now jump onto the new stack */ longjmp(kt->jmpbuf,1); } else { /* * here we are on a new, clean stack -- touch nothing, * set the state, and call * * ktRunning is global so there is no local variable * problem * * borrow this stack to try and free the last thread * if there was one */ FreeFinishedThreads(); if(setjmp(ktRunning->exitbuf) == 0) { /* * if we were killed before we ran, skip the * function call */ if(ktRunning->die_now == 0) { ktRunning->func(ktRunning->arg); } } /* * we are back and this thread is done * * make it inactive */ jb = jrb_find_int(ktActive,ktRunning->tid); if(jb == NULL) { if(Debug & KT_DEBUG) { fprintf(stderr, "KtSched: panic -- inactive return\n"); fflush(stderr); } exit(1); } jrb_delete_node(jb); /* * look to see if there is a thread waiting for this * one to exit -- careful with locals */ jb = jrb_find_int(ktBlocked,ktRunning->tid); if(jb != NULL) { WakeKThread((K_t)jval_v(jrb_val(jb))); } /* * all we can do now is to commit suicide * * don't touch the locals; * * and don't free the stack we are running on */ FreeFinishedThreads(); ktRunning->state = DEAD; dll_append(ktFree_me,new_jval_v(ktRunning)); ktRunning = NULL; goto start; } } /* The only way we get here is if there was a thread on the runnable queue whose state was not RUNNABLE or STARTING. Flag that as an error */ fprintf(stderr, "Error: non-STARTING or RUNNABLE thread on the ready queue\n"); exit(1); }
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++; }
extern JRB jval_jrb(Jval v) { return (JRB)jval_v(v); }