Counting the number of programs using a shared library

I have a library foo.so. Each time a new program starts, it is loaded using this library. I want to declare (somehow) a static variable in the library and ask through one program panel, which was also loaded using the foo.so library, how many programs were downloaded (using this library). I tried static and external, but every time the variable is initialized to zero for each each program (so when I ask the bar about the counter, it tells me 1). Is it possible to somehow get the result that I wanted?

+4
source share
1 answer

You will need to use some kind of cross-process design. I'm from Linux practice, but I suspect shared memory or a semaphore would be a decent choice. Since you will not use the semaphore for normal operation, this may look a little strange or even stop working. But I think a simple semaphore approach would be this:

Load: sem_open to create a semaphore with a unique name and number 0, or to open an already created semaphore. Then sem_post, to increase its value.

When checking: sem_getvalue to read the current account stored in the semaphore.

: sem_wait ( -), sem_close, . , sem_unlink sem_destroy, .

( , , sem_t. , , .)

+1

Source: https://habr.com/ru/post/1543435/


All Articles