当前位置:网站首页>Record the system calls and C library functions used in this project-2
Record the system calls and C library functions used in this project-2
2022-08-09 13:48:00 【to raise pigs】
文章目录
Record the system calls and syscalls used in this projectC库函数
syscall
getpw 函数组
getpw Both are a set of functions used to get users
相关函数:getpw, fgetpwent, getpwent, getpwuid
getpwnam
- 头文件:
#include <pwd.h>
函数说明:
函数说明:getpwnam()Used to search for parameters one by one name The specified account name, When found, the user's data is appended passwd 结构体返回.
passwd 结构请参考 getpwent().函数定义
struct passwd * getpwnam(const char * name);
返回值
返回 passwd 结构数据, 如果返回NULL It means that there is no data, 或有错误发生.示例代码:
/** * @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>
函数说明:
according to the clock mode,Get multiple times.函数定义
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. 从系统启动这一刻起开始计时,不受系统时间被用户改变的影响
用的是相对时间,It's time to passjiffies值来计算的.This clock is not affected by the system clock source,只受jiffies值的影响.
That is to say, the timestamp it gets is monotonic.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;
}
值得一提的是,The timing tool used in this project cannot be used time 以及 gettimeofday,Otherwise, there is a small probability of occurrence"The phenomenon of rewinding time",
具体可以参考 Linux的timedatectl —— Turn time synchronization off or on.
必须使用 clock_gettime(CLOCK_MONOTONIC, *timespec);
The timestamps that can be obtained twice in succession are incremented.
边栏推荐
- 正则表达式-re模块
- Redis源码剖析之跳表(skiplist)
- ctfshow七夕杯2022
- FFmpeg多媒体文件处理(ffmpeg操作目录及list的实现)
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面(循环不变量)
- Jenkins API groovy calling practice: Jenkins Core Api & Job DSL to create a project
- 剑指 Offer 57 - II. 和为s的连续正数序列(滑动窗口)
- 陈强教授《机器学习及R应用》课程 第十七章作业
- 新起之秀 DPU,正在掀起数据中心变革!
- WSA toolkit installed app store tip doesn't work how to solve?
猜你喜欢
GIN a preliminary study, the environment is installed
从NPU-SLAM-EDA技术分析
GET POST PUT DELETE request in GIN
uni-app - uview Swiper 轮播图组件点击跳转链接(点击后拿到 item 行数据, 取出数据做操作)
The FPGA - work summary recently
剑指offer,剪绳子2
Flutter Getting Started and Advanced Tour (8) Button Widget
ViewPager fragments of nested data blank page abnormal problem analysis
Anta and Huawei Sports Health jointly verify the champion running shoes and lead Chinese sports with innovation
jenkins api创建自定义pipeline
随机推荐
5G 联通网管设计思路
jenkins api创建自定义pipeline
OOM排查和处理
About the handling of variable parameters in the Retrofit network request URL
JVM之配置介绍(一)
CPU-MIPS32指令架构(无内锁流水线微处理器)
R 语言 2010.1至2021.12艾滋病每月发病人数 时间序列分析
第六届“强网杯”全国网络安全挑战赛
电脑重装系统后桌面图标如何调小尺寸
剑指offer,剪绳子2
19、学习MySQL 索引
Flutter Getting Started and Advanced Tour (7) GestureDetector
Redis源码剖析之robj(redisObject)
面试题精选:神奇的斐波那契数列
在已打开图片上加水印(文字)
2.微服务'黑话'集锦及Eureka注册中心相关概念
Professor Chen Qiang's "Machine Learning and R Application" course Chapter 15 Homework
read stream special attention
FFmpeg多媒体文件处理(ffmpeg操作目录及list的实现)
leetcode 20. Valid Parentheses 有效的括号(中等)