SlideShare a Scribd company logo
Signals



© 2010 Anil Kumar Pugalia <email@sarika-pugs.com>
               All Rights Reserved.
What to Expect?
W's of a Signal
Signals in Linux
Signal Handlers
Signal related Functions
Program Exit Codes




        © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   2
                       All Rights Reserved.
Asynchronous Communication
In Kernel Space
  Interrupts (Hardware to Software)
  Soft Irqs (Software to Software)
What about User Space?
Moreover, between User Space & Kernel Space
Though only software to software requirement
Do System Calls take care of that?
Communication: Yes, but they are Synchronous
Asynchronous Solution is Signals
          © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   3
                         All Rights Reserved.
What is a Signal?
Asynchronous Trigger from Kernel Space or a Process to
another Process
Mechanisms for communicating with and manipulating
processes in Linux
Handled in User Space like Interrupts in Kernel Space
  Remains Pending on Masking. Once handled,
  Immediate processing without finishing the current function or
  even the current line of code. Possible options
    Ignore
    Execute the Default Handler
    Execute the User specified Handler



              © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>    4
                             All Rights Reserved.
Signals in Linux
Specified by a Number (currently 1-31)
Also, referred by Human readable Names
On Shell
  Type 'kill -l'
  Signal Operations: Using kill
In Programming / C
  Header: /usr/include/bits/signum.h
  Signal Operations: Shall discuss further
           © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   5
                          All Rights Reserved.
Signals Originate from
Kernel Space
 Typically on an illegal operation. Namely,
   SIGBUS (bus error),
   SIGSEGV (segmentation violation)
   SIGFPE (floating point exception)
 Others like SIGCHLD
A Process
 Synchronously using kill system call


        © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   6
                       All Rights Reserved.
Signal Handler Registration
Using signal(sig_no, handler)
  Non-portable except for SIG_DFL & SIG_IGN as handlers
Using sigaction(sig_no, action, old_action)
  action & new_action are pointers to sigaction structures
  action contains the desired disposition
  old_action receives the previous disposition
  Their sa_handler field takes one of the three values
    SIG_DFL
    SIG_IGN
    A pointer to a signal handler function
Signal Hanlder Prototype
  void signal_handler(int sig_no);
Exceptions to Registration: SIGKILL, SIGSTOP
                © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   7
                               All Rights Reserved.
Sensitivity of Signal Handlers
Time Sensitive like Interrupt Handlers
Perform the minimum work necessary &
  Return control to the main program, Or
  Terminate the program
Mostly, they just record the signal
occurrence
  And the main program does the processing



         © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   8
                        All Rights Reserved.
Signal in a Signal
Signal interrupting a signal handler
  Rare Occurrence but could be a Problem
  Difficult to diagnose & debug
  Assigning to globals can be dangerous
Solutions
  Self is restricted by default
    Unless SA_NODEFER flag is used
  Disable other signals in signal handlers
    By setting sa_mask using sigsetops during registration
  Protect the global variables by using sig_atomic_t type

            © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   9
                           All Rights Reserved.
Signal related Functions
raise
  Sends a signal to calling thread
kill, killpg
  Sends a signal to a specified process or group of processes
tkill, tgkill, pthread_kill
  Sends a signal to a specified thread
sigqueue
  Sends a real-time signal with accompanying data to a specified process
pause, sigsuspend
  Suspends the calling thread until delivery of a signal whose action is
  either to execute a signal handler or to terminate the process
sigwaitinfo, sigtimedwait, sigwait, signalfd
  Accepts signals synchronously
                © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>          10
                               All Rights Reserved.
Signal related Functions ...
alarm(unsigned int secs);
  Schedules an alarm signal (SIGALRM) to be
  sent to the calling process after “secs”
  seconds
  Should not be mixed with setitimer / sleep
abort();
  Causes abnormal process termination by
  raising abort signal (SIGABRT)


           © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   11
                          All Rights Reserved.
Process Termination Revisited
Normal: System Call 'exit' is invoked
  With zero – Success Exit
  With non-zero value – Error Exit
Abnormal: A Signal has terminated it
  From Kernel Space for a Bug – Fatal Exit
    SIGBUS
    SIGSEGV
    SIGFPE
  From a Process – Kill Exit
    SIGINT (Ctrl + C)
    SIGTERM
    SIGABRT / abort()
    ...
              © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   12
                             All Rights Reserved.
Exit Codes Decoded
8-bit unsigned integer
Range: 0-255
Normal Termination (using exit(value);)
  “value” should be always between 0 and 127
  Exit code is “value”
Abnormal Termination (by signal)
  Exit code is 128 + “signal”


         © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   13
                        All Rights Reserved.
What all have we learnt?
W's of a Signal
Signals in Linux
Signal Handlers
Signal related Functions
Program Exit Codes




        © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   14
                       All Rights Reserved.
Any Queries?




© 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   15
               All Rights Reserved.

More Related Content

PDF
Real Time Systems
PDF
Block Drivers
PDF
PDF
Introduction to Linux Drivers
PDF
Linux Network Management
PDF
Linux Kernel Overview
PDF
Kernel Debugging & Profiling
Real Time Systems
Block Drivers
Introduction to Linux Drivers
Linux Network Management
Linux Kernel Overview
Kernel Debugging & Profiling

What's hot (20)

PDF
Kernel Debugging & Profiling
PDF
Architecture Porting
PDF
Interrupts
PDF
Kernel Timing Management
PDF
Introduction to Linux
PDF
Embedded Storage Management
PDF
Mobile Hacking using Linux Drivers
PDF
Embedded C
PDF
Synchronization
PDF
Embedded Software Design
PDF
Introduction to Linux
PDF
BeagleBoard-xM Booting Process
PDF
Kernel Programming
PDF
Linux User Space Debugging & Profiling
PDF
Video Drivers
PDF
PDF
BeagleBone Black Bootloaders
Kernel Debugging & Profiling
Architecture Porting
Interrupts
Kernel Timing Management
Introduction to Linux
Embedded Storage Management
Mobile Hacking using Linux Drivers
Embedded C
Synchronization
Embedded Software Design
Introduction to Linux
BeagleBoard-xM Booting Process
Kernel Programming
Linux User Space Debugging & Profiling
Video Drivers
BeagleBone Black Bootloaders
Ad

Viewers also liked (16)

PDF
System Calls
PDF
Inter Process Communication
PDF
Network Drivers
PDF
References
PDF
Character Drivers
PDF
Board Bringup
PDF
Bootloaders
PDF
Shell Scripting
PDF
Functional Programming with LISP
PDF
gcc and friends
PDF
Linux File System
PDF
Linux Memory Management
PDF
PCI Drivers
PDF
File Systems
PDF
BeagleBoard-xM Bootloaders
PDF
BeagleBone Black Bootloaders
System Calls
Inter Process Communication
Network Drivers
References
Character Drivers
Board Bringup
Bootloaders
Shell Scripting
Functional Programming with LISP
gcc and friends
Linux File System
Linux Memory Management
PCI Drivers
File Systems
BeagleBoard-xM Bootloaders
BeagleBone Black Bootloaders
Ad

Similar to Signals (20)

PDF
Signal Handling in Linux
PPTX
Unixppt (1) (1).pptx
PPTX
07 Systems Software Programming-IPC-Signals.pptx
PDF
Course 102: Lecture 19: Using Signals
DOCX
AOS Chapter 6 for internal.docx
PPT
signal power point presentation it is useful
PPTX
Unix signals
PPT
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
PPTX
PDF
Usp notes unit6-8
PDF
System Programming Assignment Help- Signals
PPT
Traffic Signal Synchronization Operating Systems
PPT
Unit 3.2 Process Control.ppt (Process Control and Scheduling)
PPTX
Process management
DOCX
Write a program called signal.c that performs the functions of t.docx
PPT
signals & message queues overview
PDF
Describe how to use sanction such that a process can send a payload w.pdf
PDF
Multithreaded Programming Part- III.pdf
PPTX
04_ForkPipe.pptx
PDF
Ctrl-C redesign for gcc cauldron in 2022 in prague
Signal Handling in Linux
Unixppt (1) (1).pptx
07 Systems Software Programming-IPC-Signals.pptx
Course 102: Lecture 19: Using Signals
AOS Chapter 6 for internal.docx
signal power point presentation it is useful
Unix signals
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
Usp notes unit6-8
System Programming Assignment Help- Signals
Traffic Signal Synchronization Operating Systems
Unit 3.2 Process Control.ppt (Process Control and Scheduling)
Process management
Write a program called signal.c that performs the functions of t.docx
signals & message queues overview
Describe how to use sanction such that a process can send a payload w.pdf
Multithreaded Programming Part- III.pdf
04_ForkPipe.pptx
Ctrl-C redesign for gcc cauldron in 2022 in prague

More from Anil Kumar Pugalia (9)

PDF
File System Modules
PDF
PDF
System Calls
PDF
Playing with R L C Circuits
PDF
Audio Drivers
PDF
Power of vi
PDF
"make" system
PDF
Hardware Design for Software Hackers
PDF
RPM Building
File System Modules
System Calls
Playing with R L C Circuits
Audio Drivers
Power of vi
"make" system
Hardware Design for Software Hackers
RPM Building

Recently uploaded (20)

PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Encapsulation theory and applications.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The AUB Centre for AI in Media Proposal.docx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
NewMind AI Weekly Chronicles - August'25 Week I
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf
Network Security Unit 5.pdf for BCA BBA.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Building Integrated photovoltaic BIPV_UPV.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
A Presentation on Artificial Intelligence
Encapsulation_ Review paper, used for researhc scholars
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced methodologies resolving dimensionality complications for autism neur...
Encapsulation theory and applications.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication

Signals

  • 1. Signals © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> All Rights Reserved.
  • 2. What to Expect? W's of a Signal Signals in Linux Signal Handlers Signal related Functions Program Exit Codes © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 2 All Rights Reserved.
  • 3. Asynchronous Communication In Kernel Space Interrupts (Hardware to Software) Soft Irqs (Software to Software) What about User Space? Moreover, between User Space & Kernel Space Though only software to software requirement Do System Calls take care of that? Communication: Yes, but they are Synchronous Asynchronous Solution is Signals © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 3 All Rights Reserved.
  • 4. What is a Signal? Asynchronous Trigger from Kernel Space or a Process to another Process Mechanisms for communicating with and manipulating processes in Linux Handled in User Space like Interrupts in Kernel Space Remains Pending on Masking. Once handled, Immediate processing without finishing the current function or even the current line of code. Possible options Ignore Execute the Default Handler Execute the User specified Handler © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 4 All Rights Reserved.
  • 5. Signals in Linux Specified by a Number (currently 1-31) Also, referred by Human readable Names On Shell Type 'kill -l' Signal Operations: Using kill In Programming / C Header: /usr/include/bits/signum.h Signal Operations: Shall discuss further © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 5 All Rights Reserved.
  • 6. Signals Originate from Kernel Space Typically on an illegal operation. Namely, SIGBUS (bus error), SIGSEGV (segmentation violation) SIGFPE (floating point exception) Others like SIGCHLD A Process Synchronously using kill system call © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 6 All Rights Reserved.
  • 7. Signal Handler Registration Using signal(sig_no, handler) Non-portable except for SIG_DFL & SIG_IGN as handlers Using sigaction(sig_no, action, old_action) action & new_action are pointers to sigaction structures action contains the desired disposition old_action receives the previous disposition Their sa_handler field takes one of the three values SIG_DFL SIG_IGN A pointer to a signal handler function Signal Hanlder Prototype void signal_handler(int sig_no); Exceptions to Registration: SIGKILL, SIGSTOP © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 7 All Rights Reserved.
  • 8. Sensitivity of Signal Handlers Time Sensitive like Interrupt Handlers Perform the minimum work necessary & Return control to the main program, Or Terminate the program Mostly, they just record the signal occurrence And the main program does the processing © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 8 All Rights Reserved.
  • 9. Signal in a Signal Signal interrupting a signal handler Rare Occurrence but could be a Problem Difficult to diagnose & debug Assigning to globals can be dangerous Solutions Self is restricted by default Unless SA_NODEFER flag is used Disable other signals in signal handlers By setting sa_mask using sigsetops during registration Protect the global variables by using sig_atomic_t type © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 9 All Rights Reserved.
  • 10. Signal related Functions raise Sends a signal to calling thread kill, killpg Sends a signal to a specified process or group of processes tkill, tgkill, pthread_kill Sends a signal to a specified thread sigqueue Sends a real-time signal with accompanying data to a specified process pause, sigsuspend Suspends the calling thread until delivery of a signal whose action is either to execute a signal handler or to terminate the process sigwaitinfo, sigtimedwait, sigwait, signalfd Accepts signals synchronously © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 10 All Rights Reserved.
  • 11. Signal related Functions ... alarm(unsigned int secs); Schedules an alarm signal (SIGALRM) to be sent to the calling process after “secs” seconds Should not be mixed with setitimer / sleep abort(); Causes abnormal process termination by raising abort signal (SIGABRT) © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 11 All Rights Reserved.
  • 12. Process Termination Revisited Normal: System Call 'exit' is invoked With zero – Success Exit With non-zero value – Error Exit Abnormal: A Signal has terminated it From Kernel Space for a Bug – Fatal Exit SIGBUS SIGSEGV SIGFPE From a Process – Kill Exit SIGINT (Ctrl + C) SIGTERM SIGABRT / abort() ... © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 12 All Rights Reserved.
  • 13. Exit Codes Decoded 8-bit unsigned integer Range: 0-255 Normal Termination (using exit(value);) “value” should be always between 0 and 127 Exit code is “value” Abnormal Termination (by signal) Exit code is 128 + “signal” © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 13 All Rights Reserved.
  • 14. What all have we learnt? W's of a Signal Signals in Linux Signal Handlers Signal related Functions Program Exit Codes © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 14 All Rights Reserved.
  • 15. Any Queries? © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 15 All Rights Reserved.