SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
SYSTEMS PROGRAMMING
LECTURE 5
SIGNALS
SIGNALS
• Signals are software interrupts that give us a way to
handle asynchronous events
• Signals can be received by or sent to any existing process
• It provides a flexible way to stop execution of a process
or to tell it that something has happened, requiring
attention
• All signals are integers which are #define’d as
constants starting with the characters SIG inside the
header signal.h
• All signals received by a process have a default action
associated with them, which are: ignore, terminate with
core or stop
SIGNALS
• Signals are of different types:
• Kill process (SIGTERM, SIGABRT, SIGKILL,
SIGQUIT, SIGSEGV)
• Suspend process (SIGSTOP, SIGSTP)
• Resume process (SIGCNT)
• Notify ^C (SIGINT)
• Memory violation (SIGSEGV)
• Child process termination (SIGCHLD)
• User defined events (SIGUSR1, SIGUSR2)
SIGNALS
SIGABRT
SIGIOT
SIGTSTP
SIGALRM
SIGKILL
SIGTTIN
SIGBUS
SIGPIPE
SIGTTOU
SIGCHLD
SIGPOLL
SIGURG
SIGCONT
SIGPROF
SIGUSR1
SIGEMT
SIGPWR
SIGUSR2
SIGFPE
SIGQUIT
SIGVTALRM
SIGHUP
SIGSEGV
SIGWINCH
SIGILL
SIGSTOP
SIGXCPU
SIGINFO
SIGSYS
SIGZFSZ
SIGINT
SIGTERM
SIGIO
SIGTRAP
SIGNALS
• Signals can occur due to one of the following events:
• Certain terminal keys (ex. ^C)
• Hardware exception (ex: divide by zero)
• Software events
• The kill() system call
• A process has one of three options on how to react to a
signal arriving
• Ignore it (except for SIGKILL and SIGSTOP)
• Perform the default action
• Catch the signal using a user defined function
SIGNAL HANDLING
#include <signal.h>
void (*signal(int signo,
void(*func)(int)))(int)
return a pointer to previous
signal handler, SIG_ERR on error
- or -
typedef void sigfunc(int)
sigfunc *signal(int, sigfunc *)
SIGNAL HANDLING
• If we register a signal handler, when that signal is
received execution is temporarily suspended until
the signal handler returns
• signal() lets us register signal handlers for
specific signals
• func may be one of: SIG_IGN, SIG_DFL, or a
function that takes an integer and returns nothing
• SIGKILL and SIGSTOP should not be trapped
and many system do not let you trap them
SIGNAL HANDLING
• On most system, when a user defined signal handler
executes, the signal handling disposition is reset and
has to be set again
• Most slow system calls on System V implementation
trap signals by returning an error value of -1 and
setting errno to EINTR
• One has to be careful about system calls which are
interrupted by a signal and the action taken inside
the signal handler. Only re-entrant functions can be
used in the signal handler but the errno variable
might still be corrupted
REENTRANT FUNCTIONS
• When a signal is being handled by a process the
normal sequence of instruction being executed is
temporarily interrupted by the signal handler
• If the signal handler returns then the normal
instruction sequence that the process was executing
continues (similar to hardware interrupt)
• In the signal handler we can’t tell where the process
was executing (say, if it was malloc’ing)
• To conserve the state of the system, signal handlers
should only use re-entrant system calls
REENTRANT FUNCTIONS
• Non re-entrant functions either:
• Use static data structures
• Call malloc or free
• Are part of the standard I/O library (printf is
not guaranteed to produce expected results)
• There is only one errno variable per thread, and
function calls in the signal handler might modify its
value
TERMINOLOGY
• A signal is generated for a process when an event
causes that signal to occur
• A signal is delivered to a process when the action for
a signal is taken
• During the time between signal generation and
delivery the signal is pending
• A process has the option of blocking delivery of a
signal. If the action is either default or caught,
signal remain pending until the process unblocks or
changes the action to ignore the signal
TERMINOLOGY
• The system determines what to do with a blocked signal
when the signal is delivered, not generated
• This allows the process to change the action for the
signal before it’s delivered
• signpending function can be called to determine
which signal are blocked and pending
• If blocked signal is generated more than once, most
systems deliver the signal once (not queued)
• If more than one signal is ready to be delivered, signal
related to the current state of the process is delivered
before others (eg SIGSEGV)
SIGNAL HANDLING
static void sig_usr(int);
int main(void)
{
if (signal(SIGUSR1, sig_usr) == SIG_ERR)
fprintf(stderr, “Can’t catch SIGUSR1”);
for ( ; ; )
pause();
}
static void sig_usr(int signo)
{
if (signo == SIGUSR1)
printf(“Received SIGUSR1n”);
else
fprintf(stderr, “Received signal %dn”, signo);
}
SENDING SIGNALS
• From the command prompt one can send a signal to
a process using:
kill -<SIGNAL> <pid>
• Default signal is SIGTERM
• kill() is used to send a signal to a specific
process while raise() sends a signal to itself
• A superuser can send a signal to any process
• A process can send signals only to process with the
same real or effective UID
SIGNAL FUNCTIONS
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int signo);
int raise(int signo);
return 0 if OK, -1 on error
unsigned int alarm(unsigned int seconds);
return 0 or number of seconds until
previously set alarm
int pause(void)
return -1 with errno=EINTR
SENDING SIGNALS
• Four different condition for the pid argument:
• pid > 0, signal sent to process with ID pid
• pid == 0, signal sent to all processes whose
process group equals the process group ID of the
sender
• pid < 0, signal sent to all processes whose
group ID equals the absolute value of pid
• pid == 1, signal sent to all processes on the
system
SIGNAL FUNCTIONS
• A process needs permission to send a signal to
another process
• pause() blocks the running process until a signal
handler returns
• Can be used as a sort of Inter Process
Communication
• alarm() allows us to set a timer that will expire
at a specified time in the future. When timer expires
the SIGALRM signal is generated. Default action is
to terminate the process
SIGNAL MASKS
• Signal sent represents a set of signals
#include <signal.h>
int sigemptyset(sigset_t *set);
int sigfillset(sigset_t *set);
int sigaddset(sigset_t *set, int signo);
int sigdelset(sigset_t *set, int signo);
Return 0 if OK, 1 on error
int sigismember(const sigset_t *set,
int signo)
Return 1 if true/error, 0 if false
SIGNAL MASKS
• Signals can be blocked so that critical sections are
not interrupted
• Every process maintains a signal mask telling which
signals are blocked
• Signal masks are stored in the data type
sigset_t
• This is guaranteed to be able to hold all signals
supported by the system
• sigemptyset() or sigfillset() must be
called at least once
MASKING SIGNALS
#include <signal.h>
int sigprocmask(int how,
const sigset_t *set,
sigset_t *oset);
Returns 0 if OK, -1 on error
How value:
SIG_BLOCK (union)
SIG_UNBLOCK (intersection)
SIG_SETMASK (equality)
SIGNAL MASKS
• If oset is NULL, the old signal mask is returned in it
• set defines the signals we want to block or unblock
• If there are any pending signals and we unblock it with
sigprocmask(), one these signals is received
before sigprocmask() returns
• Blocking signal make sure critical sections are executed
atomically
• We could call pause() if we want to wait for a signal
after unblocking the signal mask, however this could
make the process wait forever
SIGNAL MASKS
• Tells us what signals that are blocked frim delivery
are currently pending
• The list of signals is returned inside set
• sigismember() can be used to find out what
signals are present in set
#include <signal.h>
int sigpending(sigset_t *set);
returns 0 if OK, -1 on error
SIGNAL MASKS
See sigprocmask.c
SIGACTION
#include <signal.h>
int sigaction(int signo,
const struct sigaction *act,
const struct sigaction *oact)
return -1 on error
struct sigaction {
void (*sa_handler)();
sigset_t sa_mask;
int sa_flags;
};
SA_NOCLDSTOP
SA_RESTART
SA_ONSTACK
SA_NODEFER
SA_RESETHAND
SA_SIGINGFO
SIGNAL MASKS
• Allows us to examine and/or modify the action
associated with a particular signal
• Supersedes the signal function from earlier systems
• If the act pointer is not NULL, we are modifying the
action
• If the oact pointer is not NULL the system returns the
previous action for the signal
• When changing the action of a signal the
sa_handler field contains the address of a signal-
catching function and sa_mask specifies a signal set
that are added to the mask before function is called.
SIGSUSPEND
1. Sets the signal mask to sigmask
2. Calls the pause function
3. If pause() returns, signal mask is set back to its
original value
• All above steps are guaranteed to be performed
atomically and thus get no lost signals
#include <signal.h>
int sigsuspend(const sigset_t *sigmask);
returns -1 with errno set to EINT
SIGSUSPEND
See critical_sections.c
SLEEP
• Function causes the calling process to be suspended
until:
• Amount of wall clock time has elapsed
• A signal is caught by the process and the signal
handler returns
• In the latter case, the number of unslept seconds is
returned
#include <unistd.h>
unsigned int sleep(unsigned int secs);
SIGSUSPEND
1. Sets the signal mask to sigmask
2. Calls the pause function
3. If pause() returns, signal mask is set back to its
original value
• All above steps are guaranteed to be performed
atomically and thus get no lost signals
#include <signal.h>
int sigsuspend(const sigset_t *sigmask);
returns -1 with errno set to EINT

