Course 102: Lecture 19: Using Signals

Ahmed El-Arabawy
Ahmed El-ArabawyDirector of Software Engineering at Cygnus Broadband
Linux For Embedded Systems
ForArabs
Ahmed ElArabawy
Course 102:
UnderstandingLinux
Lecture 19:
Using Signals
HW Platform
UserSpaceKernelSpace
Device Drivers
Linux Kernel
Process 2Process 1
SystemCall
Signal
Signal
User Applications
What is a Signal ??
• Linux has two planes:
• Kernel Plane: Has access to system and hardware resources
• User Plane: For applications to run on
• User applications don’t have direct access to the kernel
• For a user application to communicate with the kernel, it
needs to use a System Call
• What about if the kernel needs to send a
request/command/notification/… to the user application ??
• The used mechanism is Signals
• So, Signals are the mechanism that the kernel uses to
communicate with the user applications
• Signals are also used between user applications (when one
application needs to send a request/command to the other
application)
What is a Signal ??
• Signal is the communication channel used by the Linux Kernel to
communicate with a process running in the user plane
• This can be used by the kernel to kill a process, stop it, resume it,
notify it of a timer expiry, etc…
• Signals can also be sent by another process running in the user
plane. Examples,
• When the user presses Ctrl-c to terminate the running application, or
Ctrl-z to stop it
• When the user uses the command “bg” to resume a stopped
application in the background, or “fg” to resume it in the foreground
• When the user uses the “kill” command to terminate a running
application
• In all of these cases, a signal is sent to the destination application to
achieve the required task
• When sent by another user application, it is first passed to the
kernel in a system call, which in turn passes it back to the desired
process
Signals
• Signals are described in both Unix Systems and POSIX
Standard
• There are a table of signals. Each signal has,
• Signal number
• Signal name (starting with ‘SIG’)
• Signals with numbers (1-31) are normal signals
• Signals with numbers > 31 are called “Real Time Signals”
Passing a Signal to a Process
(kill Command)
$ kill <pid>
$ kill -<Signal number> <pid>
$ kill <Signal name> <pid>
• The kill command passes a signal to the desired process
• Note that, despite that the command name is ‘kill’, it is used for all
types of signals even those that have different purposes and not just
used to kill processes
• If no signal is specified, then the signal SIGTERM is assumed by
default
• Examples:
$ kill -9 1185
$ kill SIGKILL 1185
Identical commands
Other ways to “kill”
Selecting Processes by name
(killall Command)
$ killall <Signal> <command name>
• The command killall sends the desired signal to all processes
running the specified command
$ killall chrome
• This is useful when you need to pick a process without
searching for its pid
• Also useful when you need to send the signal to all instances
of the running program
• If no signal is specified, SIGTERM is assumed
• Example:
$ killall -9 gedit (this sends a SIGKILL signal to all “gedit” instances)
Selecting Processes by Other Criteria
(pkill Command)
$ pkill <signal> -u <username>
$ pkill <siganal> -P <Parent process>
$ pkill <signal> <pattern for command>
$ pkill <signal> -t <Terminal name>
• The pkill command sends the desired signal to processes that match
different criteria
• Examples:
• Send a SIGTERM to all processes owned by user tom
$ pkill -u tom
• Send a SIGSTOP to all processes running under tty1 terminal
$ pkill SIGSTOP -t tty1
• Send SIGTERM to all children of process with pid 1107
$ pkill –P 1107
Killing GUI Processes with the Mouse
(the xkill Command)
$ xkill
• The xkill command allows the user to select the process to kill
with the mouse
• Type the command, then click with the mouse on the UI of the
process you want to kill
Dealing with Hanging GUI Apps
• If a GUI app is hanging, and you need to kill it but unable due
to the hanging screen
• get into a Linux virtual terminal via,
[Ctrl] [Alt] [F1]
• From the virtual terminal, issue the proper “kill” command
$ killall -9 chrome
• After you are done, get back to the tty-7 (for the X window)
[Alt] [F7]
Listing Signal Names
Real time Signals
• Those are the signals with numbers > 31
• They don’t have a pre-defined function, and it is left to the
user/developer to use
• Defined by the programmer
• Guaranteed order of delivery
Triggers for Sending Signals
Keyboard Hot keys
• Some signals are triggered by the user by typing special hot-
keys
• When the kernel detects these hot-keys, it sends the proper
signal to the running process (or process group)
• Examples:
• Ctrl-c triggers SIGINT
• Ctrl-z triggers SIGTSTP
• Ctrl- triggers SIGQUIT
Request from another process
• A process can trigger the kernel to send a signal to another
process by issuing a system call
• Examples for this is when the user issue the commands from
the terminal
$ kill (triggers sending the desired signal, SIGTERM by default)
$ fg (triggers sending the signal SIGCONT)
$ bg (triggers sending the signal SIGCONT)
• A process can also trigger sending a signal to another process
programmatically via some special function API
A Hardware Event
• Hardware events can trigger the kernel to send a signal to the
proper process
• An example of that, when the kernel detects a modem
disconnection, it sends the signal SIGHUP to all processes
using this session
Death of a Related Process
• When a process terminates, the kernel sends the following signals,
• A SIGHUP is sent to all children processes to inform them about the
death of their parent process
• A SIGCHLD is sent to the parent of the terminated process to inform it
about the death of a child process
Errors and Exceptions
• Some exceptions within the process triggers the kernel to
send signals to the process,
• Examples:
• If the process tries to execute an illegal instruction, the kernel is
triggered to send SIGILL signal to it
• If the process suffers from a Floating Point Exception, the kernel
sends it a SIGFPE
• If the process tries to write to a pipe while the other end is not
listening, the kernel sends it a SIGPIPE
• If the process accesses a Null Pointer (or other memory
exceptions), the kernel sends it SIGSEGV
• If a process running in the background tries to read/write to the
standard input/output, the kernel will send it a SIGTTIN/SIGTTOU
• If the process exits on error using the abort(), the kernel will send
it SIGABRT
Notification By the Kernel
• Sometimes, a process would ask the kernel for some
notification when some event happens
• Normally, the process will be waiting for this notification
• The kernel delivers this notification via a signal
• Examples:
• The kernel will use the signal SIGALRM and SIGVTALRM to notify
the process of a timer expiry
• A process can ask the kernel for a notification whenever a file
handler has been accessed
Process Response to Signals
Default Action
• When a process receives a signal, the normal behavior is to execute
the default action
• Each signal has its own default action that is part of its description
• Examples:
• Some signals have the default action of termination (graceful
termination). This is applicable for several signals such as SIGTERM,
SIGPIPE, SIGUSR1, SIGUSR2, …
• Some signals have the default action of termination with generation
of a core dump (stack trace for further debugging). This action is
useful for signals that indicate some error in the process operation
such as SIGSEGV, SIGILL, SIGABRT, SIGQUIT …
• Some signals have the default action of being ignored by the process
such as SIGCHLD
• Some signals does not allow the process to change its default action.
This means that the default action is the only allowed action. This
includes the SIGSTOP which force the process to stop (suspend) and
SIGKILL which force the process to terminate immediately
Ignoring a Signal
• A process can set itself to ignore certain incoming signals,
accordingly no action is performed when these signals are
received
• Some signals can not be ignored such as SIGSTOP, and SIGKILL
Blocking a Signal
• A process can set itself to block a certain set of signals
• Blocking a signal means, keeping it in a queue for later
handling (when the blocking is taken away)
• This is used when the process is executing some code that
should not be interrupted by the incoming signals
• Every process has a mask of which signals to block
• Only one signal of a certain type will be blocked (other
instances will be dropped).
• This last rule does not apply to real time signals (signals with
numbers > 31)
• Some signals can not be blocked (SIGSTOP and SIGKILL)
Catching the Signal
• A process can “catch the signal” before its default handler
executes, and pass it to a special handler
• This is specially useful for some of the signals that have their
default action of being ignored (such as SIGCHLD)
• Some signals can not be caught (SIGSTOP and SIGKILL)
Popular Signals
Hang-Up Signal (1)
SIGHUP
• The hang-up signal is used by the kernel to inform the processes
running in a tty session when the session closes. This can be due to,
• The modem for the session disconnects
• The network connection used to connect to the machine breaks
• It is commonly used to request daemon processes to re-read their
configuration file
• Default action for this signal for the process to terminate
Interrupt Signal (2)
SIGINT
• Interrupt signal is triggered when the user uses the hot-key Ctrl-c
• This causes the kernel to send SIGINT to the currently running
process group
• The default action for this signal is for the process to terminate
Quit Signal (3)
SIGQUIT
• The quit signal is triggered by the user when he uses the hot-
keys Ctrl-
• This triggers the kernel to send SIGQUIT to all processes in the
currently running process group
• Default action is for the process to quit and generate a core
dump file (to be used for debugging)
Illegal Command Signal (4)
SIGILL
• The illegal command signal is triggered when the process
program tries to execute an invalid instruction
• This may occur in the case of memory corruption of the code
area or in stack memory
• Default action is to terminate and generate a core dump file
Abort Signal (6)
SIGABRT
• The abort signal is sent by the kernel to the process when it
calls the abort() function to indicate an erroneous exit of the
program
• The default action is to terminate and generate a core dump
file
Trap Signal (5)
SIGTRAP
• The trap signal is normally used with debuggers and program
tracers
Bus Error Signal (7)
SIGBUS
• The bus error signal is triggered by an error in the program in
which an attempt was made to access memory incorrectly
• This can be caused by alignment errors in memory access
• The default action is to terminate and generate a core dump
file
Floating Point Exception (8)
SIGFPE
• The floating point exception signal is sent by the kernel to the
process when it falls into an exception while running a floating
point instruction
• The default action is to terminate and generate a core dump
file
Kill Signal (9)
SIGKILL
• The process was explicitly killed by a request from a different
process
• The request is to kill the process immediately and forcibly
without giving it a chance to clean-up
• A process can not ignore, block, or catch this signal
Stop Signals (17 &18)
SIGSTOP & SIGTSTP
• SIGTSTP (terminal Stop) is triggered by pressing Ctrl-z
• This signal causes the process (or process group) to suspend
operation
• SIGSTOP has the same effect with the only difference that it can not
be ignored/blocked/caught by the process while this is possible with
SIGTSTP
• Upon stopping due to either SIGSTOP & SIGTSTP, the process awaits
SIGCONT to resume operation
Terminate Signal (15)
SIGTERM
• The terminate signal is the default signal for the kill command when
no signal is specified
• The default action for SIGTERM is to gracefully terminate the
process (process group) after clean up
http://Linux4EmbeddedSystems.com
1 of 39

