C语言 time(NULL)

C语言 time(NULL),第1张

C语言time(NULL)是以当前时间为种子,产生随意数。

其中,time(NULL)用来获取当前时间,本质上得到的是一个大整数,然后用这个数来随机数。

time()这个函数其实保存的是一个历史时间,所以需要用NULL把这个历史时间清空一下,time()就会自动保存当前时间了。你可以简单的理解为NULL就是给time()初始化。

c语言调用time()函数括号里为什么要用NULL?

time是这样声明的:time_ttime(time_ttimer)

用法是你先自己定义一个time_t变量,让后把变量的地址传给它。函数会返回自1970年1月1日0点走过的秒数,同时把这个返回值保存在你传进来的那个time_t指向的变量里面

如果你传进来NULL的话,就不保存。

内存情况比较简单

MEMORYSTATUSEX mstx;

mstxdwLength = sizeof (mstx);

GlobalMemoryStatusEx( &mstx );

int iMemeryUsePercentage = mstxdwMemoryLoad;

int iTotalPhysMB = mstxullTotalPhys/1024/1024;

int iAvailPhysMB = mstxullAvailPhys/1024/1024;

int iTotalPageFileMB = mstxullTotalPageFile/1024/1024;

int iAvailPageFileMB = mstxullAvailPageFile/1024/1024;

char LogBuff[128];

memset( LogBuff , 0 , 128 );

sprintf( LogBuff , "MemAvailPct=%d%% Phys=%d/%d PageFile=%d/%d" , 100 - iMemeryUsePercentage , iAvailPhysMB , iTotalPhysMB , iAvailPageFileMB , iTotalPageFileMB );

printf("%s\n",LogBuff);

以上程序分别输出可用百分比,可以用物理内存/总物理内存,可用页面文件/总页面文件

获取CPU的比较复杂,我这边只有获取单个进程CPU占用的方法,不过可以遍历所有进程分别获取再求和就是整个cpu占用率了。

#include <stdioh>

#include <Windowsh>

typedef long long int64_t;

typedef unsigned long long uint64_t;

/// 时间转换

static uint64_t file_time_2_utc(const FILETIME ftime)

{

LARGE_INTEGER li;

liLowPart = ftime->dwLowDateTime;

liHighPart = ftime->dwHighDateTime;

return liQuadPart;

}

/// 获得CPU的核数

static int get_processor_number()

{

SYSTEM_INFO info;

GetSystemInfo(&info);

return (int)infodwNumberOfProcessors;

}

int get_cpu_usage(int pid)

{

//cpu数量

static int processor_count_ = -1;

//上一次的时间

static int64_t last_time_ = 0;

static int64_t last_system_time_ = 0;

FILETIME now;

FILETIME creation_time;

FILETIME exit_time;

FILETIME kernel_time;

FILETIME user_time;

int64_t system_time;

int64_t time;

int64_t system_time_delta;

int64_t time_delta;

int cpu = -1;

if(processor_count_ == -1)

{

processor_count_ = get_processor_number();

}

GetSystemTimeAsFileTime(&now);

HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);

if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time))

{

return -1;

}

system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time))

/ processor_count_;

time = file_time_2_utc(&now);

if ((last_system_time_ == 0) || (last_time_ == 0))

{

last_system_time_ = system_time;

last_time_ = time;

return -1;

}

system_time_delta = system_time - last_system_time_;

time_delta = time - last_time_;

if (time_delta == 0)

return -1;

cpu = (int)((system_time_delta 100 + time_delta / 2) / time_delta);

last_system_time_ = system_time;

last_time_ = time;

return cpu;

}

int main()

{

while(1)

{

int cpu;

// 参数为进程id

cpu = get_cpu_usage(5160);

printf("CPU使用率: %d%%\n",cpu);

Sleep(1000);

}

return 0;

}

是从MSVC的c runtime抄来的,my_gmtime64_s参数有变更,第二参数不用指针,而是直接传值。

gmtime转的是UTC时间,时区偏移需要在传入前加入。

可方便MCU在没有C runtime时使用。

没有完整程序, 不过能提供一点资料

int gettimeofday(struct timeval tv,struct timezone tz);

这个函数可以获取当前时间, 貌似只要第一个结构体就行了

struct timeval

{

time_t tv_sec; //秒 [long int]

suseconds_t tv_usec; //微秒 [long int], (10E-6 second)

};

struct timeval

{

long tv_sec;

long tv_usec;

};

然后取微秒的前三位就是小数了, 之后把秒 tv_sec 转化为 tm 格式, 参数用秒的指针就行

struct tm gmtime(const time_t t);

//转换成格林威治时间。有时称为GMT或UTC。

struct tm localtime(const time_t t);

//转换成本地时间。它可以透过修改TZ环境变数来在一台机器中,不同使用者表示不同时间

下面是tm的部分参数

int tm_sec; //tm_sec表「秒」数,在[0,61]之间,多出来的两秒是用来处理跳秒问题用的。/ Seconds: 0-59 (K&R says 0-61) /

int tm_min; //tm_min表「分」数,在[0,59]之间。

int tm_hour; //tm_hour表「时」数,在[0,23]之间。

int tm_mday; //tm_mday表「本月第几日」,在[1,31]之间。

int tm_mon; //tm_mon表「本年第几月」,在[0,11]之间。

int tm_year; //tm_year要加1900表示那一年。 / / 年份,其值从1900开始 //

int tm_wday; //tm_wday表「本周第几日」,在[0,6]之间。 / Days since Sunday (0-6) / /其中0代表星期天,1代表星期一,以此类推 /

int tm_yday; //tm_yday表「本年第几日」,在[0,365]之间,闰年有366日。 /其中0代表1月1日,1代表1月2日,以此类推 //

int tm_isdst; //tm_isdst表是否为「日光节约时间」

------------------------------华丽丽的分割线--------------------------------------------------

由于很长时间没编程了, 也没有Linux环境, 我就简单写几行代码, 仅作参考

#include <stdioh>

#include <timeh>

#include <sys/timeh>

#include<unistdh>

//这四个不一定够用了

struct timeval tv;

struct timezone tz;

struct tm p_tm;

//变量没有初始化习惯不好,不要学

gettimeofday(&tv, &tz);

p_tm = gmtime( (const time_t )&tvtv_sec );

字符串的组装尤其格式问题自己解决吧

年 p_tm->tm_year+ 1900

月 p_tm->tm_mon+ 1

日 p_tm->tm_mday

时 p_tm->tm_hour+ 1

分 p_tm->tm_min+ 1

秒 p_tm->tm_sec+ 1

小数点后面的部分,注意不够三位还是前面填充0 tvtv_sec/1000

以上就是关于C语言 time(NULL)全部的内容,包括:C语言 time(NULL)、请教下:C语言获取计算机系统CPU使用率,内存使用情况的思路或流程是怎样的!、localtime的c语言实现(gmtime64)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/10135295.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存