17

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?

Please clarify.

Regards, Harish

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Harish
  • 7,589
  • 10
  • 36
  • 47

2 Answers2

21

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 (with the TGID being what the code outside of the scheduler sees as the process ID).

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Ok. So to do even a single task, we need a thread right? It is NOT possible to do a single task (not multiple tasks) using only a process without thread. Right? – Harish Feb 04 '11 at 04:48
  • @Harish, yes, _something_ has to be running. It's possible that an OS might distinguish between a runnable process and a runnable thread but I've never seen this except in user-mode threads (where threading wasn't done by the OS at all). Generally, a process has one or more threads. – paxdiablo Feb 04 '11 at 05:03
  • @paxdiablo, I was wondering about this part: "The scheduler can choose to ignore thread groups entirely if it wishes." I was under impression that the scheduler cannot really ignore thread groups due to context switching (saving the state, to be precise). Could you elaborate on it just a bit more? – MK_Dev Jun 09 '16 at 23:22
  • @MK_Dev, context switching is something that happens to threads rather than thread groups. A scheduler only schedules the former, and need not worry about what group a thread may happen to be in. – paxdiablo Jun 10 '16 at 01:14
  • @paxdiablo, thank you, understood. Isn't there more of a performance hit when switching threads from different thread groups since more state needs to be preserved and restored? My understanding was that because threads in the same group share memory, it would be less expensive to switch those threads because only the state that is specific to a particular thread needs to be preserved. – MK_Dev Jun 10 '16 at 15:38
  • @MK, memory itself is not generally one of the things switched out. Handles to memory may be but it's not that much added expense and it may be done even for threads in the same group (I'd have to check this). – paxdiablo Jun 11 '16 at 03:50
4

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