What model should I use when sending receiving packets?

This is a discussion on What model should I use when sending receiving packets? within the Networking/Device Communication forums, part of the General Programming Boards category; I see, I'll put it back in then. Like this right? BTW the for loop is just there instead of ...

  1. #16
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I see, I'll put it back in then. Like this right? BTW the for loop is just there instead of sending of packets from a pre made queue.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <unistd.h>
    #include <string.h>
    #include <pthread.h>
    
    pthread_cond_t DataReady = PTHREAD_COND_INITIALIZER;
    bool ready = 0;
    
    pthread_mutex_t Lock = PTHREAD_MUTEX_INITIALIZER;
    
    void *consumer(void *Data){
            int i;
            for(i = 1; i <= 10; i++){
                    pthread_mutex_lock(&Lock);
    
                    while(!ready)
                            pthread_cond_wait(&DataReady, &Lock);
    
                    printf("Sending packet %d: %s", i, (char*)Data);
                    ready = false;
                    pthread_mutex_unlock(&Lock);
            }
    }
    
    void get_input(char *Data){
            while(1){
                    fgets(Data, 100, stdin);
    
                    pthread_mutex_lock(&Lock);
    
                    ready = true;
                    pthread_cond_signal(&DataReady);
                    pthread_mutex_unlock(&Lock);
            }
    }
    
    int main()
    {
            char Data[100];
    
            pthread_t a;
            pthread_create(&a, NULL, consumer, Data);
    
            get_input(Data);
    
            return 0;
    }
    Thanks
    Last edited by Subsonics; 05-01-2010 at 12:02 PM.

  2. #17
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    >> Like this right?
    Yes, that will handle spurious wakeups. But you still don't have a 1-to-1 correlation between the cond_signal() call and the "Sending packet" printf(). If that's required, you'd need to change "ready" to a counter.

    gg

  3. #18
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Codeplug View Post
    >> Like this right?
    Yes, that will handle spurious wakeups. But you still don't have a 1-to-1 correlation between the cond_signal() call and the "Sending packet" printf(). If that's required, you'd need to change "ready" to a counter.

    gg
    Ah, thanks. Good call on the counter, I guess I can just init counter to 0, then check for:

    while(counter < i)

    And increment it before signaling.

    Thanks for all help, much appreciated.

  4. #19
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    Would be "while (counter == 0)" for 1-to-1 correlation to cond_signal() calls. The outer loop would just stop things after 10 cond_signal() calls.

    gg

  5. #20
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Um, but it seems like just changing the name from "ready" to "counter" then? I suppose that I would then set counter to 1 before signaling, and setting it back to 0 before returning.

  6. #21
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Ok I get you, you mean to add a while loop with a counter check to pthread_cond_signal.

  7. #22
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,607
    I meant:
    Code:
    consumer:
        pthread_mutex_lock(&Lock);
        while (counter == 0)
            pthread_cond_wait(&DataReady, &Lock);
        --counter;
        pthread_mutex_unlock(&Lock);
    
    producer:
        pthread_mutex_lock(&Lock);
        ++counter;
        pthread_cond_signal(&DataReady);
        pthread_mutex_unlock(&Lock);
    If you were using a shared queue, then the predicate would be "while the Q is empty, wait".

    gg

  8. #23
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Ah I see, that makes sense. Got it all working now in my program. Thanks for the help Codeplug.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending ARP packets
    By Poincare in forum C Programming
    Replies: 5
    Last Post: 07-15-2009, 09:02 AM
  2. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 10:45 AM
  3. socket message sending and receiving problem
    By black in forum C Programming
    Replies: 5
    Last Post: 01-15-2007, 03:46 AM
  4. Help with file reading/dynamic memory allocation
    By Quasar in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2004, 03:36 PM
  5. Sending raw packets.
    By Denethor2000 in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2002, 02:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21