Recommended

Course 102: Lecture 25: Devices and Device Drivers by
Course 102: Lecture 25: Devices and Device Drivers Course 102: Lecture 25: Devices and Device Drivers
Course 102: Lecture 25: Devices and Device Drivers Ahmed El-Arabawy
1.4K views35 slides
Embedded Systems: Lecture 5: A Tour in RTOS Land by
Embedded Systems: Lecture 5: A Tour in RTOS LandEmbedded Systems: Lecture 5: A Tour in RTOS Land
Embedded Systems: Lecture 5: A Tour in RTOS LandAhmed El-Arabawy
1.2K views25 slides
Course 102: Lecture 9: Input Output Internals by
Course 102: Lecture 9: Input Output Internals Course 102: Lecture 9: Input Output Internals
Course 102: Lecture 9: Input Output Internals Ahmed El-Arabawy
1.2K views53 slides
Course 101: Lecture 4: A Tour in RTOS Land by
Course 101: Lecture 4: A Tour in RTOS Land Course 101: Lecture 4: A Tour in RTOS Land
Course 101: Lecture 4: A Tour in RTOS Land Ahmed El-Arabawy
603 views25 slides
Embedded Systems: Lecture 4: Selecting the Proper RTOS by
Embedded Systems: Lecture 4: Selecting the Proper RTOSEmbedded Systems: Lecture 4: Selecting the Proper RTOS
Embedded Systems: Lecture 4: Selecting the Proper RTOSAhmed El-Arabawy
639 views16 slides
Linux for embedded_systems by
Linux for embedded_systemsLinux for embedded_systems
Linux for embedded_systemsVandana Salve
2K views16 slides

