老话新谈,高手们进来谈多进程跟多线程的优劣吧

快乐田伯光 2008-10-31 11:49:20
我现在觉得多线程,唯一的好处就是启动结束一个线程速度比启动结束一个进程的速度快!大家的意见呢?
...全文
259 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
once_and_again 2008-11-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 guosha 的回复:]
只占用了虚拟空间,实际内存并没有多占用多少吧!
引用 8 楼 letueo 的回复:
进程占用内存空间过多吧?

[/Quote] 进程 多是 cow,
其实 很多时候 进程 和线程,如果没有切实的数据,
不好说优劣,
lihua_1687 2008-11-02
  • 打赏
  • 举报
回复
来学习!!!
unilgr 2008-11-02
  • 打赏
  • 举报
回复
* The primary motivation for using Pthreads is to realize potential program performance gains.

* When compared to the cost of creating and managing a process, a thread can be created with much less operating system overhead. Managing threads requires fewer system resources than managing processes.

For example, the following table compares timing results for the fork() subroutine and the pthreads_create() subroutine. Timings reflect 50,000 process/thread creations, were performed with the time utility, and units are in seconds, no optimization flags.

Note: don't expect the system and user times to add up to real time, because these are SMP systems with multiple CPUs working on the problem at the same time. At best, these are approximations.

Platform fork() pthread_create()
real user sys real user sys
AMD 2.4 GHz Opteron (8cpus/node) 41.07 60.08 9.01 0.66 0.19 0.43
IBM 1.9 GHz POWER5 p5-575 (8cpus/node) 64.24 30.78 27.68 1.75 0.69 1.10
IBM 1.5 GHz POWER4 (8cpus/node) 104.05 48.64 47.21 2.01 1.00 1.52
INTEL 2.4 GHz Xeon (2 cpus/node) 54.95 1.54 20.78 1.64 0.67 0.90
INTEL 1.4 GHz Itanium2 (4 cpus/node) 54.54 1.07 22.22 2.03 1.26 0.67

* All threads within a process share the same address space. Inter-thread communication is more efficient and in many cases, easier to use than inter-process communication.

* Threaded applications offer potential performance gains and practical advantages over non-threaded applications in several other ways:
o Overlapping CPU work with I/O: For example, a program may have sections where it is performing a long I/O
operation. While one thread is waiting for an I/O system call to complete, CPU intensive work can be performed by
other threads.
o Priority/real-time scheduling: tasks which are more important can be scheduled to supersede or interrupt lower
priority tasks.
o Asynchronous event handling: tasks which service events of indeterminate frequency and duration can be
interleaved. For example, a web server can both transfer data from previous requests and manage the arrival of
new requests.

* The primary motivation for considering the use of Pthreads on an SMP architecture is to achieve optimum performance. In particular, if an application is using MPI for on-node communications, there is a potential that performance could be greatly improved by using Pthreads for on-node data transfer instead.

* For example:
o MPI libraries usually implement on-node task communication via shared memory, which involves at least one memory
copy operation (process to process).
o For Pthreads there is no intermediate memory copy required because threads share the same address space within a
single process. There is no data transfer, per se. It becomes more of a cache-to-CPU or memory-to-CPU bandwidth
(worst case) situation. These speeds are much higher.
o Some local comparisons are shown below:
Pthreads Worst Case
MPI Shared Memory Bandwidth Memory-to-CPU Bandwidth
Platform (GB/sec) (GB/sec)
AMD 2.4 GHz Opteron 1.2 5.3
IBM 1.9 GHz POWER5 p5-575 4.1 16
IBM 1.5 GHz POWER4 2.1 4
Intel 1.4 GHz Xeon 0.3 4.3
Intel 1.4 GHz Itanium 2 1.8 6.4
cceczjxy 2008-11-02
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 guosha 的回复:]
这样说是不是不够准确呢? 对Linux来说,线程跟进程在内核共用同样的控制块吧.

引用 14 楼 cceczjxy 的回复:
引用楼主 guosha 的帖子:
我现在觉得多线程,唯一的好处就是启动结束一个线程速度比启动结束一个进程的速度快!大家的意见呢?


进程要占用进程号进程控制块,这也属于系统资源.
线程间切换要比进程间调度快一些.
[/Quote]

可能表达的不是太清楚,我想说的是使用进程要受到最大进程数这样的限制,而用线程能稍微逃避这样的限制.线程使用的控制块大小远小与进程的.

cceczjxy 2008-11-01
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 guosha 的回复:]
这样说是不是不够准确呢? 对Linux来说,线程跟进程在内核共用同样的控制块吧.

引用 14 楼 cceczjxy 的回复:
引用楼主 guosha 的帖子:
我现在觉得多线程,唯一的好处就是启动结束一个线程速度比启动结束一个进程的速度快!大家的意见呢?


进程要占用进程号进程控制块,这也属于系统资源.
线程间切换要比进程间调度快一些.

[/Quote]

