A
aditya
Hi all,
Can body please me that why the following code in not working as
expected.Basically,my aim was to shift the control from one function
to
another as soon as I presses Control-c keys.
In other words,I was expecting the program to execute in the following
way-
1. Initially,the control is in the while loop of the main function.
2. I presses the Control-c keys,then the control should have passed
into the g() via the handle function(which is the case).
3. When I presses the Control-c keys,the control should have gone into
f() via the handle function.(However,the control is not passing
into
the f()).
So,can somebody please tell me that why the control is not passing
into f(),when I presses the Control-c keys for the second time.
The complete code is as follows-
#include<stdio.h>
#include<signal.h>
typedef int (*ptf)(void);
int f(){
printf("in f\n");
while(1){}
return 0;
}
int g(){
printf("in g\n");
while(1){
}
return 0;
}
static toggle=0;
ptf context[]={f,g};
void handle(int sig){
toggle=1-toggle;
printf("caught:%d\n",sig);
context[toggle]();
return ;
}
int main(void){
if(signal(SIGINT,handle)==SIG_ERR)
exit(1);
while(1);
return 0;
}
Can body please me that why the following code in not working as
expected.Basically,my aim was to shift the control from one function
to
another as soon as I presses Control-c keys.
In other words,I was expecting the program to execute in the following
way-
1. Initially,the control is in the while loop of the main function.
2. I presses the Control-c keys,then the control should have passed
into the g() via the handle function(which is the case).
3. When I presses the Control-c keys,the control should have gone into
f() via the handle function.(However,the control is not passing
into
the f()).
So,can somebody please tell me that why the control is not passing
into f(),when I presses the Control-c keys for the second time.
The complete code is as follows-
#include<stdio.h>
#include<signal.h>
typedef int (*ptf)(void);
int f(){
printf("in f\n");
while(1){}
return 0;
}
int g(){
printf("in g\n");
while(1){
}
return 0;
}
static toggle=0;
ptf context[]={f,g};
void handle(int sig){
toggle=1-toggle;
printf("caught:%d\n",sig);
context[toggle]();
return ;
}
int main(void){
if(signal(SIGINT,handle)==SIG_ERR)
exit(1);
while(1);
return 0;
}