[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Another shared lib question



> On Sat, 08 Mar 1997 00:55:46 CST "Walter L. Preuninger II" 
> (walterp@rapidramp.com) wrote:
> 
> > I have been reading the gcc-howto and the elf-howto... and have made my
> > first shared library. My question is: does the code have to be
> > rewritten/redesigned to take care of any reentrantcy problems? I have a
> > feeling that globals/statics are bad news. Am I right? 

I would think that the "copy on write" feature comes into play here.
As soon as you change any value, you get your own copy of that page and
that page is no longer shared. So, globals/statics are no worse in a
shared library thatn they usually are anyway. 

As this strace shows, the shared library is mmap'ed with MAP_PRIVATE:

open("/lib/libc.so.5", O_RDONLY)        = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3"..., 4096) = 4096
mmap(0, 757760, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x4000a000
mmap(0x4000a000, 528099, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED, 3, 0)
= 0x4000a000
mmap(0x4008b000, 22860, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3,
0x80000) = 0x4008b000

And here's some snippets of the man page for mmap:

          The prot argument describes the desired memory pro-
	  tection. It has bits

	  PROT_EXEC
		    Pages may be executed.

	  PROT_READ
		    Pages may be read.

	  PROT_WRITE
		    Pages may be written.


	  The flags parameter  specifies  the  type  of  the  mapped
	  object,  mapping options and whether modifications made to
	  the mapped copy of the page are private to the process  or
	  are to be shared with other references.  It has bits

	  MAP_PRIVATE
		    Create a private copy-on-write mapping.

As somebody else already mentioned, thread-safeness is a whole other issue
and your statics I'd say would be a problem. I _think_ that each thread
shares the same copy of the "copy-on-write mapping".

...RickM...


Reply to: