Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C Programming: give command a 5 sec interval to run

  1. Dec 5, 2009 #1
    Hi all,

    I am new to C and I need some help solving an issue with network programming.
    So in general, i have two programs running: Program A and Program B.

    Description of A:
    Send out packet with SN = i
    (SN is the label for the packet. i.e first pkt SN=1, 2nd pkt SN=2 etc)
    Set timer
    If RN=x is received from B, and x=i+1, set SN=i+1.
    (RN is label for packet coming back from B)
    Otherwise, ignore.

    If timer expires, resend pkt SN=i
    Reset timer
    and repeat.

    Description of B:
    Send out packet with RN = j
    Set timer
    If SN=y is received from A, and y=i, set RN=i+1.
    Otherwise, ignore.

    In short, these two programs are just loops. A needs to receive an acknowledgment from B to proceed to the next state, otherwise resend the current packet. And for B, if B received a packet from A, it will send an acknowledgment to A telling it to move on to next state.

    My problem is with the "Set timer" part. So this part is supposed to allow about 5 seconds for each program to receive pkt. For those who know how to use the select() function, I want something similar select() except it will watch over an integer (like SN and RN) instead of just a socket.

    Thanks in advance.
  2. jcsd
  3. Dec 6, 2009 #2
    If I understand correctly, the integer would only be modified by the code that parses packets received over the socket?

    In that case, you need to call gettimeofday(), record the value, then start an infinite loop of select(), packet parsing, and more gettimeofday(), that terminates when either of two conditions is satisfied:

    - (1) a packet with the correct number has arrived
    - or (2) the predetermined time interval has elapsed since you entered into the loop.
  4. Dec 7, 2009 #3
    For a 5 seconds delay use "sleep(5);" , which doesn't waste CPU processing time.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: C Programming: give command a 5 sec interval to run
  1. MOVE command, C:, C:\ (Replies: 10)