fork()和pthread_create对VSZ的影响

时间:2022-06-13 21:02:07

I am working over an embedded http server written in C which was originally using fork() for handling each client request. I switched it to use pthread_create instead of fork().

我正在使用C语言编写的嵌入式http服务器,该服务器最初使用fork()来处理每个客户端请求。我把它切换为使用pthread_create而不是fork()。

During memory usage comparison b/w the fork() and threaded version, I observed that is a change in %VSZ utilization as listed by top. The fork() version reports higher %VSZ then of pthread_create().

在内存使用比较b / w fork()和线程版本的过程中,我发现这是由顶部列出的%VSZ利用率的变化。 fork()版本报告的%VSZ高于pthread_create()。

Can anyone explain why this change is there, because, as far as I think all the changes I have done are related to creating threads. I can't determine how it as changed the Virtual memory Size of the process.

任何人都可以解释为什么会出现这种变化,因为据我所知,我所做的所有更改都与创建线程有关。我无法确定如何更改进程的虚拟内存大小。

1 个解决方案

#1


-1  

Basically a fork()creates another process, which means that it gets assigned its own memory space, which means that you multiply the memory used.

基本上fork()创建另一个进程,这意味着它被赋予了自己的内存空间,这意味着你将使用的内存相乘。

A Thread on the other hand shares its memory space with the process that created it, therefore your memory usage will be way smaller, but you have to worry about race conditions and deadlocks if you access the same variable from multiple threads. (Does not happen with processes unless you use shared memory constructs)

另一方面,线程与创建它的进程共享其内存空间,因此您的内存使用量会更小,但如果从多个线程访问同一个变量,则必须担心竞争条件和死锁。 (除非使用共享内存构造,否则不会发生进程)

#1


-1  

Basically a fork()creates another process, which means that it gets assigned its own memory space, which means that you multiply the memory used.

基本上fork()创建另一个进程,这意味着它被赋予了自己的内存空间,这意味着你将使用的内存相乘。

A Thread on the other hand shares its memory space with the process that created it, therefore your memory usage will be way smaller, but you have to worry about race conditions and deadlocks if you access the same variable from multiple threads. (Does not happen with processes unless you use shared memory constructs)

另一方面,线程与创建它的进程共享其内存空间,因此您的内存使用量会更小,但如果从多个线程访问同一个变量,则必须担心竞争条件和死锁。 (除非使用共享内存构造,否则不会发生进程)