top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: What is call back function and when it is used ?

+2 votes
274 views
C: What is call back function and when it is used ?
posted Apr 3, 2018 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

This is a very common c programming question asked in interview. When a function (callee) is called through another function (caller) using the function pointer, known as call back. Kernel generates different kinds of signals and user space process may register its own signal handler using call back mechanism. Sample of c code is as below:

struct sigaction act;

/* signal handler definition goes here */
void sig_handler(int signo, siginfo_t *si, void *ucontext)
{
printf("Got alarm signal %d\n",signo);
/* do the required stuff here */
}

int main(void)
{
act.sa_sigaction = sig_handler;
act.sa_flags = SA_SIGINFO;

/* register signal handler */
sigaction(SIGALRM, &act, NULL);  
/* set the alarm for 10 sec */       
alarm(10);   
/* wait for any signal from kernel */                                        
pause();  
/* after signal handler execution */                                             
printf("back to main\n");                     
return 0;

}

answer Apr 7, 2018 by Harshita
...