SlideShare une entreprise Scribd logo
1  sur  18
SIGNAL
UNIX SYSTEM
PROGRAMMING
 A signal is used to notify a process that a particular
event has occurred.
 Signals are software Interrupts
 A Signal may be received synchronously or
asynchronously.
Linux Signals
 In Linux, every signal has a name that begins with
characters SIG.
 A SIGINT signal that is generated when a user
presses ctrl+c. This is the way to terminate programs
from terminal.
 A SIGFPE is generated when the divide by zero
occurs.
Signal Handling
 Every signal may be handled by one of two possible
handlers.
 A default signal handler
 A user-defined signal handler
User-defined signal handler
 Every signal has a default signal handler that is run
by the kernel when handling that signal.
 The default action can be overridden by a user-
defined signal handler that is called to handle the
signal.
 Provide the handler function which has the syntax
 void handler_name(int signo);
 Register the signal handler with the signal
 Signal(Signal_Number , handler_name);
SIGSEGV
SIGINT
SIGALRM
SIGALRM
 #include <signal.h>
 void alarmhandler(int a)
 {
 printf("TIME OVER n");
 exit(0);
 }
 int main()
 {
 int i;
 signal(SIGALRM , alarmhandler);
 alarm(5);
 for(i = 0 ; i < 10 ; i++)
 {
 printf(" %dn" , i);
 sleep(1);
 }
 printf("END OF PROGRAM n");
 }
KILL - send signal to a process
 void handler(int a)
 {
 printf(" %d n" ,a);
 }
 int main()
 {
 signal(SIGALRM , handler);
 signal(SIGSEGV , handler);
 signal(SIGFPE , handler);
 #ifdef SEGV
 kill(getpid() , SIGSEGV);
 #endif
 #ifdef FPE
 kill(getpid() , SIGFPE);
 #endif
 }
SIGSEGV
 int main()
 {
 int x = 5 ;
 signal(SIGSEGV , myfunc);
 printf("Enter a number : n");
 scanf(" %d",x);
 printf("The number is %d",x );
 }
 void myfunc(int a)
 {
 printf("ENTERED MYHANDLER n");
 printf("signal recieved : %d n",a);
 exit(1);
 }
SIGCHLD – Indicates the death of a child
process.
Assignment
 Implement wait using SIGCHILD
Ignoring a Signal SIG_IGN
 void myhandler(int a)
 {
 printf("ENTERED MYHANDLER n");
 printf("signal recieved : %d n",a);
 exit(1);
 }
 int main()
 {
 int x = 0;
 printf("1. Default 2. Ignore 3. Handler n");
 scanf(" %d",&x);
 switch(x)
 {
 case 1 : signal(SIGINT , SIG_DFL); break;
 case 2 : signal(SIGINT , SIG_IGN); break;
 case 3 : signal(SIGINT , myhandler); break;
 }
 sleep(5);
 printf("END OF PROGRAM ");
 }

Contenu connexe

Tendances (20)

Kernel (OS)
Kernel (OS)Kernel (OS)
Kernel (OS)
 
Networking in linux
Networking in linuxNetworking in linux
Networking in linux
 
Linux dma engine
Linux dma engineLinux dma engine
Linux dma engine
 
Combined paging and segmentation
Combined paging and segmentationCombined paging and segmentation
Combined paging and segmentation
 
RTOS - Real Time Operating Systems
RTOS - Real Time Operating SystemsRTOS - Real Time Operating Systems
RTOS - Real Time Operating Systems
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Memory management
Memory managementMemory management
Memory management
 
snmp
snmpsnmp
snmp
 
Linux PCI device driver
Linux PCI device driverLinux PCI device driver
Linux PCI device driver
 
Processes in unix
Processes in unixProcesses in unix
Processes in unix
 
Linux Programming
Linux ProgrammingLinux Programming
Linux Programming
 
Signal Handling in Linux
Signal Handling in LinuxSignal Handling in Linux
Signal Handling in Linux
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Kernels and its types
Kernels and its typesKernels and its types
Kernels and its types
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Part II: LLVM Intermediate Representation
Part II: LLVM Intermediate RepresentationPart II: LLVM Intermediate Representation
Part II: LLVM Intermediate Representation
 
Linux Serial Driver
Linux Serial DriverLinux Serial Driver
Linux Serial Driver
 
Advanced c programming in Linux
Advanced c programming in Linux Advanced c programming in Linux
Advanced c programming in Linux
 

Similaire à Signal

SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTESSOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES4SF20CS057LESTONREGO
 
