static void BubbleForward(My402List *pList, My402ListElem **pp_elem1, My402ListElem **pp_elem2) /* (*pp_elem1) must be closer to First() than (*pp_elem2) */ { My402ListElem *elem1=(*pp_elem1), *elem2=(*pp_elem2); void *obj1=elem1->obj, *obj2=elem2->obj; My402ListElem *elem1prev=My402ListPrev(pList, elem1); /* My402ListElem *elem1next=My402ListNext(pList, elem1); */ /* My402ListElem *elem2prev=My402ListPrev(pList, elem2); */ My402ListElem *elem2next=My402ListNext(pList, elem2); My402ListUnlink(pList, elem1); My402ListUnlink(pList, elem2); if (elem1prev == NULL) { (void)My402ListPrepend(pList, obj2); *pp_elem1 = My402ListFirst(pList); } else { (void)My402ListInsertAfter(pList, obj2, elem1prev); *pp_elem1 = My402ListNext(pList, elem1prev); } if (elem2next == NULL) { (void)My402ListAppend(pList, obj1); *pp_elem2 = My402ListLast(pList); } else { (void)My402ListInsertBefore(pList, obj1, elem2next); *pp_elem2 = My402ListPrev(pList, elem2next); } }
extern int My402ListInsertBefore(My402List* List, void* insData, My402ListElem* givenNode) { My402ListElem *newNode = (My402ListElem *) malloc(sizeof(My402ListElem)); if(newNode != NULL) { newNode->obj = insData; if(givenNode == NULL) { return My402ListPrepend(List,insData); } else { My402ListElem *prevNode = givenNode->prev;//BE CAREFULL prevNode->next = newNode; newNode->next = givenNode; givenNode->prev = newNode; newNode->prev = prevNode; (List->num_members)++; return TRUE; } } return FALSE; }
int My402ListInsertBefore(My402List* list, void* obj, My402ListElem* elem) { My402ListElem* newItem = NULL; if(NULL == elem) return My402ListPrepend(list,obj); else { newItem = (My402ListElem*)malloc(sizeof(My402ListElem)); if(NULL == newItem) { //print error return FALSE; } newItem->obj = obj; elem->prev->next = newItem; newItem->prev = elem->prev; newItem->next = elem; elem->prev = newItem; ++list->num_members; return TRUE; } return FALSE; }
int My402ListInsertBefore(My402List *list, void *obj, My402ListElem *elem){ if(list != NULL){ if(elem == NULL){ if(My402ListPrepend(list, obj)) return 1; else return 0; } else{ My402ListElem *new_elem = (My402ListElem *)malloc(sizeof(My402ListElem)); if(new_elem == 0 || new_elem == NULL){ return 0; } else{ new_elem -> obj = obj; elem -> prev -> next = new_elem; new_elem -> prev = elem -> prev; new_elem -> next = elem; elem -> prev = new_elem; list -> num_members++; return 1; } } } else return 0; }
extern int My402ListInsertBefore(My402List*list, void*item, My402ListElem*elem) { if(elem==NULL) { int x; x=My402ListPrepend(list, item); if(x==1) return(1); else return(0); } else { My402ListElem *curr=NULL;//element that is to be inserted curr=malloc(sizeof(struct tagMy402ListElem)); curr->obj=item; curr->prev=elem->prev; curr->next=elem; elem->prev->next=curr; elem->prev=curr; list->num_members++; return(1); } return(0); }
int My402ListInsertBefore(My402List* list, void* new_obj, My402ListElem* elem) { // snaity check if (!elem) return My402ListPrepend(list, new_obj); My402ListElem* temp_elem = (My402ListElem*) malloc (sizeof(My402ListElem)); if (!temp_elem) return FALSE; temp_elem->next = elem; temp_elem->prev = elem->prev; temp_elem->obj = new_obj; (elem->prev)->next = temp_elem; elem->prev = temp_elem; list->num_members ++; return TRUE; }
int My402ListInsertBefore(My402List *pList, void *obj, My402ListElem *elem){ if(elem == NULL) return My402ListPrepend(pList, obj); else{ My402ListElem *temp = (My402ListElem *)malloc(sizeof(My402ListElem)); if(temp == NULL) return FALSE; temp->obj = obj; temp->next = elem; temp->prev = elem->prev; elem->prev->next = temp; elem->prev = temp; } pList->num_members++; return TRUE; }
int My402ListInsertBefore(My402List * list, void * data, My402ListElem * elem) { if(elem == NULL) return My402ListPrepend(list, data); else { My402ListElem * new_elem = (My402ListElem *)malloc(sizeof(My402ListElem)); if(!new_elem) return 0; My402ListElem * prev = elem -> prev; new_elem -> obj = data; prev -> next = new_elem; elem -> prev = new_elem; new_elem -> next = elem; new_elem -> prev = prev; list -> num_members++; return 1; } }
// Insert Before specified list Element int My402ListInsertBefore(My402List* myList, void* elem, My402ListElem* listElement) { My402ListElem *newElement = NULL; newElement = (My402ListElem *)malloc(sizeof(My402ListElem)); if(newElement != NULL) { if(listElement != NULL) { newElement->next = listElement; newElement->prev = listElement->prev; listElement->prev->next = newElement; listElement->prev = newElement; newElement->obj = elem; myList->num_members++; return TRUE; } else { return My402ListPrepend(myList, elem); } } return FALSE; }
int My402ListInsertBefore(My402List* q, void* x, My402ListElem* n) { if (n==NULL) { return My402ListPrepend(q,x); } My402ListElem* t= NULL; My402ListElem* temp1 = NULL; temp1 = (My402ListElem*)malloc(sizeof(My402ListElem)); t = (My402ListElem*)malloc(sizeof(My402ListElem)); if (temp1==NULL) { return FALSE; } //else //{ temp1->obj= x; t = n->prev; t->next=temp1; temp1->next=n; n->prev=temp1; temp1->prev=t; //(temp1->prev)->next=temp1; //} q->num_members++; return TRUE; }
int My402ListInsertBefore(My402List* list, void* obj, My402ListElem* element) { //if element is NULL then it is same as prepend short int result=0; if(element==NULL){ result=My402ListPrepend(list,obj); return result; } else { My402ListElem *newelement; newelement=(My402ListElem *)malloc(sizeof(My402ListElem)); if(newelement){ element->prev->next=newelement; newelement->prev=element->prev; element->prev=newelement; newelement->next=element; newelement->obj=obj; list->num_members++; return TRUE; } else return FALSE; } }
void * packet_arrival_thread_function(void * args){ pthread_sigmask(SIG_BLOCK, &set, 0); long local_packet_created_count = 0; long local_packet_added_count = 0; long local_packet_dropped_count = 0; float local_packet_arrival_time = 0.0; long local_tokens_needed_for_a_packet = 0; float local_packet_service_time = 0.0; float local_total_packet_arrival_time = 0.0; float local_total_packet_time_in_Q1 = 0.0; local_packet_arrival_time = packet_arrival_time; local_packet_service_time = packet_service_time; local_tokens_needed_for_a_packet = tokens_needed_for_a_packet; struct timeval previous_time, current_time; struct packet *local_arrival_thread_packet; int stop_flag = 0; while(1){ if(is_trace_driven_mode){ local_packet_arrival_time = **(tsfile_data + local_packet_created_count) / 1000.0; local_packet_service_time = *(*(tsfile_data + local_packet_created_count) + 2) / 1000.0; local_tokens_needed_for_a_packet = *(*(tsfile_data + local_packet_created_count) + 1); } gettimeofday(&previous_time, NULL); usleep(local_packet_arrival_time * 1000000.0); gettimeofday(¤t_time, NULL); local_total_packet_arrival_time = local_total_packet_arrival_time + calculate_time_difference(current_time,previous_time); local_packet_created_count++; /* Create a new packet */ local_arrival_thread_packet = malloc(sizeof(struct packet)); local_arrival_thread_packet->id = local_packet_created_count; local_arrival_thread_packet->packet_arrival_time = local_packet_arrival_time; local_arrival_thread_packet->packet_service_time = local_packet_service_time; local_arrival_thread_packet->tokens_needed = local_tokens_needed_for_a_packet; gettimeofday(&local_arrival_thread_packet->creation_time, NULL); gettimeofday(&local_arrival_thread_packet->updation_time, NULL); /* Lock the mutex */ pthread_mutex_lock(&mutex); /*if(cntrl_c_signal){ stop_flag = 1; } else{*/ { if(local_tokens_needed_for_a_packet > bucket_capacity){ printf("%012.3fms: p%ld arrives, needs %ld tokens, " "inter-arrival time = %.3fms, dropped\n", calculate_time_difference(current_time,emulation_start_time), local_packet_created_count,local_tokens_needed_for_a_packet, calculate_time_difference(current_time,previous_time)); local_packet_dropped_count++; free(local_arrival_thread_packet); } else { printf("%012.3fms: p%ld arrives, needs %ld tokens, " "inter-arrival time = %.3fms\n", calculate_time_difference(current_time,emulation_start_time), local_packet_created_count,local_tokens_needed_for_a_packet, calculate_time_difference(current_time,previous_time)); gettimeofday(&previous_time, NULL); /* Add the packet to Q1 */ My402ListPrepend(&Q1, local_arrival_thread_packet); gettimeofday(¤t_time, NULL); printf("%012.3fms: p%ld enters Q1\n", calculate_time_difference(current_time,emulation_start_time), local_packet_created_count); local_packet_added_count++; } local_arrival_thread_packet = NULL; if(!My402ListEmpty(&Q1)){ My402ListElem * temp_arrival_thread_element = My402ListLast(&Q1); struct packet * temp_arrival_thread_packet = (struct packet *)(temp_arrival_thread_element->obj); /* Move the first packet to q2 if there are enough tokens */ if(number_of_tokens_in_bucket >= temp_arrival_thread_packet->tokens_needed){ int is_empty = My402ListEmpty(&Q2); number_of_tokens_in_bucket = number_of_tokens_in_bucket - temp_arrival_thread_packet->tokens_needed; My402ListUnlink(&Q1,temp_arrival_thread_element); gettimeofday(¤t_time, NULL); printf("%012.3fms: p%ld leaves Q1, time in Q1 = %.3fms, " "token bucket now has %ld token\n", calculate_time_difference(current_time, emulation_start_time), temp_arrival_thread_packet->id, calculate_time_difference(current_time, temp_arrival_thread_packet->updation_time), number_of_tokens_in_bucket); printf ("file is %s and function is %s\n", __FILE__, __FUNCTION__); local_total_packet_time_in_Q1 = local_total_packet_time_in_Q1 + calculate_time_difference(current_time, temp_arrival_thread_packet->updation_time); gettimeofday(&temp_arrival_thread_packet->updation_time, NULL); My402ListPrepend(&Q2, temp_arrival_thread_packet); gettimeofday(¤t_time, NULL); printf("%012.3fms: p%ld enters Q2\n", calculate_time_difference(current_time, emulation_start_time), temp_arrival_thread_packet->id); if(is_empty){ pthread_cond_broadcast(&condition_variable); } } temp_arrival_thread_element = NULL; temp_arrival_thread_packet = NULL; } } pthread_mutex_unlock(&mutex); if(stop_flag || (local_packet_created_count == total_number_of_packets)){ pthread_mutex_lock (&mutex); number_of_packets_put_in_Q1 = local_packet_added_count; if(local_total_packet_time_in_Q1 != 0.0){ if(average_number_of_packets_in_Q1 == -1.0){ average_number_of_packets_in_Q1 = 0.0; } average_number_of_packets_in_Q1 = average_number_of_packets_in_Q1 + local_total_packet_time_in_Q1; } if(local_packet_created_count !=0){ average_packet_inter_arrival_time = local_total_packet_arrival_time / (float)local_packet_created_count; packet_drop_probability = (float)local_packet_dropped_count / (float)local_packet_created_count; } packet_arrival_thread_stop = 1; pthread_mutex_unlock(&mutex); break; } } pthread_exit(0); return NULL; }
void ParseFile(FILE *file){ char buffer[1024]; memset(buffer, 0, sizeof(buffer)); transaction *obj = NULL; My402List list; memset(&list, 0, sizeof(My402List)); (void)My402ListInit(&list); while(fgets(buffer, sizeof(buffer), file) != NULL){ obj = (transaction*)malloc(sizeof(transaction)); if(obj == NULL){ fprintf(stderr, "%s\n", strerror(errno)); } else{ char *start_ptr = buffer; char *tab_ptr = strchr(start_ptr, '\t'); int noofptr = 0; while(tab_ptr != NULL){ noofptr++; tab_ptr++; start_ptr = tab_ptr; tab_ptr = strchr(start_ptr, '\t'); } if(noofptr > 3){ fprintf(stderr, "Too many tabs, Clearing memory and Quiting Program\n"); free(obj); My402ListUnlinkAll(&list); exit(0); } if(noofptr < 3){ fprintf(stderr, "Format Malformed, Clearing memory and Quiting Program\n"); free(obj); My402ListUnlinkAll(&list); exit(0); } start_ptr = buffer; tab_ptr = strchr(start_ptr, '\t'); if(Transaction_Type(obj, start_ptr, tab_ptr) == 0){ fprintf(stderr, "Transaction Type failed, Clearing Memory and Quiting Program\n"); free(obj); My402ListUnlinkAll(&list); exit(0); } tab_ptr++; start_ptr = tab_ptr; tab_ptr = strchr(start_ptr, '\t'); if(Transaction_TimeStamp(obj, start_ptr, tab_ptr) == 0){ fprintf(stderr, "Transaction Time failed, Clearing Memory and Quiting Program\n"); free(obj); My402ListUnlinkAll(&list); exit(0); } tab_ptr++; start_ptr = tab_ptr; tab_ptr = strchr(start_ptr, '\t'); if(Transaction_Amount(obj, start_ptr, tab_ptr) == 0){ fprintf(stderr, "Transaction Amount failed, Clearing Memory and Quiting Program\n"); free(obj); My402ListUnlinkAll(&list); exit(0); } tab_ptr++; start_ptr = tab_ptr; tab_ptr = strchr(start_ptr, '\n'); Transaction_Description(obj, start_ptr, tab_ptr); //Now the struct is filled with the string fields read from the first line of the file if(My402ListLength(&list) == 0) (void)My402ListPrepend(&list, obj); else{ My402ListElem *elem = NULL; for(elem = My402ListFirst(&list); elem != NULL; elem=My402ListNext(&list, elem)){ if((((transaction*)(elem->obj))->trans_time == obj->trans_time)){ fprintf(stderr, "Error - Two entry at same time, Quiting program\n"); free(obj); My402ListUnlinkAll(&list); exit(0); } if((((transaction*)(elem->obj))->trans_time > obj->trans_time)){ (void)My402ListInsertBefore(&list, obj, elem); break; } if(My402ListNext(&list, elem) == NULL){ (void)My402ListAppend(&list, obj); break; } } } } } PrintTransactionList(&list); My402ListUnlinkAll(&list); }
void * token_depositing_thread_function(void *args){ pthread_sigmask(SIG_BLOCK, &set, 0); long local_token_created_count = 0; long local_token_dropped_count = 0; float local_total_packet_time_in_Q1 = 0.0; int stop_flag = 0; struct timeval current_time; while(1){ /* Sleep for needed time */ usleep(token_arrival_time * 1000000.0); /* Lock the mutex */ pthread_mutex_lock(&mutex); if(cntrl_c_signal){ stop_flag = 1; } else{ local_token_created_count++; /* Increment token count */ if(number_of_tokens_in_bucket < bucket_capacity){ number_of_tokens_in_bucket++; gettimeofday(¤t_time, NULL); printf("%012.3fms: token t%ld arrives, token bucket now has %ld tokens\n",calculate_time_difference(current_time,emulation_start_time),local_token_created_count,number_of_tokens_in_bucket); } else { local_token_dropped_count++; gettimeofday(¤t_time, NULL); printf("%012.3fms: token t%ld arrives, dropped\n",calculate_time_difference(current_time,emulation_start_time),local_token_created_count); } if(!My402ListEmpty(&Q1)){ My402ListElem * temp_arrival_thread_element = My402ListLast(&Q1); struct packet * temp_arrival_thread_packet = (struct packet *)(temp_arrival_thread_element->obj); /* Move the first packet to q2 if there are enough tokens */ if(number_of_tokens_in_bucket >= temp_arrival_thread_packet->tokens_needed){ int is_empty = My402ListEmpty(&Q2); number_of_tokens_in_bucket = number_of_tokens_in_bucket - temp_arrival_thread_packet->tokens_needed; My402ListUnlink(&Q1,temp_arrival_thread_element); gettimeofday(¤t_time, NULL); printf("%012.3fms: p%ld leaves Q1, time in Q1 = %.3fms, token bucket now has %ld token\n",calculate_time_difference(current_time,emulation_start_time),temp_arrival_thread_packet->id,calculate_time_difference(current_time, temp_arrival_thread_packet->updation_time),number_of_tokens_in_bucket); local_total_packet_time_in_Q1 = local_total_packet_time_in_Q1 + calculate_time_difference(current_time, temp_arrival_thread_packet->updation_time); gettimeofday(&temp_arrival_thread_packet->updation_time, NULL); My402ListPrepend(&Q2, temp_arrival_thread_packet); gettimeofday(¤t_time, NULL); printf("%012.3fms: p%ld enters Q2\n",calculate_time_difference(current_time,emulation_start_time),temp_arrival_thread_packet->id); if(is_empty){ pthread_cond_broadcast(&condition_variable); } } temp_arrival_thread_element = NULL; temp_arrival_thread_packet = NULL; } else { if(packet_arrival_thread_stop){ stop_flag = 1; } } if(server_thread_stop){ stop_flag = 1; } } /* Unloack the mutex */ pthread_mutex_unlock(&mutex); if(stop_flag){ pthread_mutex_lock(&mutex); if(local_total_packet_time_in_Q1 != 0.0){ if(average_number_of_packets_in_Q1 == -1.0){ average_number_of_packets_in_Q1 = 0.0; } average_number_of_packets_in_Q1 = average_number_of_packets_in_Q1 + local_total_packet_time_in_Q1; } average_number_of_packets_in_Q1 = average_number_of_packets_in_Q1 + local_total_packet_time_in_Q1; if(local_token_created_count !=0){ token_drop_probability = (float)local_token_dropped_count / (float)local_token_created_count; } if(packet_arrival_thread_stop && !server_thread_stop){ pthread_cond_broadcast(&condition_variable); } pthread_mutex_unlock(&mutex); break; } } pthread_exit(0); return NULL; }
main( int argc, char *argv[] ) { int status; char tt; unsigned int ti; float ff; char amt[10]; char des[100]; int number = 1024; char line[1026]; My402List *list =(My402List *)malloc(sizeof(My402List)); printf("start"); FILE * fp; if( argc == 1) { fp = stdin; if(ferror (fp)) { printf("error opening file!"); clearerr(fp); exit(0); } } else { fp = fopen( "gg.txt" , "r" ); if(ferror (fp)) { printf("error opening file!"); clearerr(fp); exit(0); } else { My402ListElem *elem = (My402ListElem *)malloc(sizeof(My402ListElem)); status = My402ListInit(list); if(status != FALSE) { while( fgets(line,number,fp) != NULL) { sscanf(line,"%c\t%d\t%s\t%[^\n]s",&tt,&ti,amt,des); printf("%c\n%d\n%s\n%s\n",tt,ti,amt,des); Mytransaction * tr =(Mytransaction*)malloc(sizeof(Mytransaction)); tr->transtype = tt; tr->time = ti; strcpy( tr->amounts , amt ); strcpy( tr->desc , des ); status = Mychecker(list,tr); if(status == TRUE) { status = My402ListPrepend(list,tr); if(status != TRUE ) printf("not prepended"); } } } else { printf("list not initialised"); } } BubbleSortForwardList(list,My402ListLength(list)); //***output********* printf("\n00000000011111111112222222222333333333344444444445555555555666666666677777777778"); printf("\n12345678901234567890123456789012345678901234567890123456789012345678901234567890"); printf("\n+-----------------+--------------------------+----------------+----------------+"); printf("\n| Date | Description | Amount | Balance |"); printf("\n+-----------------+--------------------------+----------------+----------------+"); MyListTraverse(list); printf("\n+-----------------+--------------------------+----------------+----------------+"); } }