孤儿进程组
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
static void sig_hup(int signo)
{
? ? printf("SIGHUP received, pid=%d\n", getpid());
}
static void pr_ids(const char* name)
{
? ? printf("%s:pid=%d, ppid=%d, pgrp=%d,tpgrp=%d\n",
? ? ? ? name, getpid(), getppid(), getpgrp(), tcgetpgrp(STDIN_FILENO));
}
int main()
{
? ? pr_ids("parent");
? ? pid_t pid;
? ? pid = fork();
? ? if ((pid)==0)
? ? { ??
? ? ? ? pr_ids("child");
? ? ? ? signal(SIGHUP, sig_hup);
? ? ? ? kill(getpid(), SIGTSTP);
? ? ? ? pr_ids("child");
? ? ? ? char c;
? ? ? ? if (read(STDIN_FILENO, &c, sizeof(c))==-1)
? ? ? ? { ??
? ? ? ? ? ? printf("read from tty error, errno=%m\n");
? ? ? ? } ??
? ? ? ? exit(0);
? ? } ??
? ? else
? ? { ??
? ? ? ? sleep(3);
? ? ? ? exit(0);
? ? ? ? printf("parent exit\n");
? ? } ??
}
./ger?
parent:pid=31037, ppid=12525, pgrp=31037,tpgrp=31037
child:pid=31038, ppid=31037, pgrp=31037,tpgrp=31037
SIGHUP received, pid=31038 //確實接收到了
child:pid=31038, ppid=1, pgrp=31037,tpgrp=31037 //但是SIGCONT被換到了前臺進程了,所以tpgrp還是31037并且可讀
總結
- 上一篇: 特殊设备文件
- 下一篇: Apache Lucene与Lucene