分享程序员开发的那些事...
首页
NTFS
漏洞
Android
Magento
服务器
Python
powershell
技术问答
Linux
网站首页
且构网 - 分享程序员编程开发的那些事
您所在的位置:
首页
>
windows和linux下获取当前程序路径以及cpu数
windows和linux下获取当前程序路径以及cpu数
更新时间:2022-09-16 19:33:12
[cpp]
view plain
copy
#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
}
上一篇 : :[LintCode] Surrounded Regions 包围区域
下一篇 : 从缓冲上看阻塞与非阻塞socket在发送接收上的区别
相关阅读
windows和linux下获取当前程序路径以及cpu数
推荐文章
Razor语法
[转]ASP.NET 缓存:方法和***实践
使用 Microsoft Ajax Library 创建自定义客户端脚本
Microsoft AJAX Library对 Boolean的扩展
自定义 Web 服务器控件
【转】Http之Get/Post请求区别
Asp.net Ajax Accordion控件的用法
҉菊҉花҉文҉生҉成҉器҉
Microsoft AJAX Library对 Error的扩展
log4net用法实例