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); } }
/* Process the pakcets in Q1 and transfer to Q2 when enough tokens */ void ProcessPacket(){ My402ListElem *find = NULL; Packet_desc *p = NULL; find=My402ListFirst(&Q1_node); if(find->obj == NULL){ fprintf(stderr,"Error: Obj is NULL"); pthread_exit(0); } p = (Packet_desc*)find->obj; if(p->tokens <= token_limit){ // move this to Q2 token_limit -= p->tokens; // total time in Q1 p->total_Q1_time = GetMTimeOfDay()- p->Q1_time_enters; fprintf(stdout, "%012.3lfms: p%d leaves Q1, time in Q1 = %.3lfms, tokens bucket now has %d token\n",GetMTimeOfDay(),p->name_ID,p->total_Q1_time,token_limit); if(My402ListEmpty(&Q2_node)){ // signal the condition about the queue not empty p->Q2_time_enters = GetMTimeOfDay(); // Q2_time_enters[p->name_ID] = GetMTimeOfDay(); fprintf(stdout, "%012.3lfms: p%d enters Q2\n",GetMTimeOfDay(),p->name_ID); My402ListAppend(&Q2_node, (void*)p); curr_q2_size = 1; /* signal the condition variable queue*/ pthread_cond_signal(&serverQ); find = My402ListFind(&Q1_node, (void*)p); My402ListUnlink(&Q1_node, find); }else{ p->Q2_time_enters = GetMTimeOfDay(); // Q2_time_enters[p->name_ID] = GetMTimeOfDay(); fprintf(stdout, "%012.3lfms: p%d enters Q2\n",GetMTimeOfDay(),p->name_ID); My402ListAppend(&Q2_node, (void*)p); /* gaurd set true */ curr_q2_size = 1; find = My402ListFind(&Q1_node, (void*)p); My402ListUnlink(&Q1_node, find); } } }
void server_procedure(struct command_line_args *object) { struct packet *p; My402ListElem *elem = NULL; struct timespec tim; double time; long time_diff_in_nsec; int i = 0; while( i < object->no_of_packets && !EndServerThread) { pthread_mutex_lock(&token_bucket); while(My402ListEmpty(&Q2PacketList)&&!EndServerThread) pthread_cond_wait(&is_q2_empty,&token_bucket); if(EndServerThread == 1) { pthread_mutex_unlock(&token_bucket); break; } elem = My402ListFirst(&Q2PacketList); if(elem == NULL) { pthread_mutex_unlock(&token_bucket); break; } p = (struct packet *)elem->obj; My402ListUnlink(&Q2PacketList, elem); pthread_mutex_unlock(&token_bucket); gettimeofday(&(p->Q2leaves), NULL); time = (TIME_IN_USEC(p->Q2leaves) - TIME_IN_USEC(GlobalStartTime))/1000; p->time_in_Q2 = (TIME_IN_USEC(p->Q2leaves) - TIME_IN_USEC(p->Q2timestamp))/1000; LOG(stdout, "%012.3fms: p%d begin service at S, time in Q2 = %.3fms\n",time,p->packet_id,p->time_in_Q2); time_diff_in_nsec = (long)((((p->precise_packet_service_time)/1000) - (p->service_time/1000))*1000000000L); tim.tv_sec = (p->service_time)/1000; tim.tv_nsec = time_diff_in_nsec; nanosleep(&tim, NULL); gettimeofday(&(p->Leaves_server), NULL); time = (TIME_IN_USEC(p->Leaves_server) - TIME_IN_USEC(GlobalStartTime))/1000; p->time_in_system = (TIME_IN_USEC(p->Leaves_server) - TIME_IN_USEC(p->Arrival_timestamp))/1000; p->precise_packet_service_time = (TIME_IN_USEC(p->Leaves_server) - TIME_IN_USEC(p->Q2leaves))/1000; LOG(stdout, "%012.3fms: p%d departs from S, service time = %.3fms, time in system = %.3fms\n",time,p->packet_id,p->precise_packet_service_time,p->time_in_system); completed_packets ++; calculate_stats(p); if((packet_count == object->no_of_packets) &&(completed_packets == (packet_count-discarded_packets)) && My402ListEmpty(&Q2PacketList)) { EndServerThread = 1; pthread_cond_signal(&is_q2_empty); } i++; } pthread_exit(NULL); }
void interrupt_main() { //printf("<-----CTR+C RECIEVED------>\n"); SERVER_DIE = 1; pthread_kill(TOKEN, SIGUSR1); pthread_kill(PACKET, SIGUSR2); pthread_mutex_lock(&m); //printf("Inside mutex of Man\n"); while(!My402ListEmpty(Q2)){ My402ListElem *elem = My402ListFirst(Q2); My402dataElem *topEle = (My402dataElem*)(elem->obj); My402ListUnlink(Q2, elem); TIME_AT_Q1 = diff_timeval(TIME_AT_Q1, topEle->q1duration); } /* Clear the list */ My402ListUnlinkAll(Q1); pthread_mutex_unlock(&m); pthread_cond_broadcast(&cond_t); //printf("<-----MAIN DYING------>\n"); pthread_exit(0); }
void My402ListUnlinkAll(My402List *pList){ My402ListElem *elem = NULL; for(elem = My402ListFirst(pList); elem != NULL; elem = My402ListNext(pList, elem)){ My402ListUnlink(pList, elem); } pList->num_members = 0; }
//Deletes all the elements from the list void My402ListUnlinkAll(My402List* myList) { My402ListElem *listElement = NULL; int i = 0; for(;i<myList->num_members;i++) { listElement = My402ListFirst(myList); My402ListUnlink(myList,listElement); } }
void FreeListObjects(My402List *pList){ My402ListElem *elt=NULL; for (elt=My402ListFirst(pList); elt != NULL; elt=My402ListNext(pList, elt)) { struct transaction_info *obj_elt=elt->obj; free(obj_elt); elt->obj = NULL; My402ListUnlink(pList,elt); } }
//Keep on deleting the node next to anchor void My402ListUnlinkAll(My402List *list) { if(list==NULL) return; My402ListElem *listelem; while(list->anchor.next!=NULL && list->anchor.prev!=NULL) { listelem = list->anchor.next; My402ListUnlink(list, listelem); } }
void DequePacket(Packet_desc *p,My402ListElem *first_elem){ p->total_Q2_time = GetMTimeOfDay()- p->Q2_time_enters; fprintf(stdout, "%012.3lfms: p%d leaves Q2, time in Q2 = %.3lfms, begin service at S\n",GetMTimeOfDay(), p->name_ID,p->total_Q2_time); My402ListUnlink(&Q2_node, first_elem); if(My402ListEmpty(&Q2_node)) curr_q2_size = 0; }
/** * @brief Delete node in the nack list with * seq number mentioned in the param */ void delete_node_nack_list(vlong seq_num){ // Get node address from hashmap hashed_link *hash_node = (hashed_link *)(find_hashl(seq_num)); if (!hash_node) { DBG("This should never happen"); exit(1); } // Delete from the Nack list My402ListUnlink(&globals.nackl, hash_node->nack_node_ptr); //DBG("[%llu] Removing node", hash_node->seq_num); }
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 My402ListUnlinkAll(My402List * list) { if(My402ListEmpty(list)) return; My402ListElem * cur = My402ListFirst(list); while(cur) { My402ListElem * del = cur; cur = My402ListNext(list, cur); My402ListUnlink(list, del); } (list -> anchor).next = &(list -> anchor); (list -> anchor).prev = &(list -> anchor); }
int removeTokens(int num) { int i; if(!num) { My402ListUnlinkAll(&tokList); return 1; } My402ListElem *tempTok, *tempTok1; tempTok = My402ListFirst(&tokList); for(i = 0; i<num ; i++) { tempTok1 = tempTok; tempTok = My402ListNext(&tokList, tempTok); My402ListUnlink(&tokList, tempTok1); } return 1; }
void move_packet_q1_to_q2(struct command_line_args *object, My402ListElem *elem, struct packet *p) { double time; int isEmpty = 0; My402ListUnlink(&Q1PacketList, elem); gettimeofday(&(p->Q1leaves),NULL); time = (TIME_IN_USEC(p->Q1leaves) - TIME_IN_USEC(GlobalStartTime))/1000; p->time_in_Q1 = (TIME_IN_USEC(p->Q1leaves) - TIME_IN_USEC(p->Q1timestamp))/1000; LOG(stdout, "%012.3fms: p%d leaves Q1, time in Q1 = %.3fms, token bucket now has %d token%c\n",time,p->packet_id,p->time_in_Q1,tokens_in_bucket,(tokens_in_bucket > 1 ?'s':' ')); q1_temp_count++; isEmpty = My402ListEmpty(&Q2PacketList); My402ListAppend(&Q2PacketList, p); gettimeofday(&(p->Q2timestamp),NULL); time = (TIME_IN_USEC(p->Q2timestamp) - TIME_IN_USEC(GlobalStartTime))/1000; LOG(stdout, "%012.3fms: p%d enters Q2\n",time,p->packet_id); if(isEmpty) pthread_cond_signal(&is_q2_empty); }
static void RandomShuffle(My402List *pList, int num_items) { int i=0; My402List list2; My402ListElem *elem=NULL; memset(&list2, 0, sizeof(My402List)); (void)My402ListInit(&list2); for (i=0; i < num_items; i++) { int j=0, idx=0, num_in_list=num_items-i; void *obj=NULL; idx = RandomIndex(num_in_list); for (elem=My402ListFirst(pList); elem != NULL; elem=My402ListNext(pList, elem)) { if (j == idx) { break; } j++; } if (elem == NULL) { fprintf(stderr, "Unrecoverable error in RandomShuffle().\n"); exit(1); } obj = elem->obj; My402ListUnlink(pList, elem); (void)My402ListAppend(&list2, obj); } if (!My402ListEmpty(pList)) { fprintf(stderr, "List not empty in RandomShuffle().\n"); exit(1); } for (elem=My402ListFirst(&list2); elem != NULL; elem=My402ListNext(&list2, elem)) { (void)My402ListAppend(pList, elem->obj); } My402ListUnlinkAll(&list2); }
void RemoveAllPackets(My402List *queue , int queue_no){ My402ListElem *elem = My402ListFirst(queue); Packet_desc *p = NULL; while (elem != NULL) { p = (Packet_desc*)elem->obj; fprintf(stdout, "%012.3lfms: p%d removed from Q%d\n",GetMTimeOfDay(),p->name_ID,queue_no); My402ListUnlink(queue,elem); elem = My402ListFirst(queue); if(queue_no == 1) packet_arrived--; /* since packets are dropped increase counter */ packets_drop++; free(p); } }
void *tokenThread(void *id) { long sleep_time; int queue2_empty; int token_drop=0; Packet *pkt; My402ListElem *elem; while(1) { if(token_die==1) { token_drop_prob = (double)token_drop/tokencount; pthread_exit(NULL); } ttend=getinstanttime(); sleep_time = tokenarrival - (ttend-ttstart); usleep(sleep_time); ttstart = getinstanttime(); pthread_mutex_lock(&my_mutex); if(token_bucket<B) { token_bucket++; tokencount++; PrintStat(getinstanttime()); fprintf(stdout,"token t%d arrives, token bucket now has %d tokens\n",tokencount,token_bucket); } else { PrintStat(getinstanttime()); tokencount++; token_drop++; fprintf(stdout,"token t%d arrives, token bucket full, token dropped!!\n",tokencount); } if(!My402ListEmpty(&queue1)) { elem = My402ListFirst(&queue1); pkt = (Packet *)elem->obj; if(token_bucket >= pkt->num_tokens) { token_bucket-=pkt->num_tokens; My402ListUnlink(&queue1,elem); pkt->q1_exit = getinstanttime(); PrintStat(getinstanttime()); fprintf(stdout, "p%d leaves Q1, time in Q1 = %.3fms, token bucket now has %d tokens\n", pkt->pkt_id, (double)(pkt->q1_exit - pkt->q1_enter)/1000, token_bucket); avg_pkt_q1 += (pkt->q1_exit - pkt->q1_enter); pkts_left_q1++; queue2_empty = My402ListEmpty(&queue2); My402ListAppend(&queue2,pkt); pkt->q2_enter = getinstanttime(); PrintStat(getinstanttime()); fprintf(stdout,"p%d enters Q2\n", pkt->pkt_id); if(queue2_empty==1) pthread_cond_signal(&queue2_cond); } } pthread_mutex_unlock(&my_mutex); if(pkts_left_q1 == pkts_to_arrive) { token_drop_prob = (double)token_drop/tokencount; pthread_exit(NULL); } } }
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 *arrivalThread(void *id) { My402ListElem *elem; Packet *pkt; int i; long pkt_sleep, pkt_service; int pkt_token; long sleep_time; long prev_pkt_time=0, instant_time; long avg_ia=0; for (i=0;i<num_packets;i++) { getStatistics(&pkt_sleep,&pkt_service,&pkt_token,i); if(pkt_sleep > 10000000) pkt_sleep = 10000000; if(pkt_service > 1000000) pkt_service = 10000000; taend=getinstanttime(); sleep_time = pkt_sleep - (taend-tastart); usleep(sleep_time); // Creating the Packet pkt = (Packet *)malloc(sizeof(struct tagPacket)); pkt->pkt_id = i; pkt->num_tokens = pkt_token; pkt->service_time = pkt_service; pkt->sys_enter = tastart = getinstanttime(); if(pkt->num_tokens > B) { // Drop the packet pkts_to_arrive--; PrintStat(getinstanttime()); fprintf(stdout, "p%d arrives, Invalid token requirement, p%d Dropped!!\n", pkt->pkt_id, pkt->pkt_id); continue; } else { instant_time = getinstanttime(); if(prev_pkt_time==0) { prev_pkt_time = pkt->inter_arrival_time = (instant_time - temulation_start); prev_pkt_time = instant_time; } else { pkt->inter_arrival_time = instant_time - prev_pkt_time; prev_pkt_time = instant_time; } PrintStat(instant_time); avg_ia += pkt->inter_arrival_time; fprintf(stdout, "p%d arrives, needs %d tokens, inter-arrival time = %.3fms\n", pkt->pkt_id,pkt->num_tokens,(double)(pkt->inter_arrival_time)/1000); } pthread_mutex_lock(&my_mutex); if(My402ListEmpty(&queue1)){ My402ListAppend(&queue1,pkt); pkt->q1_enter = getinstanttime(); PrintStat(getinstanttime()); fprintf(stdout,"p%d enters Q1\n", pkt->pkt_id); if(!My402ListEmpty(&queue2)) { pthread_cond_signal(&queue2_cond); } else { if(token_bucket >= pkt->num_tokens) { elem = My402ListFirst(&queue1); pkt = (Packet *)elem->obj; My402ListUnlink(&queue1,elem); pkt->q1_exit = getinstanttime(); token_bucket-=pkt->num_tokens; PrintStat(getinstanttime()); fprintf(stdout, "p%d leaves Q1, time in Q1 = %.3fms, token bucket now has %d tokens\n", pkt->pkt_id, (double)(pkt->q1_exit - pkt->q1_enter)/1000, token_bucket); avg_pkt_q1 += (pkt->q1_exit - pkt->q1_enter); pkts_left_q1++; My402ListAppend(&queue2,pkt); pkt->q1_enter = 0; pkt->q1_exit = 0; pkt->q2_enter = getinstanttime(); PrintStat(getinstanttime()); fprintf(stdout,"p%d enters Q2\n", pkt->pkt_id); pthread_cond_signal(&queue2_cond); } } } else { My402ListAppend(&queue1,pkt); pkt->q1_enter = getinstanttime(); PrintStat(getinstanttime()); fprintf(stdout,"p%d enters Q1\n", pkt->pkt_id); } pthread_mutex_unlock(&my_mutex); } pthread_mutex_lock(&my_mutex); pthread_cond_signal(&queue2_cond); // This can also be a false signal just to wake up server, if pthread_mutex_unlock(&my_mutex); // say last packet is dropped and there is no pkt to be queued to q2. if(i==-1) { avg_inter_arrival = 0; pkt_drop_prob = 0; } else { avg_inter_arrival = (double)((double)avg_ia/(double)((i+1)*1000000)); pkt_drop_prob = (double)(num_packets - pkts_to_arrive)/(i+1); } pthread_exit(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 * 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 *serverThread(void *id) { Packet *pkt; My402ListElem *elem; int i,pkt_processed = 0; long serv_start, serv_end, avg_st = 0, avg_pkst = 0,sys_time; double sys_avg = 0, sys_avg_sq = 0; double var_sys, var_sys_sq; for(i=0;i<pkts_to_arrive;i++) { pthread_mutex_lock(&my_mutex); while(My402ListEmpty(&queue2) && server_die==0) { if(i==pkts_to_arrive) // This is the case when the last packet is dropped. And the break; // server thread is waiting on q2 and no pkt is there to be queued to q2. pthread_cond_wait(&queue2_cond, &my_mutex); } if(i==pkts_to_arrive) server_die=1; if(server_die!=0) { My402ListUnlinkAll(&queue1); My402ListUnlinkAll(&queue2); pthread_mutex_unlock(&my_mutex); break; } elem=My402ListFirst(&queue2); pkt = (Packet *)elem->obj; My402ListUnlink(&queue2,elem); pkt->q2_exit = getinstanttime(); PrintStat(getinstanttime()); fprintf(stdout,"p%d begins service at S, time in Q2 = %.3fms\n", pkt->pkt_id, (double)(pkt->q2_exit - pkt->q2_enter)/1000); pthread_mutex_unlock(&my_mutex); avg_pkt_q2 += (pkt->q2_exit - pkt->q2_enter); serv_start = getinstanttime(); usleep(pkt->service_time); pkt->sys_exit = serv_end = getinstanttime(); pkt->serv_time = serv_end - serv_start; avg_st += pkt->serv_time; avg_pkst += (pkt->sys_exit - pkt->sys_enter); pkt_processed++; PrintStat(getinstanttime()); sys_time = pkt->sys_exit - pkt->sys_enter; fprintf(stdout,"p%d departs from S, service time = %.3fms, time in system = %.3fms\n", pkt->pkt_id, (double)(pkt->serv_time)/1000, (double)(sys_time)/1000); avg_pkt_s += pkt->serv_time; sys_avg += (double)(sys_time)/1000000; sys_avg_sq += ((double)(sys_time)/1000000) * ((double)(sys_time)/1000000); } // Informing token thread to die. if(pkt_processed!=0) { avg_serv_time = (double)((double)avg_st/(double)(pkt_processed*1000000)); avg_pkt_sys_time = (double)((double)avg_pkst/(double)(pkt_processed*1000000)); var_sys = sys_avg/pkt_processed; var_sys_sq = sys_avg_sq/pkt_processed; std_deviation = sqrt(var_sys_sq - var_sys*var_sys); } else { avg_serv_time = 0; avg_pkt_sys_time = 0; var_sys = 0; var_sys_sq = 0; std_deviation = 0; } token_die=1; pthread_exit(NULL); }