Sunday, September 4, 2011

struct timeval in C

 * The timeval Structure,
 * strcut timval example in c
 * using timeval structure

If you remember the select function arguments in socket,the last argument, timeout, points to a structure that must be initialized unless a NULL pointer is provided instead. Listing 11.3 shows the definition of the timeval structure.

 * Listing 11.3: The Definition of the timeval Structure
 struct timeval {
   long tv_sec;    /* seconds */
   long tv_usec;   /* microseconds */

To establish a timeout value of 1.75 seconds, you would code something like this:

struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 750000;

The example shows the establishing of a timeout of 1.75 seconds. This is done by setting up a timeout of one second plus 750,000 microseconds.

Note that the timeval structure values are modified by the select(2) call. The timeval structure should be re -initialized prior to each call to select(2), unless the program requires that the time remaining be used as the timeout.


  1. can you put an example program ?

  2. Hi there, I read your blogs on a regular basis. Your humoristic style is witty, keep it up! Thank You for Providing Such a Unique and valuable information, If you are looking for the best Technical Interview Questions,then visit TutorialCup. I enjoyed this blog post.