473,725 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Call once,return twice

Hi,all!
I found an interesting problem,it is that how to implement a C
function which can be called once and return twice? Just like the POSIX
function fork() or the library function longjmp().Only via using asm?
It is strange that I have searched the google groups and FAQs of this
group and "googled" the internet but find none useful info.
Thanks for any reply!

Nov 15 '05 #1
3 4946
Cong Wang wrote:
I found an interesting problem,it is that how to implement a C
function which can be called once and return twice? Just like the POSIX
function fork() or the library function longjmp().
The structure of C (and for that matter all stack based languages) is so
that you can add (a new function call context or local var) on top of the
stack or remove (return to old function call context or remove local var)
from the top of it. IOW, you need to break out this schema to achieve what
setjmp/longjmp or fork do - since your process' memory space is yours to
access in any way, there is nothing that should keep you from doing it.
Only via using asm?


No, you can do so via normal C code, but it requires knowledge of the
implementation, so the code you produce will be highly platform dependent.
Maybe you will need a few assembler macros eventually to set the
stackpointer or similar things that would be hard to achieve without.

Uli

Nov 15 '05 #2
In article <3o************ @uni-berlin.de>,
Ulrich Eckhardt <do******@knuut .de> wrote:
Cong Wang wrote:
I found an interesting problem,it is that how to implement a C
function which can be called once and return twice? Just like the POSIX
function fork() or the library function longjmp().
The structure of C (and for that matter all stack based languages) is so
that you can add (a new function call context or local var) on top of the
stack or remove (return to old function call context or remove local var)
from the top of it. IOW, you need to break out this schema to achieve what
setjmp/longjmp or fork do - since your process' memory space is yours to
access in any way, there is nothing that should keep you from doing it.


No.

The call stack need not be accessible through normal operations.

There are processors in which the call stack is usually held
in-processor, and only "spilled" to memory when the register list
gets full.

There are processors in which the current return address is held
in a register, not on the stack, so memory manipulation cannot
change that return address [but might be able to change a previous
return address.]

There are processors in which the call stack is seperate
from the argument stack, and the call stack is held in a different
memory segment which is marked as not being writable -- processors
designed to twart buffer-overflow attacks.

Whether "your process' memory space is yours to
access in any way" depends on the architecture and implementation.
C promises only that your declared variables and malloc/alloc'd memory
are visible, and makes no promises about what might happen in
system calls, or library functions; it also makes no promises
about how function calls or argument passing are implemented.
--
Any sufficiently old bug becomes a feature.
Nov 15 '05 #3

In article <3o************ @uni-berlin.de>, Ulrich Eckhardt <do******@knuut .de> writes:
Cong Wang wrote:
I found an interesting problem,it is that how to implement a C
function which can be called once and return twice?
What you found is a bogus problem. There is no way to do what you
describe in standard C. There may be implementation-dependent ways
to do it for some implementations , but those are off-topic for
comp.lang.c.
Just like the POSIX function fork() or the library function longjmp().

Neither of those functions does what you describe.

It is illegal to pass longjmp a jmp_buf that was initialized with a
call to setjmp in a function that has since returned; it produces
undefined behavior. C90 7.6.2.1.

setjmp, which you did not cite, *appears* to (be able to) "return"
multiple times to the "caller". How this is actually implemented is
an implementation detail, and setjmp need not be implemented with an
actual function that actually returns. (setjmp itself must be a
macro. C90 7.6.1.1.)

