Wednesday, October 8, 2014

Unix Prog: Shell Execution of Programs

1. Run the foreground process in job-control shell

 ubuntu@ip-172-31-23-227:~$ ps -f  
 UID    PID PPID C STIME TTY     TIME CMD  
 ubuntu  8371 8370 0 23:45 pts/0  00:00:00 -bash  
 ubuntu  8423 8371 0 23:53 pts/0  00:00:00 ps -f  
 ubuntu@ip-172-31-23-227:~$ ps -o pid,ppid,pgid,sid
   PID  PPID  PGID   SID
  8371  8370  8371  8371
  8426  8371  8426  8371

We can see that "ps process"'s parent process is bash, but they are in different process group. And ps process is in the foreground process group, and bash process is in the background process group. Both process groups are in the same session.

2. Run the foreground process in shell not supporting job-control

Everything else is the same, unless that "ps process" is in the same process group with bash process.Since the shell doesn't support job-control, it has no concept of foreground process group, background process group. Newly generated process is in the same process group with parent process.

3. Run the background process in job-control shell

 ubuntu@ip-172-31-23-227:~$ ps -f &  
 [1] 8438  
 ubuntu@ip-172-31-23-227:~$ UID    PID PPID C STIME TTY     TIME CMD  
 ubuntu  8371 8370 0 23:45 pts/0  00:00:00 -bash  
 ubuntu  8438 8371 0 23:58 pts/0  00:00:00 ps -f  
   
 [1]+ Done          ps -f  
 ubuntu@ip-172-31-23-227:~$ ps -o pid,ppid,pgid,sid &  
 [1] 8439  
 ubuntu@ip-172-31-23-227:~$  PID PPID PGID  SID  
  8371 8370 8371 8371  
  8439 8371 8439 8371  
   
 [1]+ Done          ps -o pid,ppid,pgid,sid  

We can see that "ps process"'s parent process is bash process, but they are in different process group. And bash process is in the foreground process group, and bash process is in the background process group. Both process groups are in the same session.

4. Run the background process in shell not supporting job-control

Everything else is the same, unless that "ps process" is in the same process group with bash process. Since the shell doesn't support job-control, it has no concept of foreground process group, background process group. Newly generated process is in the same process group with parent process.

5. Run the process in pipeline in job-control shell in foreground

 ubuntu@ip-172-31-23-227:~$ ps -f | cat  
 UID    PID PPID C STIME TTY     TIME CMD  
 ubuntu  8371 8370 0 Oct08 pts/0  00:00:00 -bash  
 ubuntu  8464 8371 0 00:02 pts/0  00:00:00 ps -f  
 ubuntu  8465 8371 0 00:02 pts/0  00:00:00 cat  
 ubuntu@ip-172-31-23-227:~$ ps -o pid,ppid,pgid,sid | cat  
  PID PPID PGID  SID  
  8371 8370 8371 8371  
  8466 8371 8466 8371  
  8467 8371 8466 8371  

We can see that "ps process" and "cat process" both have bash process as the parent process, and they are in the same process group, and all three process are in the same session.
In current system, the bash process probably just fork two processes, for ps and cat separately.

6. Run the process in pipeline in shell not supporting job control in foreground

Everything else is the same, unless that all 3 processes are in the same process group.Since the shell doesn't support job control, so it has no concept of foreground process group, background process group, all processes are in the same group.

7. Background process read from controlling terminal in job control shell

 ubuntu@ip-172-31-23-227:~$ cat &  
 [1] 15593  
 ubuntu@ip-172-31-23-227:~$ ps  
  PID TTY     TIME CMD  
 15559 pts/0  00:00:00 bash  
 15593 pts/0  00:00:00 cat  
 15594 pts/0  00:00:00 ps  
   
 [1]+ Stopped         cat  

If background process is trying to read from the controlling terminal, terminal driver will send the signal SIGTTIN to make the process stop.

8. Background process read from controlling terminal in shell not supporting job control

It will turn to read from /dev/null, which means it will meet with end-of-file immediately.

If background process specifically read from the /dev/tty, the behavior is undefined.

No comments:

Post a Comment