Tuesday, October 7, 2014

Unix Prog: Job Control

1. Job Control Feature

It could allow the user to start multiple jobs(groups of processes) from a single terminal and control which job could access the terminal and which job should run in the background.

We need: a shell that supports the job control, the terminal driver in the kernel must support the job control, the kernel must support job-control related signals.

2. Start a job

start a job from foreground:
 ubuntu@ip-172-31-23-227:~$ emacs session.c  

start a job from background:
 ubuntu@ip-172-31-23-227:~$ cat session.c | less &  
 [1] 5143  
Process id is 5143, job number is 1.

3. Signal to foreground job

terminal driver will send following signals to foreground job. Background jobs are not affected
1) SIGINT signal, for interrupt character(DELETE or Ctrl - C)
2) SIGQUIT signal, for quit character(Ctrl Backslash)
3) SIGTSTP signal, for suspend character(Ctrl Z)

4. Background job read from and write to controlling terminal

terminal driver will send SIGTTIN signal to background job processes if it is trying to read from terminal

terminal driver will send SIGTTOU signal to background job processes if it is trying to write to terminal

By default, these two signals will make processes in the job stop.

5. Bring the background job to foreground

1) Run cat command from the background, so we create the job 6, which has one process with process id 5260
2) Type "Enter", we may notice that job 6 is already stopped. Because this background job is trying to read from terminal, which make terminal driver send the SIGTTIN signal to stop it.
3) We use "fg %6" command to bring the job 6 from background to foreground. And it show up "cat > temp.txt" to indicate this is the job getting stopped.
4) We type "Hello world", and the job finished
5) Print out the content of temp.txt

 ubuntu@ip-172-31-23-227:~$ cat > temp.txt &  
 [6] 5260  
 ubuntu@ip-172-31-23-227:~$  
   
 [6]+ Stopped         cat > temp.txt  
 ubuntu@ip-172-31-23-227:~$ fg %6  
 cat > temp.txt  
 Hello world  
 ubuntu@ip-172-31-23-227:~$ cat temp.txt  
 Hello world  

1) Run less command from the background, then we created job 1 with one process whose process id is 5321
2) Type enter, we noticed that the job 1 is already stopped, since it is trying to output to controlling terminal, which make terminal driver send SIGTTOU signal to make the job stop.
3) We use "fg %1" command to bring the job 1 from background to foreground, then it will turn to less screen, then we type "q" to quit less.
 ubuntu@ip-172-31-23-227:~$ less session.c &  
 [1] 5321  
 ubuntu@ip-172-31-23-227:~$  
   
 [1]+ Stopped         less session.c  
 ubuntu@ip-172-31-23-227:~$ fg %1  
 less session.c  
 ubuntu@ip-172-31-23-227:~$  

Note: fg command will try to call tcsetpgrp system call to setup the process as foreground process, and also make terminal driver send the SIGCONT to the process group to make job continue.

6. foreground background jobs interaction with terminal driver

terminal driver handles: input/output with foreground job, send SIGINT, SIGOUT, SIGTSTP signal to foreground job.

terminal driver will send SIGTTIN/SIGTTOU signal to background job in case background job read from / write to controlling terminal.

No comments:

Post a Comment