void first_fit_step(double items[], unsigned int n, item_number item,packing_list * result){ packing_container * cont = result->list; packing * pack; double item_size = items[item]; while( cont ){ if( packing_has_room_for(cont->value, items, item_size) ){ // this will fit in here if( cont->quantity == 1 ){ // modify this packing //TODO: maybe the resulting packing is already present in the packing_list insert_item(cont->value,item); }else{ // take one packing and reinsert a clone cont->quantity--; pack = clone_packing(cont->value); insert_item(pack,item); insert_packing(result, pack, 1); } return ; } cont = cont->next; } // the item didn't fit anywhere pack = alloc_packing(); insert_item(pack, item); insert_packing(result, pack, 1); }
int remove_item_test(void) { clear_queue(); insert_item(42); _assert(front() == 42); _assert(queue_size() == 1); _assert(remove_item() == 42); _assert(queue_size() == 0); _assert(is_empty() == true); insert_item(12); insert_item(13); insert_item(14); _assert(front() == 12); _assert(rear() == 14); _assert(queue_size() == 3); _assert(remove_item() == 12); _assert(front() == 13); _assert(rear() == 14); _assert(queue_size() == 2); _assert(remove_item() == 13); _assert(front() == 14); _assert(rear() == 14); _assert(queue_size() == 1); clear_queue(); return 0; }
int main(){ srand(time(0)); //Creating memory managers mm_t *MM = malloc(sizeof(mm_t)); mm_init(MM,1500,sizeof(node)); mm_t *MM2 = malloc(sizeof(mm_t)); mm_init(MM2,3,sizeof(dl_list)); //Creating Lists dl_list *dl_1 = mm_get(MM2); dl_list *dl_2 = mm_get(MM2); dl_list *dl_3 = mm_get(MM2); dl_list_init(dl_1); dl_list_init(dl_2); dl_list_init(dl_3); int i; for(i = 0; i<500; i++) insert_item(dl_1,i,MM); print_list(dl_1); for(i = 0; i<500; i++) insert_item(dl_2,rand()%500,MM); print_list(dl_2); for(i = 0; i<500; i++) insert_item(dl_3,get_element(dl_1,i)+get_element(dl_2,i),MM); print_list(dl_3); //Deleting items from list 1 for(i = 0; i<100; i++) delete_item(dl_1,get_element(dl_3,i),MM); print_list(dl_1); //Joining lists join_lists(dl_2,dl_1); join_lists(dl_3,dl_2); print_list(dl_3); empty_list(dl_3,MM); //Deallocating and freeing mm_put(MM,dl_1); mm_put(MM,dl_2); mm_put(MM,dl_3); free(MM); free(MM2); return 0; }
/** * Create a linked list of crash entries as found in CRASH_DIR. If * the previous_flag parameter is set, the list will include crash * entries that include the SUCCESSFULLY_PROCESSED_TAGFILE file. If * the parameter is set to zero, then the list will include crash * entries that do NOT include the tag file. * * \param previous_flag If set, require the * \returns Linked list of entries found, or NULL if none. */ static llist* find_crash_entries(int previous_flag) { llist* l = NULL; DIR *dp = NULL; struct dirent *ep = NULL; struct stat sb; char crash_dir[PATH_MAX]; memset(&sb, 0, sizeof(struct stat)); get_crash_dir(crash_dir, PATH_MAX); if (does_exist(crash_dir)) { debug_log("Crash directory exists (%s), examining for candidates to process.", crash_dir); dp = opendir(crash_dir); if (NULL != dp) { while ((ep = readdir(dp))) { if ((strcmp(".", ep->d_name) == 0) || (strcmp("..", ep->d_name) == 0)) { continue; } /* verify that the subdirectory contains a vmcore file */ if (is_crash_dir(crash_dir, ep->d_name)) { char touch_file[PATH_MAX]; int stat_result = 0; memset(touch_file, 0, PATH_MAX); snprintf(touch_file, PATH_MAX-1, "%s/%s/%s", crash_dir, ep->d_name, SUCCESSFULLY_PROCESSED_TAGFILE); stat_result = stat(touch_file, &sb); if (previous_flag && !stat_result) { // looking for previous, and found one l = insert_item(ep->d_name, l); debug_log("Adding previously processed: %s/%s.", crash_dir, ep->d_name); } else { if (!previous_flag && stat_result) { // looking for new, and did not find touch file l = insert_item(ep->d_name, l); debug_log("Adding candidate: %s/%s.", crash_dir, ep->d_name); } } } else { /* skip if there is no vmcore file */ debug_log ("Skipping: %s (no vmcore found).", ep->d_name); } } closedir(dp); } else { perror ("Couldn't open crash directory."); } } else { debug_log ("Crash directory does not exist (%s), skipping.", crash_dir); } return l; }
void *producer(void *param) { int item; pthread_t id = pthread_self(); printf("Producer thread created. ID: %lu\n", id); while(1) { /* Sleep for a random time */ usleep(rand() % 10); // Produce new resource buffer_item item = rand(); sem_wait(&empty); pthread_mutex_lock(&mutex); // Add resource to empty buffer printf("Inserting item. %d\n", item); insert_item(item); pthread_mutex_unlock(&mutex); sem_post(&full); } }
void *producer(void *param){ buffer_item item; while (1){ sleep(rand() % 10); //sleep for random period of time item = rand(); //generate random number printf("Thread %d waiting for an empty space\n", syscall(SYS_gettid)); fflush(stdout); sem_wait(&empty); //get empty lock printf("Thread %d waiting for critical section\n", syscall(SYS_gettid)); fflush(stdout); pthread_mutex_lock(&mutex); //get mutex lock printf("Thread %d has entered critical section\n", syscall(SYS_gettid)); fflush(stdout); if (insert_item(item)){ fprintf(stderr, " Producer report error condition\n"); }else{ printf("producer produced %d\n", item); fflush(stdout); } pthread_mutex_unlock(&mutex); //release mutex lock printf("Thread %d has exited critical section\n", syscall(SYS_gettid)); fflush(stdout); sem_post(&full); //signal full printf("Thread %d has signaled for a full space\n", syscall(SYS_gettid)); fflush(stdout); } }
/* Function: get_assigned ----------------------- Inputs Item names to this specified source structure based on the presented hint line at the first row. Parameter: file - source file name - source name src - source structure */ int get_assigned(FILE *file, char *name, Source *src) { char line[MAX_CHARS]; char **seg = (char**)malloc(sizeof(char*) * 3); char *token = NULL; src->items = (Item*)malloc(src->K * sizeof(Item)); src->items->length = 0; while(fgets(line, MAX_CHARS, file) != NULL) { seg[0] = strtok_s(line, _SEP, &token); if (seg[0] != NULL) { seg[1] = strtok_s(NULL, _SEP, &token); if (seg[1] != NULL) { seg[2] = strtok_s(NULL, _SEP, &token); if (seg[2] != NULL) { if (insert_item(name, src, seg[0])) { return 1; } } } } } free(seg); return 0; }
void *producer(void *param) { /* Variables */ buffer_item item; while(1) { sleep(rand()); item = (rand()); /* Generates random item */ sem_wait(&cEmpty); /* Lock empty semaphore if not zero */ pthread_mutex_lock(&mutex); if(insert_item(item)) { fprintf(stderr, "Producer error."); } else { printf("Producer produced %d\n", item); } pthread_mutex_unlock(&mutex); sem_post(&cFull); /* Increment semaphore for # of full */ } }
static ContentItem *parse_hhc(HHInfo *info, IStream *str, ContentItem *hhc_root, insert_type_t *insert_type) { stream_t stream; strbuf_t node, node_name; ContentItem *ret = NULL, *prev = NULL; *insert_type = INSERT_NEXT; strbuf_init(&node); strbuf_init(&node_name); stream_init(&stream, str); while(next_node(&stream, &node)) { get_node_name(&node, &node_name); TRACE("%s\n", node.buf); if(!strcasecmp(node_name.buf, "ul")) { ContentItem *item = parse_ul(info, &stream, hhc_root); prev = insert_item(prev, item, INSERT_CHILD); if(!ret) ret = prev; *insert_type = INSERT_CHILD; } strbuf_zero(&node); } strbuf_free(&node); strbuf_free(&node_name); return ret; }
void *producer(void *param) { /* create a variable for a randomly generated buffer item */ buffer_item buff_item = 0; /* create a variable for randomly generated period of time that producer will sleep just prior to producing each item */ int counter = 0; /* infinite loop */ while(TRUE){ /* generate a random number between 0 and 5 */ counter = rand()%6; /* sleep for that period of time */ sleep(counter); /* generate a random number and store in variable created above */ buff_item = rand(); /* Insert the item into the buffer using the insert_item method (that you have to create, see below)*/ printf("Producer produced %d\n", buff_item); insert_item(buff_item); } }
//Thread for Producer process void *producer(void *param) { buffer_item item; item=rand(); insert_item(item); printf("Item - %d successfully produced",item); }
static void build_LR_sets(Grammar *g) { State *s = new_state(); insert_item(s, g->productions.v[0]->rules.v[0]->elems.v[0]); build_closure(g, s); build_states_for_each_production(g); build_new_states(g); sort_Gotos(g); }
int main() { sll_item_t *list = create_item(NULL); int i; for (i = 1; i; ++i) { insert_item(insert_item(&list)); } __VERIFIER_plot("01-cyclic-sll-ready"); for (i = 1; i; ++i) { destroy_cyclic_sll(&list); } __VERIFIER_plot("02-cyclic-sll-gone"); return 0; }
static ContentItem *parse_ul(HHInfo *info, stream_t *stream, ContentItem *hhc_root) { strbuf_t node, node_name; ContentItem *ret = NULL, *prev = NULL, *new_item = NULL; insert_type_t it; strbuf_init(&node); strbuf_init(&node_name); while(next_node(stream, &node)) { get_node_name(&node, &node_name); TRACE("%s\n", node.buf); if(!strcasecmp(node_name.buf, "object")) { const char *ptr; int len; static const char sz_text_sitemap[] = "text/sitemap"; ptr = get_attr(node.buf, "type", &len); if(ptr && len == sizeof(sz_text_sitemap)-1 && !memcmp(ptr, sz_text_sitemap, len)) { new_item = parse_sitemap_object(info, stream, hhc_root, &it); prev = insert_item(prev, new_item, it); if(!ret) ret = prev; } }else if(!strcasecmp(node_name.buf, "ul")) { new_item = parse_ul(info, stream, hhc_root); insert_item(prev, new_item, INSERT_CHILD); }else if(!strcasecmp(node_name.buf, "/ul")) { break; } strbuf_zero(&node); } strbuf_free(&node); strbuf_free(&node_name); return ret; }
int main(int argc, char const *argv[]) { int n, i, temp; char c = '\0'; create_heap(516); scanf("%d", &n); for(i = 0; i < n; i++) { scanf("%d", &temp); insert_item(temp); } while (c != 'f') { scanf("%c", &c); switch(c) { case 'i': scanf("%d", &temp); insert_item(temp); break; case 'm': printf("%d\n", get_min()); //print_heap(); break; case 'M': printf("%d\n", get_max()); //print_heap(); break; } } printf("\n"); printf("Min-heap: "); print_heap_min(); printf("Max-heap: "); print_heap_max(); return 0; }
static void build_states_for_each_production(Grammar *g) { int i; for (i = 0; i < g->productions.n; i++) if (!g->productions.v[i]->internal && g->productions.v[i]->elem) { State *s = new_state(); insert_item(s, g->productions.v[i]->elem); g->productions.v[i]->state = build_closure(g, s); } }
static void copy_items(const pc_hash_t *hash, struct pc_hslot_s *slots, int alloc) { const struct pc_hslot_s *current = hash->slots; const struct pc_hslot_s *last = &hash->slots[hash->alloc]; for ( ; current < last; ++current) if (current->key != NULL && current->key != SLOT_DELETED) insert_item(current, slots, alloc); }
/* Parse the input string statement * */ void parse_inputstring(void) { TextNode *input_node = curr_node; char *name; InputItem *item; int size; char *default_value; gStringValueOk = 0; /* first get the name */ input_node->type = token.type; get_expected_token(openaxiom_Lbrace_token); name = get_input_string(); input_node->data.text = alloc_string(name); /* now get the width */ get_expected_token(openaxiom_Lbrace_token); get_expected_token(openaxiom_Word_token); get_expected_token(openaxiom_Rbrace_token); size = atoi(token.id); if (size < 0) { fprintf(stderr, "Illegal size in Input string\n"); longjmp(jmpbuf, 1); } /* get the default value */ get_expected_token(openaxiom_Lbrace_token); default_value = get_input_string(); /** now I need to malloc space for the input stuff **/ item = (InputItem *) halloc(sizeof(InputItem), "InputItem"); /* Now store all the string info */ item->name = (char *) halloc((strlen(input_node->data.text) + 1) * (sizeof(char)),"parse_inputstring"); strcpy(item->name, input_node->data.text); item->size = size; item->entered = 0; item->next = NULL; initialize_default(item, default_value); /** Now that I have all the structures made, lets make the window, and add the item to the list ****/ input_node->link = make_input_window(item); if (!make_input_file) item->win = input_node->link->win; /* TTT */ insert_item(item); gStringValueOk = 1; curr_node = input_node; return ; }
void main() { int len, pos, *del; plist pl = NULL; del = (int*)malloc(sizeof(int)); pl = init_list(); isEmpty_list(pl); insert_item(pl, 1, 1); insert_item(pl, 2, 3); insert_item(pl, 3, 5); insert_item(pl, 4, 7); insert_item(pl, 5, 9); insert_item(pl, 6, 11); display(pl); len = len_list(pl); printf("link list len: %d\n", len); pos = locate_item(pl, 7); printf("num 7 pos: %d\n", pos); delete_item(pl, 3, del); printf("delete pos 3 num: %d\n", *del); display(pl); printf("link list traverse...\n"); pl = traverse_list(pl); display(pl); destroy_list(pl); getch(); }
void producer(void) { int item; while(TRUE) { item = produce_item(); if (count == N) return; insert_item(item); count = count + 1; } }
int insert_item_test(void) { clear_queue(); _assert(queue_size() == 0); insert_item(42); _assert(front() == 42); _assert(rear() == 42); _assert(queue_size() == 1); insert_item(10); _assert(front() == 42); _assert(rear() == 10); _assert(queue_size() == 2); insert_item(-1); _assert(front() == 42); _assert(rear() == -1); _assert(queue_size() == 3); clear_queue(); return 0; }
void producer(void) { int item; while (TRUE){ /* TRUE is the constant 1 */ item = produce_item(); /* generate something to put in buffer */ down(&empty); /* decrement empty count */ down(&mutex); /* enter critical region */ insert_item(item); /* put new item in buffer */ up(&mutex); /* leave critical region */ up(&full); /* increment count of full slots */ } }
void pc_hash_set(pc_hash_t *hash, const void *key, size_t klen, void *value) { uint_fast32_t hvalue = compute_hvalue(key, klen); struct pc_hslot_s item = { key, klen, hvalue, value }; pc_bool_t existed; maybe_grow(hash); existed = insert_item(&item, hash->slots, hash->alloc); if (!existed && value != NULL) ++hash->count; else if (existed && value == NULL) --hash->count; }
void dijkstra(graph* g, int source, item** ph) { int infinity = 100000000; int vertices = g->node_count; heap* h = make_heap(); for (int i = 0; i < vertices; i++) { item* itm = (item*)malloc(sizeof(item)); if (i == source) { itm->key = 0; } else { itm->key = infinity; } int* item_val = (int*)malloc(sizeof(int)); *item_val = i; itm->value = item_val; ph[i] = itm; insert_item(itm, h); } item* val = find_min(h); delete_min(h); while (val != NULL) { if (val->key == infinity) { break; } int u_index = *((int*)(val->value)); g_node* n = g->nodes[u_index]; for (int v_index = 0; v_index < n->edge_count; v_index++) { edge* e = n->edges[v_index]; int dist_between = e->distance; item* v = ph[e->target->id]; int alt = val->key + dist_between; if (alt < v->key) { int delta = v->key - alt; decrease_key(delta, v, h); } } val = find_min(h); delete_min(h); } }
/** * Combined the contents to two lists, eliminating duplicates * as necessary. * * NOTE: This uses the does_list_contain helper function which will * recursivly check the current list for matching contents several * times over. Efficient this is not. However, for these purposes * this is AOK. This daemon only runs on boot up, and the number of * crashes seen will typically be very small. * * \param a First list to combine * \param b Second list to combine * \returns Combined list of elements from lists a and b * */ static llist* combine_lists_no_dup (llist* a, llist* b) { llist* combined = NULL; llist* tmp = NULL; if (NULL == a && NULL == b) { return NULL; } tmp = a; while (NULL != tmp) { combined = insert_item(tmp->entry, combined); tmp = tmp->next; } tmp = b; while (NULL != tmp) { if (!does_list_contain(tmp->entry, combined)) { combined = insert_item(tmp->entry, combined); } tmp = tmp->next; } return combined; }
void * producer(void *param){ buffer_item item; while(1){ /*sleep for a random period of time */ sleep(rand()%100); /*generate a random number */ item = rand(); if( insert_item(item)) printf("report error condition\n"); else printf("producer produced %d\n", item); } pthread_exit(0); }
grub_autolist_t grub_autolist_load (const char *name) { grub_autolist_t result = 0; const char *prefix; prefix = grub_env_get ("prefix"); if (prefix) { char *filename; filename = grub_xasprintf ("%s/%s", prefix, name); if (filename) { grub_file_t file; file = grub_file_open (filename); if (file) { char *buf = NULL; for (;; grub_free (buf)) { char *p; buf = grub_getline (file); if (! buf) break; if (! grub_isgraph (buf[0])) continue; p = grub_strchr (buf, ':'); if (! p) continue; *p = '\0'; while (*++p == ' ') ; insert_item (&result, buf, p); } grub_file_close (file); } grub_free (filename); } } return result; }
int main(void) { Queue q = create_queue(37); insert_item(q, 2); assert(remove_item(q) == 2); assert(is_empty(q) == true); printf("This should exit with a failure.\n\n"); /* remove_item(q); */ front(q); rear(q); return 0; }
void *producerFunc(void *arg) // 生产者线程 { int item; while(true) { dispatch_semaphore_wait(empty, DISPATCH_TIME_FOREVER); dispatch_semaphore_wait(mutex, DISPATCH_TIME_FOREVER); item = product_NO++; insert_item(item); print_items(); dispatch_semaphore_signal(mutex); dispatch_semaphore_signal(full); sleep(3); } }
void *initProducer(void *t){ buffer_item item = 0; long id = (long)t; while(1){ if(shouldExit){pthread_exit(0);} //Sleep random period of time sleep(rand()%3); //Genereate random number for item size int temp = rand(); item = temp; //Insert and item if(insert_item(item,id)){printf("Production Successful for thread %lu\n\n\n",id);} else{printf("Error in production on thread %lu\n",id);} } }