天天看點

C++擷取目前程序的程序号方法

來源: http://hi.baidu.com/dnboy/blog/item/064ef4005944d482e850cd51.html

其實我們系統中有現成的函數可以擷取到程序号,如下:

#ifdef _WIN32

#include <process.h>

#else

#include <unistd.h>

#endif

int main()

{

    int iPid = (int)getpid();

    std::cout<<"The process id is: "<<iPid<<std::endl;

    return 0;

}

有些定義為pid_t getpid(),其實pid_t的類型就是int類型,定義如下:typedef int pid_t;