More Related Content

What's hot

Course 101: Lecture 2: Introduction to Operating Systems by
Course 101: Lecture 2: Introduction to Operating Systems Course 101: Lecture 2: Introduction to Operating Systems
Course 101: Lecture 2: Introduction to Operating Systems Ahmed El-Arabawy
612 views18 slides
Course 102: Lecture 20: Networking In Linux (Basic Concepts) by
Course 102: Lecture 20: Networking In Linux (Basic Concepts) Course 102: Lecture 20: Networking In Linux (Basic Concepts)
Course 102: Lecture 20: Networking In Linux (Basic Concepts) Ahmed El-Arabawy
1.9K views65 slides
Device Drivers in Linux by
Device Drivers in LinuxDevice Drivers in Linux
Device Drivers in LinuxShreyas MM
1.2K views15 slides
Embedded Linux on ARM by
Embedded Linux on ARMEmbedded Linux on ARM
Embedded Linux on ARMEmertxe Information Technologies Pvt Ltd
10.6K views235 slides
Device Drivers by
Device DriversDevice Drivers
Device DriversKushal Modi
25K views46 slides
Intro to Embedded OS, RTOS and Communication Protocols by
Intro to Embedded OS, RTOS and Communication ProtocolsIntro to Embedded OS, RTOS and Communication Protocols
Intro to Embedded OS, RTOS and Communication ProtocolsEmertxe Information Technologies Pvt Ltd
1.5K views36 slides

What's hot(20)

