How to resolve the algorithm Handle a signal step by step in the UNIX Shell programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Handle a signal step by step in the UNIX Shell programming language
Table of Contents
Problem Statement
Most operating systems provide interrupt facilities, sometimes called signals either generated by the user or as a result of program failure or reaching a limit like file space. Unhandled signals generally terminate a program in a disorderly manner. Signal handlers are created so that the program behaves in a well-defined manner upon receipt of a signal. Provide a program that displays an integer on each line of output at the rate of about one per half second. Upon receipt of the SIGINT signal (often generated by the user typing ctrl-C ( or better yet, SIGQUIT ctrl-\ )) the program will cease outputting integers, output the number of seconds the program has run, and then the program will quit.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Handle a signal step by step in the UNIX Shell programming language
Source code in the unix programming language
c="1"
# Trap signals for SIGQUIT (3), SIGABRT (6) and SIGTERM (15)
trap "echo -n 'We ran for ';echo -n `expr $c /2`; echo " seconds"; exit" 3 6 15
while [ "$c" -ne 0 ]; do # infinite loop
# wait 0.5 # We need a helper program for the half second interval
c=`expr $c + 1`
done
#!/bin/bash
trap 'echo "Run for $((s/2)) seconds"; exit' 2
s=1
while true
do
echo $s
sleep .5
let s++
done
#!/bin/bash
trap 'echo "Run for $((s/2)) seconds"; exit' 2
s=1
half_sec_sleep()
{
local save_tty=$(stty -g)
stty -icanon time 5 min 0
read
stty $save_tty
}
while true
do
echo $s
half_sec_sleep
let s++
done
TRAPINT(){ print $n; exit }
for (( n = 0; ; n++)) sleep 1
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Cobra programming language
You may also check:How to resolve the algorithm Euler's identity step by step in the Raku programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Tailspin programming language
You may also check:How to resolve the algorithm Motzkin numbers step by step in the Fermat programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Scilab programming language