no jump_jump out

no jump_jump outCprovidesaformofuser-levelexceptionalcontrolflow,calledanonlocaljump,thattransferscontroldirectlyfromonefunctiontoanothercurrentlyexecutingfunctionwithouthavingtogothroug

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
C provides a form of user-level exceptional control flow, called a nonlocal jump, that transfers control directly from one function to another currently executing function without having to go through the normal call-and-return sequence.

      An important application of nonlocal jumps is to permit an immediate return from a deeply nested function call, usually as a result of detecting some error condition. If an error condition is detected deep in a nested function call, we can use a nonlocal jump to return directly to a common localized error handler instead of laboriously unwinding the call stack. We present an example here.

#include "csapp.h"
#include <iostream>
using namespace std;

jmp_buf buf;

int error1 = 0;
int error2 = 1;

void foo(void), bar(void);

int main()
{
        int rc;
    
		rc = setjmp(buf);
		if (rc == 0)
		        foo();
		else if (rc == 1)
				cout << "Detected an error1 condition in foo" << endl;
		else if (rc == 2)
				cout << "Detected an error2 condition in foo" << endl;
		else
				cout << "Unknown error condition in foo" << endl;
		exit(0);
}

// Deeply nested function foo
void foo(void)
{
		if (error1)
				longjmp(buf, 1);
		bar();
}

void bar(void)
{
		if (error2)
				longjmp(buf, 2);
}

In the above program, the main routine first calls setjmp to save the current calling environment, and then calls function  foo, which in turn calls function bar. If  foo or bar encounter an error, they return immediately from the setjmp via a longjmp call. The nonzero return value of the setjmp indicates the error type.

      Another important application of nonlocal jumps is to branch out of a signal handler to a specific code location, rather than returning to the instruction that was interrupted by the arrival of the signal. The following program uses signals and nonlocal jumps to do a soft restart whenever the user types ctrl-c at the keyboard. The sigsetjmp and siglongjmp functions are versions of setjmp and longjmp that can be used by signal handlers.

#include "csapp.h"
#include <iostream>
using namespace std;

sigjmp_buf buf;

void handler(int sig)
{
    siglongjmp(buf, 1);
}

int main()
{
		Signal(SIGINT, handler);

		if (!sigsetjmp(buf, 1))
				cout << "starting" << endl;
		else
				cout << "restarting" << endl;

		while(1) {
				Sleep(1);
				cout << "processing..." << endl;
		}
		exit(0);
}

The initial call to the sigsetjmp function saves the stack and signal context when the program first starts. The main routine then enters an infinite processing loop. When the user types ctrl-c, the shell sends a SIGINT signal to the process, which catches it. Instead of returning from the signal handler, which would pass back control back to the interrupted processing loop, the handler performs a nonlocal jump back to the beginning of the main program. When we ran the program on our system, we got the following output:

starting
processing…
processing…
restarting          User hits ctrl-c
processing…
restarting          User hits ctrl-c
processing…

Source:

Randal E. Bryant, David R. O’Hallaron(2011). COMPUTER SYSTEMS A Programmer’s Perspective (Second Edition).Beijing: China Machine Press.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/196806.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 初识.Net审计

    初识.Net审计前言对.net认知比较少,学习一下.net的一些简单审计。遇到.net源码能简单审审。基础概念文件类型ASPX.cs是页面后的代码,aspx负责显示,服务器端的动作就是在as

    2021年12月13日
    47
  • eclipse配置android开发环境_eclipse android开发环境搭建

    eclipse配置android开发环境_eclipse android开发环境搭建一、.安装JDK,不再赘述二、安装eclipse,不再赘述三、安装SDK,也就是安卓开发库1.下载并安装AndroidSDK首先,下载AndroidSDKTools,翻过墙的朋友可以去GoogleAndroid的官网上下载(http://developer.android.com/sdk/index.html)。不愿意翻墙的朋友,可以去我的bd网盘上下载(http://pan.baidu.com/s/1nt8BcBB),或者去这个网站下载(http://www.androiddevtools.

    2022年10月4日
    5
  • 如何在mac上查看gcc版本号[通俗易懂]

    如何在mac上查看gcc版本号[通俗易懂]linux能够很方便得查看gcc版本号,只需要输入gcc–version就能得到如下结果,能一目了然gcc版本是4.9.2gcc(GCC)4.9.2Copyright(C)2014FreeSoftwareFoundation,Inc.Thisisfreesoftware;seethesourceforcopyingconditions.ThereisNOwarranty;notevenforMERCHANTABILITYorFITNESSF

    2022年6月26日
    33
  • STP协议详解_STP

    STP协议详解_STP1、生成树技术背景交换机单线路上联,存在单点故障,上行线路及设备都不具备冗余性,一旦链路或上行设备发生故障,业务将会中断。为了使得网络更加健壮、更具有冗余性,将拓扑修改为如下图所示。接入层交换机采用双链路上联到两台汇聚设备,构成一个物理链路冗余的二层环境,解决了单链路及单设备故障问题。但是这样也带来了一个大问题,就是二层物理环境存在环路。二层环路的危害是严重的,有可能会导致广播…

    2025年6月1日
    4
  • Java的输入输出(新手上路版)

    Java的输入输出(新手上路版)

    2021年9月29日
    85
  • php框架tp3.2.3和js写的微信分享功能心得,分享的标题内容图片自定义

    php框架tp3.2.3和js写的微信分享功能心得,分享的标题内容图片自定义

    2021年10月28日
    42

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号