Contenu connexe

Tendances

06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINTAnne Lee
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process SynchronizationSonali Chauhan
 
Lec11 semaphores
Lec11 semaphoresLec11 semaphores
Lec11 semaphoresanandammca
 
Comparison of PID controller tuning methods for unstable systems
Comparison of PID controller tuning methods for unstable systemsComparison of PID controller tuning methods for unstable systems
Comparison of PID controller tuning methods for unstable systemsNidhi Yadav
 
ISA Effective Use of PID Controllers 3-7-2013
ISA Effective Use of PID Controllers 3-7-2013ISA Effective Use of PID Controllers 3-7-2013
ISA Effective Use of PID Controllers 3-7-2013Sarod Paichayonrittha
 
Ppt on simulink by vikas gupta
Ppt on simulink by vikas guptaPpt on simulink by vikas gupta
Ppt on simulink by vikas guptaVikas Gupta
 
Pid controllers
Pid controllersPid controllers
Pid controllersHussain K
 
Synchronization
SynchronizationSynchronization
SynchronizationMohd Arif
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationWayne Jones Jnr
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitorssgpraju
 
Pid controller tuning using fuzzy logic
Pid controller tuning using fuzzy logicPid controller tuning using fuzzy logic
Pid controller tuning using fuzzy logicRoni Roshni
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)Nagarajan
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronizationvinay arora
 