#define LOGFILE signal_log.txt Signal handlers void sigusr1.pdf
#define LOGFILE signal_log.txt  Signal handlers void sigusr1.pdf#define LOGFILE signal_log.txt  Signal handlers void sigusr1.pdf
#define LOGFILE signal_log.txt Signal handlers void sigusr1.pdfalstradecentreerode
 
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
 
AOS Chapter 6 for internal.docx
AOS Chapter 6 for internal.docxAOS Chapter 6 for internal.docx
AOS Chapter 6 for internal.docxKomlikaTaru
 
Unit1 jwfiles
Unit1 jwfilesUnit1 jwfiles
Unit1 jwfilesmrecedu
 
System Programming Assignment Help- Signals
System Programming Assignment Help- SignalsSystem Programming Assignment Help- Signals
System Programming Assignment Help- SignalsHelpWithAssignment.com
 
망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15종인 전
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7alish sha
 

Similaire à Signal (20)

Unix signals
Unix signalsUnix signals
Unix signals
 
Usp notes unit6-8
Usp notes unit6-8Usp notes unit6-8
Usp notes unit6-8
 
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTESSOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
SOGNAL DAEMON AND PROCESSING CRYPTOGRAPHY NOTES
 
#define LOGFILE signal_log.txt Signal handlers void sigusr1.pdf
#define LOGFILE signal_log.txt  Signal handlers void sigusr1.pdf#define LOGFILE signal_log.txt  Signal handlers void sigusr1.pdf
#define LOGFILE signal_log.txt Signal handlers void sigusr1.pdf
 
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
 
Unixppt (1) (1).pptx
Unixppt (1) (1).pptxUnixppt (1) (1).pptx
Unixppt (1) (1).pptx
 
AOS Chapter 6 for internal.docx
AOS Chapter 6 for internal.docxAOS Chapter 6 for internal.docx
AOS Chapter 6 for internal.docx
 
C Language Unit-1
C Language Unit-1C Language Unit-1
C Language Unit-1
 
Unit1 jwfiles
Unit1 jwfilesUnit1 jwfiles
Unit1 jwfiles
 
System Programming Assignment Help- Signals
System Programming Assignment Help- SignalsSystem Programming Assignment Help- Signals
System Programming Assignment Help- Signals
 
Unit2 C
Unit2 C Unit2 C
Unit2 C
 
Unit2 C
Unit2 CUnit2 C
Unit2 C
 
망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15
 
UNIT-II CP DOC.docx
UNIT-II CP DOC.docxUNIT-II CP DOC.docx
UNIT-II CP DOC.docx
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
 
Sysprog 11
Sysprog 11Sysprog 11
Sysprog 11
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
C Programming
C ProgrammingC Programming
C Programming
 
Unix presentation.pdf
Unix presentation.pdfUnix presentation.pdf
Unix presentation.pdf
 

Plus de Mohammed Sikander (20)

Operator Overloading in C++
Operator Overloading in C++Operator Overloading in C++
Operator Overloading in C++
 
Python_Regular Expression
Python_Regular ExpressionPython_Regular Expression
Python_Regular Expression
 
Modern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptxModern_CPP-Range-Based For Loop.pptx
Modern_CPP-Range-Based For Loop.pptx
 
Modern_cpp_auto.pdf
Modern_cpp_auto.pdfModern_cpp_auto.pdf
Modern_cpp_auto.pdf
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Python tuple
Python   tuplePython   tuple
Python tuple
 
Python strings
Python stringsPython strings
Python strings
 
Python set
Python setPython set
Python set
 
Python list
Python listPython list
Python list
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Introduction to Python
Introduction to Python  Introduction to Python
Introduction to Python
 
Pointer basics
Pointer basicsPointer basics
Pointer basics
 
File management
File managementFile management
File management
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
Java strings
Java   stringsJava   strings
Java strings
 
Java notes 1 - operators control-flow
Java notes   1 - operators control-flowJava notes   1 - operators control-flow
Java notes 1 - operators control-flow
 

Dernier

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Dernier (20)

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 

