You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux 0.96a内核sched.c中averunnable数组未初始化值的疑问

Linux 0.96a内核sched.c中averunnable数组未初始化的疑问

在阅读Linux 0.96a版本内核的kernel/sched.c源码时,我发现第355行定义的unsigned long averunnable[3]; /* fixed point numbers */数组在初始化阶段没有被赋值,因此疑惑这是否会导致该数组成员在使用时变为随机数。

我做了以下尝试:

  • 仔细阅读相关源码,但未找到该averunnable数组的赋值位置
  • 编写了一段测试代码运行,发现数组成员在使用时的初始值为随机数

测试代码如下:

# cat testaverunnable.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>

int main()
{

#define FSHIFT  11
#define FSCALE  (1<<FSHIFT)
/*
 * Constants for averages over 1, 5, and 15 minutes
 * when sampling at 5 second intervals.
 */
static unsigned long cexp[3] = {
        1884,   /* 0.9200444146293232 * FSCALE,  exp(-1/12) */
        2014,   /* 0.9834714538216174 * FSCALE,  exp(-1/60) */
        2037,   /* 0.9944598480048967 * FSCALE,  exp(-1/180) */
};
unsigned long averunnable[3];   /* fixed point numbers */

        int i, n=10;
        printf("before into for cycle, the averunnable [%u] is %u \n",i, averunnable[i]);

        for (i = 0; i < 3; ++i)
                {
                        printf("averunnable [%u] is %u \n",i, averunnable[i]);
                        averunnable[i] = (cexp[i] * averunnable[i] + n * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
                        printf("atfer calculation, the averunnable [%u] is %u \n",i, averunnable[i]);
                }

    return 0;
}

编译运行结果:

# gcc -o testaverunnable testaverunnable.c
# ./testaverunnable
before into for cycle, the averunnable [0] is 4195856 
averunnable [0] is 4195856 
atfer calculation, the averunnable [0] is 3861499 
averunnable [1] is 4195392 
atfer calculation, the averunnable [1] is 4126081 
averunnable [2] is 3756023344 
atfer calculation, the averunnable [2] is 3805055516 

我认为作为Linus编写的内核,这里肯定有我遗漏的点,恳请大家帮忙解答,谢谢!

内容的提问来源于stack exchange,提问作者Ben Wang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 22:05:11