Chinaunix首页 | 论坛 | 博客
  • 博客访问: 620295
  • 博文数量: 79
  • 博客积分: 848
  • 博客等级: 军士长
  • 技术积分: 1800
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-26 19:30
文章分类

全部博文(79)

文章存档

2015年(4)

2013年(39)

2012年(36)

分类: C/C++

2012-10-24 09:38:22

有些时候我们在一个工程项目中,并不希望将我们已经编写好的动态库中的子类的定义和方法暴露给其他的程序员,但是在其他的程序模块中,当需要我们在动态库中声明的子类的时候我们需要怎么办捏?
这就用到了我们今天需要总结的dlopen和dlsym这两个函数了!
函数定义:

点击(此处)折叠或打开

  1. #include<dlfcn.h>
  2. void * dlopen( const char * pathname, int mode)
在dlopen()函数以指定模式打开指定的动态链接库文件,并返回一个句柄给调用进程。使用dlclose()来卸载打开的库。
mode是打开方式,其值有多个,不同操作系统上实现的功能有所不同,在linux下,按功能可分为三类:  
1、解析方式:
RTLD_LAZY:在dlopen返回前,对于动态库中的未定义的符号不执行解析(只对函数引用有效,对于变量引用总是立即解析)。   
RTLD_NOW: 需要在dlopen返回前,解析出所有未定义符号。
2、作用范围,可与解析方式通过“|”组合使用:  
RTLD_GLOBAL:动态库中定义的符号可被其后打开的其它库重定位。   
RTLD_LOCAL: 与RTLD_GLOBAL作用相反,动态库中定义的符号不能被其后打开的其它库重定位。如果没有指明是RTLD_GLOBAL还是RTLD_LOCAL,则缺省为RTLD_LOCAL。   
3、作用方式:
RTLD_NODELETE: 在dlclose()期间不卸载库,并且在以后使用dlopen()重新加载库时不初始化库中的静态变量。这个flag不是POSIX-2001标准。   
RTLD_NOLOAD: 不加载库。可用于测试库是否已加载(dlopen()返回NULL说明未加载,否则说明已加载),也可用于改变已加载库的flag,如:先前加载库的 flag为RTLD_LOCAL,用dlopen(RTLD_NOLOAD|RTLD_GLOBAL)后flag将变成RTLD_GLOBAL。这个 flag不是POSIX-2001标准。   
RTLD_DEEPBIND:在搜索全局符号前先搜索库内的符号,避免同名符号的冲突。这个flag不是POSIX-2001标准。
返回值:   
打开错误返回NULL   
成功,返回库引用   
编译时候要加入 -ldl (指定dl库)
函数定义:

点击(此处)折叠或打开

  1. #include<dlfcn.h>
  2. void *dlsym(void *handle,const char*symbol)
handle是由打开动态链接库后返回的指针,symbol就是要求获取的函数或全局变量的名称,函数返回值是void*,指向函数的地址,供调用使用。
函数定义

点击(此处)折叠或打开

  1. #include<sys/types.h>
  2. #include<dirent.h>
  3. DIR *opendir(const char *path)
  4. struct dirent* readdir(DIR* dir_handle);
该函数的作用就是打开一个文件夹的目录,并且返回一个目录流,这个时候我们需要用到readir()函数,来读取这个目录流中的内容,返回一个dirent的对象,这样我们就能够从dirent的结构体中获取到此是我们目录下的文件名!
下面来举一个例子来说明一下,大致是这样的,我现在这边有一个项目,首先定义了一个用于监控的接口,将很多监控方法的实现封装在了多个动态链接库的文件之中,并且每个库文件返回一个子类的对象(为一个子类定义的全局变量),我先在将这些动态的链接库的文件放在了一个固定的目录下,程序通过opendir(),readdir()函数获取到此目录下我们需要的动态链接库的文件名,从而得到文件的绝对路径,然后通过dlopen()函数加载每一个动态链接库,通过dlsym()函数获取该动态链接库返回的子类对象或者函数的指针,然后将这些个函数地址放入一个map中,通过监控指标的名称进行索引,从而调用不同的子类实现。
这是头文件中定义的通用的接口:

