一段在后端执行 切换到前端的代码片段
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
int main(void)
{
pid_t pid = 0;
pid_t proPid = 0 ;
if((pid = fork()) < 0){
fprintf(stderr , " fork error : %s " , strerror(errno));
exit(1);
}
if (pid == 0){
sleep(2);
exit(0);
}
wait(0);
if( (proPid = getpgid(0)) > 0 && (proPid == tcgetpgrp(STDIN_FILENO))){ //检查是否为前台进程组
// printf("***\n");
tcsetpgrp(STDIN_FILENO , proPid); //设置当前进程组为前台进程组
}
printf("getsid :%ld \n" , getsid(0));
printf("getpid :%ld \n" , getpid());
printf("tcgetpgrp:%ld \n" , tcgetpgrp(STDIN_FILENO)); //返回 前台进程组id
exit(0);
}