static int load_from_wait(queue *q_wait, queue *q_ready) { if (empty_queue(q_wait)) { /* cannot load from an empty queue */ return FALSE; } /* attempt to load a process if it has a valid request vector and does not cause deadlock */ process *check_proc = (process *) peek(q_wait); if (!(is_valid_request(check_proc) && is_no_deadlock(check_proc))) { return FALSE; } process *proc = (process *) dequeue(q_wait); /* necessary matrix and vector updates ORDER IS IMPORTANT */ increment_row_vector(proc->max_need_vector, claim_matrix[proc->row_index]); increment_row_vector(proc->alloc_vector, allocation_matrix[proc->row_index]); decrement_row_vector(proc->alloc_vector, available_vector); clear_row_vector(proc->alloc_vector); matrix_difference(claim_matrix, allocation_matrix, difference_matrix); enqueue(q_ready, proc); return TRUE; }
int midnet_writer(struct _tcp_session* client_request) { int ret; char* buffer = client_request->buffer; char* token; if(client_request==NULL) { log_message_error("MidNet: Writing data to TCP",PROGRAM,"NULL TCP client structure"); return 0; } token = strstr(client_request->buffer, "\n\n"); if(token==NULL) { log_message_error("MidNet: Writing data to TCP",PROGRAM,"Invalid TCP buffer"); return 0; } if(token>=(buffer+BUFFER_LENGTH-1)) { log_message_error("MidNet: Writing data to TCP",PROGRAM,"Buffer Overflow"); return 0; } *(token+2) = '\0'; ret = clear_fd(0); if(ret==0) { log_message_error("MidNet: Writing data to TCP",PROGRAM,"Error while cleaning file descriptor"); return 0; } ret = is_valid_request(buffer); if(ret==0) { log_message_error("MidNet: Writing data to TCP",PROGRAM,"Wrong command"); return 0; } ret = midnet_processing_request(client_request); if(ret==0) { log_message_error("MidNet: Writing data to TCP",PROGRAM,"Error while processing the request"); return 0; } client_request->limit = client_request->position; client_request->position = 0; client_request->close_request = 1; return 1; }
static int allocate_resources(process *proc, queue *q_ready, queue *q_process, queue *q_wait) { if (!vector_leq(proc->request_vector, SYSTEM_RES)) { print_state_info(proc); printf("Process requested more resources than the system has available.\n"); if (!empty_queue(q_process)) { int process_index = proc->row_index; free_process(proc); process *new_proc = (process *) dequeue(q_process); new_proc->row_index = process_index; clear_row_vector(allocation_matrix[new_proc->row_index]); clear_row_vector(claim_matrix[new_proc->row_index]); increment_row_vector(new_proc->max_need_vector, claim_matrix[new_proc->row_index]); matrix_difference(claim_matrix, allocation_matrix, difference_matrix); enqueue(q_ready, new_proc); } free_process(proc); return FALSE; } if (!is_valid_request(proc)) { print_state_info(proc); move_to_wait(q_wait, proc); return -1; } if (is_no_deadlock(proc)) { /* the resulting state after allocation is safe therefore allocate the resources this process requires */ increment_row_vector(proc->request_vector, allocation_matrix[proc->row_index]); decrement_row_vector(proc->request_vector, available_vector); matrix_difference(claim_matrix, allocation_matrix, difference_matrix); return 0; } else { /* allocation of resources could potentially lead to a deadlock therefore enqueue this process */ printf("\n>>> DEADLOCK POTENTIALLY AVOIDED <<<\n"); print_state_info(proc); move_to_wait(q_wait, proc); return -1; } }
int rad_receive_request(struct rad_handle *h) { struct sockaddr_in from; socklen_t fromlen; int n; if (h->type != RADIUS_SERVER) { generr(h, "denied function call"); return (-1); } h->srv = -1; fromlen = sizeof(from); h->in_len = recvfrom(h->fd, h->in, MSGSIZE, MSG_WAITALL, (struct sockaddr *)&from, &fromlen); if (h->in_len == -1) { generr(h, "recvfrom: %s", strerror(errno)); return (-1); } for (n = 0; n < h->num_servers; n++) { if (h->servers[n].addr.sin_addr.s_addr == from.sin_addr.s_addr) { h->servers[n].addr.sin_port = from.sin_port; h->srv = n; break; } } if (h->srv == -1) return (-2); if (is_valid_request(h)) { h->in_len = h->in[POS_LENGTH] << 8 | h->in[POS_LENGTH+1]; h->in_pos = POS_ATTRS; return (h->in[POS_CODE]); } return (-3); }