线程使用的控制块是在是在进程内部的。而进程控制块在内核里边,由内核控制。你说的是内核线程吧。
sj307639429 2008-11-01
  • 打赏
  • 举报
回复
学习啦
快乐田伯光 2008-11-01
  • 打赏
  • 举报
回复
对Linux来说,LinuxThread的实现,线程在内核里就是对应一个进程控制块呀,NPTL的实现进程跟线程也共用同样的控制块吧.对内核调度来器来说,是没有区分线程还是进程的.
rzsheng 2008-10-31
  • 打赏
  • 举报
回复
通讯可能会更方便一些,方法多一些
xhs_lh04 2008-10-31
  • 打赏
  • 举报
回复
同意楼上的,线程由于共享进程空间,所以交互方便,但需要一些同步机制,编程复杂些,如果处理不好会带来死锁,内存出错等问题.另外一个线程CORE掉,影响多有此进程的线程
进程相对安全一些,就是交互必须用IPC或者SOCKET等机制
另外创建线程的开销应该要比fork开销少些吧
zhoujianhei 2008-10-31
  • 打赏
  • 举报
回复
线程可以共享进程的地址空间,这样可以方便一些。
快乐田伯光 2008-10-31
  • 打赏
  • 举报
回复
这样说是不是不够准确呢? 对Linux来说,线程跟进程在内核共用同样的控制块吧.

[Quote=引用 14 楼 cceczjxy 的回复:]
引用楼主 guosha 的帖子:
我现在觉得多线程,唯一的好处就是启动结束一个线程速度比启动结束一个进程的速度快!大家的意见呢?


进程要占用进程号进程控制块,这也属于系统资源.
线程间切换要比进程间调度快一些.
[/Quote]
快乐田伯光 2008-10-31
  • 打赏
  • 举报
回复
这么说版主也认同我的观点?
cceczjxy 2008-10-31
  • 打赏
  • 举报
回复
[Quote=引用楼主 guosha 的帖子:]
我现在觉得多线程,唯一的好处就是启动结束一个线程速度比启动结束一个进程的速度快!大家的意见呢?
[/Quote]

进程要占用进程号进程控制块,这也属于系统资源.
线程间切换要比进程间调度快一些.
快乐田伯光 2008-10-31
  • 打赏
  • 举报
回复
运行空间易理解成动行堆栈的话,改为内存空间吧。
如果只是因为进程多了一个创建共享内存这一点,好像线程带来的好处远不及带来的坏外多吧。
[Quote=引用 12 楼 blackbillow 的回复:]
其实我的意思是:线程和共享内存有着同样的效率和同步需要,但后者多了创建共享内存这一步,所以线程的共享要方便些
至于" 线程没有独立的运行空间,编程的复杂度大大的提高了哦,程序的稳定性也要大打折扣吧。"
何谓“独立的运行空间”?这句话不敢苟同
[/Quote]
blackbillow 2008-10-31
  • 打赏
  • 举报
回复
其实我的意思是:线程和共享内存有着同样的效率和同步需要,但后者多了创建共享内存这一步,所以线程的共享要方便些
至于" 线程没有独立的运行空间,编程的复杂度大大的提高了哦,程序的稳定性也要大打折扣吧。"
何谓“独立的运行空间”?这句话不敢苟同
快乐田伯光 2008-10-31
  • 打赏
  • 举报
回复
如果IPC用共享内存,效率不会比线程低吧。
线程没有独立的运行空间,编程的复杂度大大的提高了哦,程序的稳定性也要大打折扣吧。

[Quote=引用 10 楼 blackbillow 的回复:]
线程间并发访问数据需要同步看起来不方便,但这是避免不了的,而且效率也高
效率最高的IPC是共享内存,这种情况也是需要同步机制的
所以对比起来,还是用线程方便些
[/Quote]
blackbillow 2008-10-31
  • 打赏
  • 举报
回复
线程间并发访问数据需要同步看起来不方便,但这是避免不了的,而且效率也高
效率最高的IPC是共享内存,这种情况也是需要同步机制的
所以对比起来,还是用线程方便些
快乐田伯光 2008-10-31
  • 打赏
  • 举报
回复
只占用了虚拟空间,实际内存并没有多占用多少吧!
[Quote=引用 8 楼 letueo 的回复:]
进程占用内存空间过多吧?
[/Quote]
diandian4ever 2008-10-31
  • 打赏
  • 举报
回复
进程占用内存空间过多吧?
快乐田伯光 2008-10-31
  • 打赏
  • 举报
回复
进程跟线程的区别是我明白的, ^_^
[Quote=引用 4 楼 chaojiew 的回复:]
Although the definition of a process may seem obvious, the concept of threads makes all of this less clear-cut. A thread allows a single program to run in multiple places at the same time. All the threads created (or spun off) by a single program share most of the characteristics that differentiate processes from each other. For example, multiple threads that originate from the same program shar…
[/Quote]
加载更多回复(3)

23,114

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