Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C static variables and linux fork

Hi I created a server program that forks a new process after its accepts a socket connection. There are several statically allocated global variables defined in the program. My question is are these static buffers allocated twice after the fork? Or does the fork only duplicate address space on the heap and the call stack?

like image 315
eat_a_lemon Avatar asked Feb 02 '11 20:02

eat_a_lemon


2 Answers

The entire address space is duplicated, including all global variables and the program text.

like image 136
zwol Avatar answered Oct 13 '22 00:10

zwol


The whole address space is "duplicated" during fork(2). It's often done with copy-on-write and there are more details about sharing program text and the libraries, but that is not relevant here. Both parent and child processes end up with their own copy of the static data.

like image 27
Nikolai Fetissov Avatar answered Oct 13 '22 01:10

Nikolai Fetissov