进程调度API之yield

来源:互联网 发布:淘宝千人千面原理 编辑:程序博客网 时间:2024/06/16 21:36
void __sched yield(void) 用于让当前进程释放其占用的cpu资源,以便让其他进程有机会执行.但是kernel 并不推荐直接调用这个函数其使用的例子如下:static u16 hfa384x_allocate_fid(struct net_device *dev, int len){u16 fid;unsigned long delay;delay = jiffies + HFA384X_ALLOC_COMPL_TIMEOUT;while (!(HFA384X_INW(HFA384X_EVSTAT_OFF) & HFA384X_EV_ALLOC) &&       time_before(jiffies, delay))yield();}其源码分析如下:可见这个函数主要做了两件事情void __sched yield(void){#虽然让出cpu了,但是当前task的状态还是设置为TASK_RUNNINGset_current_state(TASK_RUNNING);#调用系统调用来让出cpu,其实在这个系统调用中是直接调用schedule 函数sys_sched_yield();}SYSCALL_DEFINE0(sched_yield){struct rq_flags rf;struct rq *rq;local_irq_disable();rq = this_rq();rq_lock(rq, &rf);schedstat_inc(rq->yld_count);current->sched_class->yield_task(rq);/* * Since we are going to call schedule() anyway, there's * no need to preempt or enable interrupts: */preempt_disable();rq_unlock(rq, &rf);sched_preempt_enable_no_resched();#这个系统调用的核心就是调用schedule来让出cpuschedule();return 0;}

原创粉丝点击