Tendances (20)

06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
Lec11 semaphores
Lec11 semaphoresLec11 semaphores
Lec11 semaphores
 
Comparison of PID controller tuning methods for unstable systems
Comparison of PID controller tuning methods for unstable systemsComparison of PID controller tuning methods for unstable systems
Comparison of PID controller tuning methods for unstable systems
 
PID Controllers
PID ControllersPID Controllers
PID Controllers
 
ISA Effective Use of PID Controllers 3-7-2013
ISA Effective Use of PID Controllers 3-7-2013ISA Effective Use of PID Controllers 3-7-2013
ISA Effective Use of PID Controllers 3-7-2013
 
Ppt on simulink by vikas gupta
Ppt on simulink by vikas guptaPpt on simulink by vikas gupta
Ppt on simulink by vikas gupta
 
Os3
Os3Os3
Os3
 
Pid controller bp ganthia
Pid controller bp ganthiaPid controller bp ganthia
Pid controller bp ganthia
 
Pid controllers
Pid controllersPid controllers
Pid controllers
 
Synchronization
SynchronizationSynchronization
Synchronization
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
 
PLC LADDER DIAGRAM
PLC LADDER DIAGRAMPLC LADDER DIAGRAM
PLC LADDER DIAGRAM
 
Pid controller tuning using fuzzy logic
Pid controller tuning using fuzzy logicPid controller tuning using fuzzy logic
Pid controller tuning using fuzzy logic
 
