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; }
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; }
/* 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; }
int searchController(Graph graph, int start, int stop, int option) { Dllist close; Dllist node; close = new_dllist(); switch(option) { case BFS_SEARCH: breathFirstSearch(graph, start, stop, close); break; case BFS_TRAVERSE: traverseBFS(graph, start, close); break; case DFS_SEARCH: deepFirstSearch(graph, start, stop, close); break; case DFS_TRAVERSE: traverseDFS(graph, start, close); break; default: printf("This is not an option\n"); free_dllist(close); return; } node = dll_first(close); printf("Visit %d\n", jval_i(node->val)); free_dllist(close); }
/* 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); }
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; }
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)); }
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; }
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 cmpInt(Jval a, Jval b) { return jval_i(a) - jval_i(b); }
void do_read(PCB *pcb){ int fd = pcb->registers[5], buf = pcb->registers[6], count = pcb->registers[7]; //printf("COUNT COUNT COUNT::::%d",count); if(valid_fd(pcb, fd, FD_READ)){ //todo: or valid fd if(buf < 0){ syscall_return(pcb,-1*EFAULT); } }else{ syscall_return(pcb,-1*EBADF); //Bad file number } struct File_Descriptor *fd_obj = pcb->fd_table[fd]; //if(!fd_obj->console_flag){ //printf("do_read: %d %d %d\n", fd, buf, count); //} if(check_buffer_address(pcb, buf) == FALSE){ syscall_return(pcb, -1*EFAULT); } if(count < 0){ syscall_return(pcb, -1*EINVAL); } int i = 0; int val; //printf("\ngot here to 1\n"); for(; i < count; i++){ //printf("obj id:%d, console flag:%d\n", fd_obj->id, fd_obj->console_flag); if(fd_obj->console_flag){ //printf("\ngot here to 0000\n"); P_kt_sem(nelem); Dllist first = dll_first(console_read_buf); val = jval_i(dll_val(first)); dll_delete_node(first); console_read_buf_size -= 1; }else{ //HMM, do we need a lock for buffer access? //printf("\ngot here to 2\n"); struct Pipe *pipe = fd_obj->pipe; //printf("read from pipe: %p\n", pipe); //printf("r:b\n"); P_kt_sem(pipe->empty_sem); val = pipe->buff[pipe->buff_start]; //printf("r:(%d) %c\n",val, val); pipe->buff_start++; if(pipe->buff_start == PIPE_BUFF_SIZE){ pipe->buff_start = 0; } //printf("\ngot here to 2.5\n"); V_kt_sem(pipe->full_sem); //printf("\ngot here to 3\n") //printf("r:a\n");; if(pipe->buff_start == pipe->buff_end){ break; } } if(val == -1 || val == 0){ break; } main_memory[pcb->User_Base+buf+i] = val; } if(!fd_obj->console_flag){ //printf("read done\n"); } //printf("syscall return\n"); syscall_return(pcb, i); }