why this C code did'nt works as expected

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;
}
 
J

John

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;
}

reset the signal after you catch it.
 
?

=?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=

#include<stdio.h>
#include<signal.h>

typedef int (*ptf)(void);

int f(){
printf("in f\n");
while(1){}
You are executing a busy loop here, thus the
signal handler never finishes. Signals do normally not preemt
each other. Just lose the while loops, it should be ok.
 
J

Jack Klein

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");

Calling printf() inside a signal handler is undefined behavior.
while(1){}
return 0;
}


int g(){

Calling printf() inside a sin gal handler is undefined behavior.
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);

Calling printf() inside a sin gal handler is undefined behavior.
context[toggle]();
return ;
}

int main(void){
if(signal(SIGINT,handle)==SIG_ERR)
exit(1);
while(1);
return 0;
}

The C standard does not define the result of signal functions unless
they are invoked by the raise() function from within your program.

Signals generated by events outside your program, such as you expect
to receive from a keyboard event, are not defined at all by C.

To find out what is supposed to happen on your compiler and OS
combination, you need to ask in a group that supports that
combination. You can also ask them if your system allows library
calls like printf() inside a signal handling function.

This is a platform-specific issue, C doesn't say anything at all about
what your program should do.
 
D

Dan Pop

In said:
The C standard does not define the result of signal functions unless
they are invoked by the raise() function from within your program.

There is only one signal function in the standard library and it is NOT
supposed to be invoked by raise().

You must be referring to signal *handlers*. But they are void functions
so talking about their result doesn't make much sense.
Signals generated by events outside your program, such as you expect
to receive from a keyboard event, are not defined at all by C.

"Not defined at all" is far too strong. Please explain the meaning of:

SIGINT receipt of an interactive attention signal

The semantics are quite obvious, even if there is no guarantee that a
given implementation will be able to generate it. Obviously, the OP's
implementation can generate it. So does any hosted implementation I have
To find out what is supposed to happen on your compiler and OS
combination, you need to ask in a group that supports that
combination. You can also ask them if your system allows library
calls like printf() inside a signal handling function.

This is a platform-specific issue, C doesn't say anything at all about
what your program should do.

OTOH, his program can be rewritten so that the C standard says
everything about what it should do when a SIGINT is generated
asynchronously. If you don't know how, there is no point in redirecting
the OP elsewhere.

Dan
 
D

Dan Pop

In said:
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;
}

Rewrite the program, keeping the following in mind:

3 When a signal occurs and func points to a function, it is
implementation-defined whether the equivalent of
signal(sig, SIG_DFL); is executed or the implementation
prevents some implementation-defined set of signals (at least
including sig) from occurring until the current signal handling
has completed; in the case of SIGILL, the implementation may
alternatively define that no action is taken. Then the equivalent
of (*func)(sig); is executed. [...]

5 If the signal occurs other than as the result of calling the
abort or raise function, the behavior is undefined if the signal
handler refers to any object with static storage duration other
than by assigning a value to an object declared as volatile
sig_atomic_t, or the signal handler calls any function in
the standard library other than the abort function, the _Exit
function, or the signal function with the first argument equal
to the signal number corresponding to the signal that caused
the invocation of the handler. Furthermore, if such a call to
the signal function results in a SIG_ERR return, the value of
errno is indeterminate.211)

____________________

211) If any signal is generated by an asynchronous signal handler,
the behavior is undefined.

If the program still doesn't work as intended, we will try to address
the remaining bugs. As it is, your code is a complete mess from the C
standard's point of view.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top