linux kernel 运行状态全局标识作用

Linux内核中,system_state全局标识用于定义系统的运行状态,如BOOTING、RUNNING、HALT等。在main.c中,当第一个用户空间进程init启动后,system_state被设置为SYSTEM_RUNNING。在kernel启动阶段,某些操作如锁、任务调度和sleep是不允许的,而system_state可以帮助在不同阶段的代码正确执行。例如,blocking_notifier_chain_register函数在system_state为SYSTEM_BOOTING时,会避免使用down_write()来适应内核启动时的任务切换未启用情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

kernel运行状态全局标识system_state:

kernel.h


/* Values used for system_state */
extern enum system_states {
SYSTEM_BOOTING,
SYSTEM_RUNNING,
SYSTEM_HALT,
SYSTEM_POWER_OFF,
SYSTEM_RESTART,
SYSTEM_SUSPEND_DISK,
} system_state;



main.c 

enum system_states system_state __read_mostly;
EXPORT_SYMBOL(system_state);


static noinline int init_post(void)
{
。。。。。。。。

  system_state = SYSTEM_RUNNING;        
。。。。。。。。

。。。。。。。。。。。。。。。。。。。。。
注:第一个用户空间进程init启动之前,系统都处于SYSTEM_BOOTING状态, 之后转入SYSTEM_RUNNING状态 !
。。。。。。。。。。。。。。。。。。。。
run_init_process("/bin/init");
。。。。。。。。。。。。。。。。。。

panic("No init found.  Try passing init= option to kernel. "
     "See Linux Documentation/init.txt for guidance.");
}


kernel 运行状态全局标识system_state使用:

  锁,任务调度,sleep 等逻辑在kernel启动阶段是不希望不出的。

  但有时为让一段代码在kernel启动阶段和运行阶段都可以正常工作,此时system_state就很好用了。


int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
struct notifier_block *n)
{
int ret;


/*
* This code gets used during boot-up, when task switching is
* not yet working and interrupts must remain disabled.  At
* such times we must not call down_write().
*/
if (unlikely(system_state == SYSTEM_BOOTING))
return notifier_chain_register(&nh->head, n);



down_write(&nh->rwsem);
ret = notifier_chain_register(&nh->head, n);
up_write(&nh->rwsem);
return ret;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值