//Ans.suppose printer is printing ...suddenly page got revolved round in printer ....a signal will go to printer..priter got a little back..then move forward continues according to its programming...but when signal has gone to printer...signal handler will execute...recovery routine will be execute in that function of signal system call....means recovery routine will handle that signal
/*signals are always process regarding ..if a signal system call is used in one process ..it cannot be accessed into another process either .....process will always create by fork in a systm...if a signal executes into one thread ..it can be accesed in another thread also....in addition that ...registeration of signal is diffrent for diffrent threads...we will see lator........*/
//signal system call may be invoked anywhere within the main process or program
#include<stdio.h>
#include<sys/types.h>
#include<signal.h>//signal.h header file is included to use signals
int x=5;
void ouch(int signal)//SIGINT is collected into int signal argument
{
printf("out!when ^c is pressed=%d",signal);//int will prints signal no.
}
void main()
{
(void) signal(SIGINT,ouch);//signal system call...1st arg is signal num.u may see all signals by command [kill -l] ,next is base address of function in which signal is get to handle...i.e.signal handler
while(x<=5)//x is not incrementing so infinite loop will execute
{
printf("\nHELLO EMBLOGIC");//after every 1sec. HELLO EMBLOGIC prints...& if i press ^C in mid of execution of program ..it will not terminate the program because..signal no. for ^C is in SIGINT macro.....whenever i press ^C ...it will execute its routine & do required task in execution......
pid_t getpid();//function is called to get parent id...
void ding(int signal)//as signal is called signal handler will execute
{
alarm_=1;//will make alarm_=1
//getpid();
}
int main()
{
getpid();
pid=fork();//will create a new process has its own pcb,,it will be called 2 times...one when parent executes ..return child pid...another when child calls ...return 0
switch(pid)//if pid==0 means child executes & if pid>0 means +ve value parent executes & if pid<0 then fork failed
{
case(-1):
perror("fork failed");
printf("234");
exit(-1);
case(0):
sleep(0);
kill(getppid(),SIGALRM);//kill system call in child will kill signal that is executing in parent process.......in child process....means this special signal is used to kill signal from one process to another
printf("1111");
exit(0);
default:
pause();
(void)signal(SIGALRM,ding);//in parent signal system call is applied ...SIGALRM is used to kill the child process ..if there is..
printf("\nwaiting for alarm to go off");
if(alarm_==1)
{
printf("\nhandler executed");//if handler is executed...it prints after execution of signal handler
}
//(void)signal(SIGALRM,ding);
/*else
{
printf("\nalarm is off");
}*/
(void)signal(SIGALRM,ding);
}
if(SIGINT,handler)==SIG_ERR)
{
printf("some error in executing signal\n");//if signal can not executed by some region..then it prints