PID Tuning
PID TuningPID Tuning
PID Tuning
 
Process synchronization(deepa)
Process synchronization(deepa)Process synchronization(deepa)
Process synchronization(deepa)
 
Ep 5512 lecture-03
Ep 5512 lecture-03Ep 5512 lecture-03
Ep 5512 lecture-03
 
OSCh7
OSCh7OSCh7
OSCh7
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 

En vedette (14)

Network Programming Assignment Help
Network Programming Assignment HelpNetwork Programming Assignment Help
Network Programming Assignment Help
 
System Programming - Interprocess communication
System Programming - Interprocess communicationSystem Programming - Interprocess communication
System Programming - Interprocess communication
 
Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715
 
Cash Dividend Assignment Help
Cash Dividend Assignment Help Cash Dividend Assignment Help
Cash Dividend Assignment Help
 
Scoping
ScopingScoping
Scoping
 
Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715Fundamentals of Transport Phenomena ChE 715
Fundamentals of Transport Phenomena ChE 715
 
Manufacturing Process Selection and Design
Manufacturing Process Selection and DesignManufacturing Process Selection and Design
Manufacturing Process Selection and Design
 
Get 24/7 Reliable Engineering Assignment Help, 100% error free, money back g...
Get 24/7 Reliable Engineering  Assignment Help, 100% error free, money back g...Get 24/7 Reliable Engineering  Assignment Help, 100% error free, money back g...
Get 24/7 Reliable Engineering Assignment Help, 100% error free, money back g...
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Factorial Experiments
Factorial ExperimentsFactorial Experiments
Factorial Experiments
 
Scope
ScopeScope
Scope
 
Game theory Bayesian Games at HelpWithAssignment.com
Game theory Bayesian Games at HelpWithAssignment.comGame theory Bayesian Games at HelpWithAssignment.com
Game theory Bayesian Games at HelpWithAssignment.com
 
Tips for writing a good biography
Tips for writing a good biographyTips for writing a good biography
Tips for writing a good biography
 
Customer relationship management
Customer relationship managementCustomer relationship management
Customer relationship management
 

Similaire à System Programming Assignment Help- Signals

Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in LinuxTushar B Kute
 
AOS Chapter 6 for internal.docx
AOS Chapter 6 for internal.docxAOS Chapter 6 for internal.docx
AOS Chapter 6 for internal.docxKomlikaTaru
 
07 Systems Software Programming-IPC-Signals.pptx
07 Systems Software Programming-IPC-Signals.pptx07 Systems Software Programming-IPC-Signals.pptx
07 Systems Software Programming-IPC-Signals.pptxKushalSrivastava23
 
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTESSOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES4SF20CS057LESTONREGO
 
Course 102: Lecture 19: Using Signals
Course 102: Lecture 19: Using Signals Course 102: Lecture 19: Using Signals
Course 102: Lecture 19: Using Signals Ahmed El-Arabawy
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linuxgeeksrik
 
Concurrency 2010
Concurrency 2010Concurrency 2010
Concurrency 2010敬倫 林
 
signals & message queues overview
signals & message queues overviewsignals & message queues overview
signals & message queues overviewVaishali Dayal
 
