Hi, I have to create a ciruclar list that is shared between two threads. One thread extends the list by storing each new packet on a new node and the other thread reads these packets and frees the node and so on. I need to you check flow of this work. Appreciate your swift replies as this is really urgent.

Creation of the circular list with a predefined 100000 nodes in main thread
Code:
  int counter=0;
  emptylist=NULL;
  pktlist=NULL;
  senderlist=NULL;
  
    while(counter <= 100000)
    {
      enode=(struct CaptureBuffer *)malloc(sizeof(struct CaptureBuffer));
      counter++;
      memset(enode,0,4136);
      if(emptylist == NULL)
    {
               senderlist=shead=snode=pnode=emptylist=ehead=ecurr=enode;
     
    }
      else
    {

      ecurr->next=enode;
      ecurr=ecurr->next;
      enode->next= ehead;
     
    }
      
      
    }
  
  etail=ecurr;
  etail->next=ehead;
  ehead->prev=etail;
-----------------------------------------------------------------

Thread-1 that captures the packets and stores each packet on a new node of the list..

Code:
pnode=pktlist; // START OF THE LIST


if(pktlist == NULL)
        {

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

        }
------------------------------------------------

Thread-2 that retrieves the packets and deletes the node after sending the packet.

Code:
snode=scurr=pktlist;
while(scurr->next!=NULL) // FIRST CHECKS FOR THE SMALLEST PACKET.
     {
       if(snode->ts.tv_sec < scurr->ts.tv_sec && snode->ts.tv_usec < scurr->ts.tv_usec)
        {
          TMPNode=snode;
          snode=scurr;
          scurr=TMPNode;
        }
       
       scurr=scurr->next;
     }
   
    
     
       gettimeofday(&curr_time,NULL);      
//SENDS THE PACKET IF THE PACKET TIMESTAMP IS SMALLER THAN THE CURRENT TIME.
       if(curr_time.tv_sec >= snode->ts.tv_sec  && curr_time.tv_usec >= snode->ts.tv_usec)
     {
       
       struct sockaddr_ll destAddr;
       memcpy(buf,&snode->pkt,snode->pkt_len);
       memcpy(&(destAddr.sll_addr),&buf,MAC_ADDR_LEN);
       destAddr.sll_family=PF_PACKET;
       destAddr.sll_protocol=htons(ETH_P_ALL);
       destAddr.sll_ifindex=2;
       destAddr.sll_pkttype=PACKET_OTHERHOST;
       destAddr.sll_halen=ETH_ALEN;
       if(snode2->rFlag==0 && snode2->wFlag==1)
         {
           
           if((retVal=write(Fd,snode->pkt,snode->pkt_len))==-1)
         {
           printf("retVal is %d",retVal);
           printf("sendto() error \n");
         }
           printf("SENDER 1 --- packet number is %d OF length %d Sent \n",snode2->pkt_id,retVal);
           snode2->rFlag=1;
           snode2->wFlag=0;
         }
       
     }
  pthread_mutex_unlock( &mut );
       snode=snode->next;
  }while(snode->next != NULL);
IN SECOND THREAD, I NEED TO FIND OUT THE SMALLEST PACKET IN THE LIST AND RETRIEVING UPON THE PACKET TIME STAMP REACHES THE PLAYOUT TIME (CURRENT CLOCK TIME) AND THEN SENDING THE PACKET AND REMOVING THE NODE SO THAT NEXT TIME AGAIN THE SAME NODE IS NOT READ AS CONTAINING THE SMALLEST PACKET.


I NEED YOUR SWIFT REPLIES. IT'S REALLY URGENT.

Last edited by shabbir; 30Jul2009 at 21:30.. Reason: Code blocks