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.

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 3500元计算机基本硬件配置清单,电脑硬件中配配置清单

    3500元计算机基本硬件配置清单,电脑硬件中配配置清单电脑硬件中配配置清单导语:攒机一直是电脑爱好者热衷的事,自己选购各配件,组装一台适合自己要求的机器给很多DIYer带来了极大的欢乐。下面小编为你整理的电脑硬件中配配置清单,希望对你有所帮助!电脑硬件中配配置清单1、实用性机型建议:首选1:intelG1620双核+H61M主板。(价格低廉性能不弱,超值!)首选2:intelG1840双核+H81M主板。(核心显卡性能比G1620更强)2、中级机…

  • spring配置文件详解_jedis连接redis集群

    spring配置文件详解_jedis连接redis集群[b]JedisCache[/b][code="java"]importjava.io.IOException;importjava.util.Set;importorg.springframework.beans.factory.InitializingBean;importorg.springframework.util.Assert;importre…

  • esxi 嵌入式vCenter 6.5 不用设置DNS安装部署说明

    esxi 嵌入式vCenter 6.5 不用设置DNS安装部署说明

  • 海量数据挖掘MMDS week2: 频繁项集挖掘 Apriori算法的改进:基于hash的方法[通俗易懂]

    http://blog.csdn.net/pipisorry/article/details/48901217海量数据挖掘MiningMassiveDatasets(MMDs)-JureLeskovec courses学习笔记之关联规则Apriori算法的改进:基于hash的方法:PCY算法,Multistage算法,Multihash算法Apriori算法的改进{

  • 关系图谱在反欺诈场景中的应用及实践「建议收藏」

    关系图谱在反欺诈场景中的应用及实践「建议收藏」关系图谱概要随着近几年互联网金融的发展,玲琅满目的信贷产品早已被羊毛党盯上,层出不穷的营销活动更是让欺诈分子有了可乘之机,伪造资料、恶意注册大量虚假账号、团伙包装、刷单、抢红包、套返利等等,他们的欺诈技术手段也越来越高明(群控、云控),成本也越来越低。为了限制这些欺诈用户,信贷机构通过建立反欺诈团队和风控防范体系,使用专家规则和预测模型来拦截欺诈份子。但是道高一尺魔高一丈,再严密的规则也…

  • 廖雪峰git学习资料-涂改笔记

    廖雪峰git学习资料-涂改笔记注意:本文章是看廖雪峰官网资料整理而来原地址如下:http://www.liaoxuefeng.com/附件为git常用命令 前言:注意的问题如果是首次提交会第一步:先在本地建立一个一样的仓库,称本地仓库。 第二步:在本地进行commit操作将把更新提交到本地仓库; 第三步:将服务器端的更新pull到本地仓库进行合并,最后将合并好的本地仓库push到服务…

发表回复

您的电子邮箱地址不会被公开。

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