0

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().

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().

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.

Rahul Shukla
  • 1,292
  • 9
  • 25
  • http://stackoverflow.com/questions/5514464/difference-between-pthread-and-fork-on-gnu-linux – Jeyaram Oct 21 '13 at 08:49
  • @Jeyaram : I have read the above post, but it doesn't answers my question. My concern here is strictly limited to the fact that if a program is just started : No requests now, it should have almost same VSZ – Rahul Shukla Oct 21 '13 at 09:15

1 Answers1

0

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

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)

UnholySheep
  • 3,967
  • 4
  • 19
  • 24
  • Since any sane os-kernel implements "copy on write" when forking processes, large parts of the address space are shared with the parent. So you just can't add size of the virtual address spaces to get the total memory used. – youdontneedtothankme Oct 22 '13 at 09:31