windows和linux下获取当前程序路径以及cpu数
- #ifdef WIN32
- #include <Windows.h>
- #else
- #include <stdio.h>
- #include <unistd.h>
- #endif
- #include <assert.h>
- std::string getCurrentAppPath()
- {
- #ifdef WIN32
- char path[MAX_PATH + 1] = {0};
- if (GetModuleFileName(NULL, path, MAX_PATH) != 0)
- return std::string(path);
- #else
- char path[256] = {0};
- char filepath[256] = {0};
- char cmd[256] = {0};
- FILE* fp = NULL;
- // 设置进程所在proc路径
- sprintf(filepath, "/proc/%d", getpid());
- // 将当前路径设为进程路径
- if(chdir(filepath) != -1)
- {
- //指定待执行的shell 命令
- snprintf(cmd, 256, "ls -l | grep exe | awk '{print $10}'");
- if((fp = popen(cmd,"r")) == NULL)
- {
- return std::string();
- }
- //读取shell命令执行结果到字符串path中
- if (fgets(path, sizeof(path)/sizeof(path[0]), fp) == NULL)
- {
- pclose(fp);
- return std::string();
- }
- //popen开启的fd必须要pclose关闭
- pclose(fp);
- return std::string(path);
- }
- #endif
- return std::string();
- }
- std::size_t getCpuCount()
- {
- #ifdef WIN32
- SYSTEM_INFO sysInfo;
- GetSystemInfo(&sysInfo);
- return sysInfo.dwNumberOfProcessors;
- #else
- long cpu_num = sysconf(_SC_NPROCESSORS_ONLN);
- if (cpu_num == -1)
- {
- assert(false);
- return 0;
- }
- // 看两者是否相等
- assert(cpu_num == sysconf(_SC_NPROCESSORS_CONF));
- return cpu_num;
- #endif
- }
windows和linux下获取当前程序路径以及cpu数的更多相关文章
- 在Windows及Linux下获取毫秒级运行时间的方法
在Windows下获取毫秒级运行时间的方法 头文件:<Windows.h> 函数原型: /*获取时钟频率,保存在结构LARGE_INTEGER中***/ WINBASEAPI BOOL W ...
- Linux下获取当前程序的绝对路径
在Linux开发应用时,我们常常需要在程序中获取当前程序绝对路径,我们可以通过readlink读取符号链接/proc/self/exe进行获取,这个符号链接代表当前程序,它的源路径就是当前程序的绝对路 ...
- 怎样在windows下和linux下获取文件(如exe文件)的具体信息和属性
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/xmt1139057136/article/details/25620685 程序猿都非常懒.你懂的! ...
- windows和linux下查看java安装路径
windows下查看版本:(默认安装路径安装就不需要去配环境变量了) java -version windows下查看安装路径: java -verbose Linux下安装版本查看方式和window ...
- 【C#遗补】获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPath的区别
原文:[C#遗补]获取应用程序路径之System.IO.Directory.GetCurrentDirectory和System.Windows.Forms.Application.StartupPa ...
- php windows与linux下的路径区别
php windows与linux下的路径区别windows用的是"\",linux用的是"/"这一点要特别清楚,, ps:在PHP windows也可以用/表 ...
- Windows与Linux下文件操作监控的实现
一.需求分析: 随着渲染业务的不断进行,数据传输渐渐成为影响业务时间最大的因素.究其原因就是因为数据传输耗费较长的时间.于是,依托于渲染业务的网盘开发逐渐成为迫切需要解决的需求.该网盘的实现和当前市场 ...
- 谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH PKG_CONFIG_PATH
谈谈Linux下动态库查找路径的问题 ldconfig LD_LIBRARY_PATH PKG_CONFIG_PATH 转载自:http://blog.chinaunix.net/xmlrpc.ph ...
- paip兼容windows与linux的java类根目录路径的方法
paip兼容windows与linux的java类根目录路径的方法 1.只有 pathx.class.getResource("")或者pathx.class.getResourc ...
随机推荐
- externn "C"解析
1.揭密extern "C" extern "C"包含双重含义,从字面上即可得到:首先,被它修饰的目标是 "extern”的:其次,被它修饰的目标是 ...
- [python] 如何用python操作Excel
直接上代码: from openpyxl import Workbook from openpyxl.cell import get_column_letter wb = Workbook() des ...
- 实测Eclipse连接小米2S调试问题
小米2S手机在Eclipse真机调试时,设备选择列表无法显示手机,DDMS也连接不上设备,解决步骤: 1.打开手机设置中开发者选项 - USB调试开启: 2.保证小米2S手机Windows下设备驱动已 ...
- android开发的问题集(二)
(1)子线程对UI线程操作的简便方法 子线程方法用 Looper.prepare(); 结束时候用 Looper.loop();
- 杭电oj 3079 Vowel Counting
Tips:可以先将输入的字符串全部转化为小写字母,然后再将元音字母变为大写,时间复杂度O(n) #include<stdio.h> #include<string.h> #in ...
- SoftLAyer VPN
1,安装softlayer-VPN(即跑VPN客户端)的机器与在SoftLAyer中的HardwareHostServers or VMIServer的privateIP互通
- 剑指offer57 删除链表中重复的结点
class Solution { public: ListNode* deleteDuplication(ListNode* pHead) { if(!pHead) return pHead; str ...
- IKAnalyzer使用停用词词典进行分词
@Test // 測试分词的效果,以及停用词典是否起作用 public void test() throws IOException { String text = "老爹我们都爱您.&qu ...
- ios5和ios6横竖屏支持及ipad和iphone设备的判断
ios5和ios6横竖屏支持及ipad和iphone设备的判断 判断是ipad还是iphone设备.此定义在PayViewControllerDemo-Prefix.pch 定义如下: #define ...
- 【深搜加剪枝】【HDU1455】【Sticks】
题目大意:有一堆木棍 由几个相同长的木棍截出来的,求那几个相同长的木棍最短能有多短? 深搜+剪枝 具体看代码 #include <cstdio> #include <cstdlib& ...