APUE习题9.2
作者:互联网
/*Program:
* APUE习题9.2
* History:
* 2021/1/21 Chen Ying-jun First release
* */
#include<apue.h>
#include<iostream>
#include<my_error.h>
#include<unistd.h>
using namespace std;
int main(){
pid_t pid,pgrp;
if((pid=fork())<0)
err_sys("fork error");
else if(pid==0){//child
int sessionPid;
//build session
if((sessionPid=setsid())<0)
err_sys("setsid error");
cout<<"process id of session="<<sessionPid<<endl;
cout<<"foreground process group id="<<tcgetpgrp(STDIN_FILENO)<<endl;
cout<<"foreground process group id="<<tcgetpgrp(STDOUT_FILENO)<<endl;
cout<<"foreground process group id="<<tcgetpgrp(STDERR_FILENO)<<endl;
}else{
//使子进程先运行,以建立会话
sleep(3);
if((pgrp=getpgid(pid))<0)
err_sys("setid error");
cout<<"child process id="<<pid<<endl;
cout<<"group id of child process="<<pgrp<<endl;
}
exit(0);
}
运行结果:
process id of session=3465
foreground process group id=-1
foreground process group id=-1
foreground process group id=-1
child process id=3465
group id of child process=3465
子进程id=会话id(即会话首进程id)=子进程所在进程组id。
所有获取控制终端的组id全部返回出错,说明该进程组不是前台进程,没有控制终端。
和题目预期相符。
标签:group,APUE,process,pid,进程,习题,include,id,9.2 来源: https://blog.csdn.net/weixin_42614575/article/details/112981299