一個進程的命令行保存在文件/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, ''))
{
std::cout << s << std::endl;
}
}