Friday, July 25, 2014

Unix Shell: Process Control(1)

1. kill command
kill command doesn't do the job to kill the process, instead, it will just send a signal to the process.
Each process has its own right to determine how to interpret the signal.

terminal:
kill -l list all signals that are supported by kill command
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ kill -l  
  1) SIGHUP     2) SIGINT     3) SIGQUIT     4) SIGILL     5) SIGTRAP  
  6) SIGABRT     7) SIGBUS     8) SIGFPE     9) SIGKILL    10) SIGUSR1  
 11) SIGSEGV    12) SIGUSR2    13) SIGPIPE    14) SIGALRM    15) SIGTERM  
 16) SIGSTKFLT    17) SIGCHLD    18) SIGCONT    19) SIGSTOP    20) SIGTSTP  
 21) SIGTTIN    22) SIGTTOU    23) SIGURG    24) SIGXCPU    25) SIGXFSZ  
 26) SIGVTALRM    27) SIGPROF    28) SIGWINCH    29) SIGIO    30) SIGPWR  
 31) SIGSYS    34) SIGRTMIN    35) SIGRTMIN+1    36) SIGRTMIN+2    37) SIGRTMIN+3  
 38) SIGRTMIN+4    39) SIGRTMIN+5    40) SIGRTMIN+6    41) SIGRTMIN+7    42) SIGRTMIN+8  
 43) SIGRTMIN+9    44) SIGRTMIN+10    45) SIGRTMIN+11    46) SIGRTMIN+12    47) SIGRTMIN+13  
 48) SIGRTMIN+14    49) SIGRTMIN+15    50) SIGRTMAX-14    51) SIGRTMAX-13    52) SIGRTMAX-12  
 53) SIGRTMAX-11    54) SIGRTMAX-10    55) SIGRTMAX-9    56) SIGRTMAX-8    57) SIGRTMAX-7  
 58) SIGRTMAX-6    59) SIGRTMAX-5    60) SIGRTMAX-4    61) SIGRTMAX-3    62) SIGRTMAX-2  
 63) SIGRTMAX-1    64) SIGRTMAX      

2. Suspend and continue one process
script_1:
 #! /bin/bash  
   
 a=5  
 while [ $a -ne 0 ]  
 do  
   ((a++))  
   echo $a  
   sleep 5  
 done  

terminal:
1) Start the script_1 process at the background
2) script_1 output "6, 7" at the standard output
3) ps command to list the running process , script_1 is running now.
4) script_1 output "8,9" at the standard output
5) 8513 is the process id, use kill command to send STOP signal to suspend the process
6) script_1 doesn't output number any more. It is suspended now. Run ps command, script_1 is still alive, but suspended now.
7) Use kill command to send the "CONT" signal to make script_1 process continue running.
8) script_1 output "10, 11, 12" at the standard output
9) Use kill command to completely "kill" the script_1 process
10) Run ps command, script_1 process is not alive any more.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 &  
 [2] 8513  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ 6  
 7  
   
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps  
  PID TTY     TIME CMD  
  2664 pts/0  00:00:01 bash  
  2838 pts/0  00:00:43 emacs  
  8513 pts/0  00:00:00 script_1  
  8515 pts/0  00:00:00 sleep  
  8516 pts/0  00:00:00 ps  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ 8  
 9  
   
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ kill -STOP 8513  
   
 [2]+ Stopped         ./script_1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps  
  PID TTY     TIME CMD  
  2664 pts/0  00:00:01 bash  
  2838 pts/0  00:00:43 emacs  
  8513 pts/0  00:00:00 script_1  
  8522 pts/0  00:00:00 sleep <defunct>  
  8523 pts/0  00:00:00 ps  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ kill -CONT 8513  
 10  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ 11  
 12  
   
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ kill -9 8513  
 [2]+ Killed         ./script_1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps  
  PID TTY     TIME CMD  
  2664 pts/0  00:00:01 bash  
  2838 pts/0  00:00:43 emacs  
  8526 pts/0  00:00:00 sleep  
  8527 pts/0  00:00:00 ps  

4. Process Handling Signals
KILL and STOP signal can't be ignored by any process, they are delivered immediately. For other signals, if the process is not waken up, signals can't be delivered until the process is up.

Also, given a signal, a process can choose to :
1) Ignore the signal
2) Execute the default action
3) Execute the customized action (with trap command)

terminal:
man signal(7): default actions of signals
     Signal   Value   Action  Comment  
     ──────────────────────────────────────────────────────────────────────  
     SIGHUP    1    Term  Hangup detected on controlling terminal  
                    or death of controlling process  
     SIGINT    2    Term  Interrupt from keyboard  
     SIGQUIT    3    Core  Quit from keyboard  
     SIGILL    4    Core  Illegal Instruction  
     SIGABRT    6    Core  Abort signal from abort(3)  
     SIGFPE    8    Core  Floating point exception  
     SIGKILL    9    Term  Kill signal  
     SIGSEGV   11    Core  Invalid memory reference  
   
     SIGPIPE   13    Term  Broken pipe: write to pipe with no  
                    readers  
     SIGALRM   14    Term  Timer signal from alarm(2)  
     SIGTERM   15    Term  Termination signal  
     SIGUSR1  30,10,16  Term  User-defined signal 1  
     SIGUSR2  31,12,17  Term  User-defined signal 2  
     SIGCHLD  20,17,18  Ign   Child stopped or terminated  
     SIGCONT  19,18,25  Cont  Continue if stopped  
     SIGSTOP  17,19,23  Stop  Stop process  
     SIGTSTP  18,20,24  Stop  Stop typed at terminal  
     SIGTTIN  21,21,26  Stop  Terminal input for background process  
     SIGTTOU  22,22,27  Stop  Terminal output for background process  

No comments:

Post a Comment