banner



How To Register Signal Handler In C


Prerequisite : Fork arrangement telephone call, Wait arrangement call
A signal is a software generated interrupt that is sent to a procedure by the Bone because of when user press ctrl-c or another process tell something to this procedure.
In that location are fix ready of signals that tin can exist sent to a process. signal are identified past integers.
Signal number have symbolic names. For example SIGCHLD is number of the signal sent to the parent process when child terminates.
Examples:

#ascertain SIGHUP  ane   /* Hangup the process */  #define SIGINT  two   /* Interrupt the procedure */  #define SIGQUIT 3   /* Quit the procedure */  #define SIGILL  iv   /* Illegal instruction. */  #ascertain SIGTRAP 5   /* Trace trap. */  #define SIGABRT vi   /* Abort. */        

OS Structures for Signals

  • For each procedure, the operating arrangement maintains 2 integers with the bits corresponding to a signal numbers.
  • The two integers go on track of: pending signals and blocked signals
  • With 32 bit integers, up to 32 dissimilar signals tin be represented.

Case :
In the example beneath, the SIGINT ( = 2) signal is blocked and no signals are pending.

A bespeak is sent to a process setting the corresponding fleck in the pending signals integer for the procedure. Each fourth dimension the Os selects a process to be run on a processor, the pending and blocked integers are checked. If no signals are pending, the process is restarted unremarkably and continues executing at its next instruction.

If 1 or more signals are pending, but each i is blocked, the process is likewise restarted normally simply with the signals even so marked as pending. If 1 or more signals are pending and Not blocked, the Os executes the routines in the process's lawmaking to handle the signals.

Default Signal Handlers

At that place are several default betoken handler routines. Each indicate is associated with one of these default handler routine. The different default handler routines typically take 1 of the following actions:

  • Ign: Ignore the signal; i.e., do nothing, but render
  • Term: cease the procedure
  • Cont: unblock a stopped process
  • Stop: block the process

#include<stdio.h>

#include<signal.h>

int main()

{

betoken (SIGINT, handle_sigint);

while (1)

{

printf ("hullo world\due north");

slumber(1);

}

return 0;

}

Output: Impress hello world infinite times. If user presses ctrl-c to terminate the procedure considering of SIGINT signal sent and its default handler to stop the process.

hello globe    hello world          hi globe          terminated        

User Defined Signal Handlers

A process tin can supersede the default signal handler for almost all signals (but not SIGKILL) by its user's own handler function.
A signal handler part tin accept any proper noun, but must have return blazon void and accept ane int parameter.
Case: you might choose the proper name sigchld_handler for a indicate handler for the SIGCHLD signal (termination of a child procedure). So the annunciation would be:

          void sigchld_handler(int sig);        

When a betoken handler executes, the parameter passed to it is the number of the signal. A programmer can apply the same signal handler function to handle several signals. In this example the handler would need to check the parameter to see which signal was sent. On the other hand, if a signal handler function only handles 1 signal, information technology isn't necessary to carp examining the parameter since it volition always exist that bespeak number.

#include<stdio.h>

#include<betoken.h>

void handle_sigint( int sig)

{

printf ( "Caught point %d\north" , sig);

}

int chief()

{

signal (SIGINT, handle_sigint);

while (i) ;

render 0;

}

Output:

^CCaught indicate 2  // when user presses ctrl-c ^CCaught indicate two        

Sending signals via kill()

We can transport a point using kill() to the process.

          int kill(pid_t pid, int signal);          pid:          id of destination procedure          signal:          the type of signal to transport          Render value:          0 if signal was sent successfully

Instance:

pid_t iPid = getpid(); /* Process gets its id.*/ kill(iPid, SIGINT);  /* Process sends itself a          SIGINT          signal    (commits suicide?)(because of          SIGINT          betoken default handler is terminate the process) */        

Questions

1. What is the Output of the following program?

#include<stdio.h>

#include<await.h>

#include<indicate.h>

int primary()

{

int stat;

pid_t pid;

if ((pid = fork()) == 0)

while (1) ;

else

{

kill(pid, SIGINT);

wait(&stat);

if (WIFSIGNALED(stat))

psignal(WTERMSIG(stat), "Kid term due to" );

}

}

Output:

          Kid term due to: Interrupt        

2. What is the Output of the following program?

#include<stdio.h>

#include<signal.h>

#include<await.h>

int val = 10;

void handler( int sig)

{

val += five;

}

int chief()

{

pid_t pid;

betoken (SIGCHLD, handler);

if ((pid = fork()) == 0)

{

val -= iii;

leave (0);

}

waitpid(pid, Zippo, 0);

printf ( "val = %d\due north" , val);

exit (0);

}

Output:

val = fifteen        

three. Consider the following code. What is the output?

#include<stdio.h>

#include<wait.h>

#include<bespeak.h>

pid_t pid;

int counter = 0;

void handler1( int sig)

{

counter++;

printf ( "counter = %d\due north" , counter);

fflush (stdout);

kill(pid, SIGUSR1);

}

void handler2( int sig)

{

counter += iii;

printf ( "counter = %d\north" , counter);

go out (0);

}

int main()

{

pid_t p;

int status;

signal (SIGUSR1, handler1);

if ((pid = fork()) == 0)

{

signal (SIGUSR1, handler2);

kill(getppid(), SIGUSR1);

while (1) ;

}

if ((p = await(&condition)) > 0)

{

counter += iv;

printf ( "counter = %d\n" , counter);

}

}

Output

counter = one         //(parent's handler)  counter = iii         //(child's handler)  counter = 5         //(parent's master)        

This article is contributed past Kadam Patel. If you lot like GeeksforGeeks and would similar to contribute, you tin also write an commodity using contribute.geeksforgeeks.org or mail your commodity to contribute@geeksforgeeks.org. See your commodity appearing on the GeeksforGeeks primary page and help other Geeks.

Please write comments if you find annihilation wrong, or you lot want to share more information near the topic discussed above.


How To Register Signal Handler In C,

Source: https://www.geeksforgeeks.org/signals-c-language/

Posted by: tribblehisavent.blogspot.com

0 Response to "How To Register Signal Handler In C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel