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

时间:2023-03-09 01:42:55
进程控制函数(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()) < ) {
perror("fork");
exit();
} else if (pid == ) {
printf("child process PID is %d\n", getpid());
printf("child process PGID is %d\n", getpgid());
printf("child process PGID is %d\n", getpgrp());
printf("child process PGID is %d\n", getpgid(getpid()));
exit();
} sleep();
printf("parent process PID is %d\n", getpid());
printf("parent process PGID is %d\n", getpgrp()); return ;
}

运行结果:

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