From the Tanenbaum OS book it is mentioned the following:
"in user level threads, if a thread starts running, no other thread in that process will ever run unless the first thread voluntarily gives up the CPU".
That means threads are going to run one after the other (sequently) not in parallel. So what is the advantage of the user-level threads?
There are two concepts of multitasking in a single process multiple thread environment.
A single thread execute in time slice of the process. And that thread takes care of scheduling of other threads.
OS takes scheduling decision of process threads and might run them in parallel on different core.
You are talking about approach 1. Yes It has no advantage of multi-threading; but it let many threads / programs run one by one and give you "multitasking" (virtually).
Related
Does user level threads take advantage of multiprocessing ? I read one such answer here. But, it's not clear though.
What does it mean by "user threads cannot take advantage of multithreading or multiprocessing"?
And one other answer here says that it is possible
How do user level threads (ULTs) and kernel level threads (KLTs) differ with regards to concurrent execution?
Am I missing something here with some important details ?
Usually, user-level threads, cannot take advantage of multiprocessing whereas, kernel-level threads can take advantage of it.
It simply means that we can run several kernel-level threads, in parallel on a multi-core computer system. But the same cannot be done for user-level threads.
This is possible because kernel-level threads are managed by the Operating System, whereas, the user-level threads are managed by the user, meaning the OS is only aware of single user-level thread(the executing one), even when there are actually more than one.
Now in your links, you provided it is mentioned that:
Some implementations base their user threads on top of several kernel
threads, to benefit from multi-processor machines (M:N model).
From what I understood after reading the links is that its possible for user-level threads to take advantage of multiprocessing, only if its implementation specific. So this would basically be like a kernel-level thread associated with a core and a user-level thread associated with the respective kernel-level thread.
So in the end, its after all the kernel-level threads running parallel on several cores(OR CPU's). We can't take advantage of multiprocessing without any assistance from kernel.
It depends upon how you define "take advantage."
User threads are scheduled by the process.
The process is scheduled by the kernel.
A user thread can then only execute on the process in which the process is scheduled.
Thus a user threads from the same process cannot execute on multiple processors concurrently. They execute interleaved.
If that is your definition of multi-processing, your answer is NO.
However, if the OS supports it, the process can execute on any available processor. Thus, a user thread can execute on any available processor.
If that is your definition of multi-processing, your answer is YES.
Can someone give more explain for single-threaded and multi-threaded processes
Is single threaded process contains only one thread? or it means that that process can contain multiple threads and can run only one thread at a time, then context-switch between them?If i run a java program on a single core processor is the second one would be true?
Can someone explain it further?
A single-threaded process is a process with a single thread. A multi-threaded process is a process with multiple threads.
The naming is based on the static configuration, i.e. you could look at the process when execution is suspended and say if it's single-threaded or multi-threaded. Whether or not the threads are executed on a single core or multiple cores doesn't matter as far as the nomenclature goes.
A process with multiple threads all executing on a single core can have race conditions, as can a process with multiple threads executing across multiple cores. Distinguishing the two situations is important for performance evaluation but counter-productive for correctness (i.e. it's useful to assume that each thread is on a separate CPU when considering potential races).
A single-threaded program is a program that only uses one thread. The process might have additional threads; for your example of the Java runtime, you can expect to have a finalizer thread and perhaps one or more threads for garbage collection. It's a single-threaded program running in a multi-threaded process.
(I've heard "process" defined as "the abstraction of a program in execution", i.e. you write a program and then execute it in a process.)
Single threaded does not mean single-core, single process.
Single threaded processes contain the execution of instructions in a single sequence. In other words, one command is processes at a time
As far as I know kernel doesn't know whether it is executing a user thread or user process because for kernel user threads are user process, it only schedules user processes and doesn't care which thread was running in that process.
I have one more question, Is there per core ready queue or a single ready queue for all the cores?
I was reading this paper and it is written that
In the stock Linux kernel the set of runnable threads is partitioned
into mostly-private per core scheduling queues; in the common case,
each core only reads, writes, and locks its own queue.
The linux kernel scheduler uses the "task" as its primary schedulable entity. This corresponds to a user-space thread. For a traditional simple Unix-style program, there is only a single thread in the process and so the distinction can be ignored. Other programs of course may have multiple threads. But in all cases, the kernel only schedules tasks (i.e. threads).
Your terminology above therefore doesn't really match the situation. The kernel doesn't really care whether the different threads it schedules are part of the same process or different processes: each thread can be scheduled independently. You can have multiple threads from the same process running on different processors/cores at the same time.
Yes, there are separate run queues for each core.
The paper you reference is, I think, slightly misleading in its phrasing. In particular, saying that the "set of runnable threads is partitioned into..." doesn't give quite the right meaning; that makes it sound like the threads are divided into multiple groups that are then assigned to different cores and can only be executed there. It would be more accurate to say that there is a separate run queue for each core containing a set of threads waiting to execute, and in common use, the scheduler doesn't need to reference the queues for other cores.
But in fact, threads can migrate from one core to another. For example, if there is a thread waiting to run on core A (hence in core A's run queue), but core A is already busy running some other thread, and there is another core that is not busy, the waiting thread may be migrated to that other core and executed there. (This is an oversimplification of course as there are other factors that go into deciding whether/when to migrate a thread.)
How does the process/thread scheduler work on a typical system, with respect to fairness and granularity? Does the scheduler pass instructions to the processor by switching between processes or between threads? If the latter is the case, then, can I improve the performance of my compute-bound jobs by spawning more threads in my process? The literature on this topic seems to use process and thread interchangeably. For clarity, I use the definition of a process where it is a collection of 1 or more threads of execution.
I presume that multiple threads do NOT improve compute-bound jobs on a single processor, implying that the scheduler's scheduling granularity is at the process level. For example, if there are N processes, each process gets 1/N-th of the processor, no matter how many threads if spawns (if the scheduler is fair, of course).
I found a related conversation here: How Linux handles threads and process scheduling
Linux doesn't differentiate between threads and processes, so threads are actually treated like processes with shared memory. If this is the case, it would seem that I could improve compute-bound run time by spawning more threads. Am I interpreting this correctly?
For Kernel-Level-Threads when one thread blocks for some I/O another thread is free to run, but in User-Level-Threads what happens if one thread is blocked?
Will that process remain blocked i.e. no other thread will execute or another thread will be scheduled to run. What happens exactly?
User-level threads are pieces of user code that execute in sequential fashion - one thread runs for a while then transfers the control to another thread and so on. If one of those threads make a syscall that blocks then the process as a whole blocks. User-level threading looks like a single threaded process to the kernel. No concurrent scheduling on multiple CPUs is possible.
The main advantage of kernel-level threads is that they run independently from one another and can be scheduled on different CPUs. If one blocks, others continue to execute.