[email protected] Article directory records the system calls and C library functions used in this project C 3 syscall syscall 4 getpw function group getpw ...">

当前位置:网站首页>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

[email protected]

Record the system calls and syscalls used in this projectC库函数

syscall

Linux 下系统调用的三种方法

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.

原网站

版权声明
本文为[to raise pigs]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091244510834.html