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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数据库水平切分的实现原理解析---分库,分表,主从,集群,负载均衡器

    数据库水平切分的实现原理解析---分库,分表,主从,集群,负载均衡器第1章 引言        随着互联网应用的广泛普及,海量数据的存储和访问成为了系统设计的瓶颈问题。对于一个大型的互联网应用,每天几十亿的PV无疑对数据库造成了相当高的负载。对于系统的稳定性和扩展性造成了极大的问题。通过数据切分来提高网站性能,横向扩展数据层已经成为架构研发人员首选的方式。水平切分数据库:可…

    2022年6月13日
    20
  • 八路抢答器c语言程序,八路抢答器-51单片机(附Proteus仿真、C代码、原理图及PCB、论文和元器件清单)…

    八路抢答器c语言程序,八路抢答器-51单片机(附Proteus仿真、C代码、原理图及PCB、论文和元器件清单)…获取全套设计资源,请见后文说明…设计要求1)抢答器同时供8名选手或2个代表队比赛,分别用8个按钮S0-S7表示;2)设置一个系统清除和抢答控制开关S,该开关由主持人控制;3)抢答器具有锁存与显示功能。即选手按动按钮,锁存相应的编号,并在优先抢答选手的编号一直保持到主持人将系统清除为止;4)抢答器具有定时抢答功能,且一次抢答的时间由主持人设定(如30s等)。当主持人启动“开始”按键后,定时器进行减计…

    2022年10月20日
    0
  • Java- Set 转换成List

    Java- Set 转换成List转载:https://blog.csdn.net/fan158/article/details/28234035Set转换成List有两种方法,假设有Set集合Set<String>myset=newHashSet<String>();1.使用Arrays.asList(T…a)转换成List,此转换返回的list…

    2022年10月19日
    0
  • postman接口自动化测试实战_postman是干嘛的

    postman接口自动化测试实战_postman是干嘛的Apifox介绍Apifox是API文档、API调试、APIMock、API自动化测试一体化协作平台,定位Postman+Swagger+Mock+JMeter。通过一套系

    2022年7月30日
    7
  • CentOS 6 Yum源更新「建议收藏」

    CentOS 6 Yum源更新「建议收藏」最近在CentOS6上安装软件一直报错,安装不了,换了163,清华,阿里yum源都不行,进去链接看才发现有关CentOS6yum源包全部下架了。官宣:CentOS6停止所有更新​​​​CentOS6已经随着2020年11月的结束进入了EOL(ReachesEndofLife)。所以在2020年12月2日,CentOS官方停止了对CentOS6的所有更新,并且下架了包括官方所有的CentOS6源,目前阿里、163、清华等CentOS6源已无法使用。以下官方redme文档的解释:.

    2022年9月6日
    5
  • 网络RJ45接口详解[通俗易懂]

    网络RJ45接口详解[通俗易懂]RJ45简介图1RJ45模块RJ45模块用于实现PHY之间的互连,包括PHY芯片经信号变压器与RJ45接口相连,如图1所示。RJ45连接器由插头和插座组成,RJ45插头又称水晶头,如图3-10所示。这两种元件组成的连接器连接于导线之间,以实现导线的电气连续性。RJ45连接器就是连接器中的最重要的一种插座。RJ45插座分屏蔽型和非屏蔽型两种。RJ是RegisteredJack的…

    2022年9月17日
    0

发表回复

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

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