Inter process communication
Inter process communicationInter process communication
Inter process communicationGift Kaliza
 
Lecture_6_Process.ppt
Lecture_6_Process.pptLecture_6_Process.ppt
Lecture_6_Process.pptwafawafa52
 
The Ring programming language version 1.6 book - Part 83 of 189
The Ring programming language version 1.6 book - Part 83 of 189The Ring programming language version 1.6 book - Part 83 of 189
The Ring programming language version 1.6 book - Part 83 of 189Mahmoud Samir Fayed
 
Basics of Automation, PLC and SCADA
Basics of Automation, PLC and SCADABasics of Automation, PLC and SCADA
Basics of Automation, PLC and SCADAIndira Kundu
 
Operating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphoresOperating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphoresVaibhav Khanna
 

Similaire à System Programming Assignment Help- Signals (20)

Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
 
AOS Chapter 6 for internal.docx
AOS Chapter 6 for internal.docxAOS Chapter 6 for internal.docx
AOS Chapter 6 for internal.docx
 
07 Systems Software Programming-IPC-Signals.pptx
07 Systems Software Programming-IPC-Signals.pptx07 Systems Software Programming-IPC-Signals.pptx
07 Systems Software Programming-IPC-Signals.pptx
 
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTESSOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
 
Usp notes unit6-8
Usp notes unit6-8Usp notes unit6-8
Usp notes unit6-8
 
Unixppt (1) (1).pptx
Unixppt (1) (1).pptxUnixppt (1) (1).pptx
Unixppt (1) (1).pptx
 
Course 102: Lecture 19: Using Signals
Course 102: Lecture 19: Using Signals Course 102: Lecture 19: Using Signals
Course 102: Lecture 19: Using Signals
 
Timers in Unix/Linux
Timers in Unix/LinuxTimers in Unix/Linux
Timers in Unix/Linux
 
Unix presentation.pdf
Unix presentation.pdfUnix presentation.pdf
Unix presentation.pdf
 
Signals
SignalsSignals
Signals
 
Concurrency 2010
Concurrency 2010Concurrency 2010
Concurrency 2010
 
signals & message queues overview
signals & message queues overviewsignals & message queues overview
signals & message queues overview
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
Lecture_6_Process.ppt
Lecture_6_Process.pptLecture_6_Process.ppt
Lecture_6_Process.ppt
 
Process management
Process managementProcess management
Process management
 
First
First First
First
 
Ch 1 overview
Ch 1 overviewCh 1 overview
Ch 1 overview
 
The Ring programming language version 1.6 book - Part 83 of 189
The Ring programming language version 1.6 book - Part 83 of 189The Ring programming language version 1.6 book - Part 83 of 189
The Ring programming language version 1.6 book - Part 83 of 189
 
Basics of Automation, PLC and SCADA
Basics of Automation, PLC and SCADABasics of Automation, PLC and SCADA
Basics of Automation, PLC and SCADA
 
Operating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphoresOperating system 24 mutex locks and semaphores
Operating system 24 mutex locks and semaphores
 

Dernier

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Dernier (20)

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