Course 101: Lecture 2: Introduction to Operating Systems by Ahmed El-Arabawy
Course 101: Lecture 2: Introduction to Operating Systems Course 101: Lecture 2: Introduction to Operating Systems
Course 101: Lecture 2: Introduction to Operating Systems
Ahmed El-Arabawy612 views
Course 102: Lecture 20: Networking In Linux (Basic Concepts) by Ahmed El-Arabawy
Course 102: Lecture 20: Networking In Linux (Basic Concepts) Course 102: Lecture 20: Networking In Linux (Basic Concepts)
Course 102: Lecture 20: Networking In Linux (Basic Concepts)
Ahmed El-Arabawy1.9K views
Device Drivers in Linux by Shreyas MM
Device Drivers in LinuxDevice Drivers in Linux
Device Drivers in Linux
Shreyas MM1.2K views
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi by Ahmed El-Arabawy
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry PiEmbedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Embedded Systems: Lecture 7: Lab 1: Preparing the Raspberry Pi
Ahmed El-Arabawy1.3K views
Unix memory management by Tech_MX
Unix memory managementUnix memory management
Unix memory management
Tech_MX35.5K views
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP by Ahmed El-Arabawy
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi APEmbedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
Embedded Systems: Lecture 8: Lab 1: Building a Raspberry Pi Based WiFi AP
Ahmed El-Arabawy2.3K views
Driver development – memory management by Vandana Salve
Driver development – memory managementDriver development – memory management
Driver development – memory management
Vandana Salve1.9K views
Embedded Systems: Lecture 6: Linux & GNU by Ahmed El-Arabawy
Embedded Systems: Lecture 6: Linux & GNUEmbedded Systems: Lecture 6: Linux & GNU
Embedded Systems: Lecture 6: Linux & GNU
Ahmed El-Arabawy640 views
Introduction to embedded linux device driver and firmware by definecareer
Introduction to embedded linux device driver and firmwareIntroduction to embedded linux device driver and firmware
Introduction to embedded linux device driver and firmware
definecareer8K views
Linux Kernel and Driver Development Training by Stephan Cadene
Linux Kernel and Driver Development TrainingLinux Kernel and Driver Development Training
Linux Kernel and Driver Development Training
Stephan Cadene3.5K views
Device drivers Introduction by vijay selva
Device drivers IntroductionDevice drivers Introduction
Device drivers Introduction
vijay selva257 views
Introduction to char device driver by Vandana Salve
Introduction to char device driverIntroduction to char device driver
Introduction to char device driver
Vandana Salve15.8K views
What is Kernel, basic idea of kernel by Neel Parikh
What is Kernel, basic idea of kernelWhat is Kernel, basic idea of kernel
What is Kernel, basic idea of kernel
Neel Parikh5.8K views

Similar to Course 102: Lecture 19: Using Signals

07 Systems Software Programming-IPC-Signals.pptx by
07 Systems Software Programming-IPC-Signals.pptx07 Systems Software Programming-IPC-Signals.pptx
07 Systems Software Programming-IPC-Signals.pptxKushalSrivastava23
4 views46 slides
Signal Handling in Linux by
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in LinuxTushar B Kute
10.4K views27 slides
13 process management by
13 process management13 process management
13 process managementShay Cohen
447 views10 slides
signals & message queues overview by
signals & message queues overviewsignals & message queues overview
signals & message queues overviewVaishali Dayal
1.8K views16 slides
04_ForkPipe.pptx by
04_ForkPipe.pptx04_ForkPipe.pptx
04_ForkPipe.pptxvnwzympx
4 views20 slides
System Programming Assignment Help- Signals by
System Programming Assignment Help- SignalsSystem Programming Assignment Help- Signals
System Programming Assignment Help- SignalsHelpWithAssignment.com
117 views29 slides

Similar to Course 102: Lecture 19: Using Signals (20)

Signal Handling in Linux by Tushar B Kute
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
Tushar B Kute10.4K views
13 process management by Shay Cohen
13 process management13 process management
13 process management
Shay Cohen447 views
signals & message queues overview by Vaishali Dayal
signals & message queues overviewsignals & message queues overview
signals & message queues overview
Vaishali Dayal1.8K views
04_ForkPipe.pptx by vnwzympx
04_ForkPipe.pptx04_ForkPipe.pptx
04_ForkPipe.pptx
vnwzympx4 views
Concurrency 2010 by 敬倫 林
Concurrency 2010Concurrency 2010
Concurrency 2010
敬倫 林797 views
Interrupt in real time system by ali jawad
Interrupt in real time system Interrupt in real time system
Interrupt in real time system
ali jawad2.9K views
Unix Internals OS Architecture by Khader Shaik
Unix Internals OS ArchitectureUnix Internals OS Architecture
Unix Internals OS Architecture
Khader Shaik3.9K views

More from Ahmed El-Arabawy

