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


相关推荐

  • win10自带恶意软件删除工具

    win10自带恶意软件删除工具win+R—>mrt回车点击下一步启动工具待完成后即检测完毕

    2022年6月24日
    33
  • java正则表达式语法详解及其使用代码实例[通俗易懂]

    java正则表达式语法详解及其使用代码实例[通俗易懂]java正则表达式语法详解及其使用代码实例http://www.zuidaima.com/share/1835085544524800.htm

    2022年7月19日
    21
  • LAMP配置相关

    LAMP配置相关为Ubuntu搭建LAMP(Apache+PHP+MYSQL)开发环境    由于LAMP大部分操作与/var/www目录相关,为了方便,修改该目录的权限为普通用户可访问     # chmod  777 /var/www配置phpMyAdmin http://blog.csdn.net/doupei2006/article/details/8005061

    2022年5月5日
    46
  • 最详细的ensp安装及使用「建议收藏」

    最详细的ensp安装及使用「建议收藏」步骤一:ensp安装1:在华为官网下载全套的ensp(网址如下)2:输入链接后点击ensp最新版本如图·:3:具体软件安装见下图的软件安装指南:4.关于在ensp中使用几个特殊的设备时操作如下:(1).只要用到以下设备,都需要去官网下载相应的镜像文件(2).例如在ensp中选用usg6000v后,右键点击启动:(3).启动设备后会弹出导入设备包的对话框:(4),点击浏览————找到·从官网上下载到的镜像文件即可。步骤二:…

    2022年10月14日
    1
  • dell服务器配置双网卡

    dell服务器配置双网卡

    2022年3月13日
    688
  • vscode前端插件简单配置

    vscode前端插件简单配置AutoCloseTaghtml标签自动闭合Chinese(Simplified)(简体中文)LanguagePackforVisualStudioCodevscode中文插件儿EasyLESS项目无框架无webpack时可以使用的一个less转换插件settings.json配置”less.compile”:{ “out”:false,//是否输出css文件,false为不输出 “compress”:false,//是否压缩 “sour..

    2022年7月25日
    4

发表回复

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

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