What the difference between lightweight process and thread?

戏子无情 提交于 2019-12-20 10:32:57

问题


I found an answer to the question here. But I don't understand some ideas in the answer. For instance, lightweight process is said to share its logical address space with other processes. What does it mean? I can understand the same situation with 2 threads: both of them share one address space, so both of them can read any variables from bss segment (for example). But we've got a lot of different processes with different bss sections and I dunno how to share all of them.


回答1:


From MSDN, Threads and Processes:

Processes exist in the operating system and correspond to what users see as programs or applications. A thread, on the other hand, exists within a process. For this reason, threads are sometimes referred to as light-weight processes. Each process consists of one or more threads.




回答2:


I am not sure that answers are correct here, so let me post my version.

There is a difference between process - LWP (lightweight process) and user thread. I will leave process definition aside since that's more or less known and focus on LWP vs user threads. LWP is what essentially are called today threads. Originally, user thread meant a thread that is managed by the application itself and the kernel does not know anything about it. LWP, on the other hand, is a unit of scheduling and execution by the kernel.

Example: Let's assume that system has 3 other processes running and scheduling is round-robin without priorities. And you have 1 processor/core.

Option 1. You have 2 user threads using one LWP. That means that from OS perspective you have ONE scheduling unit. Totally there are 4 LWP running (3 others + 1 yours). Your LWP gets 1/4 of total CPU time and since you have 2 user threads, each of them gets 1/8 of total CPU time (depends on your implementation)

Option2. You have 2 LWP. From OS perspective, you have TWO scheduling units. Totally there are 5 LWP running. Your LWP gets 1/5 of total CPU time EACH and your application get's 2/5 of CPU.

Another rough difference - LWP has pid (process id), user threads do not.

For some reason, naming got little messed and we refer to LWP as threads.

There are definitely more differences, but please, refer to slides. http://www.cosc.brocku.ca/Offerings/4P13/slides/threads.ppt

EDIT:

After posting, I found a good article that explains everything in more details and is in better English than I write. http://www.thegeekstuff.com/2013/11/linux-process-and-threads/




回答3:


IMO, LWP is a kernel thread binding which can be created and executed in the user context.

If I'm not mistaken, you can attach user threads to a single LWP to potentially increase the level of concurrency without involving a system call.




回答4:


Thread is basically task assigned with one goal and enough information to perform a specific task . A process can create multiple threads for doing its work as fast as possible. e.g A portion of program may need input output, a portion may need permissions.

User level thread are those that can be handled by thread library.

On the other hand kernel level thread (which needs to deal with hadrware)are also called LWP(light weight process) to maximize the use of system and so the system does not halt upon just one system call.




回答5:


Threads run within processes.

Each process may contain one or more threads.

If kernel doesn't know anything about the threads running in the process, we have threads running on user space and thus no multiprocessing capabilities are available.

On the other hand, we can have threads running on the kernel space; this means that each process runs on a different CPU. This enables us multiprocessing, but as you may assume it is more expensive in terms of operating system resources.

Finally, there is a solution that lies somewhere in the middle; we group threads together into LWP. Each group runs on different CPU, but the threads in the group cannot be multi processed. That's because kernel in this version knows only about the groups (which are multiprocessed) but nothing about the threads that they contain. Hope it is clear enough.




回答6:


Based on Tanenbaum's book "Distributes Systems", light weight processes is generally referred to a hybrid form of user-level thread and kernel-level thread. An LWP runs in the context of a single process, and there can be several LWPs per process. In addition each LWP can be running its own (user-level) thread. Multi-threaded applications are constructed by creating threads (with thread library package), and subsequently assigning each thread to an LWP.

The biggest advantage of using this hybrid approach is that creating, destroying, and synchronizing threads is relatively cheap and do not need any kernel intervention. Beside that, provided that a process has enough LWPs, a blocking system call will not suspend the entire process.




回答7:


A process contains one or more threads in it and a thread can do anything a process can do. Also threads within a process share the same address space because of which cost of communication between threads is low as it is using the same code section, data section and OS resources, so these all features of thread makes it a "lightweight process".




回答8:


From here. Each LWP is a kernel resource in a kernel pool, and is attached and detached to a thread on a per thread basis. This happens as threads are scheduled or created and destroyed.



来源:https://stackoverflow.com/questions/10484355/what-the-difference-between-lightweight-process-and-thread

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