Thursday, October 9, 2014

Unix Prog: Orphaned Process Groups

1. Orphaned Process Group

For each process in the process group, its parent process is either the member of the group or the member of another session.

If a stopped process becomes a member of orphan process group, the stopped process will be sent a hang-up signal(SIGHUP) followed by a continue signal(SIGCONT).

2. Example:
orphan.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
 #include<signal.h>  
   
 static void sig_hup(int signo)  
 {  
  printf("SIG_HUP received, pid = %d\n", getpid());  
 }  
   
 static void pr_ids(char *name)  
 {  
  printf("%s, pid = %d, ppid = %d, pgrp = %d, tpgrp = %d\n",  
      name, getpid(), getppid(), getpgrp(), tcgetpgrp(STDIN_FILENO));  
 }  
   
 int main(int argc, char* argv[])  
 {  
  char c;  
  pid_t pid;  
   
  pr_ids("parent");  
  if((pid = fork()) < 0) {  
   printf("fork error!\n");  
   exit(1);  
  } else if(pid > 0) { // parent process  
   sleep(3);  
   exit(0);  
  } else {  
   pr_ids("child");  
   signal(SIGHUP, sig_hup);  
   kill(getpid(), SIGTSTP);  
   pr_ids("child");  
   if(read(STDIN_FILENO, &c, 1) != 1)  
    printf("read error from controlling terminal\n");  
   exit(0);  
  }  
 }  

shell:
1) Program firstly output the parent process information, then parent process fall into sleep
2) Then program comes to the child process side. It output the child process information. Then child process send the signal SIGTSTP to itself to stop the child process
3) After 3 seconds, parent process terminated, child process become orphaned, its parent process become "init" process(id: 1). At this time, the process in orphan process group will be sent the SIGHUP signal, which program caught, and then SIGCONT signal get sent out to make child process continue running.
4) At this time child process is background process, when it is trying to read from controlling terminal, system is supposed to stop it. But for orphan process in orphan process group, it will just return error, instead of stopping it.
 ubuntu@ip-172-31-23-227:~$ ./orphan.out  
 parent, pid = 15650, ppid = 15559, pgrp = 15650, tpgrp = 15650  
 child, pid = 15651, ppid = 15650, pgrp = 15650, tpgrp = 15650  
 ubuntu@ip-172-31-23-227:~$ SIG_HUP received, pid = 15651  
 child, pid = 15651, ppid = 1, pgrp = 15650, tpgrp = 15559  
 read error from controlling terminal  
   

3. Free BSD Implementation:

1) struct tty: the structure describing the controlling terminal
t_session: point to struct session, who has this struct tty as the controlling terminal
t_pgrp: point to struct pgrp, indicating foreground process group. Terminal driver use this member to send signal to
t_termios: contain all the special characters and related information for this terminal.
t_winsize: struct winsize describing the current size of the window.

2) struct session: the structure describing the session
s_count: number of process group
s_leader: point to the struct proc, indicating the session leader process
s_ttyvp: point to the struct v-node
s_ttyp: point to the struct tty, indicating the controlling terminal of the session
s_sid: session id

3) struct pgrp: the structure describing the process group
pg_id: process group id
pg_session: point to the struct session, indicating the session the process group belong to.
pg_member: linked list of process group members

4) struct proc: the structure describing the process
p_pglist: linked list of struct proc
p_pid: process id
p_pptr: point to parent process
p_pgrp: point to struct pgrp, indicating the process group it belongs to

No comments:

Post a Comment