LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-30-2005, 08:21 AM   #1
live_dont_exist
Member
 
Registered: Aug 2004
Location: India
Distribution: Redhat 9.0,FC3,FC5,FC10
Posts: 257

Rep: Reputation: 30
Strange multithreading


Hi Guys,

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
 
Old 05-01-2005, 07:18 PM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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...
 
Old 05-01-2005, 07:26 PM   #3
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
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;
}
HTH, YMMV
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Multithreading dlublink Linux - Software 1 10-25-2005 12:23 PM
Slow multithreading with 2.6.13 andy753421 Linux - Software 8 09-16-2005 04:29 PM
Multithreading jayashrik Programming 1 07-20-2005 04:13 AM
java: multithreading ashirazi Programming 5 06-23-2005 09:50 AM
multithreading libraries Pres Programming 3 06-20-2003 03:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration