首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在qnx上获取进程内存

如何在qnx上获取进程内存
EN

Stack Overflow用户
提问于 2016-04-04 12:04:08
回答 1查看 2.6K关注 0票数 3

我想获得qnx上的进程内存。在shell上,我可以使用命令showmem -P pid获得结果。在c语言中,我为命令打开一个管道,但是我想解析命令的输出,但是我不知道它是如何完成的。

代码语言:javascript
运行
复制
int main()
{ 
    pid_t self;
    FILE *fp;
    char *command;
    self=getpid();

    sprintf(command,"showmem -P %d",self);
    fp = popen(command,"r");
    // Then I want to read the elements that results from this command line
}
EN

Stack Overflow用户

回答已采纳

发布于 2016-04-07 01:12:29

你的想法和教宗和表演是可行的。您只需解析popen()的结果就可以提取内存信息。

下面是一个假设您没有共享对象的示例:

代码语言:javascript
运行
复制
int main(int argc, char *argv[]) {
    pid_t self;
    FILE *fp;
    char command[30];
    const int MAX_BUFFER = 2048;
    char buffer[MAX_BUFFER];
    char* p;
    char* delims = { " ," };
    int memory[] = {-1, -1, -1, -1, -1 };
    int valueindex = -1;
    int parserindex = 0;
    self = getpid();
    sprintf(command, "showmem -P %d", self);

    fp = popen(command, "r");
    if (fp) {
        while (!feof(fp)) {
            if (fgets(buffer, MAX_BUFFER, fp) != NULL) {
                p = strtok( buffer, delims );
                while (p != NULL) {
                    if (parserindex >=8 && parserindex <= 13) {
                        memory[++valueindex] = atoi(p);
                    }
                    p = strtok(NULL, delims);
                    parserindex +=1;
                }
            }
        }
        pclose(fp);
    }

    printf("Memory Information:\n");
    printf("Total: %i\n", memory[0]);
    printf("Code: %i\n", memory[1]);
    printf("Data: %i\n", memory[2]);
    printf("Heap: %i\n", memory[3]);
    printf("Stack: %i\n", memory[4]);
    printf("Other: %i\n", memory[5]);
    return EXIT_SUCCESS;
}

此程序生成以下输出:

代码语言:javascript
运行
复制
Memory Information:
Total: 851968
Code: 741376
Data: 24576
Heap: 73728
Stack: 12288
Other: 0
票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36402138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档