C Programming: give command a 5 sec interval to run

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
kcirtap
Messages
5
Reaction score
0
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.
repeat

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.
 
Physics news on Phys.org
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.
 
For a 5 seconds delay use "sleep(5);" , which doesn't waste CPU processing time.