LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-21-2018, 08:13 AM   #1
baouche
LQ Newbie
 
Registered: Oct 2018
Posts: 1

Rep: Reputation: Disabled
Signals with trap


Hello,

I am preparing a small class for students on signals with trap. I understood the use of this principle with the example on signal 2 SIGINT:

trap "echo hello" 2: which displays "hello" each time you press the shortcut ctrl + c.

On the other hand, I have difficulty assimilating the use of the following cases:

1. The effect of signal 3 SIGQUIT.

2. The effect of siganl 9 SIGKILL.

3. The effect of the signal 19 SIGSTOP.

Can you guide me with simple examples please?

Best regards.
 
Old 10-21-2018, 09:35 AM   #2
average_user
Member
 
Registered: Dec 2010
Location: Warsaw, Poland
Distribution: Slackware
Posts: 560

Rep: Reputation: 220Reputation: 220Reputation: 220
In man 7 signal it says:
Code:
The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
So you cannot trap them. In order to show how they work you can use kill builtin in the script:
Code:
#!/usr/bin/env bash

printf "My PID: %d\n" "$$"

printf "Send SIGSTOP\n"
kill -STOP $$

printf "Send SIGKILL\n"
kill -KILL $$
After SIGSTOP is sent run fg to restore program execution and SIGKILL will kill it. You can also mention that you can use signal number instead of name with kill builtin, that signals and their usage are described in man 7 signal and that you can send signal from another process and that's what's usually done.

SIGQUIT can be invoked with ^\ as stty -a says (^ means Control key):
Code:
$ stty -a
speed 38400 baud; rows 65; columns 105; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>;
start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel
iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho
-extproc
You can use the following script:
Code:
#!/usr/bin/env bash

trap "echo SIGQUIT" QUIT

printf "My PID: %d\n" "$$"

printf "Press ^\\\\\n"

while true
do
    sleep 1
done
We have to run sleep in a loop because in man bash it says:
Code:
If bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes.

Last edited by average_user; 10-21-2018 at 09:45 AM.
 
1 members found this post helpful.
Old 11-08-2018, 10:12 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
A useful example maybe using SIGSTOP and SIGCONT
(which is the send process to background/foreground action.
Maybe start a video playback process, or a browser on youtube,
send a SIGSTOP and it will pause. send a SIGCONT and it will start running again.

Just tried with a tab for youtube on firefox, use top to find the process ID maybe

SIGSTOP pauses
SIGCONT continues
SIGHUP crashes the tab!
SIGQUIT closes the browser,

so quite interesting I think.

Last edited by bigearsbilly; 11-08-2018 at 10:25 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to send snmp trap & recieve trap in C program minil Programming 3 07-10-2010 09:22 AM
Regarding trap command sharp859 Linux - Newbie 1 05-11-2009 07:35 PM
Kernel trap (Fatal trap 12) m!k@EL *BSD 4 09-05-2007 11:58 PM
trap signals AMMullan Programming 3 10-19-2003 04:31 PM
How can i trap the Console signals in C++ Code Mr_ss Programming 0 05-18-2001 03:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration