曲径通幽论坛

标题: getpid() -- 获得进程ID [打印本页]

作者: beyes    时间: 2009-6-18 02:09
标题: getpid() -- 获得进程ID
系统调用 getpid()获取当前进程的 ID:

#include <sys/types.h>
#include <unistd.h>

pid_t getpid(void);

测试程序
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>

int main(void)
{
    pid_t pid;
   
    if ( (pid = fork()) == -1 ) {
        printf("fork error!\n");
        exit(1);
    }

    if( pid == 0 )
        printf("getpid return %d\n", getpid());

    exit(0);
}
运行及输出
beyes@linux-beyes:~/C/base> ./getpid.exe
getpid return 19395
返回子进程的 ID 号 19395




欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2