-
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;
-
-
- sprintf(filepath, "/proc/%d", getpid());
-
- if(chdir(filepath) != -1)
- {
-
- snprintf(cmd, 256, "ls -l | grep exe | awk '{print $10}'");
- if((fp = popen(cmd,"r")) == NULL)
- {
- return std::string();
- }
-
- if (fgets(path, sizeof(path)/sizeof(path[0]), fp) == NULL)
- {
- pclose(fp);
- return std::string();
- }
-
-
- 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
- }
-
相关阅读:
java jmap,jstat等工具的使用
jvm 参数配置
python NameError: name 'false' is not defined
aiflow Global variable explicit_defaults_for_timestamp needs to be on (1) for mysql
TX 笔试题总结
POJ 3140 Contestants Division
POJ 1018 Communication System
POJ 3260 The Fewest Coin
Leetcode: Median of two sorted Array
基础知识 (二)
-
原文地址:https://www.cnblogs.com/lidabo/p/4245437.html
Copyright © 2020-2023
润新知