0

HI there!
So ive been looking around on how to reset the function clock() and the examples ive found have not worked, basically in my code i have to loops, the first i want to run for 15 mins then the inner loop run for one, so i assigned a different variable "wait1" to clock and then after the 1 minute tried resetting it(this is what many threads have said to do) but htis does not work anyways here is the code. I apologize in advance for the terrible variable names.

#include <iostream>
#include <time.h>
#define MIN 60
#define MAXSIZE 1000
#include <Windows.h>
#define TOTTIME 15
//first random number will have to be 1-2 to create 30 msgs a min, the second will have to be 1 - 3 to remove 40 msgs a min, and 1 -4 to see if theyre processed properly(25%) fail rate
using namespace std;



struct intqueue{

	int *queueAry;
	int rear;
	int count;
	int front;
	int maxSize;
};


struct intqueue  *createQueue (int maxSize) {
	struct intqueue   *queue;
	queue = (struct intqueue  *) malloc (sizeof (struct intqueue));
	if (queue != NULL) {
		/* head structure created. Now allocate queue */
		queue->queueAry = (int *)malloc(maxSize * sizeof(int));
		queue->front   = -1;
		queue->rear    = -1;
		queue->count   =  0;
		queue->maxSize = maxSize;
	}
	return queue;
} /* createQueue */





int enqueue (struct intqueue  *queue, int   datain) {
	if (queue->count == queue->maxSize)
		return 0; /* queue is full */
	(queue->rear)++;
	if (queue->rear  == queue->maxSize)
		/* Queue wraps to element 0 */
		queue->rear  =  0;
	queue->queueAry[queue->rear] = datain; 
	if (queue->count == 0) {
		/* Inserting into null queue */
		queue->front  = 0;
		queue->count  = 1;
	} /* if */
	else (queue->count)++;
	return 1;
}


int dequeue (struct intqueue *queue, int *dataOutPtr) {
	if (!queue->count){
		cout <<"no messages to process";
		return 1;
	}
	*dataOutPtr = queue->queueAry[queue->front];
	(queue->front)++;
	if (queue->front == queue->maxSize)
		/* queue front has wrapped to element 0 */
		queue->front =  0;
	if (queue->count == 1)
		/* Deleted only item in queue */
		queue->rear = queue->front = -1;
	(queue->count)--;
	return 1;
}



struct stats{
	int totmsgprcsd;//total messages processed
	int avgarvrate;// average arival rate
	int avgmsgpermin;// average msgs sent per min
	int avgmsginQ;//average msgs in the Q per min
	int msgsentattmpt[1000][1];//average messages sent on attempt [attempt][messages]
	int avgreQ;//# of times msg had to be reQueued
}Totstats;


void wait ( int seconds )
{
	clock_t endwait;
	endwait = clock () + seconds * CLOCKS_PER_SEC ;
	while (clock() < endwait) {}
}






void main(){
	srand( time(NULL));
	struct intqueue *myqueue;
	myqueue = createQueue(MAXSIZE);

	int i= 0, temp1, temp2, temp3, *dataout = 0, msgssent = 0, totmsgssent = 0, msgpermin[TOTTIME], msginQ[TOTTIME], temp4 = 0, j;
	double avgarivrate;
	clock_t endwait;
	endwait = clock () + MIN * CLOCKS_PER_SEC ;
	clock_t wait1;
	clock_t wait2;
	wait2 = clock () + (15 * 60) * CLOCKS_PER_SEC ;
	wait1 = clock();
	while (clock() < wait2){
		while (wait1 < endwait) {

			Sleep(1000);
			temp1 = rand() % 2 + 1;
			//cout << "temp 1 is: " << temp1 << "\n";
			if(temp1 == 2){
				cout<< "msg received\n";
				enqueue(myqueue,temp1);
				temp4++;
			}

			temp2 = rand() % 3 + 1;
			//cout << "temp 2 is: " << temp2 << "\n";
			if(myqueue->count != 0){
				if(temp2 == 3){
					cout<< "attempting to process a msg \n";
					temp3 = rand() % 4 + 1;
					//cout << "temp 3 is: " << temp3 << "\n";
					if(temp3 != 4){
						cout << "message processed \n";
						dequeue(myqueue, &temp1);
						msgssent++;
						totmsgssent++;
					}
					else{
						cout << "server is busy could not process returned to the back of the queue\n";
						enqueue(myqueue,temp3);
					}
				}
			}
			j = msgssent;
			Totstats.msgsentattmpt[i][0] += Totstats.msgsentattmpt[i][j];
			msgpermin[i] = msgssent;
			msginQ[i] = temp4; 
		}
		
		i++;
		cout << "the attempt: " << i << " is now over starting attempt # " << i + 1 << " \n";
		msgssent = 0;
		temp4 = 0;
		cout << "The size of teh queue is: " << myqueue->count;
		wait1 = clock() - wait1;
	}

	Totstats.totmsgprcsd = totmsgssent;
	avgarivrate = 900 / totmsgssent;
	Totstats.avgarvrate = avgarivrate;
	for(int k = 0;k < TOTTIME; k++){
		temp1 += msgpermin[k];
		temp2 += msginQ[k];

	}
	Totstats.avgmsgpermin = temp1 / TOTTIME;
	Totstats.avgarvrate = temp2 / TOTTIME;
}
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by nezachem
0

In the inner loop

while (wait1 < endwait) {

(lines 113 to 147) neither wait1 nor endwait ever change. You have to structure the loop similar to the outer one:

outer_loop_stop_time = clock() + outer_loop_run_time;
    while(clock() < outer_loop_stop_time) {
        inner_loop_stop_time = clock() + inner_loop_run_time;
        while(clock() < inner_loop_run_time) {
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.