用C的setjmp和longjmp的实际应用 [英] Practical usage of setjmp and longjmp in C

查看:124
本文介绍了用C的setjmp和longjmp的实际应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释我到底在哪的setjmp()的longjmp()函数实际上可以在嵌入式编程中使用?我知道这些都是错误处理。但是,我想知道一些用例。

Can anyone explain me where exactly setjmp() and longjmp() functions can be used practically in embedded programming? I know that these are for error handling. But I'd like to know some use cases.

推荐答案

错误处理结果
假设有一个错误在嵌套在许多其他功能和错误处理功能深处才有意义,在顶层的功能。

Error handling
Suppose there is an error deep down in a function nested in many other functions and error handling makes sense only in the top level function.

这将是非常乏味和尴尬,如果两者之间的所有的功能必须正常返回和评估返回值或全局错误变量,以确定进一步的处理没有意义,甚至会很糟糕。

It would be very tedious and awkward if all the functions in between had to return normally and evaluate return values or a global error variable to determine that further processing doesn't make sense or even would be bad.

这就是了setjmp / longjmp的有意义的情况。
这些情况都是类似情况下的例外在其他语言研究(C ++,Java的)意义。

That's a situation where setjmp/longjmp makes sense. Those situations are similar to situation where exception in other langages (C++, Java) make sense.

协同程序结果
除了错误处理,我也可以想到的,你需要在setjmp的C / longjmp的另一种情况:

Coroutines
Besides error handling, I can think also of another situation where you need setjmp/longjmp in C:

这是这样,当你需要实现协程

It is the case when you need to implement coroutines.

下面是一个小演示的例子。
我希望它满足从Sivaprasad帕拉斯一些例如code请求和答案怎样的setjmp / longjmp的支持corroutines的实现(就像我看到它不底座上任何非标准或新的行为TheB​​lastOne的问题)。

Here is a little demo example. I hope it satisfies the request from Sivaprasad Palas for some example code and answers the question of TheBlastOne how setjmp/longjmp supports the implementation of corroutines (as much as I see it doesn't base on any non-standard or new behaviour).

编辑:结果
这可能是因为它实际上的的未定义行为做了的longjmp 的调用堆栈(见MikeMB的评论;虽然我还没有机会了验证)。


It could be that it actually is undefined behaviour to do a longjmp down the callstack (see comment of MikeMB; though I have not yet had opportunity to verify that).

#include <stdio.h>
#include <setjmp.h>

jmp_buf bufferA, bufferB;

void routineB(); // forward declaration 

void routineA()
{
    int r ;

    printf("(A1)\n");

    r = setjmp(bufferA);
    if (r == 0) routineB();

    printf("(A2) r=%d\n",r);

    r = setjmp(bufferA);
    if (r == 0) longjmp(bufferB, 20001);

    printf("(A3) r=%d\n",r);

    r = setjmp(bufferA);
    if (r == 0) longjmp(bufferB, 20002);

    printf("(A4) r=%d\n",r);
}

void routineB()
{
    int r;

    printf("(B1)\n");

    r = setjmp(bufferB);
    if (r == 0) longjmp(bufferA, 10001);

    printf("(B2) r=%d\n", r);

    r = setjmp(bufferB);
    if (r == 0) longjmp(bufferA, 10002);

    printf("(B3) r=%d\n", r);

    r = setjmp(bufferB);
    if (r == 0) longjmp(bufferA, 10003);
}


int main(int argc, char **argv) 
{
    routineA();
    return 0;
}

下图显示了执行流程:结果

Following figure shows the flow of execution:

警告注意结果
当使用了setjmp / longjmp的注意,它们对局部变量往往没有考虑的有效性产生影响。结果
参看我 href=\"http://stackoverflow.com/questions/1393443/setjmp-longjmp-and-local-variables\">问题。

Warning note
When using setjmp/longjmp be aware that they have an effect on the validity of local variables often not considered.
Cf. my question about this topic.

这篇关于用C的setjmp和longjmp的实际应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