int My402ListAppend(My402List* list, void* obj) { My402ListElem* last,*elem; last = My402ListLast(list); if(last == NULL) last = &list->anchor; elem = (My402ListElem*)malloc(sizeof(My402ListElem)); if( elem == NULL) { // print error??? return FALSE; } elem->obj = obj; elem->next = &list->anchor; list->anchor.prev = elem; elem->prev = last; last->next = elem; ++list->num_members; return TRUE; }
int My402ListInsertAfter(My402List* list, void* obj, My402ListElem* elem) { My402ListElem* newItem = NULL; if(NULL == elem || My402ListLast(list) == elem) return My402ListAppend(list,obj); else { newItem = (My402ListElem*)malloc(sizeof(My402ListElem)); if(NULL == newItem) { //print error return FALSE; } newItem->obj = obj; newItem->next = elem->next; elem->next->prev = newItem; newItem->prev = elem; elem->next = newItem; ++list->num_members; return TRUE; } return FALSE; }
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); } }
My402ListElem *My402ListNext(My402List* q, My402ListElem* n) { if (n==My402ListLast(q)) return NULL; else return n->next; }
int My402ListAppend(My402List * list, void * data) { // Create the new node My402ListElem * elem; elem = (My402ListElem *)malloc(sizeof(My402ListElem)); if(!elem) // Allocate memory failed return 0; elem -> obj = data; // Insert the node My402ListElem * last_elem = My402ListLast(list); if(last_elem) { elem -> next = &(list -> anchor); elem -> prev = last_elem; (list -> anchor).prev = elem; last_elem -> next = elem; list -> num_members++; } else // List is empty { elem -> next = &(list -> anchor); elem -> prev = &(list -> anchor); (list -> anchor).next = elem; (list -> anchor).prev = elem; list -> num_members++; } return 1; }
//#include "addlink.h" extern int My402ListAppend(My402List* List, void* insData) { My402ListElem *newNode = (My402ListElem *) malloc(sizeof(My402ListElem)); if(newNode != NULL) { newNode->obj = insData; if(My402ListEmpty(List)) { List->anchor.next = newNode; List->anchor.prev = newNode; newNode->next = &(List->anchor); newNode->prev = &(List->anchor); (List->num_members)++; return TRUE; } else { My402ListElem *lastNode = My402ListLast(List); lastNode->next = newNode; newNode->next = &(List->anchor); List->anchor.prev = newNode; newNode->prev = lastNode; (List->num_members)++; return TRUE; } } return FALSE; }
My402ListElem * My402ListNext(My402List * list, My402ListElem * cur) { if(cur == My402ListLast(list)) return NULL; else return cur -> next; }
int insertionSort(My402List *pList, My402SortElem *pKey){ My402ListElem *current = My402ListLast(pList); My402ListElem *prevCurrent = current; while(current != NULL && current->obj != NULL && ((My402SortElem *)current->obj)->transTime > pKey->transTime){ prevCurrent = current; current = My402ListPrev(pList, current); } if(current != NULL && current->obj != NULL){ if(((My402SortElem *)current->obj)->transTime == pKey->transTime){ fprintf(stderr,"\nTime stamp %lld is repeated for some entries",(long long)pKey->transTime); //clean up an return unlinkSortElements(pList); return FALSE; } } if(current == NULL){ My402ListInsertBefore(pList, pKey, prevCurrent); }else{ My402ListInsertAfter(pList, pKey, current); } return TRUE; }
int My402ListAppend(My402List *list, void *obj) { if(list==NULL) return FALSE; //the new elemenet to be appended My402ListElem *listelem = NULL; listelem = (My402ListElem *) malloc( sizeof(My402ListElem) ); if(listelem == NULL) return FALSE; //if list is empty just alter anchor ptrs if(list->num_members == 0){ list->anchor.next = listelem; list->anchor.prev = listelem; listelem->obj = (void*) obj; //store anchor address as it is not a ptr type listelem->next = &(list->anchor); listelem->prev = &(list->anchor); list->num_members++; } //Just append the new element to the last element else{ My402ListElem *lastelem = My402ListLast(list); if (lastelem != NULL){ lastelem->next = listelem; list->anchor.prev = listelem; listelem->prev = lastelem; listelem->next = &(list->anchor); listelem->obj = (void*) obj; list->num_members++; } } return TRUE; }
static void BubbleSortBackwardList(My402List *pList, int num_items) { My402ListElem *elem=NULL; int i=0; if (My402ListLength(pList) != num_items) { fprintf(stderr, "List length is not %1d in BubbleSortBackwardList().\n", num_items); exit(1); } for (i=0; i < num_items; i++) { int j=0, something_swapped=FALSE; My402ListElem *prev_elem=NULL; for (elem=My402ListLast(pList), j=0; j < num_items-i-1; elem=prev_elem, j++) { int cur_val=(int)(elem->obj), prev_val=0; prev_elem=My402ListPrev(pList, elem); prev_val = (int)(prev_elem->obj); if (cur_val < prev_val) { BubbleBackward(pList, &elem, &prev_elem); something_swapped = TRUE; } } if (!something_swapped) break; } }
// Returns next element else NULL if element is last item on list My402ListElem *My402ListNext(My402List* myList, My402ListElem* listElement) { if(listElement != My402ListLast(myList)) { return listElement->next; } else { return NULL; } }
int My402ListAppend(My402List* q, void* x) { My402ListElem* temp = (My402ListElem*)malloc(sizeof(My402ListElem)); My402ListElem* p = (My402ListElem*)malloc(sizeof(My402ListElem)); p=&(q->anchor); //addr of anchor My402ListElem* a = (My402ListElem*)malloc(sizeof(My402ListElem)); a=My402ListLast(q); //printf("im in append\n"); if (temp==NULL) { return FALSE; } if(a==NULL) { a=p; } if (My402ListEmpty(q)==1) { temp->obj= x; temp->prev=p; temp->next=p; p->prev=temp; p->next=temp; } else { temp->obj=x; temp->next=p; temp->prev=a; a->next=temp; p->prev=temp; } //printf("im at append end\n"); q->num_members++; return TRUE; }
void unlinkSortElements(My402List *pList){ My402ListElem *current = My402ListLast(pList); My402ListElem *currentPrev = current; while(currentPrev != NULL){ //obtain the prev node first and then unlink currentPrev = My402ListPrev(pList,current); free(current->obj); My402ListUnlink(pList, current); current = currentPrev; } }
void insertionSort(My402List *pList, My402SortElem *pKey){ My402ListElem *current = My402ListLast(pList); My402ListElem *prevCurrent = current; while(current != NULL && current->obj != NULL && ((My402SortElem *)current->obj)->transTime > pKey->transTime){ prevCurrent = current; current = My402ListPrev(pList, current); } if(current == NULL){ My402ListInsertBefore(pList, pKey, prevCurrent); }else{ My402ListInsertAfter(pList, pKey, current); } }
int My402ListAppend(My402List* list, void *new_obj) { My402ListElem* elem = (My402ListElem*) malloc(sizeof(My402ListElem)); if( !elem ) return FALSE; My402ListElem *last = My402ListLast(list); // elem update elem->prev = last; elem->next = &(list->anchor); elem->obj = new_obj; // list update last->next = elem; list->anchor.prev = elem; // list length update list->num_members ++; return TRUE; }
void insertionSort(My402List pList, char *pKey){ My402ListElem *current = My402ListLast(pList); My402ListElem *prevCurrent = current; while(current != NULL && current->obj != NULL && atoi(current->obj) > atoi(pKey)){ prevCurrent = current; current = My402ListPrev(pList, current); } if(current == NULL){ insertBefore(pList, pKeyElem, prevCurrent); }else{ insertAfter(pList, pKeyElem, current); } }
// Append the item to the list int My402ListAppend(My402List* myList, void* elem) { My402ListElem *newElement = NULL; My402ListElem *anchorElement = &myList->anchor; newElement = (My402ListElem *)malloc(sizeof(My402ListElem)); if(newElement != NULL) { if(My402ListEmpty(myList)) { newElement->prev = anchorElement; newElement->next = anchorElement; anchorElement->next = newElement; anchorElement->prev = newElement; newElement->obj = elem; myList->num_members++; return TRUE; } else { return My402ListInsertAfter(myList,elem,My402ListLast(myList)); } } return FALSE; }
int My402ListAppend(My402List *list, void *obj){ if(list != NULL){ My402ListElem *last = My402ListLast(list); My402ListElem *new_elem = (My402ListElem *)malloc(sizeof(My402ListElem)); if(new_elem == 0 || new_elem == NULL){ return 0; } else{ new_elem -> obj = obj; new_elem -> prev = last; new_elem -> next = last -> next; last -> next = new_elem; list -> anchor.prev = new_elem; list -> num_members++; return 1; } } else return 0; }
void sort(My402List* final_list) { My402ListElem *first_ptr, *second_ptr, *innerelemminus; Data *inner, *innerminus, *temp; int i=0; for(first_ptr=My402ListFirst(final_list);first_ptr!=NULL;first_ptr=My402ListNext(final_list,first_ptr)) { i=0; for(second_ptr=My402ListLast(final_list);second_ptr!=My402ListFirst(final_list);second_ptr=My402ListPrev(final_list,second_ptr)) { inner=(Data*)second_ptr->obj; innerelemminus= My402ListPrev(final_list,second_ptr); innerminus=(Data*)innerelemminus->obj; if(inner->time==innerminus->time) { printf("ERROR:Two transaction with same timestamp\n"); exit(0); } if(innerminus->time > inner->time) { temp = (Data*) innerelemminus->obj; innerelemminus->obj = second_ptr->obj; second_ptr->obj = (void*)temp; i=1; } } } }
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 *firstServer(void *mu) { //char buffer[30]; struct timeval in; //Step1: Stays in Loop while(!finish) { pthread_mutex_lock(&mutex); while(My402ListEmpty(depositQ) && !finish) // Changing from While to IF { pthread_cond_wait(&cv,&mutex); } packets *p = (packets*)malloc(sizeof(packets)); p->id = 0; if(!finish) { //printf("\n###DEQUEING Q2: S#1 \t"); My402ListElem *tmp = My402ListLast(depositQ); p = tmp->obj; //printf("PACKET ID# %ld\n",p->id); gettimeofday(&in,NULL); My402ListUnlink(depositQ,tmp); printTimeStamp(in); long di = ((in.tv_sec * 1000000 ) + (in.tv_usec)) - ((p->enterQ2.tv_sec * 1000000) + (p->enterQ2.tv_usec)); avgQ2Service += di; long mi = di /1000 ; int mic = di % 1000; pthread_mutex_lock(&filemu); printf("p%ld leaves Q2, time in Q2 = %ld.%03d ms\n",p->id,mi,mic); pthread_mutex_unlock(&filemu); gettimeofday(&in,NULL); printTimeStamp(in); p->service.tv_sec = in.tv_sec; p->service.tv_usec = in.tv_usec; pthread_mutex_lock(&filemu); if(fileInp) printf("p%ld begins service at S1, requesting %f ms of service\n",p->id,(*(double*)mu)); else printf("p%ld begins service at S1, requesting %.3f ms of service\n",p->id,(1 /(*(double*)mu)) * 1000); pthread_mutex_unlock(&filemu); } if(My402ListEmpty(depositQ) && My402ListEmpty(arrivalQ) && num_of_packs == 0) { finish = 1; } pthread_mutex_unlock(&mutex); if(!interuptflag && p->id != 0) { if(fileInp) { double tm = (*(double*)mu) / 1000.00; int secs = (int) tm; double decm = tm - secs; double milisec = 1000 * decm; // length of time to sleep, in milisecond struct timespec req = {0}; req.tv_sec = secs; req.tv_nsec = milisec * 1000000L; //printf("Server#1 Thread Sleeping for %ld secs %ld ms\n",req.tv_sec,req.tv_nsec); nanosleep(&req, (struct timespec *)NULL); gettimeofday(&in,NULL); printTimeStamp(in); long d1 = ((in.tv_sec * 1000000 ) + (in.tv_usec)) - ((p->service.tv_sec * 1000000) + (p->service.tv_usec)); avgPackService += d1; avgS1Service += d1; long m1 = d1 /1000 ; int mi1 = d1 % 1000; long d2 = ((in.tv_sec * 1000000 ) + (in.tv_usec)) - ((p->start.tv_sec * 1000000) + (p->start.tv_usec)); avgService += (d2/1000000.00); avgServiceSq = (avgServiceSq + (d2/1000000.00) * (d2/1000000.00)); long m2 = d2 /1000 ; int mi2 = d2 % 1000; pthread_mutex_lock(&filemu); printf("p%ld departs from S1, service time = %ld.%03d ms, time in system = %ld.%03d ms\n",p->id,m1,mi1,m2,mi2); pthread_mutex_unlock(&filemu); completedPack++; } else { double tm = 1 / (*(double*)mu); int secs = (int) tm ; double decm = tm - secs; double milisec = 1000 * decm; // length of time to sleep, in milisecond struct timespec req = {0}; req.tv_sec = secs; req.tv_nsec = milisec * 1000000L; //printf("Server#1 Thread Sleeping for %ld secs %ld ms\n",req.tv_sec,req.tv_nsec); nanosleep(&req, (struct timespec *)NULL); gettimeofday(&in,NULL); printTimeStamp(in); long d1 = ((in.tv_sec * 1000000 ) + (in.tv_usec)) - ((p->service.tv_sec * 1000000) + (p->service.tv_usec)); long m1 = d1 /1000 ; int mi1 = d1 % 1000; avgS1Service += d1; avgPackService += d1; long d2 = ((in.tv_sec * 1000000 ) + (in.tv_usec)) - ((p->start.tv_sec * 1000000) + (p->start.tv_usec)); avgService += (d2/1000000.00); avgServiceSq = (avgServiceSq + (d2/1000000.00) * (d2/1000000.00)); long m2 = d2 /1000 ; int mi2 = d2 % 1000; pthread_mutex_lock(&filemu); printf("p%ld departs from S1, service time = %ld.%03d ms, time in system = %ld.%03d ms\n",p->id,m1,mi1,m2,mi2); pthread_mutex_unlock(&filemu); completedPack++; } } //Step2: Goes to sleep } return(0); }
void * server_thread_function(void *args){ pthread_sigmask(SIG_BLOCK, &set, 0); long local_total_packet_counter = 0; float local_packet_service_time = packet_service_time; float local_total_packet_time_in_S = 0.0; float local_total_packet_time_in_Q2 = 0.0; float local_total_packet_service_time = 0.0; float local_total_time_spent_in_system = 0.0; float local_total_time_spent_in_system_squared = 0.0; struct timeval previous_time, current_time; while(1){ /* Lock the mutex */ pthread_mutex_lock(&mutex); /* wait for the queue-not-empty condition to be signaled */ while(My402ListEmpty(&Q2) && !cntrl_c_signal){ pthread_cond_wait(&condition_variable, &mutex); if(packet_arrival_thread_stop){ break; } } if(!My402ListEmpty(&Q2)){ My402ListElem * temp_server_thread_element = My402ListLast(&Q2); My402ListUnlink(&Q2, temp_server_thread_element); struct packet * temp_server_thread_packet = (struct packet *)(temp_server_thread_element->obj); if(is_trace_driven_mode){ local_packet_service_time = temp_server_thread_packet->packet_service_time; } gettimeofday(¤t_time, NULL); printf("%012.3fms: p%ld leaves Q2, time in Q2 = %.3fms, begin " "service at S\n", calculate_time_difference(current_time,emulation_start_time), temp_server_thread_packet->id, calculate_time_difference(current_time, temp_server_thread_packet->updation_time)); local_total_packet_time_in_Q2 = local_total_packet_time_in_Q2 + calculate_time_difference(current_time, temp_server_thread_packet->updation_time); gettimeofday(&temp_server_thread_packet->updation_time, NULL); /* Unlock the mutex */ pthread_mutex_unlock(&mutex); gettimeofday(&previous_time, NULL); /* Sleep for needed service time */ usleep(local_packet_service_time * 1000000.0); gettimeofday(¤t_time, NULL); printf("%012.3fms: p%ld departs from S, service time = %.3fms, " "time in system = %.3fms\n", calculate_time_difference(current_time,emulation_start_time), temp_server_thread_packet->id, calculate_time_difference(current_time, previous_time), calculate_time_difference(current_time, temp_server_thread_packet->creation_time)); local_total_packet_service_time = local_total_packet_service_time + calculate_time_difference(current_time, previous_time); local_total_packet_time_in_S = local_total_packet_time_in_S + calculate_time_difference(current_time, previous_time); float temp = calculate_time_difference(current_time, temp_server_thread_packet->creation_time); local_total_time_spent_in_system = local_total_time_spent_in_system + temp; local_total_time_spent_in_system_squared = local_total_time_spent_in_system_squared + (temp * temp); free(temp_server_thread_packet); local_total_packet_counter++; } else{ pthread_mutex_unlock(&mutex); } //Check this condition if(cntrl_c_signal || (local_total_packet_counter == number_of_packets_put_in_Q1 && packet_arrival_thread_stop)){ pthread_mutex_lock(&mutex); if(local_total_packet_counter !=0){ average_packet_service_time = local_total_packet_service_time / (float)local_total_packet_counter; average_number_of_packets_in_Q2 = local_total_packet_time_in_Q2; average_number_of_packets_at_S = local_total_packet_time_in_S; average_time_a_packet_spent_in_system = local_total_time_spent_in_system / (float)local_total_packet_counter; local_total_time_spent_in_system_squared = local_total_time_spent_in_system_squared / (float)local_total_packet_counter; standard_deviation_for_time_spent_in_system = sqrtf(local_total_time_spent_in_system_squared - ( average_time_a_packet_spent_in_system * average_time_a_packet_spent_in_system)); } server_thread_stop = 1; pthread_mutex_unlock(&mutex); break; } } pthread_exit(0); return NULL; }
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; }
void printlist(My402List *list) { My402ListElem* tmp1; tfile_list* tmp2; char *buf; tmp1=My402ListFirst(list); tmp2=(tfile_list*)tmp1->obj; //char* t; buf = (char *)malloc(20*sizeof(char)); char flaw[]="?,???,???.??"; int total=0; double f_total; double f_tot; char *str; str=(char*)malloc(sizeof(str)); //t = (char*) malloc(20*sizeof(char)); printf("12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"); printf("+-----------------+--------------------------+----------------+----------------+\n"); printf("| Date | Description | Amount | Balance |\n"); printf("+-----------------+--------------------------+----------------+----------------+\n"); while ((tmp1!=My402ListLast(list)) | (tmp1==My402ListLast(list))) { output_time(tmp2->tran_time,buf); //float i=atof(tmp2->tran_amount); float i=atof(tmp2->tran_amount); //printf("i value is =%lf\n",i); double convert_factor=100.00; int cent=round(i*convert_factor); //printf("cent is=%d\n",cent); if (strncmp(tmp2->tran_type,"-",2)==0) { total=total-cent; //printf("Balance=%s\n",total); } else { total=total+cent; //printf("Balance=%s\n",total); } //printf("Total=%d\n",total); f_total=(double)total/100; if(f_total<0) { f_tot=-f_total; //printf("f_total=%f\n",f_total); } //printf("Final_total=%lf\n",f_total); str=comma_dots(i); if ((strncmp(tmp2->tran_type,"-",2)==0) && (atof)(tmp2->tran_amount)<10000000 && (-10000000<f_total) && (f_total<0)) { fprintf(stdout,"| %14s | %-24s | (%11s) | (%12.2f) |\n",buf,tmp2->tran_desc,str,f_tot); } else if ((strncmp(tmp2->tran_type,"-",2)==0) && (atof)(tmp2->tran_amount)<10000000 && (0<f_total) && (f_total<10000000)) { fprintf(stdout,"| %14s | %-24s | (%11s) | %14.2f |\n",buf,tmp2->tran_desc,str,f_total); } else if ((strncmp(tmp2->tran_type,"-",2)==0) && (atof)(tmp2->tran_amount)<10000000 && (f_total>10000000)) { fprintf(stdout,"| %14s | %-24s | (%11s) | %13.2s |\n",buf,tmp2->tran_desc,str,flaw); } else if ((strncmp(tmp2->tran_type,"-",2)==0) && (atof)(tmp2->tran_amount)<10000000 && (f_total<=-10000000)) { fprintf(stdout,"| %14s | %-24s | (%11s) | (%12.2s) |\n",buf,tmp2->tran_desc,str,flaw); } else if ((strncmp(tmp2->tran_type,"-",2)==0) && (atof)(tmp2->tran_amount)>=10000000 && (0<f_total) && (f_total<10000000)) { fprintf(stdout,"| %14s | %-24s | (%11s) | %14.2f |\n",buf,tmp2->tran_desc,flaw,f_total); } else if ((strncmp(tmp2->tran_type,"-",2)==0) && (atof)(tmp2->tran_amount)>=10000000 && (f_total>=10000000)) { fprintf(stdout,"| %14s | %-24s | (%11s) | %13.2s |\n",buf,tmp2->tran_desc,flaw,flaw); } else if ((strncmp(tmp2->tran_type,"-",2)==0) && (atof)(tmp2->tran_amount)>=10000000 && (-10000000<f_total)&& (f_total<0)) { fprintf(stdout,"| %14s | %-24s | (%11s) | (%12.2f) |\n",buf,tmp2->tran_desc,flaw,f_tot); } else if ((strncmp(tmp2->tran_type,"-",2)==0) && (atof)(tmp2->tran_amount)>=10000000 && (f_total<-10000000)) { fprintf(stdout,"| %14s | %-24s | (%11s) | (%12.2s) |\n",buf,tmp2->tran_desc,flaw,flaw); } else if ((strncmp(tmp2->tran_type,"+",2)==0) && (atof)(tmp2->tran_amount)<10000000 && (0<f_total) && (f_total<10000000)) { fprintf(stdout,"| %14s | %-24s | %13s | %14.2f |\n",buf,tmp2->tran_desc,str,f_total); } else if ((strncmp(tmp2->tran_type,"+",2)==0) && (atof)(tmp2->tran_amount)<10000000 && (f_total>=10000000)) { fprintf(stdout,"| %14s | %-24s | %13s | %13.2s |\n",buf,tmp2->tran_desc,str,flaw); } else if ((strncmp(tmp2->tran_type,"+",2)==0) && (atof)(tmp2->tran_amount)<10000000 && (-10000000<f_total) && (f_total<0)) { fprintf(stdout,"| %14s | %-24s | %13s | (%12.2f) |\n",buf,tmp2->tran_desc,str,f_tot); } else if ((strncmp(tmp2->tran_type,"+",2)==0) && (atof)(tmp2->tran_amount)<10000000 && (f_total<=-10000000)) { fprintf(stdout,"| %14s | %-24s | %13s | (%12.2s) |\n",buf,tmp2->tran_desc,str,flaw); } else if ((strncmp(tmp2->tran_type,"+",2)==0) && (atof)(tmp2->tran_amount)>10000000 && (0<f_total) && (f_total<10000000)) { fprintf(stdout,"| %14s | %-24s | %13s | %14.2f |\n",buf,tmp2->tran_desc,flaw,f_total); } else if ((strncmp(tmp2->tran_type,"+",2)==0) && (atof)(tmp2->tran_amount)>10000000 && (f_total>=10000000)) { fprintf(stdout,"| %14s | %-24s | %13s | %13.2s |\n",buf,tmp2->tran_desc,flaw,flaw); } else if ((strncmp(tmp2->tran_type,"+",2)==0) && (atof)(tmp2->tran_amount)>10000000 && (-1000000<f_total) && (f_total<0)) { fprintf(stdout,"| %14s | %-24s | %13s | (%12.2f) |\n",buf,tmp2->tran_desc,flaw,f_tot); } else { fprintf(stdout,"| %14s | %-24s | %13s | (%12.2s) |\n",buf,tmp2->tran_desc,flaw,flaw); } if (tmp1!=My402ListLast(list)){ tmp1 = My402ListNext(list,tmp1); tmp2 = (tfile_list*)tmp1->obj;} else break; } //printf("Total=%d\n",total); f_total=(float)total/100; //printf("Final_total=%f\n",f_total); //f_t=f_total/1000000; printf("+-----------------+--------------------------+----------------+----------------+\n"); }