一个CPU进程将至少有一个线程吗? [英] Will a CPU process have at least one thread?

查看:245
本文介绍了一个CPU进程将至少有一个线程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道线程用于多任务并且重量轻。但是我的疑问是可以说我需要一个没有任何多任务处理的过程。我刚刚创建了一个流程。现在,CPU将单个线程与该进程相关联,还是将在不需要线程的情况下单独执行该进程?

I am aware that threads are used for multi-tasking and they are light weight. But my doubt is lets say I need a process without any multi-tasking. I just created a process. Now will the CPU associate a single thread to the process OR will it execute the process alone without need to have a thread?

请说明。

关于,
苦练

推荐答案

好吧,这取决于您使用的操作系统再说一次,但对于许多人来说,创建进程包括为该进程创建单个线程的行为。

Well, that depends on the OS that you're talking about but, for many, the creation of a process includes the act of creating a single thread for that process.

然后该线程可以自由创建 other 属于该进程的线程。

That thread is then free to go and create other threads belonging to the process.

谈论没有线程的进程毫无意义,因为这意味着该进程没有代码在运行因此它实际上无法做任何有用的事情。如果您希望 做某事有用,它就是为该进程创建第一个线程:-)

It makes little sense to talk about a process with no threads since that means no code is running for that process so it can't really do anything useful. And one of the things it won't be able to do is create the first thread for that process if you want it to do any useful work :-)

例如,在Linux内核中,创建进程与创建新线程几乎没有什么不同。这是因为内核调度线程而不是进程。

As an example, in the Linux kernel, the creation of a process is little different to creating a new thread. That's because the kernel schedules threads rather than processes.

现在将进程视为具有相同线程组ID(TGID)的线程组,其中TGID是线程ID为该进程创建的第一个线程的(TID)。

Processes are now considered to be groups of threads with the same thread group ID (TGID), that TGID being the thread ID (TID) of the first thread created for that process.

当您 fork vfork clone (没有 CLONE_THREAD ),您将获得一个带有新TID的新线程并将TGID设置为该TID-这是一个新过程。

When you fork or vfork or clone (without CLONE_THREAD), you get a new thread with a new TID and the TGID is set to that TID - that's a new process.

当您克隆 CLONE_THREAD ,您将获得一个具有新TID的新线程,但TGID与克隆器相同。这是同一进程中的不同线程。

When you clone with CLONE_THREAD, you get a new thread with a new TID but the TGID remains the same as your cloner. That's a different thread in the same process.

这就是Linux(例如)如何区分进程和线程,而不必使调度程序过于复杂。调度程序可以根据需要选择完全忽略线程组。

That's how Linux (as an example) distinguishes between processes and threads without having to make the scheduler too complicated. The scheduler can choose to ignore thread groups entirely if it wishes. It's actually incredibly clever.

要在调度程序之外进行编码,将一组具有相同TGID的线程视为一个进程。

To code outside the scheduler, a group of threads with the same TGID is considered a process.

这包括用户空间代码内核的其他位,因为例如,如何将线程分组到进程中会影响信号传递和退出代码。

This includes both user space code and other bits of the kernel since, for example, how threads are grouped into processes has a bearing on things like signal delivery and exit codes.

这篇关于一个CPU进程将至少有一个线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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