0

As I know, process is a common container for all the thread it hosts. Multiple threads can easily share resources if they are running in a same process. All the threads in a process share a common address space. On the other hand, thread is the unit of execution of the program.

Scheduler in the operating system schedules threads, not processes (1). A process is said to be actively running if any one of its thread is running. Otherwise the process is waiting. Scheduler cannot simply schedule a process.

Also, beside priorities, all threads in a process are equal from the OS perspective, even the main thread (2) (some application might have application specific roles assigned to each thread, which I'm ignoring here).

Based on (1) and (2), it seems there is no requirement that all processes should start with one thread which should then spawn child threads as needed. So technically, it is possible to start a process with multiple threads from the beginning, where none of the threads started the other. When that process is started, scheduler can simply schedule any one of the many starter threads. But I'm not getting how to do it!

So, How to start a process with multiple threads right from the beginning? This question is not asked in relation to any specific OS. Also, if programming languages mandating main as entry point is a problem for giving an example, I can (or try to) understand x86-64 assembly code.

Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35
  • AFAIK this is not possible. On Linux, then a fork is done, the full target processes is copied including all threads. But, only the thread that do the fork call remains: all others are (somehow) killed after the fork in the new process. On Windows, AFAIK, one need to create the threads manually during the startup of the process. The thing is why you even need that: what is the problem of creating threads in the main function? – Jérôme Richard Aug 04 '22 at 23:54
  • (1) is not completely true. On Linux, the scheduler schedule tasks that can theoretically be processes though in practice it is commonly threads. (2) is wrong regarding the threading model. For example, in C++ the main thread is special because if it is killed, then all the application is killed. This is not the case for other threads (that are typically joined). The main thread can be seen as the init process but for 1 application and not the full system (ie. if init is killed then so everything). Note that this behavior is not true in languages like D for example. – Jérôme Richard Aug 04 '22 at 23:58
  • @JérômeRichard (2) I think you are only partially correct about C++ main thread. In C and C++, when main thread returns, it implicitly calls `exit()`. That's why entire process gets killed. Any thread that calls `exit()` kills the entire process, nothing special about main thread. If you want to terminate main thread, and still keep the process running, then do `pthread_exit()` before returning from `main`, so that implicit `exit()` won't be called. [Here is an example](https://godbolt.org/z/7KeczoTqv). – Sourav Kannantha B Aug 05 '22 at 07:04
  • (1) It seems a Linux specific implementation detail.[Windows actually can create more threads at startup from a thread pool](https://stackoverflow.com/questions/34822072/why-does-windows-10-start-extra-threads-in-my-program). But I don't know how to access them, and customize them. Regarding why I need it, I actually don't 'need' it. I was just curious if there was some way possible. – Sourav Kannantha B Aug 05 '22 at 07:10
  • Your link do not show the kernel can do that. It show that are system functions creating threads and these are not part of the kernel but user-land code. The linker and the standard library are free to create threads before the main but this is still after the creation of the process. If this is what you want, then you can tweak the start procedure (which is AFAIR OS dependent, typically written in assembly and bypass the standard C library). It is still not clear to me what you would like to achieve. – Jérôme Richard Aug 05 '22 at 11:28
  • I am not clear about what you are asking, or how it can be "agnostic". It's certainly not the case that this is something that can be done in a language- or platform-independent manner; most of them can't do it at all. And a specific answer to "how to do it", if it can be done at all, must necessarily depend on the language and/or the platform you are using. Are you asking, at the level of general operating system theory, how such a feature might be abstractly designed or implemented? – Nate Eldredge Aug 05 '22 at 14:33
  • @NateEldredge by language- and platform- agnostic, I meant, question was not asked wrt any particular language or platform. – Sourav Kannantha B Aug 05 '22 at 15:23
  • @SouravKannanthaB: I understand that is what agnostic means, but I don't see how your question "how to do it" can be answerable without reference to any particular language or platform. – Nate Eldredge Aug 05 '22 at 15:26
  • @NateEldredge I didn't meant answerer should not refer to any platforms. Instead I intended one answer might refer to one platform, and other might refer to other. In case that tag is not suitable, should I remove? – Sourav Kannantha B Aug 05 '22 at 15:41
  • So, do you want a negative answer that says something like "In Unix you cannot do this"? – Nate Eldredge Aug 05 '22 at 20:21
  • There is no way to do what you want, and *no good reason you should want it!* Why do you think you want to do this? – Matt Timmermans Aug 06 '22 at 02:35
  • 1
    Short answer: although an operating system could be written to support what you're asking for, current operating systems (at least all of which I'm aware) simply don't support it. Under current operating systems, each process starts with only one thread, and if you want more threads, you have that thread spawn others as needed. – Jerry Coffin Aug 08 '22 at 21:02

0 Answers0