<OT>The POSIX fork function does not return twice; nor does it cause
any other function to return twice. Describing it as "returning
twice" is misleading shorthand for what it actually does: create a
second process that is initially nearly identical to the calling
process. Each of those processes will return from fork, but in the
context of any one process fork only returns once. And, of course,
the manner in which fork achieves this is entirely implementation-
dependent and has nothing to do with portable C.</OT>
The structure of C (and for that matter all stack based languages) is so
that you can add (a new function call context or local var) on top of the
stack or remove (return to old function call context or remove local var)
from the top of it.
While a conforming C implementation must behave in a manner consis-
tent with a stack-based calling sequence, it is not required to use
an actual stack to implement it (modulo very generous definitions of
"stack"), and it is not required to keep automatic variables with
function call contexts.
IOW, you need to break out this schema to achieve what
setjmp/longjmp or fork do - since your process' memory space is yours to
access in any way,
This is not true for some conforming implementations .
there is nothing that should keep you from doing it.


Certainly there is. First, it is impossible in portable C. Second,
doing arbitrary things to "your process' memory space" (assuming such
a thing is well-defined in a given implementation) will almost
certainly produce undefined behavior. Third, there are implementa-
tions (eg C for the AS/400) which most definitely disallow it.

--
Michael Wojcik mi************@ microfocus.com

Memory, I realize, can be an unreliable thing; often it is heavily coloured
by the circumstances in which one remembers, and no doubt this applies to
certain of the recollections I have gathered here. -- Kazuo Ishiguro
Nov 15 '05 #4

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

Similar topics

8
2555
by: MNQ | last post by:
Hello All Im trying to relearn ANSI C. I have written some code but cannot understand why it is not do what i think it should. It prints the error message and the menu twice but i cant understand why. Can any one help me please? naveed /* Last modified 5 February 2004
8
3854
by: ThomasR | last post by:
I understand that virtual methods on inherited objects are slower than non-virtual methods because of the indirection required to support the call. However, when looking at IL code produced by the compiler, I notice that all methods on object variables use callvirt instead of call, regardless of whether the method is truly virtual or not. I assume this allows .NET to check if the object reference is null or not before invoking the...
2
1214
by: Justin Harrison | last post by:
Hi, I've got a function I am calling asynchronously, with a callback. The callback gets called once, with valid return data. It gets called a second time, with no return data. Does anybody have any idea of what is going on? Justin
5
3489
by: Jon Booth | last post by:
Hi All, I'm trying to create a button that once clicked will disable itself (so as not to be clicked twice) I have the following in my cs Button1.Attributes.Add("onClick","return document.Form1.Button1.disabled=true;"); It disables the button but does not perform the post back. What am I doing wrong?
1
6284
by: Chris Morse | last post by:
WARNING: Verbosity: skip to the very bottom paragraph for succinct version of my question.) Hi- I can't seem to find an answer to this. I am playing around with a variation of the ".NET Framework Developer's Guide" (in the MSDN docs) "Using an Asynchronous Server Socket" sample. As usual with docs like this, the example isn't very good. I had to
16
2611
by: DaTurk | last post by:
Hi, I have a c# application that needs to access c++ libraries, so it does this by using a managed layer of c++ CLI. Anyway, in the CLI function call, that calls the unmanaged function it expects a boolean return value, which it gets, but the value changes! I walked throuhg the code, and the unmanged code is returning false, but when I look at the variable in the managed code it's return value it true. Even after I initialized the...
0
2023
by: shama | last post by:
Hi all, I want to develop add-in for OUTLOOK, In that for event handling use IDispEventSimpleImpl Interface, use BEGIN_SINK_MAP and declare handler function (fn) for the specified event using SINK_ENTRY_INFO, but whenever that particular event occurs my handler function gets call twice... Is there any solution so handler function should call only once? Thanks, Shyam.
6
3366
by: alho | last post by:
The web service is called by a program running on pocket pc. When to call the web service, the first call is still ok, but for the second or later calls, it will throw "403 Forbidden" WebException. When to test it using the web interface on pocket IE, everything is fine. Before there was no such problem, it only happened within a week or so. I suspect it's caused by the server's firewall settings, but how come the first call is ok? This...
12
2305
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and switch statements. My first approach was to rely on partial template specialization. Therefore, I have: // .h file template <int vSomeClass;
0
8889
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9116
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6011
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3228
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 we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.