1. #ifdef WIN32
  2. #include <Windows.h>
  3. #else
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #endif
  7. #include <assert.h>
  8. std::string getCurrentAppPath()
  9. {
  10. #ifdef WIN32
  11. char path[MAX_PATH + 1] = {0};
  12. if (GetModuleFileName(NULL, path, MAX_PATH) != 0)
  13. return std::string(path);
  14. #else
  15. char path[256] = {0};
  16. char filepath[256] = {0};
  17. char cmd[256] = {0};
  18. FILE* fp = NULL;
  19. // 设置进程所在proc路径
  20. sprintf(filepath, "/proc/%d", getpid());
  21. // 将当前路径设为进程路径
  22. if(chdir(filepath) != -1)
  23. {
  24. //指定待执行的shell 命令
  25. snprintf(cmd, 256, "ls -l | grep exe | awk '{print $10}'");
  26. if((fp = popen(cmd,"r")) == NULL)
  27. {
  28. return std::string();
  29. }
  30. //读取shell命令执行结果到字符串path中
  31. if (fgets(path, sizeof(path)/sizeof(path[0]), fp) == NULL)
  32. {
  33. pclose(fp);
  34. return std::string();
  35. }
  36. //popen开启的fd必须要pclose关闭
  37. pclose(fp);
  38. return std::string(path);
  39. }
  40. #endif
  41. return std::string();
  42. }
  43. std::size_t getCpuCount()
  44. {
  45. #ifdef WIN32
  46. SYSTEM_INFO sysInfo;
  47. GetSystemInfo(&sysInfo);
  48. return sysInfo.dwNumberOfProcessors;
  49. #else
  50. long cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
  51. if (cpu_num == -1)
  52. {
  53. assert(false);
  54. return 0;
  55. }
  56. // 看两者是否相等
  57. assert(cpu_num == sysconf(_SC_NPROCESSORS_CONF));
  58. return cpu_num;
  59. #endif
  60. }

windows和linux下获取当前程序路径以及cpu数的更多相关文章

  1. 在Windows及Linux下获取毫秒级运行时间的方法

    在Windows下获取毫秒级运行时间的方法 头文件:<Windows.h> 函数原型: /*获取时钟频率,保存在结构LARGE_INTEGER中***/ WINBASEAPI BOOL W ...

  2. Linux下获取当前程序的绝对路径

    在Linux开发应用时,我们常常需要在程序中获取当前程序绝对路径,我们可以通过readlink读取符号链接/proc/self/exe进行获取,这个符号链接代表当前程序,它的源路径就是当前程序的绝对路 ...

  3. 怎样在windows下和linux下获取文件(如exe文件)的具体信息和属性

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/xmt1139057136/article/details/25620685 程序猿都非常懒.你懂的! ...

  4. windows和linux下查看java安装路径

    windows下查看版本:(默认安装路径安装就不需要去配环境变量了) java -version windows下查看安装路径: java -verbose Linux下安装版本查看方式和window ...

  5. 【C#遗补】获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPath的区别

    原文:[C#遗补]获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPa ...

  6. php windows与linux下的路径区别

    php windows与linux下的路径区别windows用的是"\",linux用的是"/"这一点要特别清楚,, ps:在PHP windows也可以用/表 ...

  7. Windows与Linux下文件操作监控的实现

    一.需求分析: 随着渲染业务的不断进行,数据传输渐渐成为影响业务时间最大的因素.究其原因就是因为数据传输耗费较长的时间.于是,依托于渲染业务的网盘开发逐渐成为迫切需要解决的需求.该网盘的实现和当前市场 ...

  8. 谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH PKG_CONFIG_PATH

    谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH  PKG_CONFIG_PATH 转载自:http://blog.chinaunix.net/xmlrpc.ph ...

  9. paip兼容windows与linux的java类根目录路径的方法

    paip兼容windows与linux的java类根目录路径的方法 1.只有 pathx.class.getResource("")或者pathx.class.getResourc ...

随机推荐

  1. python 文件移动(shutil)

    # encoding=utf-8 # /home/bergus/tongbu/360共享/编程语言 # /home/bergus/桌面 # /home/bergus/test/hh import os ...

  2. python 序列类型

    1.不可变的序列类型:tuple.range.str.set 001:对于tuple 类型有如下几种构造方式 1.() 构造一个空的元组. 2.a | (a,) 构造一个只有一个元素的元组. 3.tu ...

  3. 一个.java文件中可以有几个同级类

    1.在一个.java文件中可以有几个类.修饰符只可以public abstract final和无修饰符,不能是其他的private等修饰符.2.public修饰的只能有一个,且要与文件名相同 若没有 ...

  4. win7系统怎样备份

    利用系统自带的备份还原 1 这种方法的缺点是如果以后系统出现问题,无法进入系统的话,就无法恢复系统了.首先我们点击开始菜单,打开控制面板! 2 在控制面板中点击系统和安全! 3 我们选择备份和还原中的 ...

  5. [置顶] 获取系统时间的方法--linux

    一. localtime 函数获取(年/月/日/时/分/秒)数值. 1.感性认识 #include<time.h>     //C语言的头文件 #include<stdio.h> ...

  6. 剑指offer57 删除链表中重复的结点

    class Solution { public: ListNode* deleteDuplication(ListNode* pHead) { if(!pHead) return pHead; str ...

  7. Asp.net异步IHttpAsyncHandler示例

    /// <summary> /// 异步IHttpHandler,实现了一个简单的统计流量的功能, /// 由于是示例代码,所以没有判断IP或者MAC /// </summary&g ...

  8. Reverse Key Indexes反向索引

    Reverse Key Indexes反向索引A reverse key index is a type of B-tree index that physically reverses the by ...

  9. Cassandra - Non-system keyspaces don't have the same replication settings, effective ownership information is meaningless

    In cassandra 2.1.4, if you run "nodetool status" without any keyspace specified, you will ...

  10. [jQuery]无法获取隐藏元素(display:none)宽度(width)和高度(height)的新解决方案

    在做茶城网改版工作的时候,又遇到一个新问题,我需要用jQuery写一个通过点击左右图标来翻阅图片的小插件,写好后测试可以正常运行,但是放到Tab中后发现只有第一个Tab中的代码能够正常运行,其它全部罢 ...