内核如何区分线程和进程 [英] how kernel distinguishes between thread and process

查看:340
本文介绍了内核如何区分线程和进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

被称为轻量级进程.无论是进程还是线程,它们都是通过task_struct数据结构实现的.

in Linux threads are called light weight processes. Whether process or thread, they are implemented by task_struct data structure.

1>那么,从这个意义上说,内核如何区分线程和进程?

1> So, in that sense how kernel distinguishes between thread and process?

2>发生上下文切换时,线程如何在上下文切换中获得更少的开销?因为在此线程之前,可能正在运行来自另一个进程的另一个线程.因此,即使资源在进程的线程之间共享,内核也应加载所有资源.

2> when context switching happens, how do threads get less overhead in context switching? because prior to this thread, another thread from another process may be running. So kernel should load all resources even if resources are shared between threads of a processes.

推荐答案

在内核内部,每个进程和线程都有一个唯一的ID(甚至是同一进程的线程),该ID存储在pid变量中,并且同一进程的线程也共享一个通用ID,存储在tgid变量中,并在调用getpid()时返回给用户,因此允许内核将它们区分为可自行计划的不同实体.

Inside kernel, each process and threads have a unique id (even threads of same process) which is stored in pid variable and threads of same process also share a common id which is stored in tgid variable, and is returned to user when getpid() is invoked therefore allowing kernel to distinguish them as different entities which are schedulable in themselves.

当一个线程被同一进程的另一个线程抢占时,由于共享了.text,.bss,.data,文件描述符等各个段,因此与进行不同进程的上下文切换相比,可以进行快速的上下文切换,或在不同进程的线程进行上下文切换时.

When a thread is preempted by another thread of same process, since various segments such as .text, .bss, .data, file descriptors etc. are shared and hence allows a fast context switch compared to when different processes are context switched, or when threads of different processes are context switched.

这篇关于内核如何区分线程和进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