日期:2014-05-16 浏览次数:20674 次
#include<stdio.h>
#include<unistd.h>
int main()
{
if(fork() == 0)
{
setpgid(0,getpid());
tcsetpgrp(0,getpid());
printf("After tcsetpgrp,the fore process group is %d\n",(int)tcgetpgrp(0));
sleep(5);
printf("I am child,process id is %d,process group id is %d\n",getpid(),getpgrp());
}
else
{
sleep(5);
printf("I am father,process id is %d,process group id is %d\n",getpid(),getpgrp());
}
return 0;
}
int main()
{
pid_t pid;
printf("foreground pgid is %d\n", tcgetpgrp(0));
if((pid = fork()) == 0)
//if(fork() == 0)
{
printf("foreground pgid is %d\n", tcgetpgrp(0));
setpgid(0,getpid()); // 子进程新建了一个进程组,失去了控制终端,所以后面的tcsetpgrp会失败
// tcsetpgrp(0,getpid());
printf("After tcsetpgrp,the fore process group is %d\n",(int)tcgetpgrp(0));
sleep(5);
printf("I am child,process id is %d,process group id is %d\n",getpid(),getpgrp());
printf("foreground pgid is %d\n", tcgetpgrp(0));
}
else
{
sleep(1);
printf("foreground pgid is %d\n", tcgetpgrp(0));
tcsetpgrp(0, pid); // 父进程把控制终端让给子进程
printf("foreground pgid is %d\n", tcgetpgrp(0));
sleep(5);
printf("I am father,process id is %d,process group id is %d\n",getpid(),getpgrp());
}
return 0;
}
#include<stdio.h>
#include<unistd.h>
int main()
{
pid_t pid;
if((pid = fork()) == 0)
//if(fork() == 0)
{
setpgid(0,getpid()); // 子进程新建了一个进程组,失去了控制终端,所以后面的tcsetpgrp会失败
// tcsetpgrp(0,ge