windows和linux下获取当前程序路径以及cpu数

简介: [cpp] view plaincopy   #ifdef WIN32   #include    #else   #include    #include    #endif      #include           std::string getCur...
[cpp]  view plain copy
 
  1. #ifdef WIN32  
  2. #include <Windows.h>  
  3. #else  
  4. #include <stdio.h>  
  5. #include <unistd.h>  
  6. #endif  
  7.   
  8. #include <assert.h>  
  9.   
  10.     std::string getCurrentAppPath()  
  11.     {  
  12. #ifdef WIN32  
  13.         char path[MAX_PATH + 1] = {0};  
  14.         if (GetModuleFileName(NULL, path, MAX_PATH) != 0)  
  15.             return std::string(path);  
  16. #else  
  17.         char path[256] = {0};  
  18.         char filepath[256] = {0};  
  19.         char cmd[256] = {0};  
  20.         FILE* fp = NULL;  
  21.   
  22.         // 设置进程所在proc路径  
  23.         sprintf(filepath, "/proc/%d", getpid());  
  24.         // 将当前路径设为进程路径  
  25.         if(chdir(filepath) != -1)  
  26.         {  
  27.             //指定待执行的shell 命令  
  28.             snprintf(cmd, 256, "ls -l | grep exe | awk '{print $10}'");  
  29.             if((fp = popen(cmd,"r")) == NULL)  
  30.             {  
  31.                 return std::string();  
  32.             }  
  33.             //读取shell命令执行结果到字符串path中  
  34.             if (fgets(path, sizeof(path)/sizeof(path[0]), fp) == NULL)  
  35.             {  
  36.                 pclose(fp);  
  37.                 return std::string();  
  38.             }  
  39.   
  40.             //popen开启的fd必须要pclose关闭  
  41.             pclose(fp);  
  42.             return std::string(path);  
  43.         }  
  44. #endif  
  45.         return std::string();  
  46.     }  
  47.   
  48.     std::size_t getCpuCount()  
  49.     {  
  50. #ifdef WIN32  
  51.         SYSTEM_INFO sysInfo;  
  52.         GetSystemInfo(&sysInfo);  
  53.         return sysInfo.dwNumberOfProcessors;  
  54. #else  
  55.         long cpu_num = sysconf(_SC_NPROCESSORS_ONLN);  
  56.         if (cpu_num == -1)  
  57.         {  
  58.             assert(false);  
  59.             return 0;  
  60.         }  
  61.         // 看两者是否相等  
  62.         assert(cpu_num == sysconf(_SC_NPROCESSORS_CONF));  
  63.         return cpu_num;  
  64. #endif  
  65.     }  
目录
相关文章
|
19天前
|
iOS开发 MacOS Windows
|
5天前
|
Java Linux
Linux下如何定位最耗CPU的JAVA代码
Linux下如何定位最耗CPU的JAVA代码
12 0
|
19天前
|
Windows
LabVIEW启用/禁用Windows屏幕保护程序
LabVIEW启用/禁用Windows屏幕保护程序
20 4
LabVIEW启用/禁用Windows屏幕保护程序
|
19天前
|
NoSQL Linux Redis
Redis的介绍,以及Redis的安装(本机windows版,虚拟机Linux版)和Redis常用命令的介绍
Redis的介绍,以及Redis的安装(本机windows版,虚拟机Linux版)和Redis常用命令的介绍
35 0
|
19天前
|
缓存 监控 前端开发
如何在 Linux 命令行中检查 CPU 使用率
【5月更文挑战第8天】
22 0
|
19天前
|
存储 安全 搜索推荐
Windows之隐藏特殊文件夹(自定义快捷桌面程序)
Windows之隐藏特殊文件夹(自定义快捷桌面程序)
|
19天前
|
Windows
Windows 程序自启动实现方法详解
Windows 程序自启动实现方法详解
32 0
|
19天前
|
前端开发 Linux iOS开发
【Flutter前端技术开发专栏】Flutter在桌面应用(Windows/macOS/Linux)的开发实践
【4月更文挑战第30天】Flutter扩展至桌面应用开发,允许开发者用同一代码库构建Windows、macOS和Linux应用,提高效率并保持平台一致性。创建桌面应用需指定目标平台,如`flutter create -t windows my_desktop_app`。开发中注意UI适配、性能优化、系统交互及测试部署。UI适配利用布局组件和`MediaQuery`,性能优化借助`PerformanceLogging`、`Isolate`和`compute`。
【Flutter前端技术开发专栏】Flutter在桌面应用(Windows/macOS/Linux)的开发实践
|
19天前
|
存储 Linux
深入探索Linux文件系统:属性、路径与隐藏之谜
深入探索Linux文件系统:属性、路径与隐藏之谜
28 1