/* * Initialization of Serail communication */ struct RS232 initRS232() { struct RS232 com_local; com_local.receivePackets = initQueue(); com_local.sendPackets = initQueue(); com_local.pendingPacketSize = initQueue(); com_local.client_ack = 0; com_local.host_ack = 0; com_local.isRdySend = 0; com_local.failReceive = 0; com_local.stateMachine = (enum States*)malloc(sizeof(enum States)); com_local.pastState = (enum States*)malloc(sizeof(enum States)); *com_local.stateMachine = startInit; *com_local.pastState = startInit; com_local.num_packets = com_local.index_packets = 0; com_local.num_send_packets = com_local.index_send_packets = 0; com_local.packetBuf = NULL; printf("UART Initialization\n"); alt_up_rs232_dev* uart = alt_up_rs232_open_dev(RS232_0_NAME); up_dev.RS232_dev = uart; printf("Clearing read buffer to start\n"); while (alt_up_rs232_get_used_space_in_read_FIFO(uart)) { alt_up_rs232_read_data(uart, &com.data[0], &com.parity); } alt_alarm_start(&alarm, alt_ticks_per_second(), RS232_ISR, (void*)&up_dev); printf("UART Initialization finished\n"); return com_local; }
void start(void) { int i; //exercise 4A //sys_priority (PRIORITY); //exercise 7 queue_t frontground = initQueue(); queue_t background = initQueue(); sys_multilevelq (QUEUE); sys_yield(); for (i = 0; i < RUNCOUNT; i++) { if (current->queue_name == q_foreground) { enQueue (frontground, pid_t current->p_pid); } else if (current->queue_name == q_background) { enQueue (background, pid_t current->p_pid); } else { console_printf(cursorpos, 0x100, "error on enqueue"); } // Write characters to the console, yielding after each one. *cursorpos++ = PRINTCHAR; sys_yield(); } // Yield forever. //while (1) //sys_yield(); sys_exit(0); }
void start(void) { int i; // Set up hardware (schedos-x86.c) segments_init(); interrupt_controller_init(0); console_clear(); // Initialize process descriptors as empty memset(proc_array, 0, sizeof(proc_array)); for (i = 0; i < NPROCS; i++) { proc_array[i].p_pid = i; proc_array[i].p_state = P_EMPTY; } // Set up process descriptors (the proc_array[]) queue_t frontground = initQueue(); queue_t background = initQueue(); for (i = 1; i < NPROCS; i++) { process_t *proc = &proc_array[i]; uint32_t stack_ptr = PROC1_START + i * PROC_SIZE; // Initialize the process descriptor special_registers_init(proc); // Set ESP proc->p_registers.reg_esp = stack_ptr; // Load process and set EIP, based on ELF image program_loader(i - 1, &proc->p_registers.reg_eip); // Mark the process as runnable! proc->p_state = P_RUNNABLE; } // Initialize the cursor-position shared variable to point to the // console's first character (the upper left). cursorpos = (uint16_t *) 0xB8000; // Initialize the scheduling algorithm. // USE THE FOLLOWING VALUES: // 0 = the initial algorithm // 2 = strict priority scheduling (exercise 2) // 41 = p_priority algorithm (exercise 4.a) // 42 = p_share algorithm (exercise 4.b) // 7 = any algorithm that you may implement for exercise 7 scheduling_algorithm = 7; // Switch to the first process. run(&proc_array[1]); // Should never get here! while (1) /* do nothing */; }
void init_Position(struct Position* _p, char* _pos) { initQueue(&_p->lockGrantees); initQueue(&_p->removedGrantees); int n = strlen(_pos)+ 1; _p->id = (char*)malloc((n+ 1)* sizeof(char)); strcpy(_p->id, _pos); _p->inHist = false; }
void MergingSortedBlockInputStream::init(MutableColumns & merged_columns) { /// Read the first blocks, initialize the queue. if (first) { first = false; for (size_t i = 0; i < source_blocks.size(); ++i) { SharedBlockPtr & shared_block_ptr = source_blocks[i]; if (shared_block_ptr.get()) continue; shared_block_ptr = new detail::SharedBlock(children[i]->read()); const size_t rows = shared_block_ptr->rows(); if (rows == 0) continue; if (expected_block_size < rows) expected_block_size = std::min(rows, max_block_size); cursors[i] = SortCursorImpl(*shared_block_ptr, description, i); shared_block_ptr->all_columns = cursors[i].all_columns; shared_block_ptr->sort_columns = cursors[i].sort_columns; has_collation |= cursors[i].has_collation; } if (has_collation) initQueue(queue_with_collation); else initQueue(queue_without_collation); } /// Let's check that all source blocks have the same structure. for (const SharedBlockPtr & shared_block_ptr : source_blocks) { if (!*shared_block_ptr) continue; assertBlocksHaveEqualStructure(*shared_block_ptr, header, getName()); } merged_columns.resize(num_columns); for (size_t i = 0; i < num_columns; ++i) { merged_columns[i] = header.safeGetByPosition(i).column->cloneEmpty(); merged_columns[i]->reserve(expected_block_size); } }
struct Simulation initSimulation(double arrivalRate, double serviceTime, double simTime) { struct Simulation res; res.currTime = 0; res.arrivalRate = arrivalRate; res.serviceTime = serviceTime; res.timeForNextArrival = getRandTime(res.arrivalRate); res.timeForNextDeparture = res.timeForNextArrival + res.serviceTime; res.totalSimTime = simTime; res.buffer = initQueue(); res.eventQueue = initQueue(); return res; }
struct Simulation initSimulation(double arrivalRate, double serviceTime, double simTime) { struct Simulation sim; sim.currTime = 0; sim.arrivalRate = arrivalRate; sim.serviceTime = serviceTime; sim.timeForNextArrival = getRandTime(arrivalRate); sim.timeForNextDeparture = sim.timeForNextArrival + serviceTime; sim.totalSimTime = simTime; sim.buffer = initQueue(); sim.eventQueue = initQueue(); return sim; }
int main(int argc, const char * argv[]) { LinkQueue queue; puts("初始化队列 queue"); initQueue(&queue); traversal(queue); puts("队尾依次插入0 1 2 3"); insertQueue(&queue, 0); insertQueue(&queue, 1); insertQueue(&queue, 2); insertQueue(&queue, 3); traversal(queue); puts("先进先出,删除队列从头开始, 0 "); deleteQueue(&queue); traversal(queue); puts("先进先出,删除队列从头开始, 1 "); deleteQueue(&queue); traversal(queue); puts("先进先出,删除队列从头开始, 2 "); deleteQueue(&queue); traversal(queue); puts("先进先出,删除队列从头开始, 3"); deleteQueue(&queue); traversal(queue); destoryQueue(&queue); return 0; }
void visualize(struct TreeNode *root, int len) { int count = 0, level = 1, width = 1; Queue *q = initQueue(len); enqueue(q, root); struct TreeNode *cnode = NULL; while ( q->size != 0 ) { count ++; if ( count == width ) { width *= 2; level += 1; printf("\n"); } cnode = front(q); printf("%d ", cnode->val); dequeue(q); if ( cnode->left != NULL ) { enqueue(q, cnode->left); } if ( cnode->right != NULL ) { enqueue(q, cnode->right); } } printf("\n"); return; }
static never_inline void soleOutfixStreamExec(struct hs_stream *stream_state, struct hs_scratch *scratch) { assert(stream_state); assert(scratch); const struct RoseEngine *t = stream_state->rose; assert(t->outfixEndQueue == 1); assert(!t->amatcherOffset); assert(!t->ematcherOffset); assert(!t->fmatcherOffset); const struct NFA *nfa = getNfaByQueue(t, 0); struct mq *q = scratch->queues; initQueue(q, 0, t, scratch); if (!scratch->core_info.buf_offset) { nfaQueueInitState(nfa, q); pushQueueAt(q, 0, MQE_START, 0); pushQueueAt(q, 1, MQE_TOP, 0); pushQueueAt(q, 2, MQE_END, scratch->core_info.len); } else { nfaExpandState(nfa, q->state, q->streamState, q->offset, queue_prev_byte(q, 0)); pushQueueAt(q, 0, MQE_START, 0); pushQueueAt(q, 1, MQE_END, scratch->core_info.len); } if (nfaQueueExec(q->nfa, q, scratch->core_info.len)) { nfaQueueCompressState(nfa, q, scratch->core_info.len); } else if (!told_to_stop_matching(scratch)) { scratch->core_info.broken = BROKEN_EXHAUSTED; } }
void initDimmunix() { histSize = 0; init_Cycle(&cycle); init_Cycle(&stack); joinPointFound = NULL; blocked = false; nSyncs = 0; nSyncThreads = 0; nYields = 0; pthread_mutex_init(&avoidanceLock, NULL); if((positions=(struct Queue*)malloc_zero(sizeof(struct Queue)*MAXPOSITIONS))==NULL){ LOGD("ERROR ALLOCATING DIMMUNIX positions"); } if((history=(struct Template*)malloc_zero(sizeof(struct Template)*MAXTEMPLATES))==NULL){ LOGD("ERROR ALLOCATING DIMMUNIX history"); } int i; for (i = 0; i < MAXPOSITIONS; i++) { initQueue(&positions[i]); } loadHistory(); // pthread_create(&monitorThread, NULL, printStats, NULL); enabled = true; LOGD("initialized Dimmunix"); }
int main(int argc, char const *argv[]) { void *status; pthread_t thread1, thread2; //Declaring pthread type pthread_attr_t attr; //Initialize and set thread detached attribute pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); system("clear"); srand(time(NULL)); struct queue q; initQueue(&q); //windows-only BULLOCKS... //HANDLE fillThread = (HANDLE)_beginthread(fillQThread, 0, &q); //HANDLE popThread = (HANDLE)_beginthread(popQThread, 0, &q); //WaitForSingleObject(fillThread, 1000 * 300); //timeout //WaitForSingleObject(popThread, 1000 * 300); //timeout pthread_create(&thread1, NULL, fillQThread, &q); pthread_create(&thread2, NULL, popQThread, &q); pthread_attr_destroy(&attr); pthread_join(thread2, &status); pthread_exit(NULL); return 0; }
void bfs(GRAPH *g, int data) { int i, vet = g->vetNum; g->adj[data].color = 1; for (i = 0; i < vet; i++) { if (i != data) { g->adj[i].color = 0; } } QUEUE *q = initQueue(); enqueue(q, data); while (!isQueueEmpty(q)) { int u = dequeue(q); printf("%c ", u + 'A'); EDGE *p = g->adj[u].adj; while (p != NULL) { int next = p->dest; if (g->adj[next].color == 0) { g->adj[next].color = 1; enqueue(q, next); } p = p->link; } g->adj[u].color = 2; } printf("\n"); }
/* ** Perform Re-Pair on one block */ static void executeRepair_OneBlock (PROG_INFO *prog_struct, BLOCK_INFO *block_struct) { R_UINT i; /* Perform scanPairs */ scanPairs (prog_struct, block_struct); /* Allocate queue and initialize to NULL */ /* Make the priority queue bigger by one since position 0 is ** unused. */ block_struct -> pqueue_size = block_struct -> max_count + 1; block_struct -> pqueue = wmalloc ((block_struct -> pqueue_size) * (sizeof (TPHRASE*))); for (i = 0; i < block_struct -> pqueue_size; i++) { block_struct -> pqueue[i] = NULL; } /* Populate queue with tentative phrases */ initQueue (prog_struct, block_struct); /* Recursively pair phrases */ rePairPhrases (prog_struct, block_struct); /* Sort primitives */ sortPrimitives (block_struct); block_struct -> sort_phrases = wmalloc (((block_struct -> num_prims) + (block_struct -> num_phrases)) * (sizeof (PHRASE))); /* Sort phrases */ sortPhrases (prog_struct, block_struct); return; }
int main(void) { QueuePtr queue = initQueue(); char buffer[20]; char sel = 0; while (sel != 'X'&&sel!='x') { printf("+++ Queue +++\n"); printQueue(queue); printf("++++++\n"); printf("1) Enqueue printID\n"); printf("2) Enqueue printHello\n"); printf("3) Enqueue printDate\n"); printf("4) Dequeue and execute\n"); printf("X) Exit\n"); sel = getchar(); while (getchar() != '\n'); switch (sel) { case '1': enqueue(queue, printID, "printID"); break; case '2': enqueue(queue, printHello, "printHello"); break; case '3': enqueue(queue, printDate, "printDate"); break; case '4': dequeue(queue, buffer); if(buffer[0]!='\0'&&buffer[0]!=NULL){ printf("Executed: %s\n",buffer); } } } destroyQueue(queue); return 0; }
void p1(void) { int i,n; struct Data d[MaxHeapSize-1]; struct Data tempD; int A[]={5,2,8,6,1,9,21,42,0}; struct Queue * pQ; struct Heap * mH=initMinHeap(); initQueue(&pQ); for (i=0; i<MaxHeapSize-1; i++) { d[i].data=A[i]; } for (i=0; i<MaxHeapSize-1; i++) { insertMinHeap(mH,d[i]); } for (i=0; i<MaxHeapSize-1; i++) { tempD=removeMinHeap(mH); enqueue(pQ, tempD); } printf("Elements in the priority queue are:"); while (isEmpty(pQ)!=1) { dequeue(pQ, &tempD); printf(" %d ", tempD.data); } printf("\n"); freeQueue(pQ); freeMinHeap(mH); }
void bfs_traverse(double road[][MAX_POINT]) { int visited[MAX_POINT]; int i; for (i = 1; i < MAX_POINT; i++) visited[i] = 0; struct queueLK *hq; initQueue(hq); for (i = 1; i < MAX_POINT; i++) { if (!visited[i]) { visited[i] = 1; visit(road, i); enQueue(hq, i); while (!emptyQueue(hq)) { int u = outQueue(hq); int j; for (j = 1; j < MAX_POINT; j++) { if (road[u][j] > 0.0001 && !visited[j]) { visited[j] = 1; visit(road, j); enQueue(hq, j); } } } } } clearQueue(hq); }
/** * Handle a phoneme * @remarks Originally called 'trait_ph' */ void SpeechManager::handlePhoneme() { const uint16 deca[3] = {300, 30, 40}; uint16 startPos = _cfiphBuffer[_phonemeNumb - 1] + deca[_typlec]; uint16 endPos = _cfiphBuffer[_phonemeNumb] + deca[_typlec]; int wordCount = endPos - startPos; startPos /= 2; endPos /= 2; for (int i = startPos, currWord = 0; i < endPos; i++, currWord += 2) WRITE_BE_UINT16(&_vm->_mem[(kAdrWord * 16) + currWord], _cfiphBuffer[i]); _ptr_oct = 0; int currWord = 0; initQueue(); do { moveQueue(); charg_car(currWord); trait_car(); } while (currWord < wordCount); moveQueue(); trait_car(); entroct((int)'#'); }
void traverseBFT(int g[GRAPHSIZE][GRAPHSIZE]){ int startingNode; printf("\n\tBreadth First Traversal:\n\tStarting Node: "); scanf("%d",&startingNode); VisitMark mark[GRAPHSIZE]; // to know which nodes to add List *Q = initQueue(); int i,j; for (i = 0; i < GRAPHSIZE; i++){ mark[i] = UNVISITED; // For starters, mark all nodes as UNVISITED } mark[startingNode] = VISITED; NQ(&Q,startingNode); while (!isEmpty(&Q)){ int temp = DQ(&Q); printf("%d ",temp); for (j = 0; j < GRAPHSIZE; j++){ if (g[j][temp] == 1){ if (mark[j] == UNVISITED){ mark[j] = VISITED; NQ(&Q,j); } } } } }
void breadthFirstTraversal(FILE*out, Graph G, int s){ int j; for (j = 1; j <= G->numV; j++){ G->vertex[j].colour = White; G->vertex[j].parent = 0; } G->vertex[s].colour = Grey; G->vertex[s].parent = 0; Queue Q = initQueue(); enqueue(Q, newQueueData(s)); fprintf(out, "\nBreadth-first : "); while (!empty(Q)){ int aParent = dequeue(Q).nv; fprintf(out, "%s ", G->vertex[aParent].id); GEdgePtr edge = G->vertex[aParent].firstEdge; while (edge != NULL){ if (G->vertex[edge->child].colour == White){ G->vertex[edge->child].colour = Grey; G->vertex[edge->child].parent = aParent; enqueue(Q, newQueueData(edge->child)); } edge = edge->nextEdge; } G->vertex[aParent].colour = Black; } fprintf(out, "\n"); }
int main( void ) { first f; last l; if ( ( f = malloc( sizeof( referenceToNodePointer ) ) ) == NULL ) exit( EXIT_FAILURE ); if ( ( l = malloc( sizeof( referenceToNodePointer ) ) ) == NULL ) exit( EXIT_FAILURE ); initQueue( f, l ); insertQueue( 10, f, l ); insertQueue( 20, f, l ); insertQueue( 30, f, l ); printQueue( f ); printf( "------\n\n" ); extractQueue( f ); printQueue( f ); return 0; }
/** \ingroup xbcdeamon * \brief main function for the apllication */ int main() { signal(SIGINT, closeAndExit); // Create a log entry in /var/log/ openlog (DEAMON_LOG_NAME, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0); if (daemon(0,0) != 0) { LOGERR("daemon() failed"); } initDevices(VENDOR_ID, PRODUCT_ID); initButtonSHM(SHM_NAME); initQueue(MQ_CMDS_NAME); if (pthread_create(&cmd_thread, NULL, commandListenerThread, NULL) != 0) { LOGERR("Error creating commandlistenerThread"); } if (pthread_create(&btn_thread, NULL, buttonReaderThread, NULL) != 0) { LOGERR("Error creating buttonReaderThread"); } while(1) { } return (0); // not reached }
Tid ULT_CreateThread(void (*fn)(void *), void *parg) { if(isInit == 0) { initQueue(); } ucontext_t context, *newContext = &context; getcontext(newContext); ucontext_t *currentContext = (ucontext_t *)currentBlock->p; // updating program counter newContext->uc_mcontext.gregs[REG_EIP] = (unsigned int)&stub; // allocate new stacka newContext->uc_mcontext.gregs[REG_ESP] = (unsigned int)malloc(ULT_MIN_STACK); // push pargs onto stack newContext->uc_mcontext.gregs[REG_EBP+8]= (unsigned int)parg; // push fn onto stack newContext->uc_mcontext.gregs[REG_EBP+12]= (unsigned int)fn; // save old frame pointer onto stack newContext->uc_mcontext.gregs[REG_EBP+16] = currentContext->uc_mcontext.gregs[REG_EIP]; // newContext->uc_mcontext.gregs[REG_EBP] = currentContext->uc_mcontext.gregs[REG_EBP]; // push args onto stack //newContext->uc_mcontext.gregs[REG_EBP+8]= (unsigned int)parg; ThrdCtlBlk *newBlock = malloc(sizeof(ThrdCtlBlk)); newBlock->p = newContext; newBlock->tid = tidInc++; enqueue(Q,*newBlock); // create a new context // intilize it // set to this context //assert(0); /* TBD */ return newBlock->tid; }
int main(int argc, char const *argv[]) { queue_t* head = malloc(sizeof(queue_t)); initQueue(head, 11); add(head, 10); add(head, 9); add(head, 8); add(head, 7); add(head, 6); add(head, 5); add(head, 4); add(head, 3); add(head, 2); add(head, 1); add(head, 0); printf("%d\n", poll(&head)); printf("%d\n", poll(&head)); printf("%d\n", poll(&head)); printf("%d\n", poll(&head)); printf("%d\n", poll(&head)); printf("%d\n", poll(&head)); printf("%d\n", poll(&head)); printf("%d\n", poll(&head)); printf("%d\n", poll(&head)); printf("%d\n", poll(&head)); printf("%d\n", poll(&head)); printf("%d\n", poll(&head)); add(head, 100); printf("%d\n", poll(&head)); printf("%d\n", poll(&head)); free(head); return 0; }
int main(int argc,char* argv[]) { Stack* temp=initStack(); Queue* postfix=initQueue(); push(temp,'\n'); for(int i=0;argv[1][i]!='\0';i++){ if(isdigit(argv[1][i])){printf("%c",argv[1][i]);continue;} if(argv[1][i]=='('){push(temp,'(');continue;} if(argv[1][i]==')'){ while(top(temp)!='('){ printf("%c",top(temp)); pop(temp); } pop(temp); continue; } if(prioritycompare(top(temp),argv[1][i])==-1)continue; if(prioritycompare(top(temp),argv[1][i])){ push(temp,argv[1][i]); continue; } while(prioritycompare(top(temp),argv[1][i])==0){ printf("%c",top(temp)); pop(temp); } push(temp,argv[1][i]); } while(temp->length!=0){ printf("%c",top(temp)); pop(temp); } freeStack(temp); return 0; }
int main(int nargs, char * args[]) { if(nargs < 2) { fprintf(stderr, "USAGE: %s <symbol_string>\n", args[0]); exit(EXIT_FAILURE); } //symbol index is symbol char val - 'a' //ASSUMES chars are all lowercase //determine frequency of symbols calcFrequency(args[1]); printFrequency(); //build the priority q for each char/frequency in frequency array initQueue(); buildQueue(); //priority queue is done so build the huffman tree root = NULL; buildHuffmanTree(); if(root == NULL) { freeQueue();//should already be empty, but just in case return(EXIT_FAILURE); } calcCodes(); if(nargs > 2) printHuffmanCode(args[2]); else printHuffmanCode(args[1]); freeTree(); return(EXIT_SUCCESS); }
static never_inline void soleOutfixBlockExec(const struct RoseEngine *t, struct hs_scratch *scratch) { assert(t); assert(scratch); initSomState(t, (u8 *)scratch->core_info.state); assert(t->outfixEndQueue == 1); assert(!t->amatcherOffset); assert(!t->ematcherOffset); assert(!t->fmatcherOffset); const struct NFA *nfa = getNfaByQueue(t, 0); size_t len = nfaRevAccelCheck(nfa, scratch->core_info.buf, scratch->core_info.len); if (!len) { return; } struct mq *q = scratch->queues; initQueue(q, 0, t, scratch); q->length = len; /* adjust for rev_accel */ nfaQueueInitState(nfa, q); pushQueueAt(q, 0, MQE_START, 0); pushQueueAt(q, 1, MQE_TOP, 0); pushQueueAt(q, 2, MQE_END, scratch->core_info.len); char rv = nfaQueueExec(q->nfa, q, scratch->core_info.len); if (rv && nfaAcceptsEod(nfa) && len == scratch->core_info.len) { nfaCheckFinalState(nfa, q->state, q->streamState, q->length, q->cb, q->som_cb, scratch); } }
void p1(void) { int i,n; struct Queue Q=initQueue(); struct Data d; d.arrivalTime=3; d.departureTime=3.1; enqueue(&Q, d); d.arrivalTime=3.5; d.departureTime=3.6; enqueue(&Q, d); d.arrivalTime=3.8; d.departureTime=3.9; enqueue(&Q, d); d.arrivalTime=4.1; d.departureTime=4.2; enqueue(&Q,d); n=Q.currSize; for (i=0; i<n-1; i++) { d=dequeue(&Q); printf("Arrival Time: %f, Departure Time: %f\n", d.arrivalTime, d.departureTime); } freeQueue(&Q); }
void BFSTravel(GraphAdjList *g) { int i; int tmp; EdgeNode *p; queue q; for(i=0;i<g->numVertexes;i++) visited[i]= 0; initQueue(&q); for(i=0;i<g->numVertexes;i++) { if(!visited[i]) { visited[i]=1; printf("%c ",g->adjList[i].data); push(&q,i); while(!isEmpty(&q)) { tmp = pop(&q); p = g->adjList[tmp].firstedge; while(p) { if(!visited[p->adjvex]) { visited[p->adjvex]=1; printf("%c ",g->adjList[p->adjvex].data); push(&q,p->adjvex); } p = p->next; } } } } }
void breadthFirstSearch(Graph *graph, int vertex) { int visited[SIZE]; int i, currVertex; Queue *queue; for(i = 0; i < graph->V; i++) { visited[i] = 0; } queue = initQueue(graph->V); enqueue(queue, vertex); while(!isEmptyQueue(queue)) { currVertex = dequeue(queue); visited[currVertex] = 1; printf(" %d", currVertex); for(i = 0; i < graph->V; i++) { //if there is an edge and that vertex is not visited if((graph->edgeMat[currVertex][i] == 1) && (visited[i] == 0)) { enqueue(queue, i); } } } releaseQueue(queue); }