int main(void) { init_queue(); put('-');put('+');put('*');put(9);put('+');put(2);put(8);put('*');put('+');put(4);put(8);put(6);put(3); printf("queue를 처음 출력하면 : "); print_queue(); printf("\n"); calc_prefix(); printf("queue를 다시 출력하면 : "); print_queue(); printf("\n"); return 0; }
void init_mii_mem() { int i; init_queues(); init_queue(&rx_free_queue, NUM_MII_RX_BUF, 0); init_queue(&tx_free_queue, NUM_MII_TX_BUF, NUM_MII_RX_BUF); init_queue(&filter_queue, 0, 0); init_queue(&internal_queue, 0, 0); init_queue(&ts_queue, 0, 0); for(i=0;i<2;i++) init_queue(&tx_queue[i], 0, 0); return; }
int main(int argc, char *argv[]) { char s[100]; QUEUE *q = NULL; Q_ELEMENT_WHAT *qpt; int i, n; q = init_queue(q, what_cmp); qpt = (Q_ELEMENT_WHAT *) malloc(sizeof(double)); while (fgets(s, 99, stdin) != NULL) { switch (s[0]) { case 'n': n = atoi(strtok(s+1, " ")); printf("n: %d\n", n); if (n < 1) break; qpt = (Q_ELEMENT_WHAT *) malloc(n * sizeof(Q_ELEMENT_WHAT)); printf("enqueued "); for (i = 0; i < n; i++) { qpt[i] = atof(strtok(NULL, " \n")); printf("%g ", qpt[i]); } printf("\n"); enqueue(q, qpt, n); break; case 'e': *qpt = atof(s+1); enqueue(q, qpt, 1); break; case 'd': printf("d %g\n", dequeue(q)); break; case 'p': print_queue(q); break; case 'q': free_queue(q); exit(0); break; default: printf("%s", Q_HELP); break; } } return 0; }
void MyInitThreads () { int i; int setjum_ret; thread[0].valid = 1; /* the initial thread is 0 */ thread[0].clean = 1; for (i = 1; i < MAXTHREADS; i++) { /* all other threads invalid */ thread[i].valid = 0; thread[i].clean = 1; } MyInitThreadsCalled = 1; init_queue(&tid_queue); enqueue(&tid_queue, 0); char s[STACKSIZE]; setjum_ret = setjmp(thread[0].clean_env); if(DEBUG == 1) Printf("thread %d env %d\n", 0, thread[0].clean_env); if( setjum_ret == 0){ if (((int) &s[STACKSIZE-1]) - ((int) &s[0]) + 1 != STACKSIZE) { if(DEBUG == 1) Printf ("Stack space reservation failed\n"); Exit (); } setStackSpace(MAXTHREADS - 1); }else if(setjum_ret == 2){ if(DEBUG == 1) Printf("finish carving stack\n"); return; }else{ thread[0].clean = 0; if(DEBUG == 1) Printf("setting jump for thread %d\n", 0); if (setjmp (thread[0].env) == 0) { /* save context of 1 */ if(DEBUG == 1) Printf("setting env as %d\n", thread[0].env); longjmp (thread[current_tid].env, 1); /* back to thread 0 */ } void (*f)() = thread[current_tid].func; /* f saves func on top of stack */ int p = thread[current_tid].param; /* p saves param on top of stack */ /* here when thread 1 is scheduled for the first time */ (*f) (p); /* execute func (param) */ MyExitThread(); return; } }
int main() { queue_t cb; init_queue(&cb); int j; int i; for (i = 0; i < QUEUE_SIZE; i++) enqueue(&cb, i); for (i = 0; i < QUEUE_SIZE; i++) { j = dequeue(&cb); printf("%d\n", j); } return 0; }
int main(int argc,char *argv[]) { init_queue(&buf); pthread_t producer,consumer1,consumer2; pthread_create(&producer,NULL,producer,NULL); pthread_create(&consumer1,NULL,consumer,NULL); pthread_create(&consumer2,NULL,consumer,NULL); pthread_join(&producer,NULL); pthread_join(&consumer1,NULL); pthread_join(&consumer2,NULL); destory_queue(&buf); }
int main(void){ struct node *aux; struct queue q; size_t i; init_queue(&q); for(i=0; i<10; i++){ insert(&q, (void *)i); } while((aux = dequeue(&q))){ printf("--> %zu\n", (size_t)aux->data); free(aux); } return 0; }
int main(int argc, char** argv) { init_queue(); int i; long long sum, *arr; printf("Usage: %s numelements numthreads\n", argv[0]); if (argc > 3) { abort(); } if (argc > 2) numelems = atoi(argv[1]); if (argc == 3) numthreads = atoi(argv[2]); // ---------------------------------------- arr = (long long*)malloc(sizeof(long long) * (numthreads/2)); local_block_t *q = init_thread(); printf("Q created and initialized: %p\n",q); printf("Starting %d producers and consumers, each will pass %d elements.\n", numthreads/2, numelems); for (i=0; i<numthreads/2; i++) cilk_spawn producer(q); for (i=0; i<numthreads/2; i++) arr[i] = cilk_spawn consumer(q); cilk_sync; printf("All producers and consumers finished.\n"); sum = 0; for (i=0; i<numthreads/2; i++) sum += arr[i]; printf("Total: %ld\n", sum); // Default parameters should result in: 1999998000000 if ( numelems == 1000000 && numthreads == 8 && sum != 1999998000000) { printf("ERROR: ANSWER NOT CORRECT, EXPECTING %ld\n", 1999998000000); fflush(stdout); abort(); } }
int main() { int x; pnode queue = NULL; init_queue(&queue); while (1) { scanf("%d", &x); queue_in(queue, x); show_queue(queue); scanf("%d", &x); queue_in(queue, x); show_queue(queue); queue_out(queue); show_queue(queue); printf("***************************\n"); } return 0; }
int init_supervisor(supervisor_thread_t *thiz, tesr_config_t *config) { LOG_LOC; int ret = 0; if(thiz && config) { thiz->config = config; if(thiz->config->num_workers == 0) { LOG_WARN("you are running in single threaded mode (num_workers=0)\n"); } ret = bind_dgram_socket(&thiz->sd, &thiz->addr, thiz->config->recv_port); if (ret == 0) { LOG_ERROR("could not bind dgram socket\n"); exit(EXIT_FAILURE); } else { thiz->queue = create_queue(); init_queue(thiz->queue); connect_pipe(&thiz->int_fd, &thiz->ext_fd); thiz->event_loop = EV_DEFAULT; //or ev_default_loop (0); //Set up rate limiting thiz->rate_limiter = create_rate_limiter(); init_rate_limiter(thiz->rate_limiter, thiz->config->irl_max, thiz->config->irl_inactivity_timeout, thiz->config->irl_garbage_collect_count); //Initialize pthread thiz->next_thread_idx = 0; thiz->worker_threads = create_worker_array(thiz->config->num_workers); //We must initialize workers last as we pass the supervisor data to them int th=0; for(th=0; th < thiz->config->num_workers; th++) { thiz->worker_threads[th] = create_worker(); init_worker(thiz->worker_threads[th], thiz, th); pthread_attr_t attr; pthread_attr_init(&attr); pthread_create(&thiz->worker_threads[th]->thread, &attr, worker_thread_run, thiz->worker_threads[th]); } ev_io_init(&thiz->udp_read_watcher, udp_read_cb, thiz->sd, EV_READ); ev_io_init(&thiz->inbox_watcher, udp_write_cb, thiz->int_fd, EV_READ); ev_signal_init(&thiz->sigint_watcher, sigint_cb, SIGINT); ev_signal_init(&thiz->sigchld_watcher, sigchld_cb, SIGCHLD); } } return ret; }
int main(int argc, char *argv[]) { init_queue(); put(10); put(9); put(8); put(7); put(8); put(9); put(2); put(110); put(13094); put(989899); print_queue(); get(); get(); get(); print_queue(); return 0; }
int main(int argc, char *argv[]) { QUEUE Q; int val; init_queue(&Q); en_queue(&Q,1); en_queue(&Q,2); en_queue(&Q,3); en_queue(&Q,4); en_queue(&Q,5); traverse_queue(&Q); if(del_queue(&Q,&val)) printf("出队成功。出队元素为:%d\n",val); else printf("抱歉!出队失败。\n"); traverse_queue(&Q); return 0; }
int main() { // Example use of priority queue pqueue queue; node n1, n2, n3; n1.data = 1; n1.cost = 1; n2.data = 0; n2.cost = 0; n3.data = 3; n3.cost = 3; init_queue(&queue); enqueue(&queue, &n1); enqueue(&queue, &n2); enqueue(&queue, &n3); print_queue(&queue); return 0; }
/* queue_test: test queue functions, data_type_s is int here */ static void queue_test(void) { struct queue *q; q = init_queue(); print_queue(q); enqueue(q, 1); enqueue(q, 2); enqueue(q, 3); print_queue(q); dequeue(q); enqueue(q, 4); print_queue(q); dequeue(q); print_queue(q); dequeue(q); print_queue(q); dequeue(q); print_queue(q); }
/** Constructor */ Virtio::Queue::Queue(uint16_t size, uint16_t q_index, uint16_t iobase) : _size(size),_size_bytes(virtq_size(size)),_iobase(iobase), _free_head(0), _num_added(0),_last_used_idx(0),_pci_index(q_index), _data_handler(delegate<int(uint8_t*,int)>(empty_handler)) { // Allocate page-aligned size and clear it void* buffer = memalign(PAGE_SIZE, _size_bytes); memset(buffer, 0, _size_bytes); debug(">>> Virtio Queue of size %i (%li bytes) initializing \n", _size,_size_bytes); init_queue(size,buffer); // Chain buffers debug("\t * Chaining buffers \n"); for (int i=0; i<size; i++) _queue.desc[i].next = i+1; _queue.desc[size -1].next = 0; debug(" >> Virtio Queue setup complete. \n"); }
// set up the stream for appropriate useage void init_stream (Args *args, pthread_t *thread, Stream *s, void *data) { // Null protection needed? if (s != NULL) { s->next = NULL; s->ce = NULL; s->args = data; s->id = idcnt++; s->last_pos = -1; s->first_pos = 0; s->thread = thread; init_queue(&s->buffer); pthread_mutex_init(&s->mutex, NULL); pthread_cond_init(&s->full, NULL); pthread_cond_init(&s->empty, NULL); } args->self = s; args->prod = NULL; }
void bfs(graph *g, int start) { queue q; init_queue(&q); enqueue(&q, start); discovered[start] = true; while (empty_queue(&q) == false) { int v = dequeue(&q); process_vertex_early(v); processed[v] = true; edgenode *p = g->edges[v]; while (p != NULL) { int y = p->y; if (valid_edge(p)) { if ((!processed[y]) || g->directed) { process_edge(v, y); } if (!discovered[y]) { enqueue(&q, y); discovered[y] = true; parent[y] = v; } } p = p->next; } process_vertex_late(v); } }
main(void) { int n; init_queue(); enqueue(1); print_queue(); enqueue(2); print_queue(); enqueue(3); print_queue(); n = dequeue(); print_queue(); enqueue(4); print_queue(); }
int main() { int i; init_queue(); for(i=1;i<10;i++){ append_tail(2*i); } print_queue(); for(i=1;i<6;i++){ delete_head(); } print_queue(); for(i=15;i<30;i++){ append_tail(2*i); } print_queue(); return 0; }
/* Here we can finally define the main function to put everything together */ int main(int argc, char* argv[]) { int num_producer, num_consumer ; int i ; if(argc != 3) { printf("Usage: %s <# producer> <# consumer>\n", argv[0]) ; return 1 ; } num_producer = atoi(argv[1]) ; num_consumer = atoi(argv[2]) ; /* we need to initialize the global queue buffer */ init_queue(&fifo_buffer) ; /* you need to replace the following function calls with actual thread launching */ /* we create num_consumer consumers first */ printf("Creating %d consumers\n", num_consumer) ; for(i=0;i<num_consumer;++i) { consumer( (void*)i); } printf("Creating %d producers\n", num_producer) ; /* we create num_producer producers then */ for(i=0;i<num_producer;++i) { producer( (void*)i); } /* since consumers never quit, we'll notify the user to */ /* end the program by press "Ctrl-C" */ printf("********************************************************\n") ; printf("* All Producers finished, please terminate the program *\n") ; printf("* by \"Ctrl-C\" when all consumers print their items *\n") ; printf("********************************************************\n") ; return 0 ; }
int main(int argc, char *argv[]){ QUEUE Q; int i; Q = init_queue(8); for(i=0; i<6; i++){ enqueue(i, Q); } for(i=0; i<6; i++){ printf("%d ", dequeue(Q)); } printf("\n"); dispose_queue(Q); return 0; }
void main(void) { int i; init_queue(); printf("\nPut 5, 4, 7, 8, 2, 1"); put(5); put(4); put(7); put(8); put(2); put(1); print_queue(); printf("\nGet"); i = get(); print_queue(); printf("\n getting value is %d", i); printf("\nPut 3, 2, 5, 7"); put(3); put(2); put(5); put(7); print_queue(); printf("\nNow queue is full, put 3"); put(3); print_queue(); printf("\nInitialize queue"); clear_queue(); print_queue(); printf("\nNow queue is empty, get"); i = get(); print_queue(); printf("\n getting value is %d", i); }
void multiprogram(FILE *ptr_file, queue *q_ready, queue *q_process) { process *curr_proc = NULL; /* the current process removed from the front of the ready queue */ char **token_arr = NULL; /* array of tokens used to store the current instruction */ char buffer[256]; /* buffer used to store the string read from the input file */ queue *q_wait = init_queue(); while (!(empty_queue(q_ready))) { /* continue executing processes until no processes remain in the ready state */ curr_proc = (process *) dequeue(q_ready); fsetpos(ptr_file, &(curr_proc->process_fpos)); /* update the file read position curr_proc's next instruction */ fgets(buffer, 256, ptr_file); token_arr = tokenize_str(buffer); int continue_processing = TRUE; int ret_val = -999; while (continue_processing) { ret_val = process_instruction(curr_proc, q_ready, q_process, q_wait, token_arr); free_token(token_arr); if (ret_val == 0) { /* alloc/dealloc was successful continue processing at next instruction */ fgetpos(ptr_file, &(curr_proc->process_fpos)); fgets(buffer, 256, ptr_file); token_arr = tokenize_str(buffer); continue_processing = TRUE; } else if (ret_val == 1) { /* a "SL #" instruction was encountered continue processing later */ fgetpos(ptr_file, &(curr_proc->process_fpos)); /* update the processes fpos */ continue_processing = FALSE; } else if (ret_val == -1) { /* alloc/dealloc failed continue processing later at same instruction */ continue_processing = FALSE; } } } free_queue(q_wait); return; }
int main(void){ printf("文字を一つ入力して下さい\n"); printf("i: 後に続いて入力されたデータをキューの末尾に挿入する.\n"); printf("キューが満杯の場合はその旨をディスプレイに出力する.\n"); printf("d: キューの先頭のデータを削除する.\n"); printf("キューが空の場合はその旨をディスプレイに出力する.\n"); printf("p: キューのデータを先頭から順番にディスプレイに出力する.\n"); printf("q: 実行を終了する.\n"); printf("上記以外: i,d,p,または q を入力する旨のメッセージをディスプレイに出力する.\n"); int n; char c; init_queue(); while(1){ printf("\n>>>"); scanf("%1s", &c); switch (c) { case 'i': scanf("%d", &n); enqueue(n); break; case 'd': dequeue(); break; case 'p': print_queue(); break; case 'q': exit(0); break; default: printf("i,d,p,またはqを入力して下さい。\n"); } } }
// Initializes an arduino. // Parameters: the arduino pointer // the device the arduino is connected to // the device the joystick is connected to // return: 0 on success, non-zero on failure. int init_arduino(rov_arduino *a,const char *af,const char *jf){ struct termios topts; if ((a->fd = open(af,O_RDWR | O_NONBLOCK)) == -1){ perror("init_arduino: could not open af"); return -1; } if (tcgetattr(a->fd,&topts) < 0){ perror("init_arduino: could not get term attr"); return -1; } // Sets up 'the terminal' settings, we treat the arduino as a tty. cfsetispeed(&topts,B9600); cfsetospeed(&topts,B9600); topts.c_cflag &= ~PARENB; topts.c_cflag &= ~CSTOPB; topts.c_cflag &= ~CSIZE; topts.c_cflag |= CS8; topts.c_cflag &= ~CRTSCTS; topts.c_cflag |= CREAD | CLOCAL; topts.c_iflag &= ~(IXON | IXOFF | IXANY); topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); topts.c_oflag &= ~OPOST; topts.c_cc[VMIN] = 0; // Minimum number of characters to read. topts.c_cc[VTIME] = 0; // VTIME * 0.1 = Time to wait for input. tcsetattr(a->fd,TCSANOW,&topts); if (tcsetattr(a->fd,TCSAFLUSH,&topts) < 0){ perror("init_arduino: could not set term attributes"); return -1; } if (!init_joystick(&a->joystick,jf)){ perror("init_arduino: could not read joystick file"); return -1; } init_ctrlstate(&a->ctrl); init_pinlayout(&a->layout); init_queue(&a->queue,a,2500,100); return 0; }
int _myrg_init_queue(MYRG_INFO *info,int inx,enum ha_rkey_function search_flag) { int error=0; QUEUE *q= &(info->by_key); if (inx < (int) info->keys) { if (!is_queue_inited(q)) { if (init_queue(q,info->tables, 0, (myisam_readnext_vec[search_flag] == SEARCH_SMALLER), queue_key_cmp, info->open_tables->table->s->keyinfo[inx].seg)) error=my_errno; } else { if (reinit_queue(q,info->tables, 0, (myisam_readnext_vec[search_flag] == SEARCH_SMALLER), queue_key_cmp, info->open_tables->table->s->keyinfo[inx].seg)) error=my_errno; } } else { /* inx may be bigger than info->keys if there are no underlying tables defined. In this case we should return empty result. As we check for underlying tables conformance when we open a table, we may not enter this branch with underlying table that has less keys than merge table have. */ DBUG_ASSERT(!info->tables); error= my_errno= HA_ERR_END_OF_FILE; } return error; }
int gen_children(int start, int dest) { int d[4], v, p, found; queue q; dprintf("start %d, search for %d\n", start, dest); init_queue(&q); p = start; enqueue(&q, p); while (!queue_empty(&q)) { v = dequeue(&q); if (visited[v]) continue; visited[v] = 1; dprintf("dequeued: %d\n", v); if (v == dest) { dprintf("found it!!! %d, press count %d\n", dest, press_cnt[dest]); return press_cnt[dest]; } disassemble(v, d); rollup(&q, v, d); rolldown(&q, v, d); } return 0; }
Queue *trie_find(Trie *trie, void *element) { TrieNode *node = trie->root; char *p = trie->findPrefixFunc(element); if (prefix_out_of_range(p)) { HashNode *hashNode = get_table_element(trie->table, p, hash_code_string); Queue *queue = init_queue(); while (hashNode != NULL) { enqueue(queue, hashNode->element); hashNode = hashNode->next; } return queue; } int index; while (*p && node) { index = *p - BASE_CHAR; node = node->next[index]; p++; } if (node == NULL) return NULL; return node->queue; }
/* main function */ void * run_server_args(void * port_number) { int port; if (!port_number) port = LISTEN_PORT_NUM; else port = *(int *) port_number; if (port <= 1024) port = LISTEN_PORT_NUM; init_queue(&q); struct sockaddr sock_addr; int listen_fd = init(port, &sock_addr); is_init = true; wait_for_listen(listen_fd, &sock_addr); return NULL; }
void test_queue (void){ struct queue queue; struct foo buf[5]; init_queue(&queue, &buf, sizeof(struct foo), 5); ((struct foo *) enqueue(&queue))->a = 1; ((struct foo *) enqueue(&queue))->a = 2; ((struct foo *) enqueue(&queue))->a = 3; ((struct foo *) enqueue(&queue))->a = 4; ((struct foo *) enqueue(&queue))->a = 5; EXPECT(!enqueue(&queue)); EXPECT(!enqueue(&queue)); EXPECT(((struct foo *) dequeue(&queue))->a == 1); EXPECT(((struct foo *) dequeue(&queue))->a == 2); ((struct foo *) enqueue(&queue))->a = 6; ((struct foo *) enqueue(&queue))->a = 7; EXPECT(((struct foo *) dequeue(&queue))->a == 3); EXPECT(((struct foo *) dequeue(&queue))->a == 4); EXPECT(((struct foo *) dequeue(&queue))->a == 5); EXPECT(((struct foo *) dequeue(&queue))->a == 6); EXPECT(((struct foo *) dequeue(&queue))->a == 7); EXPECT(!dequeue(&queue)); EXPECT(!dequeue(&queue)); }