- 论坛徽章:
- 0
|
我创建了几个子进程,然后让他们创建加入到一个新的进程组中去。源代码如下
main()
{ int shmid;
int pid;
int *t;
int d;
printf("the father pid is %d,my father is %d,my term is %d\n",
getpid(),getppid(),getpgrp());
shmid=shmget(100,10,IPC_CREAT|0666);
t=shmat(shmid,NULL,0);
*t=0;
for(d=1;d<6;d++)
{ if((pid=fork())<0)
/*sleep(10);*/
shmid=shmget(100,10,IPC_CREAT|0666);
t=shmat(shmid,NULL,0);
printf("t=%d\n",*t);
printf("i am %d,my father is %d,my term is %d\n",getpid (),getppid(),getpgrp());
if(*t==0)
{
setpgid(getpid(),getpid());
*t=getpid();
printf("i am %d,my father is %d,my term is %d\n",getpid(),getppid(),getpgrp());
}
else
{
setpgid(getpid(),*t);
printf("i am %d,my father is %d, my term is %d\n",getpid(),getppid(),getpgrp());}
exit(0);
}
}
exit(0);
}
我创建了五个子进程,然后以第一个子进程为新的进程组的队长 ,其他子进程顺利加入。
但是如果我加入sleep(10)语句 让父进程先结束,让init 接管五个子进程,能够顺利接管,但是第一个子进程顺利创建新的进程组后,其他四个子进程却没有顺利加入。
其他四个子进程的进程组号还是以前父进程的组号,不知道为什么??我估计是
setpgid(getpid(),*t);这条语句没有运行,但为什么了? |
|