进程组
进程组就是一个或者多个进程的集合,里面的每一个进程都有统一的进程标识
进程组有一个特殊的,进程该进程与进程组ID相同。
- setpgid() 和 getgpid()
#include <sys/types.h>
#include <unistd.h>
//获取进程组ID
pid_t getpgrp(void);
//创建一个新的进程组或讲一个进程加入到进程组中
//成功返回0,失败返回-1
int setpgid(pid_t pid,pid_t gpid);
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int main(){
int a;
pid_t pid,gpid;
pid = getpid();
gpid = getgpid();
a = setpgid(pid,getpid);
cout << "pid = " << pid << endl;
cout << "gpid = " << gpid << endl;
cout << "a = " << a << endl;
return 0;
}
- 获取进程ID和父进程ID
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int main(){
cout << "父进程ID:" << (long)getppid() << endl;
cout << "进程ID:" << (long)getpid() << endl;
}