I am trying to write a program where I have to call some functions through a (shared) library (its source is available). The C code for the library has several global variables, and many functions change the values of these global variables. What I have to do in my program requires that each function call that I make gets to work with a fresh set of variables.
我正在编写一个程序,在这个程序中,我必须通过(共享)库调用一些函数(它的源代码是可用的)。库的C代码有几个全局变量,许多函数改变这些全局变量的值。我在程序中要做的是,我所做的每一个函数调用都需要使用一组新的变量。
For example, let this function be a part of the library:
例如,让这个函数成为库的一部分:
int x = 1;
int foo()
{
int a = 0;
//do somethings to 'a'
//...
x++;
return a;
}
Now every time I invoke foo()
from my program, the value of x
gets update from 1 to 2 then 3 then 4 and so on... I am try to construct a program so that every time foo()
is invoked, it sees x = 1.
每次我从程序调用foo()时,x的值从1到2,然后是3然后是4,等等。我尝试构造一个程序,以便每次调用foo()时,它会看到x = 1。
I am sorry to say that my knowledge of how C/linux treat these variable spaces is insufficient, so this question may seem vague. The above is just a small example; in reality, there are so many variables that is practically impossible to reset their values manually.
我很遗憾地说,我对C/linux如何处理这些变量空间的了解还不够,因此这个问题可能显得很模糊。以上只是一个小例子;实际上,有太多的变量实际上不可能手工重置它们的值。
What may be the best way to compile that library and/or use it my program so as to refresh the variables?
什么是最好的方法来编译这个库和/或使用它我的程序,以便刷新变量?
(On a side note, what I am also trying to do is to parallelize calls to foo(), but because of the shared variables, I cannot do that.)
(顺便说一下,我还试图将对foo()的调用并行化,但是由于共享变量,我不能这样做)。
EDIT: When working on some web dev projects, I used to encapsulate some code in webservices and then invoke those services from the main program. Does a similar framework exist in C/Linux? Please note that functions are returning data.
编辑:在处理一些web开发项目时,我曾经将一些代码封装在webservices中,然后从主程序调用这些服务。在C/Linux中是否存在类似的框架?请注意,函数正在返回数据。
2 个解决方案
#1
3
You have discovered one of the main reasons that global variables (or global state in general) are a really bad idea.
您已经发现了全局变量(或一般的全局状态)是一个非常糟糕的想法的主要原因之一。
Since you have access to the source, I would suggest investing some time to refactor the source code.
既然您可以访问源代码,我建议您花一些时间重构源代码。
You can achieve the ability to parallelize calls to foo with the following strategy:
您可以通过以下策略实现对foo的调用并行化的能力:
- Gather up all of the global variables into a single struct. Call it something like Context.
- 将所有全局变量收集到一个结构中。把它叫做上下文。
- Change each function that acts on a global variable to take a pointer to a Context, and change the function to update the variables in the Context instead of updating global variables.
- 更改作用于全局变量的每个函数以获取指向上下文的指针,并更改函数以更新上下文中的变量,而不是更新全局变量。
- Now each thread that wants to use the library can create a new Context and pass that into foo and related functions.
- 现在,希望使用库的每个线程都可以创建一个新的上下文,并将其传递给foo和相关函数。
If it's not feasible to make such a change to the source code, you can use more than one CPU core by starting child processes. Each child process has it's own memory space. That option is not nearly as efficient as using multiple threads.
如果对源代码进行这样的更改是不可行的,您可以通过启动子进程来使用多个CPU内核。每个子进程都有自己的内存空间。这个选项并不像使用多个线程那样有效。
#2
0
I have no answer in details. But you can try one of the following:
我没有详细的答复。但是你可以试试下面的方法:
- unload and load library
- 卸载和加载库
- try to clear library's .bss and fill .data section with values from the library (ref dl_iterate_phdr() call).
- 尝试清除库的.bss和fill .data部分,其中包含来自库的值(ref dl_iterate_phdr()调用)。
#1
3
You have discovered one of the main reasons that global variables (or global state in general) are a really bad idea.
您已经发现了全局变量(或一般的全局状态)是一个非常糟糕的想法的主要原因之一。
Since you have access to the source, I would suggest investing some time to refactor the source code.
既然您可以访问源代码,我建议您花一些时间重构源代码。
You can achieve the ability to parallelize calls to foo with the following strategy:
您可以通过以下策略实现对foo的调用并行化的能力:
- Gather up all of the global variables into a single struct. Call it something like Context.
- 将所有全局变量收集到一个结构中。把它叫做上下文。
- Change each function that acts on a global variable to take a pointer to a Context, and change the function to update the variables in the Context instead of updating global variables.
- 更改作用于全局变量的每个函数以获取指向上下文的指针,并更改函数以更新上下文中的变量,而不是更新全局变量。
- Now each thread that wants to use the library can create a new Context and pass that into foo and related functions.
- 现在,希望使用库的每个线程都可以创建一个新的上下文,并将其传递给foo和相关函数。
If it's not feasible to make such a change to the source code, you can use more than one CPU core by starting child processes. Each child process has it's own memory space. That option is not nearly as efficient as using multiple threads.
如果对源代码进行这样的更改是不可行的,您可以通过启动子进程来使用多个CPU内核。每个子进程都有自己的内存空间。这个选项并不像使用多个线程那样有效。
#2
0
I have no answer in details. But you can try one of the following:
我没有详细的答复。但是你可以试试下面的方法:
- unload and load library
- 卸载和加载库
- try to clear library's .bss and fill .data section with values from the library (ref dl_iterate_phdr() call).
- 尝试清除库的.bss和fill .data部分,其中包含来自库的值(ref dl_iterate_phdr()调用)。