#! /bin/sh # Example to show signal handling for (( i = 1; i < 32; ++i )) do trap "echo caught signal $i; exit 1" $i done echo My process ID is $$ echo "Now copy and paste the following into another window:" echo 'for ((i=0;i < 32; ++i));do if ((i==9 || i==19));then let ++i;fi;echo Sending signal $i:;kill -$i '$$';sleep 1;done' echo Then try pasting this code, and understand why this process terminates: echo 'for ((i=0;i < 32; ++i));do echo Sending signal $i:;kill -$i '$$';sleep 1;done' # So how can we terminate it? # Note: # The colon `:' is the null statement that can be used wherever the # syntax requires a statement. It returns the value 0 (i.e., true). # In this case, it gives us an infinite loop. # Try: # $ help : while : do sleep 1 done # Try running this in one window, and in another window, type this: # for ((i=0;i < 32; ++i));do if ((i==9 || i==19));then let ++i;fi;echo Sending signal $i:;kill -$i 21306;sleep 1;done # Of course, instead of 21306, put the process ID of this process. # Then try removing the "if ((i==9 || i==19));then let ++i;fi;" statement, # and run it again. # Question 6: # Download the script # http://ictlab.tyict.vtc.edu.hk/ossi/lectures/shell/bin/trapall. # Make it executable and run it. Send the executing process all # signals in the range 1..31 as described in the existing comments # in the file. Modify it so that it will still print the message, but # it will exit when it receives any signal. In the modified program, # add comments that: # o explain how you could terminate the original program; # o list all signals in the range 1..31 that the original program could # not trap; # o explain why the signals you list cannot be trapped by the original # program. # Nick's answers: # 1. I saw the process ID of the trapall process, and in another window, # typed: # $ kill -9 process_id # or # $ kill -KILL process_id # where process_id is the PID of the trapall process. # 2. Only signals 9 (SIGKILL) and 19 (SIGSTOP) could not be handled # 3. The operating system ensures that these two processes cannot be # handled by a process. The KILL signal ensures that the system # administrator always has a way to terminate a process. The STOP # signal ensures that debuggers can always single step through a # program, and that a system administrator may "freeze" a process and # examine the process at leisure. This may be useful to examine a # process started by a cracker who has intruded into a system and has # run a program that needs to be examined to understand what damage # the cracker is trying to achieve.