kernel/info.c

/*
 * linux/kernel/info.c
 *
 * Copyright (C) 1992 Darren Senn
 */

/* This implements the sysinfo() system call */

#include <asm/segment.h>

#include <linux/sched.h>
#include <linux/string.h>
#include <linux/unistd.h>
#include <linux/types.h>
#include <linux/mm.h>

//系统信息
asmlinkage int sys_sysinfo(struct sysinfo *info)
{
    int error;
    struct sysinfo val;
    struct task_struct **p;

    //校验
    error = verify_area(VERIFY_WRITE, info, sizeof(struct sysinfo));
    if (error)
        return error;
    //初始化变量val
    memset((char *)&val, 0, sizeof(struct sysinfo));

    //系统信息初始化
    val.uptime = jiffies / HZ;

    val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
    val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
    val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);

    //遍历任务进程,统计任务进程数量
    for (p = &LAST_TASK; p > &FIRST_TASK; p--)
        if (*p) val.procs++;

    si_meminfo(&val);
    si_swapinfo(&val);

    //将系统信息输出到用户空间
    memcpy_tofs(info, &val, sizeof(struct sysinfo));
    return 0;
}

原文地址:https://www.cnblogs.com/xiaofengwei/p/3774126.html