Linux timed timer operation __linux

Source: Internet
Author: User
Tags assert local time

First, the use of the Select () function to achieve non-blocking wait time, using the structure of struct Timeval {}, here is not much to say.

Second, use Gettimeofday () can obtain the subtle level (0.000001 seconds) of the system time, call two times Gettimeofday (), before and after subtraction, so as to achieve the purpose of timing or calculation time.

Prototype: int gettimeofday (struct timeval *tv,struct timezone *tz) Returns the structure that the current time TV refers to, and the local time zone information is placed in the structure referred to in TZ. Both of these structures are placed in/usr/include/sys/time.h.

#include <stdio.h>
#include <stdlib.h>//malloc to use, if not, there will be a warning message: implicit declaration is incompatible with the built-in function ' malloc '. But the warning message is fine.

#include <assert.h>
#include <sys/time.h>

int main ()
{
float time_use=0;
struct Timeval start;
struct Timeval end;
struct timezone tz; There's a note behind it.
Gettimeofday (&start,null);//gettimeofday (&start,&tz); same result.
printf ("start.tv_sec:%d\n", start.tv_sec);
printf ("start.tv_usec:%d\n", start.tv_usec);

Sleep (3);
Gettimeofday (&end,null);
printf ("end.tv_sec:%d\n", end.tv_sec);
printf ("end.tv_usec:%d\n", end.tv_usec);
Time_use= (end.tv_sec-start.tv_sec) *1000000+ (end.tv_usec-start.tv_usec);//microseconds
printf ("Time_use is%f\n", time_use);

Output: Time_use is 3001410.000000

The following pointers can also be used, but note that the pointer type is not allocated memory, the compilation is correct, but the result will be wrong

/*************************************************

struct Timeval *start;
struct Timeval *end;
struct timezone *tz;

start= (struct Timeval *) malloc (sizeof (struct timeval));
ASSERT (Start!=null);
end= (struct Timeval *) malloc (sizeof (struct timeval));
ASSERT (Start!=null);
Gettimeofday (Start,null);//gettimeofday (Start,tz);

printf ("start->tv_sec:%d\n", start->tv_sec); printf ("(*start). tv_sec:%d\n", (*start). tv_sec);
printf ("start->tv_usec:%d\n", start->tv_usec);

printf ("tz->tz_minuteswest:%d\n", tz->tz_minuteswest);
printf ("tv->tz_dsttime:%d\n", tz->tz_dsttime);
Sleep (3);
Gettimeofday (End,null);//gettimeofday (End,tz);
printf ("end->tv_sec:%d\n", end->tv_sec);
printf ("end->tv_usec:%d\n", end->tv_usec);
Time_use= (end->tv_sec-start->tv_sec) *1000+ (end->tv_usec-start->tv_usec)/1000;//ms
printf ("Time_use is%f\n", time_use);

Output: Time_use is 3001.000000

**********************************************/
/*****************************
struct Timeval
{
time_t tv_sec; Seconds
suseconds_t tv_usec; Subtle 10-6
};

struct TIMEZONE
{
int tz_minuteswest;//and Greenwich GMT how many minutes bad
int tz_dsttime; The state of daylight saving time
}
******************************/

}

Third, the minimum to the second time to obtain

int time (char cnt)
{

time_t T; Instantiate time_t structure

struct TM *timenow1; Instantiating a TM structure pointer
struct TM *timenow2; Instantiating a TM structure pointer

Time (&t); the//time function reads now (international time is not Beijing time) and then passes the value to T
Timenow1=localtime (&t); The LocalTime function converts the time that you have obtained from times T to the time in your computer (the area you set up)
printf ("The current time1 is:%02d:%02d:%02d\n", timenow1->tm_hour,timenow1->tm_min,timenow1->tm_sec);
printf ("Local time1 is%s\n", Asctime (Timenow1)); The Asctime function in the previous sentence converts the time to a character and outputs it through the printf () function

Time (&t);

Timenow2=localtime (&t);
printf ("The current time2 is:%02d:%02d:%02d\n", timenow2->tm_hour,timenow2->tm_min,timenow1->tm_sec);
printf ("Local time2 is%s\n", Asctime (Timenow2));

if ((Timenow2->tm_hour==timenow1->tm_hour) && (timenow2->tm_min==timenow1->tm_min)
{
n=timenow2->tm_sec-timenow2->tm_sec;
printf ("N is%d\n", n);//sec
}

Note: If you want to get international standard Time, replace localtime with gmtime function
Note: time_t is a structure that is defined in the time.h. The original prototype of the TM structure is as follows:

/*
struct TM//min to sec, #include <time.h>
{
int Tm_sec;//seconds 0-61
int Tm_min;//minutes 1-59
int tm_hour;//hours 0-23
int tm_mday;//day of the month 1-31
int tm_mon;//months since 0-11
int Tm_year;//years from 1900
int tm_wday;//days since Sunday, 0-6
int tm_yday;//days since 1, 0-365
int Tm_isdst;//daylight Saving Time indicator
};
*/
}

IV, NA sec

function prototypes: int nanosleep (const struct TIMESPEC *rqtp struct timespec)

Where the parameter timespec is defined as:

struct TIMESPEC
{
time_t tv_sec; /* seconds */
Long tv_nsec; /* nanoseconds */
}

Practical applications (partial, incomplete):

struct TIMESEPC req;
struct TIMESPEC rem;

int ret;

Req.tv_sec = 2; That means 2 seconds.
req.tv_nsec = 0;

ret = Nanosleep (&req, &rem);

if (Ret < 0)
{
//....
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.