C 102 lec_29_what_s_next by
C 102 lec_29_what_s_nextC 102 lec_29_what_s_next
C 102 lec_29_what_s_nextAhmed El-Arabawy
2.4K views9 slides
Course 102: Lecture 28: Virtual FileSystems by
Course 102: Lecture 28: Virtual FileSystems Course 102: Lecture 28: Virtual FileSystems
Course 102: Lecture 28: Virtual FileSystems Ahmed El-Arabawy
1.2K views21 slides
Course 102: Lecture 27: FileSystems in Linux (Part 2) by
Course 102: Lecture 27: FileSystems in Linux (Part 2)Course 102: Lecture 27: FileSystems in Linux (Part 2)
Course 102: Lecture 27: FileSystems in Linux (Part 2)Ahmed El-Arabawy
1K views23 slides
Course 102: Lecture 26: FileSystems in Linux (Part 1) by
Course 102: Lecture 26: FileSystems in Linux (Part 1) Course 102: Lecture 26: FileSystems in Linux (Part 1)
Course 102: Lecture 26: FileSystems in Linux (Part 1) Ahmed El-Arabawy
1.7K views39 slides
Course 102: Lecture 24: Archiving and Compression of Files by
Course 102: Lecture 24: Archiving and Compression of Files Course 102: Lecture 24: Archiving and Compression of Files
Course 102: Lecture 24: Archiving and Compression of Files Ahmed El-Arabawy
1.3K views20 slides
Course 102: Lecture 22: Package Management by
Course 102: Lecture 22: Package Management Course 102: Lecture 22: Package Management
Course 102: Lecture 22: Package Management Ahmed El-Arabawy
2.2K views33 slides

More from Ahmed El-Arabawy(20)

Course 102: Lecture 28: Virtual FileSystems by Ahmed El-Arabawy
Course 102: Lecture 28: Virtual FileSystems Course 102: Lecture 28: Virtual FileSystems
Course 102: Lecture 28: Virtual FileSystems
Ahmed El-Arabawy1.2K views
Course 102: Lecture 27: FileSystems in Linux (Part 2) by Ahmed El-Arabawy
Course 102: Lecture 27: FileSystems in Linux (Part 2)Course 102: Lecture 27: FileSystems in Linux (Part 2)
Course 102: Lecture 27: FileSystems in Linux (Part 2)
Ahmed El-Arabawy1K views
Course 102: Lecture 26: FileSystems in Linux (Part 1) by Ahmed El-Arabawy
Course 102: Lecture 26: FileSystems in Linux (Part 1) Course 102: Lecture 26: FileSystems in Linux (Part 1)
Course 102: Lecture 26: FileSystems in Linux (Part 1)
Ahmed El-Arabawy1.7K views
Course 102: Lecture 24: Archiving and Compression of Files by Ahmed El-Arabawy
Course 102: Lecture 24: Archiving and Compression of Files Course 102: Lecture 24: Archiving and Compression of Files
Course 102: Lecture 24: Archiving and Compression of Files
Ahmed El-Arabawy1.3K views
Course 102: Lecture 22: Package Management by Ahmed El-Arabawy
Course 102: Lecture 22: Package Management Course 102: Lecture 22: Package Management
Course 102: Lecture 22: Package Management
Ahmed El-Arabawy2.2K views
Course 102: Lecture 18: Process Life Cycle by Ahmed El-Arabawy
Course 102: Lecture 18: Process Life CycleCourse 102: Lecture 18: Process Life Cycle
Course 102: Lecture 18: Process Life Cycle
Ahmed El-Arabawy924 views
Course 102: Lecture 17: Process Monitoring by Ahmed El-Arabawy
Course 102: Lecture 17: Process Monitoring Course 102: Lecture 17: Process Monitoring
Course 102: Lecture 17: Process Monitoring
Ahmed El-Arabawy731 views
Course 102: Lecture 16: Process Management (Part 2) by Ahmed El-Arabawy
Course 102: Lecture 16: Process Management (Part 2) Course 102: Lecture 16: Process Management (Part 2)
Course 102: Lecture 16: Process Management (Part 2)
Ahmed El-Arabawy869 views
Course 102: Lecture 14: Users and Permissions by Ahmed El-Arabawy
Course 102: Lecture 14: Users and PermissionsCourse 102: Lecture 14: Users and Permissions
Course 102: Lecture 14: Users and Permissions
Ahmed El-Arabawy1.5K views
Course 102: Lecture 13: Regular Expressions by Ahmed El-Arabawy
Course 102: Lecture 13: Regular Expressions Course 102: Lecture 13: Regular Expressions
Course 102: Lecture 13: Regular Expressions
Ahmed El-Arabawy826 views
Course 102: Lecture 12: Basic Text Handling by Ahmed El-Arabawy
Course 102: Lecture 12: Basic Text Handling Course 102: Lecture 12: Basic Text Handling
Course 102: Lecture 12: Basic Text Handling
Ahmed El-Arabawy919 views
Course 102: Lecture 11: Environment Variables by Ahmed El-Arabawy
Course 102: Lecture 11: Environment VariablesCourse 102: Lecture 11: Environment Variables
Course 102: Lecture 11: Environment Variables
Ahmed El-Arabawy1.9K views
Course 102: Lecture 10: Learning About the Shell by Ahmed El-Arabawy
Course 102: Lecture 10: Learning About the Shell Course 102: Lecture 10: Learning About the Shell
Course 102: Lecture 10: Learning About the Shell
Ahmed El-Arabawy1.2K views
Course 102: Lecture 8: Composite Commands by Ahmed El-Arabawy
Course 102: Lecture 8: Composite Commands Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands
Ahmed El-Arabawy872 views
Course 102: Lecture 7: Simple Utilities by Ahmed El-Arabawy
Course 102: Lecture 7: Simple Utilities Course 102: Lecture 7: Simple Utilities
Course 102: Lecture 7: Simple Utilities
Ahmed El-Arabawy665 views
Course 102: Lecture 5: File Handling Internals by Ahmed El-Arabawy
Course 102: Lecture 5: File Handling Internals Course 102: Lecture 5: File Handling Internals
Course 102: Lecture 5: File Handling Internals
Ahmed El-Arabawy1.3K views
Course 102: Lecture 4: Using Wild Cards by Ahmed El-Arabawy
Course 102: Lecture 4: Using Wild CardsCourse 102: Lecture 4: Using Wild Cards
Course 102: Lecture 4: Using Wild Cards
Ahmed El-Arabawy858 views
Course 102: Lecture 3: Basic Concepts And Commands by Ahmed El-Arabawy
Course 102: Lecture 3: Basic Concepts And Commands Course 102: Lecture 3: Basic Concepts And Commands
Course 102: Lecture 3: Basic Concepts And Commands
Ahmed El-Arabawy896 views

