DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  4737 随笔 :: 2 文章 :: 542 评论 :: 1615万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
[cpp] view plaincopy
 
  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.     }  
posted on   DoubleLi  阅读(1864)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2013-01-24 oracle 10g 数据类型占用字节介绍
点击右上角即可分享
微信分享提示