linux使用系统调用和C库函数调用

1)系统调用
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <linux/unistd.h>
#include <linux/kernel.h>
#include <sys/syscall.h>

int main(void)
{
    struct sysinfo s_info;
    int error;

    error = syscall(__NR_sysinfo, &s_info);
    printf("code error = %d\n", error);
    printf("Uptime = %1ds\nLoad: 1 min %1u / 5 min %1u /15 min %1u\n"
            "RAM: total %1u / free %1u / shared %1u\n"
            "Memory in buffers = %1u\nSwap: total %1u / free %1u\n" "Procs: %d\n", s_info.uptime, s_info.loads[0],
            s_info.loads[1], s_info.loads[2],
            s_info.totalram, s_info.freeram,
            s_info.sharedram, s_info.bufferram,
            s_info.totalswap, s_info.freeswap,s_info.procs);
    exit(EXIT_SUCCESS);

}

结果:
code error = 0
Uptime = 23889s
Load: 1 min 8928 / 5 min 5472 /15 min 2304
RAM: total 1968865280 / free 98590720 / shared 0
Memory in buffers = 66826240
Swap: total 2113921024 / free 2113908736
Procs: 328

2)C函数库调用

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/sysinfo.h>

int main(void)
{
    struct sysinfo s_info;
    int error;

    error = sysinfo(&s_info);
    printf("code error = %d\n", error);
    printf("Uptime = %1ds\nLoad: 1 min %1u / 5 min %1u /15 min %1u\n"
            "RAM: total %1u / free %1u / shared %1u\n"
            "Memory in buffers = %1u\nSwap: total %1u / free %1u\n" "Procs: %d\n", s_info.uptime, s_info.loads[0],
            s_info.loads[1], s_info.loads[2],
            s_info.totalram, s_info.freeram,
            s_info.sharedram, s_info.bufferram,
            s_info.totalswap, s_info.freeswap,s_info.procs);
    exit(EXIT_SUCCESS);

}

结果:
code error = 0
Uptime = 24018s
Load: 1 min 8064 / 5 min 8032 /15 min 3584
RAM: total 1968865280 / free 94662656 / shared 0
Memory in buffers = 66973696
Swap: total 2113921024 / free 2113867776
Procs: 324


你可能感兴趣的:(linux使用系统调用和C库函数调用)