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; }
void trav_dir(Dllist dir_list, JRB inode, JRB path, char *name){ DIR* dir; struct dirent* entry; char dir_name[1000], buf[PATH_MAX+1]; dir = opendir(name); if(dir == NULL){ fprintf(stderr,"Error: directory opening failed\n"); exit(1); } while((entry = readdir(dir)) != NULL){ strcpy(dir_name,entry->d_name); if(strcmp(dir_name,".") && strcmp(dir_name,"..")){ char entry_path[PATH_MAX+1]; if(strcmp(name,dir_name)){ strcpy(entry_path,name); strcat(entry_path,"/"); strcat(entry_path,dir_name); } char *res = realpath(entry_path,buf); if(res){ char buf[PATH_MAX+1]; dll_append(dir_list,new_jval_s(strdup(res))); }else{ fprintf(stderr,"ERROR: entry path does not exist\n"); exit(1); } } } closedir(dir); }
void endQueue(Queue l, Jval val, CFUNC cmp) { Queue ptr; int k = 0; dll_traverse(ptr, l) if (cmp(ptr->val,val) == 0) k == 1; if (k != 1) dll_append(l,val); }
void pushStack(Stack l, Jval val, CFUNC cmp) { Stack res; int k = 0; dll_traverse(res, l) if (cmp(res->val,val) == 0) k = 1; if (k != 1) dll_append(l,val); }
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(); }
void readAndCreateStruct(const char * filename) { DlList_T myList=dll_create(); FILE* pFile = fopen(filename, "r"); if (!pFile) { perror("The following error occurred:"); } else { char* buf = (char*) malloc(sizeof(char) * MAX_LINE); size_t n=MAX_LINE; while(getline(&buf, &n, pFile)!=-1) { strtok(buf,"\n"); void* input=malloc(sizeof(char)*(strlen(buf))); printf("%s\n",buf ); memcpy(input,buf,strlen(buf)); dll_append(myList, input); } //printList(myList); free(buf); fclose(pFile); } dll_move_to(myList,dll_size(myList)); startLookingforInput(myList,filename); }
/* 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++; }
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; } }
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; }
main(int argc, char **argv) { IS is; int n; Dllist l; Dllist tmp; if (argc != 2) { fprintf(stderr, "usage: dlltail n\n"); exit(1); } n = atoi(argv[1]); if (n < 0) { fprintf(stderr, "usage: dlltail n -- n must be >= 0\n"); exit(1); } is = new_inputstruct(NULL); l = new_dllist(); while (get_line(is) >= 0) { dll_append(l, new_jval_s(strdup(is->text1))); if (is->line > n) { tmp = dll_first(l); free(jval_s(dll_val(tmp))); dll_delete_node(tmp); } } dll_traverse(tmp, l) printf("%s", jval_s(tmp->val)); }
main() { IS is; Queue q; Stack s; Dllist l; int i; Jval j; is = new_inputstruct(NULL); while (get_line(is) > 0) { q = new_queue(); s = new_stack(); l = new_dllist(); for (i = 0; i < strlen(is->fields[0]); i++) { queue_enqueue(q, new_jval_c(is->fields[0][i])); stack_push(s, new_jval_c(is->fields[0][i])); dll_append(l, new_jval_c(is->fields[0][i])); dll_prepend(l, new_jval_c(is->fields[0][i])); } while (!queue_empty(q)) { j = queue_dequeue(q); printf("%c", j.c); j = stack_pop(s); printf("%c", j.c); printf("%c", l->flink->val.c); dll_delete_node(l->flink); printf("%c", l->flink->val.c); dll_delete_node(l->flink); printf(" "); } printf("\n"); free_queue(q); free_stack(s); free_dllist(l); } }
void kt_yield() { InitKThreadSystem(); ktRunning->state = RUNNABLE; dll_append(ktRunnable,new_jval_v(ktRunning)); KtSched(); return; }
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 main (int argc, char **argv) { struct msgbuff{ long mtype; pid_t pid; }message; pid_t pid, npid; int i; int msqid; key_t keyx; struct msqid_ds msq; Dllist q = new_dllist(); Dllist n; keyx = ftok(KEYFILE_PATH, (int)ID); msqid = msgget(keyx, 0666 | IPC_CREAT); if (msqid == -1) { perror("msgget"); exit(1); } while(1) { if((msgrcv(msqid, &message, sizeof(pid_t), 1, 0)) == MSGQ_NG){ perror("msgrcv"); exit(1); } pid = message.pid; if(pid != 1) { if(dll_empty(q)) kill(pid, SIGUSR1); else { n = dll_first(q); npid = jval_i(dll_val(n)); if(pid == npid) { kill(pid, SIGUSR1); dll_delete_node(n); } else dll_append(q, new_jval_i(pid)); } } else { if(!dll_empty(q)) { n = dll_first(q); npid = jval_i(dll_val(n)); kill(npid, SIGUSR1); dll_delete_node(n); } } } return 0; }
void stress_test_dll(int amt) { dll l1; dll l2; gendata x, y; int i; l1 = dll_create(); l2 = dll_create(); assert(dll_empty(l1)); assert(dll_empty(l2)); printf("Filling two dlls with 2 * %d items...\n", amt); for (i = 0; i < amt; ++i) { x.num = i; l1 = dll_prepend_head(l1, x); l2 = dll_prepend_head(l2, x); assert(!dll_empty(l1)); assert(!dll_empty(l2)); l1 = dll_append_head(l1, x); l2 = dll_append_head(l2, x); assert(!dll_empty(l1)); assert(!dll_empty(l2)); } /* Do some funky inserting at a `random' position. */ dll_append_head(dll_forward(l1, 1), x); assert(x.num == dll_get_data(dll_forward(l1, 1)).num); dll_remove_head(dll_forward(l1, 2), NULL); l1 = dll_append(l1, l2); assert(dll_count(l1) == (unsigned int)(4 * amt)); /* From now on, l2 is `invalid' */ printf("Removing 2 * 2 * %d items from the appended dll...\n", amt); for (i = 0; i < (2 * amt); ++i) { assert(!dll_empty(l1)); x = dll_get_data(l1); l1 = dll_remove_head(l1, NULL); assert(!dll_empty(l1)); y = dll_get_data(l1); l1 = dll_remove_head(l1, NULL); /* * We have to count backwards in this check, since we're * using the list like a stack, prepending all the time. */ assert(x.num == amt - (i % amt) - 1); assert(x.num == y.num); } assert(dll_empty(l1)); dll_destroy(l1, NULL); }
int breathFirstSearch(Graph graph, int start, int stop, Dllist close) { Dllist node, queue; JRB visited; int output[100]; int temp; int i, n; 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) { // reportFunc(temp); dll_append(close, new_jval_i(temp)); jrb_insert_int(visited, temp, new_jval_i(temp)); if(temp == stop) { jrb_free_tree(visited); free_dllist(queue); return 1; } 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])); } } } } jrb_free_tree(visited); free_dllist(queue); return 0; }
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)); } } }
/* * 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; } } }
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; }
/* 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); }
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; }
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(); }
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); }
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(); }
int main(void) { DlList_T myList =dll_create(); void *one=5; void *two=6; void *three=9; dll_append(myList,one); dll_append(myList,two); dll_append(myList,three); dll_move_to(myList,2); showCursor(myList); dll_move_to(myList,1); showCursor(myList); dll_move_to(myList,3); showCursor(myList); dll_move_to(myList,257); showCursor(myList); dll_move_to(myList,1); showCursor(myList); printList(myList); printf("SIZE IS %d\n",dll_size(myList)); dll_clear(myList); dll_append(myList,one); dll_append(myList,two); dll_append(myList,three); dll_move_to(myList,2); showCursor(myList); dll_move_to(myList,1); showCursor(myList); dll_move_to(myList,3); showCursor(myList); dll_move_to(myList,257); showCursor(myList); dll_move_to(myList,1); showCursor(myList); printList(myList); printf("SIZE IS %d\n",dll_size(myList)); return 0; }
/* * 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(); }
void *elevator(void *arg){ Elevator *e = (Elevator *) arg; Dllist item, next, pickup; Person *p; int direction = 1; pickup = new_dllist(); while(1){ if(e -> onfloor >= top) direction = -1; else if(e -> onfloor <= 1) direction = 1; //printf("\tElevator[%i] on floor %i going %s:\n", // e -> id, e -> onfloor, direction == 1 ? "up": "down"); /* pick people up */ pthread_mutex_lock(lock); item = dll_first(people); while(!dll_empty(people) && item != dll_nil(people)){ next = dll_next(item); p = (Person *) item -> val.v; //printf("\t\tShould I get %s %s going from %i to %i? ", // p -> fname, p -> lname, p -> from, p -> to); if(e -> onfloor == p -> from){ if(p -> to > e -> onfloor && direction == 1 || p -> to < e -> onfloor && direction == -1){ dll_append(pickup, item -> val); dll_delete_node(item); //printf("yes!\n"); } //else printf("no!\n"); } //else printf("no!\n"); item = next; } pthread_mutex_unlock(lock); item = dll_first(pickup); while(!dll_empty(pickup) && item != dll_nil(pickup)){ next = dll_next(item); p = (Person *) item -> val.v; if(!e -> door_open) open_door(e); pthread_mutex_lock(p -> lock); p -> e = e; pthread_cond_signal(p -> cond); pthread_mutex_lock(e -> lock); pthread_mutex_unlock(p -> lock); pthread_cond_wait(e -> cond, e -> lock); pthread_mutex_unlock(e -> lock); dll_delete_node(item); item = next; } if(e -> door_open) close_door(e); move_to_floor(e, e -> onfloor + direction); /* drop people off */ item = dll_first(e -> people); while(!dll_empty(e -> people) && item != dll_nil(e -> people)){ next = dll_next(item); p = (Person *) item -> val.v; if(p -> to == e -> onfloor){ if(!e -> door_open) open_door(e); pthread_mutex_lock(p -> lock); pthread_cond_signal(p -> cond); pthread_mutex_lock(e -> lock); pthread_mutex_unlock(p -> lock); pthread_cond_wait(e -> cond, e -> lock); pthread_mutex_unlock(e -> lock); } item = next; } //if(e -> door_open) close_door(e); } return NULL; }
int main ( void ) { DlList_T myList; myList = dll_create(); if( myList == 0 ) { fputs( "Cannot create list!\n", stderr ); return( 1 ); } printf( "Initial list is %s\n", dll_empty( myList ) ? "empty" : "not empty" ); char* one = (char*)malloc( 11 * sizeof(char) ); char* two = (char*)malloc( 12 * sizeof(char) ); char* three = (char*)malloc( 11 * sizeof(char) ); strcpy( one, "First Line" ); strcpy( two, "Second Line" ); strcpy( three, "Third Line" ); printf( "Checking cursor initialized null...\n"); if( dll_has_next( myList ) ) { printf( "Your possition is valid\n" ); } else { printf( "Your possition is NOT valid\n" ); } // Test append printf( "List size: %d\n", dll_size( myList ) ); printf( "Adding \"%s\"\n", one ); dll_append( myList, one ); printf( "List size: %d\n", dll_size( myList ) ); printf( "Adding \"%s\"\n", two ); dll_append( myList, two ); printf( "List size: %d\n", dll_size( myList ) ); printf( "Adding \"%s\"\n", three ); dll_append( myList, three ); printf( "List size: %d\n", dll_size( myList ) ); printf( "Checking cursor fixed with appends...\n"); if( dll_has_next( myList ) ) { printf( "Your possition is valid\n" ); } else { printf( "Your possition is NOT valid\n" ); } printf( "Test cursor movement...\n" ); if( dll_move_to( myList, 3 ) ) { printf( "You moved to an index you shouldn't be able to\n" ); } else { printf( "You can't move the cursor to 3\n" ); } if( dll_move_to( myList, 2 ) ) { printf( "moved to the last index\n" ); } else { printf( "movement problem to index 2\n" ); } if( dll_move_to( myList, 0 ) ) { printf( "moved to the first index\n" ); } else { printf( "movement problem to index 0\n" ); } printf( "Checking cursor still valid...\n" ); if( dll_has_next( myList ) ) { printf( "Your possition is valid\n" ); } else { printf( "Your possition is NOT valid\n" ); } printf( "Print state and test dll_next:\n" ); void* data = dll_next( myList ); int index = 0; // Index 0 printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_next( myList ); index++; // Index 1 printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_next( myList ); index++; // Index 2 printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_next( myList ); index++; // Index 3 (Should be the same as index 2 as it should not exist) printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_next( myList ); printf( "Lets work backwards:\n" ); data = dll_prev( myList ); index = dll_size( myList ) - 1; // Index 2 printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_prev( myList ); index--; // Index 1 printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_prev( myList ); index--; // Index 0 printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_prev( myList ); index--; // Index -1 (Should be same as index 0 as it should not exist) printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_prev( myList ); char* four = (char*)malloc( 12 * sizeof(char) ); char* five = (char*)malloc( 11 * sizeof(char) ); char* six = (char*)malloc( 11 * sizeof(char) ); char* seven = (char*)malloc( 13 * sizeof(char) ); char* eight = (char*)malloc( 12 * sizeof(char) ); strcpy( four, "Fourth Line" ); strcpy( five, "Fifth Line" ); strcpy( six, "Sixth Line" ); strcpy( seven, "Seventh Line" ); strcpy( eight, "Eighth Line" ); printf( "Testing inserts\n" ); dll_insert_at( myList, 0, six ); printf( "List size: %d\n", dll_size( myList ) ); dll_insert_at( myList, 2, seven ); printf( "List size: %d\n", dll_size( myList ) ); dll_insert_at( myList, 4, eight ); printf( "List size: %d\n", dll_size( myList ) ); printf( "Test full print and check inserts\n" ); index = 0; data = dll_get( myList, index ); while( data != NULL ) { printf( "[%d] \"%s\"\n", index, (char*)data ); index++; data = dll_get( myList, index ); } printf( "Test Sets\n" ); data = dll_set( myList, 0, five ); printf( "Switched \"%s\" with \"%s\"\n", (char*)data, five ); free( data ); data = dll_set( myList, 2, four ); printf( "Switched \"%s\" with \"%s\"\n", (char*)data, four ); free( data ); printf( "Test full print and check sets\n" ); index = 0; data = dll_get( myList, index ); while( data != NULL ) { printf( "[%d] \"%s\"\n", index, (char*)data ); index++; data = dll_get( myList, index ); } printf( "Testing popping\n" ); data = dll_pop( myList, dll_size( myList ) -1 ); printf( "Last element is: \"%s\"\n", (char*)data ); free( data ); data = dll_pop( myList, 2 ); printf( "Third element is: \"%s\"\n", (char*)data ); free( data ); printf( "Poping the rest...\n"); index = 0; data = dll_pop( myList, 0 ); while( data != NULL ) { printf( "[%d] \"%s\"\n", index, (char*)data ); free( data ); index++; data = dll_pop( myList, 0 ); } printf( "Destroying\n" ); dll_destroy( myList ); }