ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Am writing code for a TCP timer ; and evrything seems okay but halfway through the code I came across this extremely weird happening where my timer returns with widely differing values each time.Heres teh code :-
Code:
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include <time.h>
using namespace std;
u_int32_t store_ack_tid[2][20];
int row1=0;int row2=0; int segment_send_count=0;
int srtt=0;int rttvar=0;
struct timeval *tp; struct timezone *tz;
typedef struct time_recording{
u_int16_t oldtimesec;
u_int16_t oldtimemicsec;
u_int16_t newtimesec;
u_int16_t newtimemicsec;
};
struct time_recording *time_record = new struct time_recording;
void seg_recv_handler()
{
//CODE FOR RECEIVING THE SEGMENT...AFTER RECEIVING THIS SEGMENT WE FIND OUT THE RTT FOR THIS AND USE THIS VALUE IN OUR
//DYNAMIC CALCULATION OF RTO.
cout<<"Inside seg recv handler"<<endl;
if ((gettimeofday(tp,tz))!=0)
{
cout<<"Problem in gettimeofday : "<<endl;
exit(0);
}else
{
time_record->newtimesec=tp->tv_sec;
time_record->newtimemicsec=tp->tv_usec;
}
//cout<<"RTT is "<<(((time_record->newtimesec) - (time_record->oldtimesec))*(1000))+
// (((time_record->newtimemicsec)-(time_record->oldtimemicsec))/(1000))<<endl;
cout<<"New : "<<time_record->newtimesec<<" "<<time_record->newtimemicsec<<endl;
}
void * check_timer_exp(void *tid)
{
int RTO=5;
store_ack_tid[1][row2]=(pthread_t)tid;
row2++;
while (clock()< RTO)
{
//cout<<"Clock looping "<<endl;
}
//int x=clock();
seg_recv_handler();
//cout<<"Return value is "<<x<<endl;
}
/*void dynamic_timer_calc()
{
int rtt=0;
if (segment_send_count == 0)
rtt=3;
segment_send_count++;
}*/
void start_timer(int storeseq)
{
int temp=0;
cout<<"Storeack is "<<storeseq<<endl;
pthread_t tid;
store_ack_tid[0][row1]=storeseq;
row1++;
if((temp=pthread_create(&tid,NULL,check_timer_exp,&tid))!=0)
{
cout<<"Thread failed : "<<"Error code is "<<temp<<endl;
exit(0);
}
//cout<<store_ack_tid[0][0]<<" "<<store_ack_tid[1][0]<<endl;
}
void send_segment()
{
tp = new struct timeval;
tz = new struct timezone;
int seq=5; // IN THE REAL CODE THIS WILL NOT BE THERE AS THE SEQ WILL ALREADY BE DEFINED
cout<<"CODE FOR SENDING SEGMENT"<<endl;
//TIMER STARTS HERE AND VALUES WILL BE STORED In the User Defined structure
if ((gettimeofday(tp,tz))!=0)
{
cout<<"Problem in gettimeofday : "<<endl;
exit(0);
}
else
{
time_record->oldtimesec=tp->tv_sec;
time_record->oldtimemicsec=tp->tv_usec;
}
cout<<"Old : "<<time_record->oldtimesec<<" "<<time_record->oldtimemicsec<<endl;
//VALUES HAVE BEEN STORED NOW AND THE TIMER IS STARTED
start_timer(seq);
}
int main()
{
int i=0; int j=0;
cout<<"inside main"<<endl;
for (i=0; i<2; i++)
for (j=0; j<20; j++)
store_ack_tid[i][j]=0;
send_segment();
sleep(1); //I was forced to do this else my code wasn't even printing my time sometimes
return 0;
}
Code:
Heres the O/p :
[root@LABPC02 timers]# ./timer
inside main
CODE FOR SENDING SEGMENT
Old : 33811 28598
Storeack is 5
Inside seg recv handler
New : 33811 37714
[root@LABPC02 timers]# ./timer
inside main
CODE FOR SENDING SEGMENT
Old : 33820 10185
Storeack is 5
Inside seg recv handler
New : 33820 14026
[root@LABPC02 timers]# ./timer
inside main
CODE FOR SENDING SEGMENT
Old : 33826 51672
Storeack is 5
Inside seg recv handler
New : 33826 59074
[root@LABPC02 timers]# ./timer
inside main
CODE FOR SENDING SEGMENT
Old : 33829 31218
Storeack is 5
Inside seg recv handler
New : 33829 36932
The old and new values are chnaging drastically each time.....I mean the code's running okay..but is this ok?...and if it is can someone tell me why it happens.
Thnx a lot
Arvind
I see a lot of C-ism in the code and some unnecessary stuff,
but I find it hard to help you because I don't understand what
kind of output the program should display if it was working correctly...
Your code didn't even compile on GCC 4.0.0, I change some things. Try this, maybe it's some help. Really hard since I don't know what you're trying to do.
Code:
#include <cstring>
#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include <time.h>
using namespace std;
u_int32_t store_ack_tid[2][20];
int row1=0;int row2=0;
struct time_recording
{
u_int16_t oldtimesec;
u_int16_t oldtimemicsec;
u_int16_t newtimesec;
u_int16_t newtimemicsec;
};
time_recording time_record;
timeval tp;
timezone tz;
void seg_recv_handler()
{
//CODE FOR RECEIVING THE SEGMENT...AFTER RECEIVING THIS SEGMENT WE FIND OUT THE RTT FOR THIS AND USE THIS VALUE IN OUR
//DYNAMIC CALCULATION OF RTO.
cout<<"Inside seg recv handler"<<endl;
if(gettimeofday(&tp, &tz) != 0)
{
cout<<"Problem in gettimeofday : "<<endl;
exit(0);
}
else
{
time_record.newtimesec = tp.tv_sec;
time_record.newtimemicsec = tp.tv_usec;
}
//cout<<"RTT is "<<(((time_record.newtimesec) - (time_record.oldtimesec))*(1000))+
// (((time_record.newtimemicsec)-(time_record.oldtimemicsec))/(1000))<<endl;
cout<<"New : "<<time_record.newtimesec<<" "<<time_record.newtimemicsec<<endl;
}
void * check_timer_exp(void *tid)
{
int RTO = 35; /* 5 was too small a value on my system. */
store_ack_tid[1][row2]=(u_int32_t)tid;
row2++;
unsigned int loop_count = 0;
while(clock() < RTO)
{
++loop_count;
}
cout << "Looped " << loop_count << " times." << endl;
//int x=clock();
seg_recv_handler();
//cout<<"Return value is "<<x<<endl;
return NULL;
}
/*void dynamic_timer_calc()
{
int rtt=0;
if (segment_send_count == 0)
rtt=3;
segment_send_count++;
}*/
void start_timer(int storeseq)
{
int temp=0;
cout<<"Storeack is "<<storeseq<<endl;
pthread_t tid;
store_ack_tid[0][row1]=storeseq;
row1++;
if((temp=pthread_create(&tid,NULL,check_timer_exp,&tid))!=0)
{
cout<<"Thread failed : "<<"Error code is "<<temp<<endl;
exit(0);
}
//cout<<store_ack_tid[0][0]<<" "<<store_ack_tid[1][0]<<endl;
}
void send_segment()
{
int seq=5; // IN THE REAL CODE THIS WILL NOT BE THERE AS THE SEQ WILL ALREADY BE DEFINED
cout<<"CODE FOR SENDING SEGMENT"<<endl;
//TIMER STARTS HERE AND VALUES WILL BE STORED In the User Defined structure
if(gettimeofday(&tp, &tz) != 0)
{
cout<<"Problem in gettimeofday : "<<endl;
exit(0);
}
else
{
time_record.oldtimesec = tp.tv_sec;
time_record.oldtimemicsec = tp.tv_usec;
}
cout<<"Old : "<<time_record.oldtimesec<<" "<<time_record.oldtimemicsec<<endl;
//VALUES HAVE BEEN STORED NOW AND THE TIMER IS STARTED
start_timer(seq);
}
int main()
{
memset(&store_ack_tid, 0, sizeof(store_ack_tid));
send_segment();
sleep(1); //I was forced to do this else my code wasn't even printing my time sometimes
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.