进程控制函数(1)-getpgid() getpgrp() 获取当前进程的进程组ID

定义:
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
", getpid());
        printf("child process PGID is %d
", getpgid(0));
        printf("child process PGID is %d
", getpgrp());
        printf("child process PGID is %d
", getpgid(getpid()));
        exit(0);
    }
    
    sleep(3);
    printf("parent process PID is %d
", getpid());    
    printf("parent process PGID is %d
", 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

原文地址:https://www.cnblogs.com/yongdaimi/p/8191727.html