概述
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 = %dn", error);
printf("Uptime = %1dsnLoad: 1 min %1u / 5 min %1u /15 min %1un"
"RAM: total %1u / free %1u / shared %1un"
"Memory in buffers = %1unSwap: total %1u / free %1un" "Procs: %dn", 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 = %dn", error);
printf("Uptime = %1dsnLoad: 1 min %1u / 5 min %1u /15 min %1un"
"RAM: total %1u / free %1u / shared %1un"
"Memory in buffers = %1unSwap: total %1u / free %1un" "Procs: %dn", 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库函数调用的全部内容,希望文章能够帮你解决linux使用系统调用和C库函数调用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复