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)
上一篇 2025年9月19日 下午7:15
下一篇 2025年9月19日 下午7:43


相关推荐

  • SQL Server——数据库创建及修改[通俗易懂]

    SQL Server——数据库创建及修改[通俗易懂]文章目录一、SQL Server数据库的相关概念1、逻辑数据库2、物理数据库二、SQL Server数据库的基本操作1、使用SQL Server Management Studio (创建/修改/删除)数据库2、使用SQL语句(创建/修改/删除)数据库【数据库文件的类型】【主要】【次要】【事务日志】文件组1.主文件组(PRIMARY)2.用户定义文件组【文件组特性】一、SQL Server数据库…

    2022年8月18日
    9
  • 关于代理服务器的原理及用法「建议收藏」

    关于代理服务器的原理及用法「建议收藏」关于代理服务器的原理及用法一,什么是代理服务器(Proxy)?答:以类似代理人的身份去取的用户需要的数据。由于它的【代理】能力,使得我们可以透过代理服务器来达成防火墙与用户数据的分析。除此之外我们还可以借助代理服务器来达成节省带宽的目的,以及加快内部网络对因特网的万维网的访问速度。当客户端有因特网的数据要求时,Proxy会帮用户去向目的地取得用户所需要的数据。所以当客户端指定www的代理服务器之后,用户的所有www相关要求就会通过代理服务器去捉取。代理服务器会架设在整个区网的单点对外防火墙上头,而在区

    2022年5月4日
    93
  • Redis管理工具安装和使用「建议收藏」

    Redis管理工具安装和使用「建议收藏」一、AnotherRedisDesktopManagergithub地址https://github.com/qishibo/AnotherRedisDesktopManagercsdn下载地址https://download.csdn.net/download/g313105910/18412412选择适合你的版本下载安装,后运行输入ip和密码然后就可以查看和修改数据了二、RedisDesktopManagergithub下载地址h

    2025年5月24日
    4
  • 谱图理论(spectral graph theory)

    谱图理论(spectral graph theory)介绍如何理解特征值和特征向量此部分参考了马同学的文章:如何理解矩阵特征值和特征向量?我们知道一个矩阵可以看做是线性变换又或者是某种运动,可以将一个向量进行旋转,平移等等操作,正常来说,对于一个向量vvv,并对其乘上一个A会出现下图的情况:可以看到乘了A之后v发生了一些旋转。然而所有向量中存在一种稳定的向量,他不会发生旋转,平移,只会使得向量变长或变短,而这种稳定的向量正是矩阵的特征向…

    2025年6月6日
    4
  • 浅析SkipList跳跃表原理及代码实现

    浅析SkipList跳跃表原理及代码实现SkipList 在 leveldb 以及 lucence 中都广为使用 是比较高效的数据结构 由于它的代码以及原理实现的简单性 更为人们所接受 我们首先看看 SkipList 的定义 为什么叫跳跃表 Skiplistsare Asaresult thealgorithm

    2026年3月26日
    3
  • 群晖 winscp php,群晖DSM开启ROOT权限及WinSCP使用ROOT登录

    群晖 winscp php,群晖DSM开启ROOT权限及WinSCP使用ROOT登录本文以群晖DSM6.1.7(以下简称DSM)为例:一、准备工具1、putty2、WinSCP下载地址:http://pan.myxzy.com/download.php?id=81二、DSM开启SSHDSM的“控制面板”—>“终端机和SNMP”,勾上“启动Telnet功能”和“启动SSH功能”的勾,然后点击“应用”三、开启ROOT账号和修改密码1、使用putty连接DSM主机名称填…

    2022年6月6日
    1.4K

发表回复

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

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