Questions about shared libraries (.so)

P

Paulo da Silva

Hi.

I am writing a shared library to override some libc
functions.

I have the following questions:

Ex.
int XXX=0;
void f() {++XXX; ...; --XXX;}

1.
Are global data (extern) shared among processes
or threads? If a process or a thread accesses f while
another one is also executing f it sees XXX as 0 or 1?

2.
Is there any problem not freeing malloc allocated areas in f
relying on the automatic free at the end of the job?
I am asking this because I want to share some information
between sucessive calls to f in a process but I don't know what
is the last call. So, I don't know when to free the malloc'ed
areas.

Any links to literature about these is welcome.

Thanks for any help.
 
T

Thomas Matthews

Paulo said:
Hi.

I am writing a shared library to override some libc
functions.

I have the following questions: [snip]

Any links to literature about these is welcome.

Thanks for any help.

Wrong newsgroup. This newsgroup, discusses the _standard_ C language which has no
facilities for shared libraries. These are outside
the scope of the language.

Not all platforms support shared libraries. Please
consult a newsgroup dedicated to your platform.
Many newsgroups are listed in the FAQs below.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
L

Lawrence Kirby

Hi.

I am writing a shared library to override some libc
functions.

I have the following questions:

Ex.
int XXX=0;
void f() {++XXX; ...; --XXX;}

1.
Are global data (extern) shared among processes
or threads?

That would depend on the implementation and how it implements processes
and thread which are not C concepts. Typically however threads share the
same address space whereas processes for normal operations don't.

Given your reference ot .so lubraries comp.unix.prorgammer might be a
better place to discuss this.
If a process or a thread accesses f while
another one is also executing f it sees XXX as 0 or 1?

That would depend on when XXX is accessed. Also consider makin XXX
volatile.
2.
Is there any problem not freeing malloc allocated areas in f relying on
the automatic free at the end of the job?

That depends on what you mean by a "job". You should free memory when you
no longer need it or else you might have a memory leak. On reasonable
systems the memory allocated by a program is reclaimed when the program
terminates.
I am asking this because I
want to share some information between sucessive calls to f in a process
but I don't know what is the last call. So, I don't know when to free
the malloc'ed areas.

If it is a single allocation that's not too large that may not be a
problem, you might think of it like a static variable.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top