void *Token(void* arg){ int j=0; useconds_t wait_time = 0; if(r < 0.1) r = 0.1; wait_time = (1/r)*SECTOMSEC; while( 1 ) { pthread_mutex_lock(&lock); if( total_packets_prod == num && My402ListEmpty(&Q1_node)){ curr_q2_size = 1; pthread_cond_signal(&serverQ); pthread_mutex_unlock(&lock); break; } pthread_mutex_unlock(&lock); usleep(wait_time); pthread_mutex_lock(&lock); if( total_packets_prod == num && My402ListEmpty(&Q1_node)){ pthread_mutex_unlock(&lock); continue; } pthread_mutex_unlock(&lock); pthread_mutex_lock(&lock); if(token_limit < B){ j++; total_tokens_prod = j; token_limit++; fprintf(stdout, "%012.3lfms: token t%d arrives, token bucket now has %d token\n",GetMTimeOfDay(),j,token_limit); }else{ j++; total_tokens_prod = j; tokens_drop++; fprintf(stdout, "%012.3lfms: token t%d arrives, dropped\n",GetMTimeOfDay(), j ); } //check if it can move first packet from Q1 to Q2 if(!My402ListEmpty(&Q1_node)){ ProcessPacket(); } pthread_mutex_unlock(&lock); } return 0; }
extern int My402ListPrepend(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 *firstNode = My402ListFirst(List); List->anchor.next = newNode; newNode->next = firstNode; firstNode->prev = newNode; newNode->prev = &(List->anchor); (List->num_members)++; return TRUE; } } return FALSE; }
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 My402ListUnlink(My402List *pList, My402ListElem *elem){ if(elem == NULL || My402ListEmpty(pList)) return; elem->prev->next = elem->next; elem->next->prev = elem->prev; free(elem); pList->num_members--; }
void insert_list(My402List * list, My402TransData * data) { if(My402ListEmpty(list)) (void)My402ListAppend(list, (void*)data); else { My402ListElem * elem = My402ListFirst(list); while(elem) { My402TransData * temp = (My402TransData *)(elem -> obj); if(temp -> timestamp < data -> timestamp) { elem = My402ListNext(list, elem); if(!elem) (void)My402ListAppend(list, (void*)data); } else if(temp -> timestamp > data -> timestamp) { (void)My402ListInsertBefore(list, (void*)data, elem); break; } else print_error("two identical timestamp"); } } }
My402ListElem * My402ListLast(My402List * list) { if(My402ListEmpty(list)) return NULL; else return (list -> anchor).prev; }
My402ListElem * My402ListFirst(My402List * list) { if(My402ListEmpty(list)) return NULL; else return (list -> anchor).next; }
extern int My402ListPrepend(My402List*list, void*item) { My402ListElem *elem; elem=malloc(sizeof(struct tagMy402ListElem)); elem->obj=item; if(My402ListEmpty(list)==1) //if list is empty { elem->next=&list->anchor; elem->prev=&list->anchor; list->anchor.next=elem; list->anchor.prev=elem; list->num_members++; return(1); } else { elem->next=list->anchor.next; elem->prev=&list->anchor; list->anchor.next->prev=elem; list->anchor.next=elem; list->num_members++; return(1); } return(0); }
extern int My402ListAppend(My402List*list, void*item) //DRAW AND CHECK THIS------------------------------------------ { My402ListElem *elem; elem=malloc(sizeof(struct tagMy402ListElem)); elem->obj=item; if(My402ListEmpty(list)==1) //if list is empty { elem->next=&list->anchor; elem->prev=&list->anchor; list->anchor.next=elem; list->anchor.prev=elem; list->num_members++; //printf("appended %d when list is empty\n",(int*)elem->obj); return(1); } else { elem->next=&list->anchor; elem->prev=list->anchor.prev; list->anchor.prev->next=elem;//last element list->anchor.prev=elem; list->num_members++; //printf("appended %d when list is not empty\n",(int*)elem->obj); return(1); } return(0); }
My402ListElem *My402ListFirst(My402List *pList){ if(My402ListEmpty(pList)){ return NULL; } else{ return pList->anchor.next; } }
My402ListElem *My402ListLast(My402List *pList){ if(My402ListEmpty(pList)){ return NULL; } else{ return pList->anchor.prev; } }
/* 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 My402ListUnlink(My402List * list, My402ListElem * elem) { if(My402ListEmpty(list)) return; My402ListElem * prev = elem -> prev; My402ListElem * next = elem -> next; prev -> next = next; next -> prev = prev; list -> num_members--; free(elem); }
extern My402ListElem *My402ListLast(My402List*list) { if(My402ListEmpty(list)==1) //if list is empty { return(NULL); } else { return(list->anchor.prev); } }
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; }
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 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 My402ListPrepend(My402List* q, void* x) { My402ListElem* temp1 = (My402ListElem*)malloc(sizeof(My402ListElem)); My402ListElem* p = (My402ListElem*)malloc(sizeof(My402ListElem)); p=&(q->anchor); My402ListElem* a = (My402ListElem*)malloc(sizeof(My402ListElem)); a=My402ListFirst(q); if (temp1==NULL) { return FALSE; } if (a==NULL) { a=p; } if (My402ListEmpty(q)==1) { temp1->obj= x; temp1->prev=p; temp1->next=p; //temp1->next=p->next; q->anchor.next=temp1; q->anchor.prev=temp1; //(temp1->next)->prev=temp1; } else { temp1->obj=x; temp1->prev=p; q->anchor.next=temp1; //(temp1->prev)->next=temp1; temp1->next=a; a->prev=temp1; //(temp1->next)->prev=temp1; } q->num_members++; return TRUE; }
int My402ListPrepend(My402List *pList, void *obj){ My402ListElem *elem = (My402ListElem *)malloc(sizeof(My402ListElem)); if(elem == NULL) return FALSE; elem->obj = obj; if(My402ListEmpty(pList)){ pList->anchor.next = elem; pList->anchor.prev = elem; elem->prev = &pList->anchor; elem->next = &pList->anchor; } else{ elem->prev = &pList->anchor; elem->next = pList->anchor.next; pList->anchor.next->prev = elem; pList->anchor.next = elem; } pList->num_members++; return TRUE; }
// 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; }
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 *Service(void* arg){ My402ListInit(&Q2_node); double time_in_system = 0.0; while(1){ My402ListElem *first_elem = NULL ; Packet_desc *p = NULL; if(total_packets_prod == num && My402ListEmpty(&Q2_node) && My402ListEmpty(&Q1_node)){ pthread_cond_broadcast(&serverQ); break; } pthread_mutex_lock(&lock); while(curr_q2_size == 0 && !interrupt_called){ pthread_cond_wait(&serverQ, &lock); } if((total_packets_prod == num && My402ListEmpty(&Q2_node) && My402ListEmpty(&Q1_node)) || interrupt_called){ pthread_exit(0); } first_elem = My402ListFirst(&Q2_node); if(first_elem->obj == NULL){ fprintf(stderr,"Error: Obj is NULL"); pthread_exit(0); } p = (Packet_desc*)first_elem->obj; DequePacket(p,first_elem); p->S_time_enters = GetMTimeOfDay(); pthread_mutex_unlock(&lock); usleep(p->ser_time*THOUSAND); pthread_mutex_lock(&lock); packet_served++; p->total_S_time = GetMTimeOfDay()- p->S_time_enters; time_in_system = GetMTimeOfDay()- p->Q1_time_enters; sys_variance += time_in_system * time_in_system ; /* Calculate total time for the packets served */ total_ser_time += p->total_S_time; total_sys_time += time_in_system; time_in_Q1 += p->total_Q1_time; time_in_Q2 += p->total_Q2_time; fprintf(stdout, "%012.3lfms: p%d departs from S, service time = %.3fms, time in system = %.3fms\n",GetMTimeOfDay(), p->name_ID,p->total_S_time,time_in_system); free(p); pthread_mutex_unlock(&lock); } return 0; }
void deterministic_packet_procedure(struct command_line_args *object) { struct packet *p,*p2; int i = 0; double time, precise_inter_arrival_time, precise_service_time; int inter_arrival_time = 0, service_time = 0; struct timespec tim; struct timeval prev_packet_arrival_time, now, delta; long time_diff_in_nsec,delta_time; My402ListElem *elem = NULL; precise_inter_arrival_time = ((double)1/object->packet_rate); inter_arrival_time = (int)(precise_inter_arrival_time); if(inter_arrival_time > 10) { precise_inter_arrival_time = 10;inter_arrival_time = 10; } time_diff_in_nsec = (long)((precise_inter_arrival_time - inter_arrival_time)*1000000000L); precise_service_time = ((double)1/object->serving_rate); service_time = (int)(precise_service_time); if(service_time > 10) { precise_service_time = 10;service_time = 10; } prev_packet_arrival_time.tv_sec = GlobalStartTime.tv_sec; prev_packet_arrival_time.tv_usec = GlobalStartTime.tv_usec; while(i < object->no_of_packets) { if((packet_count == object->no_of_packets) && (q1_temp_count == (packet_count-discarded_packets)) && My402ListEmpty(&Q1PacketList)) { pthread_cancel(token_thread); } gettimeofday(&delta,NULL); delta_time = TIME_IN_USEC(delta) - TIME_IN_USEC(prev_packet_arrival_time); tim.tv_sec = inter_arrival_time - (delta_time/1000000); tim.tv_nsec = (time_diff_in_nsec - (delta_time*1000)) <= 0 ? time_diff_in_nsec : ((time_diff_in_nsec - (delta_time*1000))); nanosleep(&tim,NULL); packet_count++; p = create_packet((precise_inter_arrival_time*1000),(precise_service_time*1000),object->token_per_packet); gettimeofday(&(p->Arrival_timestamp), NULL); p->precise_packet_inter_arrival = ((TIME_IN_USEC(p->Arrival_timestamp) - TIME_IN_USEC(prev_packet_arrival_time)))/1000; time = (TIME_IN_USEC(p->Arrival_timestamp) - TIME_IN_USEC(GlobalStartTime))/1000; sum_packet_inter_arrival += p->precise_packet_inter_arrival; prev_packet_arrival_time = p->Arrival_timestamp; pthread_mutex_lock(&token_bucket); if(p->token_per_packet > object->bucket_depth) { gettimeofday(&now, NULL); p->time_in_system = (TIME_IN_USEC(now) - TIME_IN_USEC(p->Arrival_timestamp))/1000; LOG(stdout, "%012.3fms: packet p%d arrives, needs %d token%c, dropped\n",time, p->packet_id,p->token_per_packet,(p->token_per_packet > 1?'s':' ')); free(p); discarded_packets++; pthread_mutex_unlock(&token_bucket); i++; continue; } LOG(stdout, "%012.3fms: p%d arrives, needs %d token%c, inter-arrival time = %.3fms\n",time,p->packet_id, p->token_per_packet,(p->token_per_packet > 1? 's':' ') ,p->precise_packet_inter_arrival); My402ListAppend(&Q1PacketList, p); gettimeofday(&(p->Q1timestamp), NULL); time = (TIME_IN_USEC(p->Q1timestamp) - TIME_IN_USEC(GlobalStartTime))/1000; LOG(stdout, "%012.3fms: p%d enters Q1\n",time,p->packet_id); elem = My402ListFirst(&Q1PacketList); p2 = (struct packet *)elem->obj; if(p2->token_per_packet <= tokens_in_bucket) { tokens_in_bucket -= p2->token_per_packet; move_packet_q1_to_q2(object,elem, p2); } pthread_mutex_unlock(&token_bucket); if((packet_count == object->no_of_packets) && (q1_temp_count == (packet_count-discarded_packets)) && My402ListEmpty(&Q1PacketList)) { pthread_cancel(token_thread); } i++; } if((packet_count == object->no_of_packets) &&(q1_temp_count == (packet_count-discarded_packets)) && My402ListEmpty(&Q1PacketList)) { pthread_cancel(token_thread); } if((packet_count == object->no_of_packets) &&(completed_packets == (packet_count - discarded_packets)) && My402ListEmpty(&Q2PacketList)) { EndServerThread = 1; pthread_cond_signal(&is_q2_empty); } }
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 *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); }
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 * 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 *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); } } }