C Programming: give command a 5 sec interval to run

In summary, the conversation discusses two programs, A and B, which are responsible for sending and receiving packets and acknowledging each other. The main issue is with setting a timer for the programs to receive packets within a 5-second time interval. The solution suggested is to use the select() function and gettimeofday() to monitor the arrival of packets and terminate the loop after the desired time interval has passed.
  • #1
kcirtap
5
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.
 
Technology news on Phys.org
  • #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.
 
  • #3
For a 5 seconds delay use "sleep(5);" , which doesn't waste CPU processing time.
 

1. How can I give a command a 5 second interval to run in C programming?

To give a command a 5 second interval to run in C programming, you can use the sleep() function from the unistd.h library. This function pauses the execution of the program for the specified number of seconds.

2. Can I use a loop to create a 5 second interval in C programming?

Yes, you can use a loop to create a 5 second interval in C programming. You can use the sleep() function within a loop to pause the execution of the program for 5 seconds before continuing.

3. Is there a way to make a command run every 5 seconds continuously in C programming?

Yes, you can use a while loop and the sleep() function to continuously run a command every 5 seconds in C programming. The loop will continue until the program is terminated or the loop is broken.

4. How can I make my program wait for 5 seconds before executing a command in C programming?

You can use the sleep() function before the command in your program to make it wait for 5 seconds before executing. This will pause the program for 5 seconds before moving on to the next line of code.

5. Are there any other ways to create a 5 second interval in C programming besides using the sleep function?

Yes, there are other ways to create a 5 second interval in C programming. You can use the usleep() function to pause the program in microseconds, or you can also use the nanosleep() function to pause the program in nanoseconds.

Similar threads

  • Programming and Computer Science
Replies
22
Views
907
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Computing and Technology
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
29
Views
5K
  • Programming and Computer Science
Replies
1
Views
936
Back
Top