当前位置:网站首页>记录本项目中用到的系统调用与C库函数-2
记录本项目中用到的系统调用与C库函数-2
2022-08-09 12:45:00 【养猪去】
记录本项目中用到的系统调用与C库函数
syscall
getpw 函数组
getpw 都是用于获取用户的一组函数
相关函数:getpw, fgetpwent, getpwent, getpwuid
getpwnam
- 头文件:
#include <pwd.h>
函数说明:
函数说明:getpwnam()用来逐一搜索参数 name 指定的账号名称, 找到时便将该用户的数据以 passwd 结构体返回。
passwd 结构请参考 getpwent()。函数定义
struct passwd * getpwnam(const char * name);
返回值
返回 passwd 结构数据, 如果返回NULL 则表示已无数据, 或有错误发生。示例代码:
/** * @Author: 吉松阳 * @Date: 2021/9/23 * @Description: */
#include <stdio.h>
#include <pwd.h>
int main() {
struct passwd *pw;
char *username = "jisongyang";
pw = getpwnam(username);
if (!pw) {
printf("%s is not exist\n", username);
return -1;
}
printf("pw->pw_name = %s\n", pw->pw_name);
printf("pw->pw_passwd = %s\n", pw->pw_passwd);
printf("pw->pw_uid = %d\n", pw->pw_uid);
printf("pw->pw_gid = %d\n", pw->pw_gid);
printf("pw->pw_gecos = %s\n", pw->pw_gecos);
printf("pw->pw_dir = %s\n", pw->pw_dir);
printf("pw->pw_shell = %s\n", pw->pw_shell);
return 0;
}
/* 输出结果: pw->pw_name = jisongyang pw->pw_passwd = ******** pw->pw_uid = 501 pw->pw_gid = 20 pw->pw_gecos = 吉松阳 pw->pw_dir = /Users/jisongyang pw->pw_shell = /bin/zsh */
时间相关
time
- 头文件:
#include <time.h>
- 函数说明:
C 库函数: 返回自纪元 Epoch(1970-01-01 00:00:00 UTC)起经过的时间,以秒为单位。
如果 seconds 不为空,则返回值也存储在变量 seconds 中。 - 函数定义
time_t time(time_t *seconds)
- 返回值
以 time_t 对象返回当前日历时间。
localtime
- 头文件:
#include <time.h>
函数说明:
C 库函数 使用 timer 的值来填充 tm 结构。
timer 的值被分解为 tm 结构,并用本地时区表示。函数定义
struct tm *localtime(const time_t *timer)
- 返回值
该函数返回指向 tm 结构的指针,该结构带有被填充的时间信息。下面是 tm 结构的细节:
struct tm {
int tm_sec; /* 秒,范围从 0 到 59 */
int tm_min; /* 分,范围从 0 到 59 */
int tm_hour; /* 小时,范围从 0 到 23 */
int tm_mday; /* 一月中的第几天,范围从 1 到 31 */
int tm_mon; /* 月份,范围从 0 到 11 */
int tm_year; /* 自 1900 起的年数 */
int tm_wday; /* 一周中的第几天,范围从 0 到 6 */
int tm_yday; /* 一年中的第几天,范围从 0 到 365 */
int tm_isdst; /* 夏令时 */
};
strftime
- 头文件:
#include <time.h>
函数说明:
C 库函数,根据 format 中定义的格式化规则,格式化结构 timeptr 表示的时间,并把它存储在 str 中。函数定义
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)
- str – 这是指向目标数组的指针,用来复制产生的 C 字符串。
- maxsize – 这是被复制到 str 的最大字符数。
- format – 这是 C 字符串,包含了普通字符和特殊格式说明符的任何组合。这些格式说明符由函数替换为表示 tm 中所指定时间的相对应值。
具体格式详见:
参考链接
- 返回值
如果产生的 C 字符串小于 size 个字符(包括空结束字符),则会返回复制到 str 中的字符总数(不包括空结束字符),否则返回零。
gettimeofday
- 头文件:
#include <sys/time.h>
- 函数说明:
返回当前距离1970年的秒数和微妙数,后面的tz是时区,一般不用(传 NULL 即可)。 - 函数定义
int gettimeofday(struct timeval *tv, struct timezone *tz);
clock_gettime
- 头文件:
#include <time.h>
函数说明:
根据时钟模式,获取多种时间。函数定义
int clock_gettime(clockid_t clock_id, struct timespec * tp );
CLOCK_REALTIME 0
Systemwide realtime clock. 系统实时时间,随系统实时时间改变而改变。
即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户该成其他,则对应的时间相应改变CLOCK_MONOTONIC 1
Represents monotonic time. Cannot be set. 从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
用的是相对时间,它的时间是通过jiffies值来计算的。该时钟不受系统时钟源的影响,只受jiffies值的影响。
也就是说它获得的时间戳是单调的。CLOCK_PROCESS_CPUTIME_ID 2
High resolution per-process timer. 本进程到当前代码系统CPU花费的时间CLOCK_THREAD_CPUTIME_ID 3
Thread-specific timer. 本线程到当前代码系统CPU花费的时间CLOCK_REALTIME_HR 4
High resolution version of CLOCK_REALTIME. 0
CLOCK_REALTIME 的 高精度版本CLOCK_MONOTONIC_HR 5
High resolution version of CLOCK_MONOTONIC.
CLOCK_MONOTONIC 的高精度版本返回值
时间结构struct timespec
示例代码
#include<stdio.h>
#include <sys/time.h>
int main() {
struct timeval tv;
gettimeofday(&tv, NULL);
printf("gettimeofday : %ld, %d\n", tv.tv_sec,tv.tv_usec);
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
printf("CLOCK_REALTIME: %ld, %ld\n", ts.tv_sec, ts.tv_nsec);
//打印出来的时间跟 cat /proc/uptime 第一个参数一样
clock_gettime(CLOCK_MONOTONIC, &ts);
printf("CLOCK_MONOTONIC: %ld, %ld\n", ts.tv_sec, ts.tv_nsec);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
printf("CLOCK_PROCESS_CPUTIME_ID: %ld, %ld\n", ts.tv_sec, ts.tv_nsec);
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
printf("CLOCK_THREAD_CPUTIME_ID: %ld, %ld\n", ts.tv_sec, ts.tv_nsec);
printf("\n%ld\n", time(NULL));
return 0;
}
值得一提的是,本项目使用的计时工具不能使用 time 以及 gettimeofday,否则有小概率发生"时间回溯现象",
具体可以参考 Linux的timedatectl —— 关闭或开启时间同步.
必须使用 clock_gettime(CLOCK_MONOTONIC, *timespec);
才可以先后两次拿到的时间戳是递增的。
边栏推荐
- Yocto 可以下载的第三方库
- Clock frequency and baud rate count for serial communication in FPGA
- 乐东消防救援大队应邀为干部开展消防安全培训
- 二维数组&指针
- ftplib+ tqdm upload and download progress bar
- Introduction to Flutter advanced trip Dialog&Toast (10)
- glibc memory management model freeing C library memory cache
- ctfshow七夕杯2022
- NFS 特别注意权限的问题
- GIN文件上传与返回
猜你喜欢
Data Mining-05
Rust从入门到精通04-数据类型
Flutter Getting Started and Advanced Tour (8) Button Widget
透明tune proxy
Intra-group reverse order adjustment of K nodes
Customize VIEW to realize in-app message reminder to rotate up and down
How to reduce the size of desktop icons after the computer is reinstalled
kustomize入门示例及基本语法使用说明
ctfshow七夕杯2022
The new features of ABP 6.0.0 - rc. 1
随机推荐
Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
Report: The number of students who want to learn AI has increased by 200%, and there are not enough teachers
农村区县域农业电商如何做?数字化转型如何进行?
5G China unicom AP:B SMS ASCII Transcoding Requirements
glibc 内存管理模型 释放 C库内存缓存
腾讯发布第二代四足机器人Max,梅花桩上完成跳跃、空翻
[HCIP Continuous Update] Principle and Configuration of IS-IS Protocol
read stream 特别注意
Data Mining-06
WSA toolkit installed app store tip doesn't work how to solve?
MySQL备份与恢复 (转载非原创)
Rust 入门指南(使用JSON)
陈强教授《机器学习及R应用》课程 第十四章作业
Flutter入门进阶之旅(五)Image Widget
流量焦虑背后是企业对客户关系管理的不足
JVM之配置介绍(一)
#WeArePlay | 与更多开发者一起,探索新世界
jenkins api创建自定义pipeline
5G China unicom repeater network management protocol real-time requirements
Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