Recently uploaded

iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...Bernd Ruecker
48 views69 slides
2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe by
2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe
2024: A Travel Odyssey The Role of Generative AI in the Tourism UniverseSimone Puorto
13 views61 slides
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading... by
Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading...The Digital Insurer
24 views52 slides
Vertical User Stories by
Vertical User StoriesVertical User Stories
Vertical User StoriesMoisés Armani Ramírez
17 views16 slides
Evolving the Network Automation Journey from Python to Platforms by
Evolving the Network Automation Journey from Python to PlatformsEvolving the Network Automation Journey from Python to Platforms
Evolving the Network Automation Journey from Python to PlatformsNetwork Automation Forum
17 views21 slides
SAP Automation Using Bar Code and FIORI.pdf by
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdfVirendra Rai, PMP
25 views38 slides

Recently uploaded(20)

iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker48 views
2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe by Simone Puorto
2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe
2024: A Travel Odyssey The Role of Generative AI in the Tourism Universe
Simone Puorto13 views
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading... by The Digital Insurer
Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...Webinar : Desperately Seeking Transformation - Part 2:  Insights from leading...
Webinar : Desperately Seeking Transformation - Part 2: Insights from leading...
SAP Automation Using Bar Code and FIORI.pdf by Virendra Rai, PMP
SAP Automation Using Bar Code and FIORI.pdfSAP Automation Using Bar Code and FIORI.pdf
SAP Automation Using Bar Code and FIORI.pdf
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf by Dr. Jimmy Schwarzkopf
STKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdfSTKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdf
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf
PharoJS - Zürich Smalltalk Group Meetup November 2023 by Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi139 views
Business Analyst Series 2023 - Week 3 Session 5 by DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10345 views
Future of AR - Facebook Presentation by ssuserb54b561
Future of AR - Facebook PresentationFuture of AR - Facebook Presentation
Future of AR - Facebook Presentation
ssuserb54b56122 views

