朵小喵儿|操作系统与 CPU 是怎么执行线程的?
操作系统与 CPU 是怎么执行线程的?查看 CPU 信息
cat /proc/cpuinfo查询结果
processor: 0vendor_id: GenuineIntelcpu family: 6model: 60model name: Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHzstepping: 3microcode: 0x22cpu MHz: 2393.631cache size: 6144 KBphysical id : 0siblings: 8core id: 0cpu cores: 4
- physical id 机器上就安装了几个物理CPU
- cpu core 记录了每个物理CPU,内部有几个物理核
- siblings 代表每个物理CPU有多少个逻辑核
线程线程是 CPU 调度的最小单位 , 程序代码执行的最小单元 进程是资源管理用的,Linux 线程是用户空间的线程 , 采用的是线程-进程 一对一模型
内核线程与用户线程内核线程就是内核分身 , 一个没和线程处理一个事务 , 很少有直接调取内核线程 , 而是操作用户线程 , 用户线程与内核线程一对一 , 多对一 , 多对多 。
多对一
- JavaThread: 创建线程执行任务 , 持有java_lang_thread// Allocate the OSThread object (<_<)可能空指针OSThread* osthread = new OSThread(NULL, NULL);if (osthread == NULL) {return false;}// java_threadosthread->set_thread_type(thr_type);// Initial state is ALLOCATED but not INITIALIZEDosthread->set_state(ALLOCATED);thread->set_osthread(osthread);pthread_attr_t attr;pthread_attr_init(// 所以java线程都是分离状态 , join也并非用结合状态pthread_attr_setdetachstate(// -Xss默认1M , Thread没设置stackSize , 在Linux-x86默认512K , 取最大值size_t stack_size = os::Posix::get_initial_stack_size(thr_type, req_stack_size);//这里设置栈警戒缓冲区 , 默认系统页大小//原注解的意思是 , Linux的NPTL没有完全按照posix标准//理应guard_size + stack_size , 且二者大小相等 , 而不是从stack_size取guard_size作为警戒取//所以这里模仿实现posix标准size_t guard_size = os::Linux::default_guard_size(thr_type);if (stack_size <= SIZE_MAX - guard_size) {stack_size += guard_size;}assert(is_aligned(stack_size, os::vm_page_size()), "stack_size not aligned");int status = pthread_attr_setstacksize(assert_status(status == 0, status, "pthread_attr_setstacksize");pthread_attr_setguardsize(ThreadState state;{//欧了 , 创建线程 , 函数指针thread_native_entry是重点pthread_t tid;int ret = pthread_create(pthread_attr_destroy(if (ret != 0) {// Need to clean up stuff we've allocated so farthread->set_osthread(NULL);delete osthread;return false;}// Store pthread info into the OSThreadosthread->set_pthread_id(tid);// 等待thread_native_entry设置osthread为INITIALIZED , 或收到终止信号{Monitor* sync_with_child = osthread->startThread_lock();MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);while ((state = osthread->get_state()) == ALLOCATED) {sync_with_child->wait(Mutex::_no_safepoint_check_flag);}}}// Aborted due to thread limit being reachedif (state == ZOMBIE) {thread->set_osthread(NULL);delete osthread;return false;}// The thread is returned suspended (in state INITIALIZED),// and is started higher up in the call chainassert(state == INITIALIZED, "race condition");return true;}
推荐阅读
- 朵小喵儿|2021年迎来考验,美国企业或将再次入局,国产手机
- 朵小喵儿|国产手机,2021年迎来考验,美国企业或将再次入局
- 江湖车侠|国产操作系统发布:售价99元,你愿意花费这个价格去尝试吗
- 操作系统|最漂亮的国产操作系统,实用性不输Windows,因收费99元被吐槽
- 朵小喵儿|突破旗舰封锁,魅族今年性能旗舰亮新玩法,销量真稳了
- 朵小喵儿|小天才手表背后,是成人世界的焦虑
- 朵小喵儿|主板维修必备!海量主板电路图+CPU程序下载
- 朵小喵儿|如何成为制造大国?曹德旺一语中的:先把“这个”取消再说
- 游龙战神|从iphone崛起之路看移动操作系统的前世今生
- 小镇的夕阳|修改硬盘MBR代码,用1024字节实现一个操作系统启动管理器
