linux系统编程(一)--时间与错误处理

#inlcude<error.h>

 errno   

perror();  //打印错误
strerror   //打印错误

#inlcude<time.h>


//使用CLOCKS_PER_SEC  //常量可以得到秒数

clock_t  \\ typedef long clock_t;

clock_t  clock(void) //只是检测处理器的时间,如果睡眠不算。即进程占用cpu时间。    


clock_t clk_start ,clk_end;

clk_start = clock();

.........
clk_end = clock();

int clk = clk_end - clk_start;

---------------------------------------------------------------------
time_t  //日历时间   typedef long  time_t

time_t time(time_t *tloc);//参数可以为NULL,     srand(time(NULL)); //设置随机因子 rand();
time_t t1,t2;
t1 = time (&t2);
t1 = t2;

-----------------------------------
char* ctime(&t1);             //把time_t转为一个字符串


int stime(time_t *t);
  
t1 =time(NULL);   //获取时间
t1 +=(24*60*60)UL; //增加时间

stime(&t1);    //设置时间

printf...   ctime(&t1);  //打印时间

struct tm  //分解时间


           struct tm {
               int tm_sec;    /* Seconds (0-60) */
               int tm_min;    /* Minutes (0-59) */
               int tm_hour;   /* Hours (0-23) */
               int tm_mday;   /* Day of the month (1-31) */
               int tm_mon;    /* Month (0-11) */
               int tm_year;   /* Year - 1900 */
               int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
               int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
               int tm_isdst;  /* Daylight saving time */
           };



//------------------------内核 定时器也有个时间结构体   struct timer_list  
//定一个定时器
struct timer_list my_timer;

//初始化定时器
void init_timer(struct timer_list *timer);
mytimer.function = my_function;
mytimer.expires = jiffies +HZ;

//增加定时器
void add_timer(struct timer_list *timer);

//删除定时器
int del_tiemr(struct timer_list *timer);






//----------time_t ----------->>>>>>>>struct tm   获取真实的世界时间
struct tm gmtime(time_t *timep)


struct tm localtime(time_t *timep)

//-  struct tm ----->>>>>  字符串      time_t---------->字符串

 char * asctime(struct tm *tm);              char *ctime(time_t *t);

//-------struct tm  ---->time_t 

mktime(struct tm *tm)


-----------------------------------------------
struct timeval    //时间要求较高
struct timezone   //时区结构体

struct timeval tv;

if(-1 = gettimeofdate(&tv ,NULL))
{
    perror("获取时间错误");
    return -1;
}

settimeofdate(&tv,NULL)

/---------------------------------------------------
struct timespec  
sleep  usleep  nanosleep  


struct timespec tsp={2,200000},rem;   //rem  剩余时间,因为中断打断
nanosleep(&tsp ,&rem);//

while(-1 == nanosleep(&tsp ,&rem)&&(errno==EINTR))
{

    tsp = rem;
    rem.tv_sec = 0 ;
    rem.tv_nsec = 0 ;
}



ctrl +  c  是发 sigint  信号

总个五个时间,一个内核时间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值