点击(此处)折叠或打开

  1. #ifndef JSFPROBER_H
  2. #define JSFPROBER_H
  3. #include<string>
  4. using namespace std;
  5. class JsfProber
  6. {
  7.     public:
  8.     virtual double probe(){}
  9.     virtual double probe(const pid_t pid){}
  10.     string name;
  11. };
  12. JsfProber* getProber();
  13. #endif
一个子类的实现用于监控cpu使用率:

点击(此处)折叠或打开

  1. #include<iostream>
  2. #include<vector>
  3. #include<fstream>
  4. #include"jsfprober.h"
  5. using namespace std;
  6. class CpurateProber : public JsfProber
  7. {
  8.     public:
  9.     CpurateProber();
  10.     void readToVector(vector<double> &dvec,ifstream &in);
  11.     double calcCpuRate(vector<double> &dvec);
  12.     double probe();
  13.     private:
  14.     string name;
  15.     double ltotal;
  16.     double lbusy;
  17. };
  18. CpurateProber::CpurateProber()
  19. {
  20.     name = "Cpurate";
  21.     ltotal = 0;
  22.     lbusy = 0;
  23. }
  24. void CpurateProber::readToVector(vector<double> &dvec,ifstream &in)
  25. {
  26.     double temp;
  27.     string s;
  28.     in>>s;//skip the first string of the line;
  29.     //cout<<s<<endl;
  30.     for(int i = 0;i <= 3; i++)
  31.     {
  32.         in>>temp;
  33.         //cout<<temp<<" ";
  34.         dvec.push_back(temp);
  35.     }
  36. }
  37. double CpurateProber::calcCpuRate(vector<double> &dvec)
  38. {
  39.     double rate = 0.0;
  40.     double ctotal = dvec[0] + dvec[1] + dvec[2] + dvec[3];
  41.     double cbusy = dvec[0] + dvec[1] + dvec[2];
  42.     rate = 100 * (cbusy - lbusy) / (ctotal - ltotal);
  43.     lbusy = cbusy;
  44.     ltotal = ctotal;
  45.     return rate;
  46. }
  47. double CpurateProber::probe()
  48. {
  49.     vector<double> dvec;
  50.     double rate = 0.0;
  51.     ifstream in("/proc/stat");
  52.     if(!in)
  53.     {
  54.         cout<<"the file can not be used!!"<<endl;
  55.     }
  56.     readToVector(dvec,in);
  57.     rate =    calcCpuRate(dvec);
  58.     cout<<"the cpu rate is:"<<rate<<"%"<<endl;
  59.     return rate;
  60. }
  61. JsfProber* getProber()
  62. {
  63.     JsfProber* p = new CpurateProber();
  64.     return p;
  65. }
另外一个子类的实现用于监控内存使用率:

