getpid是一种函数,功能是取得进程识别码,许多程序利用取到的此值来建立临时文件,以避免临时文件相同带来的问题。
函数功能:取得进程识别码
相关函数:fork,kill,getpid
头文件:旧版本:unistd.h,在VC++6.0下可以用process.h
函数原型:旧的原型为pid_t getpid(void);,推荐使用int _getpid( void );这种形式。注意,函数名第一个字符是下划线。
返回值:目前进程的进程ID
fork – 创建新进程;exit – 终止进程;exec – 执行一个应用程序wait – 将父进程挂起,等待子进程终止;getpid – 获取当前进程的PID;nice – 改变进程的优先
示例一: #include <stdio.h> #include <process.h> int main( void ) { /* If run froom command line, shows different ID for * command line than for operating system shell. */ printf( "Process id: %d\n", _getpid() ); return 0; } 示例二: #include<unistd.h> main() { printf(“pid=%d\n”,getpid()); } 执行: pid=1494 /*每次执行结果都不一定相同*/