Thread: Predefined size circular list shared between two threads

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    Predefined size circular list shared between two threads

    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
    
    //ADDING PACKET TO LIST USING PNODE HERE (CODE OMITTED)
    
    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.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  3. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  4. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 08:02 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

Tags for this Thread