1

I'm playing with fork in linux and i see something that don't like me

I read this link After forking, are global variables shared? and i did a test with gdb


[c0m0@worktux programming]$ ps aux | grep env

c0m0 3108 0.0 0.0 4288 324 pts/1 S 22:53 0:00 ./env c0m0 3110 0.0 0.0 4288 80 pts/1 S 22:53 0:00 ./env

[c0m0@worktux programming]$ gdb -q -p 3108 Attaching to process 3108

(gdb) call getenv("toto") $1 = 4195989 (gdb) x/s $1 0x400695: "root"

Now i'll attach to child process

[c0m0@worktux programming]$ gdb -q -p 3110

Attaching to process 3110

(gdb) call getenv("toto")

$1 = 4195989

(gdb) x/s $1

0x400695: "root"

So my what my question is, why i see the variable what i set into fork in the both process?

I used the function putenv for set toto=root, i try to be more clear.

Can i see a environment variable that was setting in the child process from the parent?

Community
  • 1
  • 1
c4f4t0r
  • 1,563
  • 15
  • 24
  • You mean you called `putenv` and then `fork`? Then of course both of them have the same variable set. – Carl Norum Jul 18 '13 at 21:58
  • 1
    The answer is still valid, the `fork` function duplicate the process memory (and some other resources like file descriptors) at the time of the fork. This include environment variables. – Sylvain Defresne Jul 18 '13 at 21:59
  • I used putenv with fork() { putenv("toto=root"); }, so i know the child receive a copy of parent resources, but i would like to know if enviroment variables of the child process are shared with the parent. – c4f4t0r Jul 19 '13 at 06:58
  • Even after your edit, it seems to me that the other question answers this one: No, you can't unless you implement something. – madth3 Jul 19 '13 at 15:34

0 Answers0