Signal

  • 2.  A signal is used to notify a process that a particular event has occurred.  Signals are software Interrupts  A Signal may be received synchronously or asynchronously.
  • 3. Linux Signals  In Linux, every signal has a name that begins with characters SIG.  A SIGINT signal that is generated when a user presses ctrl+c. This is the way to terminate programs from terminal.  A SIGFPE is generated when the divide by zero occurs.
  • 4. Signal Handling  Every signal may be handled by one of two possible handlers.  A default signal handler  A user-defined signal handler
  • 5. User-defined signal handler  Every signal has a default signal handler that is run by the kernel when handling that signal.  The default action can be overridden by a user- defined signal handler that is called to handle the signal.  Provide the handler function which has the syntax  void handler_name(int signo);  Register the signal handler with the signal  Signal(Signal_Number , handler_name);
  • 7.
  • 8.
  • 9.
  • 12. SIGALRM  #include <signal.h>  void alarmhandler(int a)  {  printf("TIME OVER n");  exit(0);  }  int main()  {  int i;  signal(SIGALRM , alarmhandler);  alarm(5);  for(i = 0 ; i < 10 ; i++)  {  printf(" %dn" , i);  sleep(1);  }  printf("END OF PROGRAM n");  }
  • 13. KILL - send signal to a process  void handler(int a)  {  printf(" %d n" ,a);  }  int main()  {  signal(SIGALRM , handler);  signal(SIGSEGV , handler);  signal(SIGFPE , handler);  #ifdef SEGV  kill(getpid() , SIGSEGV);  #endif  #ifdef FPE  kill(getpid() , SIGFPE);  #endif  }
  • 14.
  • 15. SIGSEGV  int main()  {  int x = 5 ;  signal(SIGSEGV , myfunc);  printf("Enter a number : n");  scanf(" %d",x);  printf("The number is %d",x );  }  void myfunc(int a)  {  printf("ENTERED MYHANDLER n");  printf("signal recieved : %d n",a);  exit(1);  }
  • 16. SIGCHLD – Indicates the death of a child process.
  • 18. Ignoring a Signal SIG_IGN  void myhandler(int a)  {  printf("ENTERED MYHANDLER n");  printf("signal recieved : %d n",a);  exit(1);  }  int main()  {  int x = 0;  printf("1. Default 2. Ignore 3. Handler n");  scanf(" %d",&x);  switch(x)  {  case 1 : signal(SIGINT , SIG_DFL); break;  case 2 : signal(SIGINT , SIG_IGN); break;  case 3 : signal(SIGINT , myhandler); break;  }  sleep(5);  printf("END OF PROGRAM ");  }

Notes de l'éditeur

  1. What is a signal? Signals are software interrupts. signals are a way to deliver asynchronous events to the application. A user hitting ctrl+c, referring a invalid address etc are all such cases where a process needs to do signal handling.
  2. When the signal occurs, the process has to tell the kernel what to do with it.  There can be three options through which a signal can be disposed : The signal can be ignored. By ignoring we mean that nothing will be done when signal occurs. Most of the signals can be ignored but signals generated by hardware exceptions like divide by zero, if ignored can have weird consequences. SIGKILL and SIGSTOP signals cannot be ignored.
  3. The signal can be caught. When this option is chosen, then the process registers a function with kernel. This function is called by kernel when that signal occurs. If the signal is non fatal for the process then in that function the process can handle the signal properly or otherwise it can chose to terminate gracefully. Let the default action apply. Every signal has a default action. This could be process terminate, ignore etc. As we already stated that two signals SIGKILL and SIGSTOP cannot be ignored. This is because these two signals provide a way for root user or the kernel to kill or stop any process in any situation .The default action of these signals is to terminate the process. Neither these signals can be caught nor can be ignored.
  4. Floaterror.c
  5. Sigfpe.c #include <stdio.h> #include <stdlib.h> #include <signal.h> void myhandler(int signo) { printf("\n Signal Recieved \n"); printf(" Signal Number = %d \n" , signo); exit(1); } int main() { signal(SIGFPE , myhandler); int x = 0 , y = 0,res; printf("Enter the numerator :"); scanf(" %d",&x); printf("Enter the denominator : "); scanf("%d",&y); printf("WE ARE PERFORMING DIVISION "); res = x / y; printf("\n result = %d \n" , res); }
  6. Sigint.c
  7. SIGCHLD – Indicates the death of a child process. Without sleep, child ends before parent. Signal caught With sleep, parent ends before child. No Signal
  8. #include <signal.h> #include <unistd.h> int flag = 0; void myfunc(int a); int main() { signal(SIGCHLD , myfunc); int ret = fork(); if(ret == 0) { printf("CHILD id = %d \n" , getpid()); sleep(2); printf("AFTER SLEEP \n"); } else { while(flag == 0) pause(); printf("CHILD TERMINATED \n"); } } void myfunc(int a) { printf("ENTERED MYHANDLER \n"); printf("signal recieved : %d \n",a); if(a == SIGCHLD) flag = 1; printf(" id = %d \n",getpid()); }