定义:
pid_t getpid(void);


表头文件:
#include<unistd.h>


说明:
getpid()用来取得目前进程的进程识别码, 许多程序利用取到的此值来建立临时文件, 以避免临时文件相同带来的问题。


返回值:
目前进程的进程识别码


相关函数:
fork, kill, getpid


示例:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{

    pid_t pid;
    
    if ((pid = fork()) < 0) {
        perror("fork");
        exit(1);
    } else if (pid == 0) {
        printf("child process PID is %d\n", getpid());
        printf("child process PGID is %d\n", getpgid(0));
        printf("child process PGID is %d\n", getpgrp());
        printf("child process PGID is %d\n", getpgid(getpid()));
        exit(0);
    }
    
    sleep(3);
    printf("parent process PID is %d\n", getpid());    
    printf("parent process PGID is %d\n", getpgrp());

    return 0;
}

运行结果:

child process PID is 4471
child process PGID is 4470
child process PGID is 4470
child process PGID is 4470
parent process PID is 4470
parent process PGID is 4470

 

相关文章:

  • 2022-12-23
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2021-11-12
  • 2022-03-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
相关资源
相似解决方案