咨询电话:010-82823766

怎样用C语言得到一个进程的全路径
  • 2008-11-18 10:40:33
  • 发表时间:
  • 浏览次数:
  • 本站原创
  • 文章来源:
  • 佚名
  • 作者:

一个进程的命令行保存在文件/proc/pid/cmdline中,参数之间是字节0分隔。下面的小程序举例说明如何去读这个文件。

#include <iostream>
#include <fstream>

int main(int argc, char* argv[]) ...{
  if(argc != 2) ...{
    printf("usage: %s pid ", argv[0]);
    exit(0);
  }

  std::string path(argv[1]);
  path = "/proc/" + path + "/cmdline";
  std::ifstream fin(path.c_str());
  if(!fin) ...{
    std::cout << "Open /proc/" << argv[1] << "/cmdline failed! ";
    exit(-1);
  }
  std::string s;
  while (getline(fin, s, '\0'))
  {
    std::cout << s << std::endl;
  }
}

top
推荐导读
推荐导读
bottom
top
热门文章
热门文章
bottom