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 ...
随机推荐
- python 文件移动(shutil)
# encoding=utf-8 # /home/bergus/tongbu/360共享/编程语言 # /home/bergus/桌面 # /home/bergus/test/hh import os ...
- python 序列类型
1.不可变的序列类型:tuple.range.str.set 001:对于tuple 类型有如下几种构造方式 1.() 构造一个空的元组. 2.a | (a,) 构造一个只有一个元素的元组. 3.tu ...
- 一个.java文件中可以有几个同级类
1.在一个.java文件中可以有几个类.修饰符只可以public abstract final和无修饰符,不能是其他的private等修饰符.2.public修饰的只能有一个,且要与文件名相同 若没有 ...
- win7系统怎样备份
利用系统自带的备份还原 1 这种方法的缺点是如果以后系统出现问题,无法进入系统的话,就无法恢复系统了.首先我们点击开始菜单,打开控制面板! 2 在控制面板中点击系统和安全! 3 我们选择备份和还原中的 ...
- [置顶] 获取系统时间的方法--linux
一. localtime 函数获取(年/月/日/时/分/秒)数值. 1.感性认识 #include<time.h> //C语言的头文件 #include<stdio.h> ...
- 剑指offer57 删除链表中重复的结点
class Solution { public: ListNode* deleteDuplication(ListNode* pHead) { if(!pHead) return pHead; str ...
- Asp.net异步IHttpAsyncHandler示例
/// <summary> /// 异步IHttpHandler,实现了一个简单的统计流量的功能, /// 由于是示例代码,所以没有判断IP或者MAC /// </summary&g ...
- Reverse Key Indexes反向索引
Reverse Key Indexes反向索引A reverse key index is a type of B-tree index that physically reverses the by ...
- 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 ...
- [jQuery]无法获取隐藏元素(display:none)宽度(width)和高度(height)的新解决方案
在做茶城网改版工作的时候,又遇到一个新问题,我需要用jQuery写一个通过点击左右图标来翻阅图片的小插件,写好后测试可以正常运行,但是放到Tab中后发现只有第一个Tab中的代码能够正常运行,其它全部罢 ...