System Programming Assignment Help- Signals

  • 2. SIGNALS • Signals are software interrupts that give us a way to handle asynchronous events • Signals can be received by or sent to any existing process • It provides a flexible way to stop execution of a process or to tell it that something has happened, requiring attention • All signals are integers which are #define’d as constants starting with the characters SIG inside the header signal.h • All signals received by a process have a default action associated with them, which are: ignore, terminate with core or stop
  • 3. SIGNALS • Signals are of different types: • Kill process (SIGTERM, SIGABRT, SIGKILL, SIGQUIT, SIGSEGV) • Suspend process (SIGSTOP, SIGSTP) • Resume process (SIGCNT) • Notify ^C (SIGINT) • Memory violation (SIGSEGV) • Child process termination (SIGCHLD) • User defined events (SIGUSR1, SIGUSR2)
  • 5. SIGNALS • Signals can occur due to one of the following events: • Certain terminal keys (ex. ^C) • Hardware exception (ex: divide by zero) • Software events • The kill() system call • A process has one of three options on how to react to a signal arriving • Ignore it (except for SIGKILL and SIGSTOP) • Perform the default action • Catch the signal using a user defined function
  • 6. SIGNAL HANDLING #include <signal.h> void (*signal(int signo, void(*func)(int)))(int) return a pointer to previous signal handler, SIG_ERR on error - or - typedef void sigfunc(int) sigfunc *signal(int, sigfunc *)
  • 7. SIGNAL HANDLING • If we register a signal handler, when that signal is received execution is temporarily suspended until the signal handler returns • signal() lets us register signal handlers for specific signals • func may be one of: SIG_IGN, SIG_DFL, or a function that takes an integer and returns nothing • SIGKILL and SIGSTOP should not be trapped and many system do not let you trap them
  • 8. SIGNAL HANDLING • On most system, when a user defined signal handler executes, the signal handling disposition is reset and has to be set again • Most slow system calls on System V implementation trap signals by returning an error value of -1 and setting errno to EINTR • One has to be careful about system calls which are interrupted by a signal and the action taken inside the signal handler. Only re-entrant functions can be used in the signal handler but the errno variable might still be corrupted
  • 9. REENTRANT FUNCTIONS • When a signal is being handled by a process the normal sequence of instruction being executed is temporarily interrupted by the signal handler • If the signal handler returns then the normal instruction sequence that the process was executing continues (similar to hardware interrupt) • In the signal handler we can’t tell where the process was executing (say, if it was malloc’ing) • To conserve the state of the system, signal handlers should only use re-entrant system calls
  • 10. REENTRANT FUNCTIONS • Non re-entrant functions either: • Use static data structures • Call malloc or free • Are part of the standard I/O library (printf is not guaranteed to produce expected results) • There is only one errno variable per thread, and function calls in the signal handler might modify its value
  • 11. TERMINOLOGY • A signal is generated for a process when an event causes that signal to occur • A signal is delivered to a process when the action for a signal is taken • During the time between signal generation and delivery the signal is pending • A process has the option of blocking delivery of a signal. If the action is either default or caught, signal remain pending until the process unblocks or changes the action to ignore the signal
  • 12. TERMINOLOGY • The system determines what to do with a blocked signal when the signal is delivered, not generated • This allows the process to change the action for the signal before it’s delivered • signpending function can be called to determine which signal are blocked and pending • If blocked signal is generated more than once, most systems deliver the signal once (not queued) • If more than one signal is ready to be delivered, signal related to the current state of the process is delivered before others (eg SIGSEGV)
  • 13. SIGNAL HANDLING static void sig_usr(int); int main(void) { if (signal(SIGUSR1, sig_usr) == SIG_ERR) fprintf(stderr, “Can’t catch SIGUSR1”); for ( ; ; ) pause(); } static void sig_usr(int signo) { if (signo == SIGUSR1) printf(“Received SIGUSR1n”); else fprintf(stderr, “Received signal %dn”, signo); }
  • 14. SENDING SIGNALS • From the command prompt one can send a signal to a process using: kill -<SIGNAL> <pid> • Default signal is SIGTERM • kill() is used to send a signal to a specific process while raise() sends a signal to itself • A superuser can send a signal to any process • A process can send signals only to process with the same real or effective UID
  • 15. SIGNAL FUNCTIONS #include <sys/types.h> #include <signal.h> int kill(pid_t pid, int signo); int raise(int signo); return 0 if OK, -1 on error unsigned int alarm(unsigned int seconds); return 0 or number of seconds until previously set alarm int pause(void) return -1 with errno=EINTR
  • 16. SENDING SIGNALS • Four different condition for the pid argument: • pid > 0, signal sent to process with ID pid • pid == 0, signal sent to all processes whose process group equals the process group ID of the sender • pid < 0, signal sent to all processes whose group ID equals the absolute value of pid • pid == 1, signal sent to all processes on the system
  • 17. SIGNAL FUNCTIONS • A process needs permission to send a signal to another process • pause() blocks the running process until a signal handler returns • Can be used as a sort of Inter Process Communication • alarm() allows us to set a timer that will expire at a specified time in the future. When timer expires the SIGALRM signal is generated. Default action is to terminate the process
  • 18. SIGNAL MASKS • Signal sent represents a set of signals #include <signal.h> int sigemptyset(sigset_t *set); int sigfillset(sigset_t *set); int sigaddset(sigset_t *set, int signo); int sigdelset(sigset_t *set, int signo); Return 0 if OK, 1 on error int sigismember(const sigset_t *set, int signo) Return 1 if true/error, 0 if false
  • 19. SIGNAL MASKS • Signals can be blocked so that critical sections are not interrupted • Every process maintains a signal mask telling which signals are blocked • Signal masks are stored in the data type sigset_t • This is guaranteed to be able to hold all signals supported by the system • sigemptyset() or sigfillset() must be called at least once
  • 20. MASKING SIGNALS #include <signal.h> int sigprocmask(int how, const sigset_t *set, sigset_t *oset); Returns 0 if OK, -1 on error How value: SIG_BLOCK (union) SIG_UNBLOCK (intersection) SIG_SETMASK (equality)
  • 21. SIGNAL MASKS • If oset is NULL, the old signal mask is returned in it • set defines the signals we want to block or unblock • If there are any pending signals and we unblock it with sigprocmask(), one these signals is received before sigprocmask() returns • Blocking signal make sure critical sections are executed atomically • We could call pause() if we want to wait for a signal after unblocking the signal mask, however this could make the process wait forever
  • 22. SIGNAL MASKS • Tells us what signals that are blocked frim delivery are currently pending • The list of signals is returned inside set • sigismember() can be used to find out what signals are present in set #include <signal.h> int sigpending(sigset_t *set); returns 0 if OK, -1 on error
  • 24. SIGACTION #include <signal.h> int sigaction(int signo, const struct sigaction *act, const struct sigaction *oact) return -1 on error struct sigaction { void (*sa_handler)(); sigset_t sa_mask; int sa_flags; }; SA_NOCLDSTOP SA_RESTART SA_ONSTACK SA_NODEFER SA_RESETHAND SA_SIGINGFO
  • 25. SIGNAL MASKS • Allows us to examine and/or modify the action associated with a particular signal • Supersedes the signal function from earlier systems • If the act pointer is not NULL, we are modifying the action • If the oact pointer is not NULL the system returns the previous action for the signal • When changing the action of a signal the sa_handler field contains the address of a signal- catching function and sa_mask specifies a signal set that are added to the mask before function is called.
  • 26. SIGSUSPEND 1. Sets the signal mask to sigmask 2. Calls the pause function 3. If pause() returns, signal mask is set back to its original value • All above steps are guaranteed to be performed atomically and thus get no lost signals #include <signal.h> int sigsuspend(const sigset_t *sigmask); returns -1 with errno set to EINT
  • 28. SLEEP • Function causes the calling process to be suspended until: • Amount of wall clock time has elapsed • A signal is caught by the process and the signal handler returns • In the latter case, the number of unslept seconds is returned #include <unistd.h> unsigned int sleep(unsigned int secs);
  • 29. SIGSUSPEND 1. Sets the signal mask to sigmask 2. Calls the pause function 3. If pause() returns, signal mask is set back to its original value • All above steps are guaranteed to be performed atomically and thus get no lost signals #include <signal.h> int sigsuspend(const sigset_t *sigmask); returns -1 with errno set to EINT