int insertTask(ReadyQueue *ready_queue, Task *new_task) { int priority = new_task->priority; // Change the task state to Ready new_task->state = Ready; new_task->next = NULL; HeapNode *node = &(ready_queue->heap_nodes[priority]); TaskList *task_list = (TaskList *)(node->datum); if (task_list->head == NULL) { task_list->head = new_task; task_list->tail = new_task; minHeapInsert(ready_queue->heap, node); } else { task_list->tail->next = new_task; task_list->tail = new_task; } // Update head task ready_queue->head = ((TaskList *)(ready_queue->heap->data[0]->datum))->head; DEBUG(DB_TASK, "| TASK:\tInserted\tTid: %d SP: 0x%x\n", new_task->tid, new_task->current_sp); return 1; }
/* Driver program to test above functions*/ int main() { printf("Hello World\n"); int *try; try=(int *)malloc(sizeof(int)*10); try[0]=4; try[1]=1; try[2]=3; try[3]=2; try[4]=16; try[5]=9; try[6]=10; try[7]=14; try[8]=8; try[9]=7; struct heap* H1; H1=initHeap(H1,try,10,10); //heapify(H1,2); printHeap(H1); H1=buildMinHeap(H1); printHeap(H1); heapSort(H1); //int max=heapExtractMax(H1); //printf("\nmax:%d\n",max); printHeap(H1); H1=minHeapInsert(H1,5); printHeap(H1); return 0; }
void clockserver() { char cs_name[] = CS_REG_NAME; assert(RegisterAs(cs_name) == 0, "Clockserver register failed"); unsigned int time = 0; TimeReply reply; ClockServerMsg message; int tid; /* heap implement */ Heap minheap; HeapNode *heap_data[TASK_MAX]; heapInitial(&minheap, heap_data, TASK_MAX); HeapNode nodes[TASK_MAX]; heapNodesInitial(nodes, TASK_MAX); int tid_array[TASK_MAX]; //actual datum in HeapNode int i = -1; for (i = 0; i < TASK_MAX; i++) { tid_array[i] = -1; } int notifier_tid = Create(1, notifier); while (1) { Receive(&tid, (char *)&message, sizeof(ClockServerMsg)); if (tid == notifier_tid) { Reply(tid, NULL, 0); time++; DEBUG(DB_CS, "| CS:\tTime : %d\n", time); while (minheap.heapsize > 0 && time >= minheap.data[0]->key) { HeapNode *top = minHeapPop(&minheap); Reply(*(int *)(top->datum), NULL, 0); } continue; } switch(message.type) { case CS_QUERY_TYPE_DELAY: // delay tid_array[tid % TASK_MAX] = tid; nodes[tid % TASK_MAX].key = message.delayQuery.delay_tick + time; nodes[tid % TASK_MAX].datum = &(tid_array[tid % TASK_MAX]); minHeapInsert(&minheap, &(nodes[tid % TASK_MAX])); break; case CS_QUERY_TYPE_DELAY_UNTIL: // delay until if (message.delayUntilQuery.delay_time <= time) { Reply(tid, NULL, 0); } else { tid_array[tid % TASK_MAX] = tid; nodes[tid % TASK_MAX].key = message.delayUntilQuery.delay_time; nodes[tid % TASK_MAX].datum = &(tid_array[tid % TASK_MAX]); minHeapInsert(&minheap, &(nodes[tid % TASK_MAX])); } break; case CS_QUERY_TYPE_TIME: // time reply.time = time; Reply(tid, (char*)(&reply), sizeof(TimeReply)); break; default: assert(0, "Clockserver received unknown query type"); break; } } }