void Dijkstra(int** adj_matrix, int num, int start, int dist[]) { int i, u, v; int id, key; bool* access = malloc(sizeof(bool) * num); struct PQueue* pq = create_pqueue(num); for (i = 0; i < num; i++) { if (i == start) { dist[i] = 0; } else { dist[i] = infinite; } access[i] = false; insert(pq, i, dist[i]); } for(i = 0; i < num; i++) { extract_min(pq, &u, &key); access[u] = true; for (v = 0; v < num; v++) { if (!access[v] && adj_matrix[u][v] && dist[v] > dist[u] + adj_matrix[u][v]) { dist[v] = dist[u] + adj_matrix[u][v]; decrease_key(pq, v, dist[v]); } } } destroy_pqueue(pq); free(access); }
/** * Create process list * @param pnum Number of processes in the system * @return List of processes of the system */ process init_processes(int pnum) { process plist = malloc(sizeof(struct process_t) * pnum); if (plist == NULL) return NULL; for(int i = 0; i < pnum; ++i) { plist[i].state = ST_RELEASED; plist[i].timestamp = 0; plist[i].request_timestamp = 0; plist[i].recv_queue = facility("recvq", 2 * pnum); plist[i].recvd_from = create_pqueue(2 * pnum); plist[i].nreplies = 0; plist[i].pending = malloc(sizeof(int) * pnum); memset(plist[i].pending, 0, sizeof(int) * pnum); } return plist; }
int main() { p_queue * priority_Q = create_pqueue(); //sets up a priority queue srand(time(0)); int i, n, pid = 0, dequeue_Count; // Queue count int queue_count = 0; for (i = 0; i < 10; i++) { // Creates the PCB's and puts them in the Priority Queue. queue_count++; // Create and add 10 pcb's to enqueue for (n = 0; n < 10; n++) { PCB * pcb = create(); pcb->priority = rand() % 32;; // Assign random priority (0-31 range) add(priority_Q, pcb); pcb->pid = pid++; } // Print off current state of priority queue: display(priority_Q, 0, queue_count); //displays the current state of the priority queue after ten additions printf("\n\n"); //gives gap in display // Print off PCB's that have been dequeued: printf("PCB's that have been dequeued:\n"); for (dequeue_Count = rand() %3 + 4; dequeue_Count > 0; dequeue_Count--) { //creates a loop for a random number between 4 and 6 PCB * removed = pop_pcb(priority_Q); //pops a pcb from the priority queue toString(removed); //prints the contents of the removed pcb } printf("\n\n"); //gives a gap in the display } display(priority_Q, 1, queue_count); //displays the priority queue return 0; }