说明:
本文章旨在总结备份、方便以后查询,由于是个人总结,如有不对,欢迎指正;另外,内容大部分来自网络、书籍、和各类手册,如若侵权请告知,马上删帖致歉。
QQ 群 号:513683159 【相互学习】
内容来源:
《Linux系统编程》、《Linux网络编程》、《Unix环境高级编程》、man 手册
、
一、函数介绍
序号 | 函数 | 说明 | 头文件 | 注意 |
---|---|---|---|---|
1 | pid_t getpid(void); | 获取调用进程的进程ID | unistd.h | |
2 | pid_t getppid(void); | 获取调用进程的父进程的进程ID | ||
3 | uid_t getuid(void); | 获取调用进程的实际用户ID | ||
4 | uid_t geteuid(void); | 获取调用进程的有效用户ID | ||
5 | gid_t getgid(void); | 取调用进程的实际组ID | ||
6 | gid_t getegid(void); | 取调用进程的有效组ID | ||
7 | pid_t getpgrp(void); | 获取调用进程的进程组ID,即领头进程的PID | ||
8 | pid_t getpgid(pid_t pid); | 获取指定进程pid的进程组ID. | pid=0相当于getpgrp() |
二、示例:
(1)功能介绍:
获取PID、PPID、PGID.
(2)源文件:get.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(int argc,char *argv[])
{
/* 变量定义 */
pid_t pid,ppid,pgrp,pid_pgid,ppid_pgid;
/* 获取pid与ppid */
pid = getpid(); //获取当前进程的PID
ppid = getppid(); //获取当前进程的PPID
pgrp = getpgrp(); //获取当前进程的PGID
pid_pgid = getpgid(pid); //获取进程pid的PGID
ppid_pgid = getpgid(ppid); //获取进程ppid的PGID
/* 输出pid与ppid */
printf("The PID of the current process is %d\n",pid); //输出当前进程的PID
printf("The PPID of the current process is %d\n",ppid); //输出当前进程的PPID
printf("ID of the process group of the current process %d\n",pgrp); //输出当前进程的PGID
printf("%d ,%d\n",pid_pgid,ppid_pgid); //输出进程pid与ppid的PGID
return 0;
}
(3)编译运行及结果
1.编译:gcc get.c -o get
2.运行:./get
3.结果:
The PID of the current process is 14929
The PPID of the current process is 14207
ID of the process group of the current process 14929
14929 ,14207
4.总结:
可知,当前进程PID:14929,其父进程PID:14207.进程组PID:14929
pid的进程组PID:14929,ppid的进程组PID:14207
使用ps命令与grep命令查看进程14207是什么程序
hhb@xsndz:~/桌面$ ps -x|grep 14207
14207 pts/0 Ss 0:00 bash
17116 pts/0 S+ 0:00 grep 14207
发现PID:14207,进程为bash,即:当前环境中脚本程序。因为实在当前bash中运行的此程序
PS:
1️⃣ps = Process Status = 列出系统中当前运行的那些进程.
2️⃣grep = global search regular expression and print out the line = 采用正则表达式的模式对字符串进行搜索,并输出搜索结果