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

news/2024/7/5 9:42:16
#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  信号

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


http://www.niftyadmin.cn/n/3540330.html

相关文章

借助Citrix XenApp实现应用程IPAD访问

Windows2008TS 轻松解决了内网应用程序的共享访问&#xff0c;但随着Ipad、Android 及智能手机的的兴起&#xff0c;随时随地办公受到热捧&#xff0c;今天介绍一下如何在IPAD、IPHONE、及Android 智能手机上访问企业内网的应用。 要实现在IPAD上访问windows应用程序需要借助Ci…

springBoot bean注入

1、Component&#xff1a;把普通pojo实例化到spring容器中&#xff0c;相当于配置文件中的<bean id"" class""/> 2、Autowired&#xff1a;Spring框架中进行对象注入 Component public class Person {private String id;private String name;privat…

Nodemcu 驱动WS2812点灯红色变绿色的原因及解决

初次接触FastLed,先点灯。开发环境VSCODE PlatformIO 。 直接使用FastLed的示例blink程序&#xff0c;略有修改DATA_PIN D1&#xff0c;注释掉CLOCK_PIN。 #include <FastLED.h>// How many leds in your strip? #define NUM_LEDS 1// For led chips like WS2812, whi…

windows在与time.windows.com进行同步时出错

windows在与time.windows.com进行同步时出错 CreateTime--2017年6月29日10:28:16Author:Marydon 参考地址&#xff1a;http://www.jb51.net/os/windows/274285.html 解决方案&#xff1a; UpdateTime--2017年7月3日09:21:58 将Windows Time设置为开机自启动 WINR-->service.…

如何给Infopath表单保存时自动命名和自动关闭

这是课程中的一个小问题和例子&#xff0c;分享出来给更多朋友参考 问题&#xff1a; 默认情况下&#xff0c;我们在SharePoint中通过Forms Service填写Infopath表单的时候&#xff0c;当我们点击了“Save”菜单项&#xff0c;会弹出一个对话框来&#xff0c;这里我们需要输入一…

《向量数据库指南》——什么是比较 Embedding?

目录 比较 Embedding 准备工作 示例 0:Marlon Brando 示例 1:国王与王后 示例 2:Apple,水果还是公司 欢迎回到向量数据库 101 系列教程。 之前的教程中,我们介绍了非结构化数据、向量数据库和 Milvus——全球最受欢迎的开源向量数据库。我们还简单介绍了 Embedding …

Arduino Modbus 笔记(1)

开发环境&#xff1a;arduino IDE 2.0.3 库下载链接 库测试&#xff0c;选用库自带的simple_slave例程 源码 /*** Modbus slave example 1:* The purpose of this example is to link a data array* from the Arduino to an external device.** Recommended Modbus Maste…