【Linux 内核】调度器 ⑥ ( task_woken 函数 | set_cpus_allowed 函数 | rq_online 函数 | rq_offline 函数 )


Linux 内核源码 linux-5.6.18\kernel\sched\sched.h 中 , 定义的 struct sched_class 调度类结构体 , 就是 " 调度器 " 对应的类 ;





一、task_woken 函数 ( 唤醒阻塞进程 )



sched_class 调度类结构体 中的 task_woken 函数指针 , 指向一个函数 , 调用该函数 , 可以将 休眠或阻塞中" 进程 " 唤醒 ;

  • 进程 是一个 调度实体 ;
  • 执行队列 是一个 红黑树 ;
void (*task_woken)(struct rq *this_rq, struct task_struct *task);

源码路径 : linux-5.6.18\kernel\sched\sched.h#1732 ;





二、set_cpus_allowed 函数 ( 修改进程在 CPU 中的亲和力 )



sched_class 调度类结构体 中的 set_cpus_allowed 函数指针 , 指向一个函数 , 调用该函数 , 可以修改 " 进程 "CPU 中的 " 亲和力 " ;

  • 进程 是一个 调度实体 ;
  • 执行队列 是一个 红黑树 ;
	void (*set_cpus_allowed)(struct task_struct *p,
				 const struct cpumask *newmask);

源码路径 : linux-5.6.18\kernel\sched\sched.h#1734 ;





三、rq_online 函数 ( 启动执行队列 )



sched_class 调度类结构体 中的 rq_online 函数指针 , 指向一个函数 , 调用该函数 , 可以 启动 执行队列 ;

  • 进程 是一个 调度实体 ;
  • 执行队列 是一个 红黑树 ;
void (*rq_online)(struct rq *rq);

源码路径 : linux-5.6.18\kernel\sched\sched.h#1737 ;





四、rq_offline 函数 ( 禁止执行队列 )



sched_class 调度类结构体 中的 rq_offline 函数指针 , 指向一个函数 , 调用该函数 , 可以 禁止 执行队列 ;

  • 进程 是一个 调度实体 ;
  • 执行队列 是一个 红黑树 ;
void (*rq_offline)(struct rq *rq);

源码路径 : linux-5.6.18\kernel\sched\sched.h#1738 ;

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pthread_cond_timedwait函数是POSIX线程库中用于等待条件变量的函数之一。它可以在指定的时间内等待条件变量的状态发生变化。 下面是一个使用pthread_cond_timedwait函数的例子: ```c #include <stdio.h> #include <pthread.h> #include <unistd.h> pthread_mutex_t mutex; pthread_cond_t cond; void* thread_func(void* arg) { pthread_mutex_lock(&mutex); printf("Thread waiting...\n"); struct timespec timeout; clock_gettime(CLOCK_REALTIME, &timeout); timeout.tv_sec += 5; // 设置等待时间为5秒 int result = pthread_cond_timedwait(&cond, &mutex, &timeout); if (result == 0) { printf("Thread woken up!\n"); } else if (result == ETIMEDOUT) { printf("Thread timed out!\n"); } else { printf("Thread wait error!\n"); } pthread_mutex_unlock(&mutex); return NULL; } int main() { pthread_t thread; pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond, NULL); pthread_create(&thread, NULL, thread_func, NULL); sleep(2); // 等待2秒,确保子线程已经开始等待 pthread_mutex_lock(&mutex); printf("Main thread signaling...\n"); pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); pthread_join(thread, NULL); pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond); return 0; } ``` 在这个例子中,我们创建了一个子线程,子线程会在条件变量上等待。主线程等待2秒后,通过调用pthread_cond_signal函数来通知子线程条件变量的状态发生了变化。子线程在等待时间到达或者收到信号时会被唤醒,并打印相应的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值