Will a CPU process have at least one thread?

*爱你&永不变心* 提交于 2019-12-02 21:06:43

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.

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 :-)


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.

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.

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.

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.

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.

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.

A process -is- a thread.

When a process begins, it begins with a single thread.

Before the days of multi-threading, the term thread was unnecessary because you couldn't have a process with more than one thread.

Now days, you can create additional threads, and so have a process with multiple threads.

A process is also a bunch of other things - memory, stack, whathaveyou; one of the things it is, is threads. The threads share some of the other things in the process (such as memory), but have their own individual instances of others (such as stacks).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!