Course 102: Lecture 19: Using Signals

  • 1. Linux For Embedded Systems ForArabs Ahmed ElArabawy Course 102: UnderstandingLinux
  • 3. HW Platform UserSpaceKernelSpace Device Drivers Linux Kernel Process 2Process 1 SystemCall Signal Signal User Applications
  • 4. What is a Signal ?? • Linux has two planes: • Kernel Plane: Has access to system and hardware resources • User Plane: For applications to run on • User applications don’t have direct access to the kernel • For a user application to communicate with the kernel, it needs to use a System Call • What about if the kernel needs to send a request/command/notification/… to the user application ?? • The used mechanism is Signals • So, Signals are the mechanism that the kernel uses to communicate with the user applications • Signals are also used between user applications (when one application needs to send a request/command to the other application)
  • 5. What is a Signal ?? • Signal is the communication channel used by the Linux Kernel to communicate with a process running in the user plane • This can be used by the kernel to kill a process, stop it, resume it, notify it of a timer expiry, etc… • Signals can also be sent by another process running in the user plane. Examples, • When the user presses Ctrl-c to terminate the running application, or Ctrl-z to stop it • When the user uses the command “bg” to resume a stopped application in the background, or “fg” to resume it in the foreground • When the user uses the “kill” command to terminate a running application • In all of these cases, a signal is sent to the destination application to achieve the required task • When sent by another user application, it is first passed to the kernel in a system call, which in turn passes it back to the desired process
  • 6. Signals • Signals are described in both Unix Systems and POSIX Standard • There are a table of signals. Each signal has, • Signal number • Signal name (starting with ‘SIG’) • Signals with numbers (1-31) are normal signals • Signals with numbers > 31 are called “Real Time Signals”
  • 7. Passing a Signal to a Process (kill Command) $ kill <pid> $ kill -<Signal number> <pid> $ kill <Signal name> <pid> • The kill command passes a signal to the desired process • Note that, despite that the command name is ‘kill’, it is used for all types of signals even those that have different purposes and not just used to kill processes • If no signal is specified, then the signal SIGTERM is assumed by default • Examples: $ kill -9 1185 $ kill SIGKILL 1185 Identical commands
  • 8. Other ways to “kill”
  • 9. Selecting Processes by name (killall Command) $ killall <Signal> <command name> • The command killall sends the desired signal to all processes running the specified command $ killall chrome • This is useful when you need to pick a process without searching for its pid • Also useful when you need to send the signal to all instances of the running program • If no signal is specified, SIGTERM is assumed • Example: $ killall -9 gedit (this sends a SIGKILL signal to all “gedit” instances)
  • 10. Selecting Processes by Other Criteria (pkill Command) $ pkill <signal> -u <username> $ pkill <siganal> -P <Parent process> $ pkill <signal> <pattern for command> $ pkill <signal> -t <Terminal name> • The pkill command sends the desired signal to processes that match different criteria • Examples: • Send a SIGTERM to all processes owned by user tom $ pkill -u tom • Send a SIGSTOP to all processes running under tty1 terminal $ pkill SIGSTOP -t tty1 • Send SIGTERM to all children of process with pid 1107 $ pkill –P 1107
  • 11. Killing GUI Processes with the Mouse (the xkill Command) $ xkill • The xkill command allows the user to select the process to kill with the mouse • Type the command, then click with the mouse on the UI of the process you want to kill
  • 12. Dealing with Hanging GUI Apps • If a GUI app is hanging, and you need to kill it but unable due to the hanging screen • get into a Linux virtual terminal via, [Ctrl] [Alt] [F1] • From the virtual terminal, issue the proper “kill” command $ killall -9 chrome • After you are done, get back to the tty-7 (for the X window) [Alt] [F7]
  • 14. Real time Signals • Those are the signals with numbers > 31 • They don’t have a pre-defined function, and it is left to the user/developer to use • Defined by the programmer • Guaranteed order of delivery
  • 16. Keyboard Hot keys • Some signals are triggered by the user by typing special hot- keys • When the kernel detects these hot-keys, it sends the proper signal to the running process (or process group) • Examples: • Ctrl-c triggers SIGINT • Ctrl-z triggers SIGTSTP • Ctrl- triggers SIGQUIT
  • 17. Request from another process • A process can trigger the kernel to send a signal to another process by issuing a system call • Examples for this is when the user issue the commands from the terminal $ kill (triggers sending the desired signal, SIGTERM by default) $ fg (triggers sending the signal SIGCONT) $ bg (triggers sending the signal SIGCONT) • A process can also trigger sending a signal to another process programmatically via some special function API
  • 18. A Hardware Event • Hardware events can trigger the kernel to send a signal to the proper process • An example of that, when the kernel detects a modem disconnection, it sends the signal SIGHUP to all processes using this session
  • 19. Death of a Related Process • When a process terminates, the kernel sends the following signals, • A SIGHUP is sent to all children processes to inform them about the death of their parent process • A SIGCHLD is sent to the parent of the terminated process to inform it about the death of a child process
  • 20. Errors and Exceptions • Some exceptions within the process triggers the kernel to send signals to the process, • Examples: • If the process tries to execute an illegal instruction, the kernel is triggered to send SIGILL signal to it • If the process suffers from a Floating Point Exception, the kernel sends it a SIGFPE • If the process tries to write to a pipe while the other end is not listening, the kernel sends it a SIGPIPE • If the process accesses a Null Pointer (or other memory exceptions), the kernel sends it SIGSEGV • If a process running in the background tries to read/write to the standard input/output, the kernel will send it a SIGTTIN/SIGTTOU • If the process exits on error using the abort(), the kernel will send it SIGABRT
  • 21. Notification By the Kernel • Sometimes, a process would ask the kernel for some notification when some event happens • Normally, the process will be waiting for this notification • The kernel delivers this notification via a signal • Examples: • The kernel will use the signal SIGALRM and SIGVTALRM to notify the process of a timer expiry • A process can ask the kernel for a notification whenever a file handler has been accessed
  • 23. Default Action • When a process receives a signal, the normal behavior is to execute the default action • Each signal has its own default action that is part of its description • Examples: • Some signals have the default action of termination (graceful termination). This is applicable for several signals such as SIGTERM, SIGPIPE, SIGUSR1, SIGUSR2, … • Some signals have the default action of termination with generation of a core dump (stack trace for further debugging). This action is useful for signals that indicate some error in the process operation such as SIGSEGV, SIGILL, SIGABRT, SIGQUIT … • Some signals have the default action of being ignored by the process such as SIGCHLD • Some signals does not allow the process to change its default action. This means that the default action is the only allowed action. This includes the SIGSTOP which force the process to stop (suspend) and SIGKILL which force the process to terminate immediately
  • 24. Ignoring a Signal • A process can set itself to ignore certain incoming signals, accordingly no action is performed when these signals are received • Some signals can not be ignored such as SIGSTOP, and SIGKILL
  • 25. Blocking a Signal • A process can set itself to block a certain set of signals • Blocking a signal means, keeping it in a queue for later handling (when the blocking is taken away) • This is used when the process is executing some code that should not be interrupted by the incoming signals • Every process has a mask of which signals to block • Only one signal of a certain type will be blocked (other instances will be dropped). • This last rule does not apply to real time signals (signals with numbers > 31) • Some signals can not be blocked (SIGSTOP and SIGKILL)
  • 26. Catching the Signal • A process can “catch the signal” before its default handler executes, and pass it to a special handler • This is specially useful for some of the signals that have their default action of being ignored (such as SIGCHLD) • Some signals can not be caught (SIGSTOP and SIGKILL)
  • 28. Hang-Up Signal (1) SIGHUP • The hang-up signal is used by the kernel to inform the processes running in a tty session when the session closes. This can be due to, • The modem for the session disconnects • The network connection used to connect to the machine breaks • It is commonly used to request daemon processes to re-read their configuration file • Default action for this signal for the process to terminate
  • 29. Interrupt Signal (2) SIGINT • Interrupt signal is triggered when the user uses the hot-key Ctrl-c • This causes the kernel to send SIGINT to the currently running process group • The default action for this signal is for the process to terminate
  • 30. Quit Signal (3) SIGQUIT • The quit signal is triggered by the user when he uses the hot- keys Ctrl- • This triggers the kernel to send SIGQUIT to all processes in the currently running process group • Default action is for the process to quit and generate a core dump file (to be used for debugging)
  • 31. Illegal Command Signal (4) SIGILL • The illegal command signal is triggered when the process program tries to execute an invalid instruction • This may occur in the case of memory corruption of the code area or in stack memory • Default action is to terminate and generate a core dump file
  • 32. Abort Signal (6) SIGABRT • The abort signal is sent by the kernel to the process when it calls the abort() function to indicate an erroneous exit of the program • The default action is to terminate and generate a core dump file
  • 33. Trap Signal (5) SIGTRAP • The trap signal is normally used with debuggers and program tracers
  • 34. Bus Error Signal (7) SIGBUS • The bus error signal is triggered by an error in the program in which an attempt was made to access memory incorrectly • This can be caused by alignment errors in memory access • The default action is to terminate and generate a core dump file
  • 35. Floating Point Exception (8) SIGFPE • The floating point exception signal is sent by the kernel to the process when it falls into an exception while running a floating point instruction • The default action is to terminate and generate a core dump file
  • 36. Kill Signal (9) SIGKILL • The process was explicitly killed by a request from a different process • The request is to kill the process immediately and forcibly without giving it a chance to clean-up • A process can not ignore, block, or catch this signal
  • 37. Stop Signals (17 &18) SIGSTOP & SIGTSTP • SIGTSTP (terminal Stop) is triggered by pressing Ctrl-z • This signal causes the process (or process group) to suspend operation • SIGSTOP has the same effect with the only difference that it can not be ignored/blocked/caught by the process while this is possible with SIGTSTP • Upon stopping due to either SIGSTOP & SIGTSTP, the process awaits SIGCONT to resume operation
  • 38. Terminate Signal (15) SIGTERM • The terminate signal is the default signal for the kill command when no signal is specified • The default action for SIGTERM is to gracefully terminate the process (process group) after clean up