473,473 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

why this C code did'nt works as expected

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;
}
Nov 14 '05 #1
8 1427
On 11 Oct 2004 12:52:25 -0700, ad************@gmail.com (aditya)
wrote:
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.

Nov 14 '05 #2
On Mon, 11 Oct 2004 12:52:25 -0700, aditya wrote:
#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.

Nov 14 '05 #3
On 11 Oct 2004 12:52:25 -0700, ad************@gmail.com (aditya) wrote
in comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
In <3m********************************@4ax.com> Jack Klein <ja*******@spamcop.net> writes:
On 11 Oct 2004 12:52:25 -0700, ad************@gmail.com (aditya) wrote
in comp.lang.c:
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.


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
ever used (freestanding implementations need not provide <signal.h> in
the first place, so they are irrelevant here).
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #5
In <2c**************************@posting.google.com > ad************@gmail.com (aditya) writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #6
Jack Klein wrote:

Calling printf() inside a sin gal handler is undefined behavior.

A sin gal? How do you handle one of those?

Brian Rodenborn
Nov 14 '05 #7
Default User wrote:
Jack Klein wrote:
Calling printf() inside a sin gal handler is undefined behavior.


A sin gal? How do you handle one of those?


It depends highly on the gal.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8
In article <I5********@news.boeing.com>, fi********@boeing.com.invalid says...
A sin gal? How do you handle one of those?


As often as possible in a busy loop.

--
Randy Howard (2reply remove FOOBAR)
"Show me a young Conservative and I'll show you someone with no heart.
Show me an old Liberal and I'll show you someone with no brains."
-- Winston Churchill

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Razarr69 | last post by:
I have a ASP application that I am using for my company and it is currently running on a Windows 2000 server box with IIS 5. The code works perfectly there (except for handling large files - due...
4
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) {...
1
by: Avi | last post by:
Hi All. This code works very fine in Firefox but not in I.E. Can anybody help me out? it gives ... "Unknown Runtime Error" in I.E. This code ... Stores N*2 Matrix at Client Side & provide the...
6
by: Crash | last post by:
C# VS 2003 ..Net Framework V1.1 SP1 SQL Server 2000 SP3 Enterprise Library June 2005 I'm working with some code {not of my creation} that performs the following sequence of actions: - Open...
4
by: sgurminder | last post by:
Can anyone explain how this shell spawning code works...... I am not able to figure out exactly..... I got this from Aleph1's Smashing the stack file. :) Here it is...
3
by: Dale | last post by:
Access 2000 Frt End (mdb) Connected to SQL data This code works when connected to an access backend, but it hangs on the Set Rec = db.OpenRecordset("Classes&Students", dbSeeChanges) with...
11
by: John | last post by:
Access 2003 After my app is finished I'd like to create an mde file. Therefore my db has to be made in the 2002-2003 format. So I've created a new mdb (default in 2000 format), I converted this...
1
by: reemamg | last post by:
I've two frames in a page. On the left Frame i've buttons , if we click on button the particular pages will be loaded in the middle Frame. This code works perfectly in Firefox but not in IE ... ...
1
by: reemamg | last post by:
Have a piece of code which works in Firefox however doesnt work in IE <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <meta...
3
by: PrabodhanP | last post by:
I hv following javascript form validation code works in IE but not in Mozilla-Firefox ...please suggest <script type="text/javascript"> function IsNumeric(strString) // check for valid...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.