Quote:
Originally Posted by xpi0t0s View Post
> I am using two different mutex variable

That's likely to be the problem. All you're doing by using two mutex variables is preventing thread1 from interfering with itself (which it won't do anyway even without a mutex) and likewise for thread2. To stop thread1 and thread2 interfering with each other you need to use the SAME mutex flag.

> What is the shortfall if 4136 is used
None if 4136 is exactly equal to sizeof(CaptureBuffer). If it isn't and the problem is that you get odd behaviour, it could be that you're getting UB due to corrupted memory. Remember, you didn't say what the actual problem was, you just posted partial code and said "check the flow", so if I failed to guess the problem correctly then I guess my mind reading skills aren't up to your expectations.

Despite me asking, you still haven't said what the actual problem is. Is there one? What are you observing that you didn't expect?

Here is the code of the first thread.
Code:
pthread_mutex_t mutex2=PTHREAD_MUTEX_INITIALIZER;

void* capture(void* ptr)
{
  printf("I am inside the capture thread to capture packets \n");

  struct sockaddr_ll packet_info;
  int packet_info_size = sizeof(packet_info);
  struct timeval time2;
  union semun args;
  capProcess *cProc;
   int sd;
   char* nic;
   extern int semaphore;
   int pktCnt;

   cProc=(capProcess*)ptr;
   sd=cProc->sd;
   nic=cProc->nic;
   semaphore=cProc->semaphore;
   pktCnt=cProc->pktCnt;
  int len1;
  // int pktCnt=1;
  
  printf("CAP1 THREAD--sd is %d \n",sd); 
 
  // wPos=0;
  int  packets_to_sniff=10000000;
  args.val=1;
  unsigned char *pkt=(unsigned char *)malloc(2048*(sizeof(unsigned char *)));
   
  
    while( packets_to_sniff--)
    {

      if((len1=recvfrom(sd,pnode->pkt,2048,0,(struct sockaddr*)&packet_info,&packet_info_size)) == -1)
    {
      perror("Recvfrom() returned -1");
      exit(-1);
    }
      else
    {
      
      ioctl(sd,SIOCGSTAMP,&time2);


      pthread_mutex_lock(&mutex2);

          pnode->pkt_id=pktCnt;
          printf("packet number is %d \n",pnode->pkt_id);
          //      memcpy(&pnode->pkt,&pkt,len1);
          pnode->pkt_len=len1;
          printf("packet of length %d bytes received \n",pnode->pkt_len);
          pnode->ts.tv_sec=time2.tv_sec;
          printf("packet of time seconds are  %ld  \n",pnode->ts.tv_sec);
          pnode->ts.tv_usec=time2.tv_usec;
          printf("packet of time micro seconds are  %ld  \n",pnode->ts.tv_usec);
          pnode->wPos=wPos;
          rPos=wPos-3;
          pnode->rPos=rPos;
          pnode->wFlag=1;
          pnode->rFlag=0;
          char s[20];
          gettimeofday(&time2);      
          wPos++;
          //   }


      /* current system clocke time in hour-min-sec*/
      time_t rawtime;
      struct tm *timeinfo,*timeinfo2;
      char buffer[80],hr[10],min[10],sec[10];
      int str;int cnt;

      time(&rawtime);
      timeinfo=localtime(&rawtime);
      strftime(hr,10,"%I",timeinfo);
      strftime(min,10,"%M",timeinfo);
      strftime(sec,10,"%S",timeinfo);

      if(strcmp(min,"00")==0)
        {
          printf("New Hour starts \n");
          cnt=0;
        }
      else
        {
          int whichsecond;
          int mins, secs;
          mins = atoi(min);
          printf("mins=%d\n",mins);
          secs=atoi(sec);
          printf("secs=%d\n",secs);

          whichsecond= (mins*60)+secs;
          printf("whichsecond is %d",whichsecond);
          cnt=whichsecond;
        




          /* ADD DELAY AMOUNT TAKEN FROM THE ARRAY IN THE TIMESTAMP OF THE PACKETS*/

          struct timeval delay;
          delay.tv_usec=(1000*Shaper[cnt])+time2.tv_usec;
          pnode->ts.tv_usec=delay.tv_usec;
          printf("pnode->ts.tv_usec = %ld.......... \n",pnode->ts.tv_usec);
          cnt++;

        }

                pthread_mutex_unlock(&mutex2);      

    
      int n=0;int m=0;
    

            
      if(pktlist == NULL)
        {

      
          pktlist=pnode;
          phead=pnode;
          pcurr=pnode;
        }
      else
        {
          
          pcurr->next =pnode;
          pcurr=pcurr->next;

        }


      
    }

      pktCnt++;

    }

Last edited by shabbir; 31Jul2009 at 09:25.. Reason: Code blocks