点击(此处)折叠或打开

  1. #include<fstream>
  2. #include<vector>
  3. #include<iostream>
  4. #include"jsfprober.h"
  5. using namespace std;
  6. typedef double T;
  7. class MemrateProber : public JsfProber
  8. {
  9.     public:
  10.     T probe();
  11.     void readToVector(vector<T> &tvec,ifstream &in);
  12.     T calcMemRate(vector<T> &tvec);
  13.     T freeMem(vector<T> &tvec);
  14.     string name;
  15. };
  16. void MemrateProber::readToVector(vector<T> &tvec,ifstream &in)
  17. {
  18.     string s;
  19.         T temp;
  20.         in>>s;
  21.         in>>temp;
  22.         tvec.push_back(temp);
  23.         in>>s;
  24.         in>>s;
  25.         in>>temp;
  26.         tvec.push_back(temp);
  27. }
  28. T MemrateProber::calcMemRate(vector<T> &tvec)//calc the rate of the used mem
  29. {
  30.      T rate ;
  31.         rate = 100 * (tvec[0] - tvec[1]) / tvec[0];
  32.         return rate;
  33. }
  34. T MemrateProber::freeMem(vector<T> &tvec)//return the free mem mount
  35. {
  36.     T free;
  37.     free = tvec[1]/1024/1024;
  38.     return free;
  39. }
  40. T MemrateProber::probe()
  41. {
  42.     name="memrate";
  43.     vector<T> tvec;
  44.         T temp,free;
  45.         ifstream in("/proc/meminfo");
  46.         if(!in)
  47.         {
  48.                 cout<<"this file can not be opened!"<<endl;
  49.         }
  50.         else
  51.         {
  52.                 readToVector(tvec,in);
  53.                 temp = calcMemRate(tvec);
  54.         free = freeMem(tvec);
  55.                 cout<<"the memory using rate is:"<<temp<<"%"<<endl;
  56.         cout<<"the free mem is:"<<free<<"GB"<<endl;
  57.         }
  58.         return temp;
  59. }

  60. JsfProber * getProber ()
  61. {
  62.     JsfProber * p = new MemrateProber ();
  63.     return p;
  64. }
大家可以看到,在两个子类实现中别重新定义了头文件中声明的虚函数probe()完成了不同的监控功能,并且为了能够使程序能够统一的调用,分别返回了一个基类对象的指针。我将这两个子类的实现分别打包在了:libCpurate.so和libMemrate.so这两个动态库中。在另外一个程序main.cpp中我定义这样的一个map m_prober,这样就可以实现当其他的程序有监控需求时,我很就可以查找这个map根据indicename来匹配索引通过Jsfprober*这样的基类指针来实现相应的监控!但是现在有这样的一个问题,就我在编译我的main.cpp时,因为看不错子类的动态库文件,所以编译通过不了,这里就用到了我们这里写到的dlopen函数:下面我只把这个这个简单实现的部分写一下,感兴趣的可以再联系我:

点击(此处)折叠或打开

  1. JsfProber *(* func)(void);
  2.     DIR *p = NULL;
  3.     void *s = NULL;
  4.     struct dirent *entry;
  5.     string path = "/home/mini/桌面/jsf/src/plugin/prober/";
  6.     if((p = opendir(path.c_str())) == NULL)
  7.     {
  8.         perror("opendir()");
  9.         exit(-1);
  10.     }
  11.     while((entry = readdir(p)) != NULL)
  12.     {
  13.         if(!strcmp(entry->d_name,"libcpurate.so"))
  14.         {
  15.             path += "libcpurate.so";
  16.             if((s = dlopen(path.c_str(), RTLD_LAZY)) == NULL)
  17.             {
  18.                 perror("dlopen()");
  19.                 exit(-1);
  20.             }
  21.             else
  22.             {
  23.                 func = (JsfProber *(*)(void))dlsym(s, "getProber");
  24.                 m_probers.insert(make_pair("Cpurate", func()));
  25.                 dlclose(s);
  26.             }
  27.         }
  28.         else if(!strcmp(entry->d_name,"libmemrate.so"))
  29.         {
  30.             path += "libmemrate.so";
  31.             if((s = dlopen(path.c_str(), RTLD_LAZY)) == NULL)
  32.             {
  33.                 perror("dlopen()");
  34.                 exit(-1);
  35.             }
  36.             else
  37.             {
  38.                 func = (JsfProber *(*)(void))dlsym(s, "getProber");
  39.                 m_probers.insert(make_pair("Memrate", func()));
  40.                 dlclose(s);
  41.             }
  42.         }
  43. }
现在总结一下吧:在这篇博文中我们想要总结是:如何在不暴露动态库中的内容的情况下来使用动态库中实现的方法,当然代码中涉及到了:如何利用c++读取指定目录下的所有文件的文件名,以及如何在linux系统下用c++来监控CPU和内存的使用率,以及虚函数和多态的实现等一些问题,大家如果感兴趣可以参考!




阅读(3135) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~