void append_job(struct userqueue *q, char *username, char *sha512, char *uuid, char *title, unsigned long created, int pages, int duplex, struct in_addr *src, struct in_addr *dst){ struct jobinfo *j, *p; q->cleaned = clean_queue(q, EXPIRE_TIME); /* don't allocate memory or add job if it exists already even though jobs are uniquely identified by uuid and the cups server to which it was submitted, creation time is a much eaiser first pass comparison */ if ( find_job(q, uuid, created, dst) != NULL ) return; /* Hey, remember that freelist idea? */ j = JOBINFO_ALLOC(); memset(j, 0, sizeof(struct jobinfo)); memcpy(j->uuid, uuid, 32); memcpy(j->sha512, sha512, 128); j->created = created; j->pageinfo = (pages << 1) + duplex; memcpy(&j->src, src, sizeof(struct in_addr)); memcpy(&j->dst, dst, sizeof(struct in_addr)); strncpy(j->username, username, 15); strncpy(j->title, title, 63); if (q->head == NULL){ q->head = j; }else{ p = q->head; /* Progress through the joblist until the next slot is either NULL or occupied by a job of equal or greater creation time */ while (p->next && (p->next->created < created) ){ p = p->next; } /* If the next slot is non-NULL, then move that slot's jobinfo into the next address of this new jobinfo structure and change the p->next to point to our new structure, inserting it before the occupied slot */ if ( p->next ){ j->next = p->next; } p->next = j; } }
void reaper(struct circular_queue *cq){ struct circular_queue *q = cq; unsigned int sleeptime = 6; unsigned int sleepevery = 100; unsigned int counter = 1; int shutdown = 0; unsigned long examine; while (!shutdown){ examine = time(NULL) - sleeptime; if ( *q->cleaned < examine ) clean_queue(q->queue, EXPIRE_TIME); q = q->next; if ( (counter++ % sleepevery) == 1) { counter = 0; sleep(sleeptime); } } return; }
void close_disk() { //permanent caching data if (1 == CACHE_SWITCH) { int i = 0; //TODO: when cache is enabled //you need to clean cache, //if block is dirty, you need to write back the block data to disk //NOTICE: you don't need to update the performance metrics here while (cacheEntity.blockqueue->queue_len != 0) { QueueElement *qPop = pop(cacheEntity.blockqueue); // If dirty write it to the disk. if (qPop->dirtyflag == 1) { fseek(diskEntity.pBlockStore, (qPop->blkidx * BLOCK_SIZE), SEEK_SET); for (i = 0; i < BLOCK_SIZE; i++) { fwrite(qPop->content + i, 1, 1, diskEntity.pBlockStore); } } } // Deleting the queue. clean_queue(cacheEntity.blockqueue); } fclose(diskEntity.pBlockStore); diskEntity.pBlockStore = NULL; }
void io (const char *what) { static int first_time = 1; long clock_timeout = 0, timer_timeout = 0, real_timeout = 0; static struct timeval my_now, my_timer, *time_ptr = &my_timer; int hold_over, rc; fd_set rd, wd; get_time(&my_now); now = my_now.tv_sec; /* CHECK FOR CPU SAVER MODE */ if (!cpu_saver && get_int_var(CPU_SAVER_AFTER_VAR)) if (now - idle_time > get_int_var(CPU_SAVER_AFTER_VAR) * 60) cpu_saver_on(0, NULL); rd = readables; wd = writables; FD_ZERO(&wd); FD_ZERO(&rd); set_screens(&rd, &wd); set_server_bits(&rd, &wd); set_process_bits(&rd); set_socket_read(&rd, &wd); icmp_sockets(&rd, &wd); #if defined(WANT_THREAD) #ifdef WANT_NSLOOKUP set_dns_output_fd(&rd); #endif #ifdef WANT_PTEST set_ptest_output_fd(&rd); #endif #ifdef WANT_MP3PLAYER set_mp3_output_fd(&rd); #endif # if defined(GTK) if (tgtk_okay()) tgtk_set_output_fd(&rd); # endif #endif clock_timeout = (timeout_select - (my_now.tv_sec % timeout_select)) * 1000; if (cpu_saver && get_int_var(CPU_SAVER_EVERY_VAR)) clock_timeout += (get_int_var(CPU_SAVER_EVERY_VAR) - 1) * 60000; timer_timeout = TimerTimeout(); if ((hold_over = unhold_windows())) real_timeout = 0; else if (timer_timeout <= clock_timeout) real_timeout = timer_timeout; else real_timeout = clock_timeout; if (real_timeout == -1) time_ptr = NULL; else { time_ptr->tv_sec = real_timeout / 1000; time_ptr->tv_usec = ((real_timeout % 1000) * 1000); } /* GO AHEAD AND WAIT FOR SOME DATA TO COME IN */ switch ((rc = new_select(&rd, &wd, time_ptr))) { case 0: break; case -1: { /* if we just got a sigint */ first_time = 0; if (cntl_c_hit) edit_char('\003'); else if (errno && errno != EINTR) { int ii = 0; fd_set rd1, wd1; char ii_buff_r[500]; char ii_buff_w[500]; int ii_r[FD_SETSIZE]; int ii_w[FD_SETSIZE]; yell("Select failed with [%d:%s]", errno, strerror(errno)); /* Reseed fd_sets so we can dig further */ yell("Packing fd_sets... Dump of fd's set in fd_set"); ii_buff_r[0] = '\0'; ii_buff_w[0] = '\0'; for (ii = 0; ii < FD_SETSIZE; ii++) { ii_r[ii] = 0; ii_w[ii] = 0; } FD_ZERO(&wd1); FD_ZERO(&rd1); set_screens(&rd1, &wd1); set_server_bits(&rd1, &wd1); set_process_bits(&rd1); set_socket_read(&rd1, &wd1); icmp_sockets(&rd1, &wd1); #if defined(WANT_THREAD) #ifdef WANT_NSLOOKUP set_dns_output_fd(&rd1); #endif #ifdef WANT_PTEST set_ptest_output_fd(&rd1); #endif #ifdef WANT_MP3PLAYER set_mp3_output_fd(&rd1); #endif # if defined(GTK) if (tgtk_okay()) tgtk_set_output_fd(&rd1); # endif #endif for (ii = 0; ii <= global_max_fd; ii++) { fd_set rblah, wblah; memcpy(&rblah, &rd1, sizeof(fd_set)); FD_SET(ii, &rblah); if (memcmp(&rblah, &rd1, sizeof(fd_set)) == 0) { char blahblah[20]; sprintf(blahblah, "%d ", ii); strcat(ii_buff_r, blahblah); ii_r[ii] = 1; } memcpy(&wblah, &wd1, sizeof(fd_set)); FD_SET(ii, &wblah); if (memcmp(&wblah, &wd1, sizeof(fd_set)) == 0) { char blahblah[20]; yell("blah"); sprintf(blahblah, "%d ", ii); strcat(ii_buff_w, blahblah); ii_w[ii] = 1; } } yell("Read fd's in set: %s", (ii_buff_r[0] == '\0') ? "<NONE>" : ii_buff_r); for (ii = 0; ii <= global_max_fd; ii++) { if (ii_r[ii] == 1) { struct stat st; if (fstat(ii, &st) == -1) { yell("READ FD %d is causing the select failure!", ii); } else { if (S_ISSOCK(st.st_mode)) yell("READ FD %d is a socket!", ii); else if (S_ISREG(st.st_mode)) yell("READ FD %d is a regular file!", ii); else if (S_ISFIFO(st.st_mode)) yell("READ FD %d is a FIFO!", ii); else ; } } } yell("Write fd's in set: %s", (ii_buff_w[0] == '\0') ? "<NONE>" : ii_buff_w); for (ii = 0; ii <= global_max_fd; ii++) { if (ii_w[ii] == 1) { struct stat st; if (fstat(ii, &st) == -1) { yell("WRITE FD %d is causing the select failure!", ii); } else { if (S_ISSOCK(st.st_mode)) yell("WRITE FD %d is a socket!", ii); else if (S_ISREG(st.st_mode)) yell("WRITE FD %d is a regular file!", ii); else if (S_ISFIFO(st.st_mode)) yell("WRITE FD %d is a FIFO!", ii); else ; } } } sleep(10); } else { #if 0 yell("errno 0 rc = -1, maybe it'll go away"); sleep(10); #endif } break; } /* we got something on one of the descriptors */ default: { cntl_c_hit = 0; now = time(NULL); make_window_current(NULL); do_screens(&rd); check_icmpresult(&rd, &wd); #if defined(WANT_THREAD) #ifdef WANT_NSLOOKUP dns_check(&rd); #endif #ifdef WANT_PTEST ptest_check(&rd); #endif #ifdef WANT_MP3PLAYER mp3_check(&rd); #endif # if defined(GTK) if (tgtk_okay()) tgtk_check(&rd); # endif #endif do_server(&rd, &wd); do_processes(&rd); scan_sockets(&rd, &wd); clean_sockets(); break; } } now = time(NULL); ExecuteTimers(); get_child_exit(-1); if (update_refresh) { update_refresh = 0; refresh_screen(0, NULL); } if (!hold_over) cursor_to_input(); if (update_clock(RESET_TIME)) { if (get_int_var(CLOCK_VAR)) { update_all_status(current_window, NULL, 0); cursor_to_input(); } clean_queue(get_int_var(TRANSFER_TIMEOUT_VAR)); /* timeout if send time is greater than 5 minutes */ } /* (set in term.c) -- we should redraw the screen here */ if (need_redraw) refresh_screen(0, NULL); #ifdef WANT_THREAD if (scan_done) scan_is_done(); #endif alloca(0); return; }
void compute_n_max(int n) { int i,k,j,m; EG_NODE_PTR eg_ptr; EG_PATH_PTR path_ptr; V_LIST_PTR queue_ptr; V_LIST_PTR top_n_ptr,next_top_n_ptr,new_top_n_ptr,old_top_n_last; V_ENT_PTR v_ent; V_ENT_PTR v_ent_next; double p; int inserted; int old_mth_index,new_mth_index; EG_NODE_PTR mth_child; for (i = 0; i < sorted_egraph_size; i++) { eg_ptr = sorted_expl_graph[i]; eg_ptr->inside = -1.0; eg_ptr->outside = -1.0; if (eg_ptr->path_ptr != NULL) { eg_ptr->top_n = (V_ENT_PTR *)MALLOC(sizeof(V_ENT_PTR) * n); for (j = 0; j < n; j++) eg_ptr->top_n[j] = NULL; } else { eg_ptr->top_n = NULL; } eg_ptr->top_n_len = 0; } for (i = 0; i < sorted_egraph_size; i++) { eg_ptr = sorted_expl_graph[i]; queue_len = 0; queue_first = queue_last = NULL; path_ptr = eg_ptr->path_ptr; if (path_ptr == NULL) continue; /* Constructing the initial queue: */ while (path_ptr != NULL) { /* Create an entry which is the most probable for the path */ v_ent = (V_ENT_PTR)MALLOC(sizeof(struct ViterbiEntry)); v_ent->goal_id = eg_ptr->id; v_ent->path_ptr = path_ptr; v_ent->children_len = path_ptr->children_len; v_ent->top_n_index = (int *)MALLOC(sizeof(int) * path_ptr->children_len); for (k = 0; k < path_ptr->children_len; k++) { v_ent->top_n_index[k] = 0; } if (log_scale) { p = 0.0; for (k = 0; k < path_ptr->children_len; k++) { if (path_ptr->children[k]->top_n != NULL) p += path_ptr->children[k]->top_n[0]->max; } for (k = 0; k < path_ptr->sws_len; k++) { p += log(path_ptr->sws[k]->inside); } } else { p = 1.0; for (k = 0; k < path_ptr->children_len; k++) { if (path_ptr->children[k]->top_n != NULL) p *= path_ptr->children[k]->top_n[0]->max; } for (k = 0; k < path_ptr->sws_len; k++) { p *= path_ptr->sws[k]->inside; } } v_ent->max = p; /* Enqueue the entry */ queue_ptr = (V_LIST_PTR)MALLOC(sizeof(struct ViterbiList)); queue_ptr->entry = v_ent; queue_ptr->prev = NULL; /* Never use for the queue */ queue_ptr->next = NULL; if (queue_first == NULL) { queue_first = queue_last = queue_ptr; queue_len = 1; } else { queue_last->next = queue_ptr; queue_last = queue_ptr; queue_len++; } path_ptr = path_ptr->next; } /* Create the header of top-N list */ top_n_first = (V_LIST_PTR)MALLOC(sizeof(struct ViterbiList)); top_n_first->entry = NULL; /* null entry */ top_n_first->prev = NULL; top_n_first->next = NULL; top_n_last = top_n_first; top_n_len = 0; while (queue_len > 0) { /* Dequeue */ v_ent = queue_first->entry; queue_ptr = queue_first; queue_first = queue_ptr->next; free(queue_ptr); queue_len--; /** Add the element to the top-N list **/ top_n_ptr = top_n_first; next_top_n_ptr = top_n_first->next; inserted = 0; while (next_top_n_ptr != NULL) { /* compare the current entry with the ones in the top-N list */ if (v_ent->max > next_top_n_ptr->entry->max) { new_top_n_ptr = (V_LIST_PTR)MALLOC(sizeof(struct ViterbiList)); new_top_n_ptr->entry = v_ent; new_top_n_ptr->prev = top_n_ptr; new_top_n_ptr->next = next_top_n_ptr; next_top_n_ptr->prev = new_top_n_ptr; top_n_ptr->next = new_top_n_ptr; top_n_len++; inserted = 1; break; } top_n_ptr = next_top_n_ptr; next_top_n_ptr = next_top_n_ptr->next; } if (top_n_len < n) { if (!inserted) { new_top_n_ptr = (V_LIST_PTR)MALLOC(sizeof(struct ViterbiList)); new_top_n_ptr->entry = v_ent; new_top_n_ptr->prev = top_n_ptr; new_top_n_ptr->next = NULL; top_n_ptr->next = new_top_n_ptr; top_n_last = new_top_n_ptr; top_n_len++; inserted = 1; } } else if (top_n_len == n) { if (!inserted) { /* Erase the current entry */ free(v_ent->top_n_index); free(v_ent); v_ent = NULL; } } else { /* top_n_len > n */ if (!inserted) { /* Erase the current entry */ free(v_ent->top_n_index); free(v_ent); v_ent = NULL; } else { /* Erase the last entry */ old_top_n_last = top_n_last; top_n_last = top_n_last->prev; top_n_last->next = NULL; free(old_top_n_last->entry->top_n_index); free(old_top_n_last->entry); free(old_top_n_last); top_n_len--; } } /* If the current entry is not added to the top-N list, there is no * need to pursue the entries that have lower probabilities than * the current entry's probability. */ if (!inserted) continue; /* Otherwise, propose the futher entries based on the current entry */ for (m = 0; m < v_ent->children_len; m++) { old_mth_index = v_ent->top_n_index[m]; new_mth_index = v_ent->top_n_index[m] + 1; mth_child = v_ent->path_ptr->children[m]; if (new_mth_index >= mth_child->top_n_len) continue; v_ent_next = (V_ENT_PTR)MALLOC(sizeof(struct ViterbiEntry)); v_ent_next->goal_id = v_ent->goal_id; v_ent_next->path_ptr = v_ent->path_ptr; v_ent_next->children_len = v_ent->children_len; v_ent_next->top_n_index = (int *)MALLOC(sizeof(int) * v_ent_next->children_len); for (k = 0; k < v_ent_next->children_len; k++) { v_ent_next->top_n_index[k] = (k == m) ? (v_ent->top_n_index[k] + 1) : v_ent->top_n_index[k]; } if (log_scale) { v_ent_next->max = v_ent->max - mth_child->top_n[old_mth_index]->max + mth_child->top_n[new_mth_index]->max; } else { v_ent_next->max = v_ent->max * mth_child->top_n[new_mth_index]->max / mth_child->top_n[old_mth_index]->max; } /* Enqueue the derived entries */ queue_ptr = (V_LIST_PTR)MALLOC(sizeof(struct ViterbiList)); queue_ptr->entry = v_ent_next; queue_ptr->prev = NULL; /* Never use for the queue */ queue_ptr->next = NULL; if (queue_first == NULL) { queue_first = queue_last = queue_ptr; queue_len = 1; } else { queue_last->next = queue_ptr; queue_last = queue_ptr; queue_len++; } } } j = 0; top_n_ptr = top_n_first->next; while (top_n_ptr != NULL) { if (eg_ptr->top_n != NULL) eg_ptr->top_n[j] = top_n_ptr->entry; /* shallow copy */ j++; top_n_ptr = top_n_ptr->next; } eg_ptr->top_n_len = j; clean_queue(); clean_top_n(); } }
// Gets the conn_fd to the server. // Returns 0 if game ended properly. // The main logic of the client. int Play(int conn_fd) { game_state = WAITING_FOR_CONNECTION_RESPONSE; fd_set read_fds, write_fds; int read_buf_current_index = 0; char read_buffer[BUF_MAX_SIZE] = { 0 }; int client_input_buf_current_index = 0; char client_input[CLIENT_INPUT_MAX_SIZE] = { 0 }; int send_buf_current_index = 0; char* send_buf = NULL; queue send_queue = { 0 }; printf("nim\n"); while (!quit) { FD_ZERO(&read_fds); FD_SET(conn_fd, &read_fds); FD_SET(STDIN_FILENO, &read_fds); FD_ZERO(&write_fds); FD_SET(conn_fd, &write_fds); if (select(FD_SETSIZE, &read_fds, &write_fds, NULL, NULL) < 0) { clean_resources(conn_fd, &send_queue); perror("select"); exit(EXIT_FAILURE); } if (FD_ISSET(conn_fd, &read_fds)) { int ret = receive_buffer(conn_fd, read_buffer, read_buf_current_index); if (-1 == ret) { clean_resources(conn_fd, &send_queue); exit(EXIT_FAILURE); } else if(-2 == ret){ if(game_state == WAITING_FOR_CONNECTION_RESPONSE){ printf("Client rejected.\n"); } else { printf("Server disconnected.\n"); } clean_resources(conn_fd, &send_queue); exit(EXIT_FAILURE); } read_buf_current_index += ret; // Received a whole message, size equals the size in the message header. if (read_buf_current_index >= 2 && read_buf_current_index == read_buffer[0]) { if(-1 == handle_received_buf(read_buffer)){ clean_resources(conn_fd, &send_queue); exit(EXIT_FAILURE); } read_buf_current_index = 0; } } if (FD_ISSET(STDIN_FILENO, &read_fds)) { if(get_input_from_client(client_input, &client_input_buf_current_index, BUF_MAX_SIZE + 2) == 0){ // Received a whole message, size equals the size in the message header. if(client_input[client_input_buf_current_index - 1] == '\n') { if (-1 == handle_client_input_buffer(&send_queue, client_input, client_input_buf_current_index)) { clean_resources(conn_fd, &send_queue); exit(EXIT_FAILURE); } client_input_buf_current_index = 0; } } else{ client_input_buf_current_index = 0; } } if (FD_ISSET(conn_fd, &write_fds)) { if (send_buf == NULL) { send_buf = queue_pop(&send_queue); send_buf_current_index = 0; } if(send_buf != NULL) { int ret = send_buffer(conn_fd, send_buf, send_buf_current_index); if (-1 == ret) { clean_resources(conn_fd, &send_queue); exit(EXIT_FAILURE); } send_buf_current_index += ret; if (send_buf_current_index == send_buf[0]) { free(send_buf); send_buf = NULL; send_buf_current_index = 0; } } } if (quit == true) { break; } } clean_queue(&send_queue); return 0; }
// Gets a con_fd and a pointer to a queue. // closes the fd and properly clieans the queue void clean_resources(int con_fd, queue * q) { if (0 != con_fd) { close(con_fd); } clean_queue(q); }