Exceptional Control Flow

The program counter assumes a sequence of values

                                 a0,a1,...,an−1

where each ak is the address of some corresponding instruction Ik. Each transition from ak to ak +1 is called a control transfer. A sequence of such control transfers is called the
flow of control,orcontrol flowof the processor.

Typically, abrupt changes to this smooth flow, where Ik +1 is not adjacent to Ik, are caused by familiar program instructions such as jumps, calls, and returns.

In general, we refer to these abrupt changes asexceptional control flow (ECF).   At the operating systems level, the kernel transfers control from one user process to another
via context switches.

8.1 Exceptions

Exceptions are a form of exceptional control flow that are implemented partly by the hardware and partly by the operating system.

An
exception is an abrupt change in the control flow in response to some change in the processor’s state.

The change in state is known as anevent.

In any case, when the processor detects that the event has occurred, it makes an indirect procedure call (the exception), through a jump table called anexception table, to
an operating system subroutine (the exception handler ) that is specifically designed to process this particular kind of event.

When the exception handler finishes processing, one of three things happens, depending on the type of event that caused the exception:

1. The handler returns control to the current instruction Icurr, the instruction that was executing when the event occurred.

2. The handler returns control to Inext, the instruction that would have executed next had the exception not occurred.

3. The handler aborts the interrupted program

exception 和 procedure call 的异同点

  • As with a procedure call, the processor pushes a return address on the stack before branching to the handler. However, depending on the class of exception, the return address is either the current instruction (the
    instruction that was executing when the event occurred) or the next instruction (the instruc-tion that would have executed after the current instruction had the event not occurred).
  • The processor also pushes some additional processor state onto the stack that will be necessary to restart the interrupted program when the handler returns. For example, an IA32 system pushes the EFLAGS register
    containing, among other things, the current condition codes, onto the stack.
  • If control is being transferred from a user program to the kernel, all of these items are pushed onto the kernel’s stack rather than onto the user’s stack.
  • Exception handlers run inkernel mode(Section 8.2.4), which means they have complete access to all system resources.

8.1.2 Classes of Exceptions

Exceptions can be divided into four classes:interrupts, traps, faults , and aborts .

The table in Figure 8.4 summarizes the attributes of these classes.

Interrupts

Interrupts occur asynchronously as a result of signals from I/O devices that are external to the processor. Hardware interrupts are asynchronous in the sense that they are not caused by the execution of any particular
instruction. Exception handlers for hardware interrupts are often called interrupt handlers .

Traps and System Calls

Traps are intentional exceptions that occur as a result of executing an instruction. Like interrupt handlers, trap handlers return control to the next instruction. The most important use of traps is to provide
a procedure-like interface between user programs and the kernel known as a system call

Faults

Faults result from error conditions that a handler might be able to correct. When a fault occurs, the processor transfers control to the fault handler. If the handler is able to correct the error condition, it
returns control to the faulting instruction, thereby reexecuting it. Otherwise, the handler returns to an abort routine in the kernel that terminates the application program that caused the fault.

Aborts

Aborts result from unrecoverable fatal errors, typically hardware errors such as parity errors that occur when DRAM or SRAM bits are corrupted. Abort handlers never return control to the application program. As
shown in Figure 8.8, the handler returns control to an abort routine that terminates the application program.

Aside A note on terminology

The terminology for the various classes of exceptions varies from system to system. Processor macroar-chitecture specifications often distinguish between asynchronous “interrupts” and synchronous “exceptions,”  we use the word “exception” as
the general term and distinguish between asynchronous exceptions (interrupts) and synchronous ex-ceptions (traps, faults, and aborts) only when it is appropriate.  You should be aware that some manufacturers’ manuals use the word “exception” to refer only
to those changes in control flow caused by synchronous events.

8.2 Processes

Each time a user runs a program by typing the name of an executable object file to the shell, the shell creates a new process and then runs the executable object file in the context of this new process.

An independentlogical control flow that provides the illusion that our pro-gram has exclusive use of the processor.

A private address space that provides the illusion that our program has exclu-sive use of the memory system.

关于进程的概念,这里能够看看 《MOS》的第二章,额。。还没做笔记,但愿这个月能够搞定补上啊。。。。

8.2.1 Logical Control Flow

This sequence of PC values is known as a logical control flow , or simply logical flow .

The key point in Figure 8.12 is that processes take turns using the processor. Each process executes a portion of its flow and then ispreempted (temporarily suspended) while other processes take their turns.

8.2.2 Concurrent Flows

A logical flow whose execution overlaps in time with another flow is called a concurrent flow , and the two flows are said to run concurrently.

More precisely, flows X and Y are concurrent with respect to each other if and only if X begins after Y begins and before Y finishes, or Y begins after X begins and before X finishes. For example, in Figure 8.12,
processes A and B run concurrently, as do A and C. On the other hand, B and C do not run concurrently, because the last instruction of B executes before the first instruction of C.

The general phenomenon of multiple flows executing concurrently is known as concurrency.

Notice that the idea of concurrent flows is independent of the number of processor cores or computers that the flows are running on. If two flows overlap in time, then they are concurrent, even if they are running
on the same processor. However, we will sometimes find it useful to identify a proper subset of concurrent flows known as parallel flows . If two flows are running concurrently on different processor cores or computers, then we say that they are parallel flows
, that they are running in parallel , and have parallel execution.

8.2.3 Private Address Space

On a machine withn-bit addresses, the address space is the set of 2^n possible addresses, 0, 1,..., (2 ^n) − 1. A process provides each program with its ownprivate address space. This space is private in the sense that
a byte of memory associated with a particular address in the space cannot in general be read or written by any other process.

8.2.4 User and Kernel Modes

When the mode bit is set, the process is running in kernel mode (sometimes called supervisor mode). A process running in kernel mode can execute any instruction in the instruction set and access any memory location
in the system. When the mode bit is not set, the process is running inuser mode.

User programs must instead access kernel code and data indirectly via the system call interface.

8.2.5 Context Switches

The kernel maintains a context for each process. The context is the state that the kernel needs to restart a preempted process.

At certain points during the execution of a process, the kernel can decide to preempt the current process and restart a previously preempted process. This decision is known as scheduling , and is handled by code
in the kernel called the scheduler.

A context switch can occur while the kernel is executing a system call on behalf of the user. If the system call blocks because it is waiting for some event to occur, then the kernel can put the current process
to sleep and switch to another process.

8.4 Process Control

进程控制不去看APUE天理不容啊!

眼下为止关于APUE全部的笔记:

APUE的Process control

http://blog.csdn.net/cinmyheart/article/details/22298649

8.5 Signals

信号嘛,还是去看APUE

只是这里讲的非常好:

The transfer of a signal to a destination process occurs in two distinct steps:



                Sending a signal.The kernel sends (delivers) a signal to a destination process by updating some state in the context of the destination process. The signal is delivered for one of two reasons: (1) The kernel
has detected a system event such as a divide-by-zero error or the termination of a child process. (2) A process has invoked the killfunction (discussed in the next section) to explicitly request the kernel to send a signal to the destination process. A process
can send a signal to itself.



                 Receiving a signal. A destination processreceives a signal when it is forced by the kernel to react in some way to the delivery of the signal. The process can either ignore the signal, terminate, or catchthe
signal by executing a user-level function called a signal handler. Figure 8.26 shows the basic idea of a handler catching a signal.

A signal that has been sent but not yet received is called apending signal.At any point in time, there can be at most one pending signal of a particular type. If a process has a pending
signal of typek , then any subsequent signals of type k sent to that process are not queued; they are simply discarded.

A pending signal is received at most once. For each process, the kernel main-tains the set of pending signals in the pending bit vector, and the set of blocked signals in the blocked
bit vector. The kernel sets bit k in pending whenever a sig-nal of type k is delivered and clears bit k in pending whenever a signal of typek is received.

8.7 Tools for Manipulating Processes

Linux systems provide a number of useful tools for monitoring and manipulating processes:

strace : Prints a trace of each system call invoked by a running program and its children. A fascinating tool for the curious student. Compile your
program with -static to get a cleaner trace without a lot of output related

to shared libraries.

ps : Lists processes (including zombies) currently in the system.

top: Prints information about the resource usage of current processes.

pmap: Displays the memory map of a process.

/proc : A virtual filesystem that exports the contents of numerous kernel data structures in an ASCII text form that can be read by user programs. For example,
type “ cat/proc/loadavg ” to see the current load average on

your Linux system.

衡山下的老巷子

版权声明:本文博主原创文章。博客,未经同意不得转载。

《CS:APP》 chapter 8 Exceptional Control Flow 注意事项的更多相关文章

  1. Chapter 8: Exceptional Control Flow

    概述: 我们可以用一种“流”的概念来理解处理器的工作流程,PC(Program Counter)依次为a0,a1,a2,...,an-1,这个序列可以称作control flow.当然我们并不总是按顺 ...

  2. CS:APP Chapter 3 程序的机器级表示-读书笔记

    3.1 程序的机器级表示 发展历史 Intel,AMD,ARM 等企业各有又是,CPU 从 8 位发展到 16 位,再到 32 位,近几年发展到 64 位,当下的 CPU 体系被称为 x86-64 体 ...

  3. CSAPP Chapter 8:Exception Control Flow

    prcesssor在运行时,假设program counter的值为a0, a1, ... , an-1,每个ak表示相对应的instruction的地址.从ak到ak+1的变化被称为control ...

  4. Core Java Volume I — 3.8. Control Flow

    3.8. Control FlowJava, like any programming language, supports both conditional statements and loops ...

  5. CS:APP配套实验 Data Lab

    刚刚完成注册博客,想写一篇随笔,方便以后自己回顾.如果恰好也能帮助到你,是我的荣幸. 这次随笔是记载我的计算机系统(CS:APP,Computer Systems:A Programer's Pers ...

  6. SSIS的 Data Flow 和 Control Flow

    Control Flow 和 Data Flow,是SSIS Design中主要用到的两个Tab,理解这两个Tab的作用,对设计更高效的package十分重要. 一,Control Flow 在Con ...

  7. Control Flow 如何处理 Error

    在Package的执行过程中,如果在Data Flow中出现Error,那么Data Flow component能够将错误行输出,只需要在组件的ErrorOutput中进行简单地配置,参考<D ...

  8. 关于Control flow

    1.一个package包含一个control flow并且一个或多个data flow. (这个项目叫做 Integration services project,提供了三种不同类型的control  ...

  9. SSIS ->> Control Flow And Data Flow

    In the Control Flow, the task is the smallest unit of work, and a task requires completion (success, ...

随机推荐

  1. Android 4.0新组件:GridLayout详细说明

    于Android 4.0(API 14)它提供了一个新的组件GridLayout,它继承自Linearlayout,用于执行网络格样式布局. 在某些方面,GridLayout与TableLayout和 ...

  2. HDU 2063 过山车 二分图题解

    一个男女搭配的关系图,看能够凑成多少对,基本和最原始的一个二分图谜题一样了,就是 一个岛上能够凑成多少对夫妻的问题. 所以是典型的二分图问题. 使用匈牙利算法,写成两个函数,就很清晰了. 本程序还带分 ...

  3. java编程规范之java注释规范

    代码要是没有注释,对读者来说就是一堆乱七八糟的字母,为了提高代码的可读性和可维护性,必须对代码进行必要的注释,这里小编整理了一下java注释规范. (一)技巧 1:注释当前行快捷方式:ctrl+/ 2 ...

  4. memset功能的具体说明

    1.void *memset(void *s,int c,size_t n)总的效果:内存空间开辟了 s 第一 n 字节的值设置为一个值 c. 2.样本#include void main(){cha ...

  5. iOS编程之前

    iOS编程之前 更新:帖子已经重新被更新过,以便能更好的兼容Xcode 5和iOS 7.       至今为止,已经超过6000位读者加入了这个iOS免费教程.首先,我要感谢这些加入我们社区的朋友.在 ...

  6. Visual Studio 连接不上NuGet 官方程序包源的解决办法

    修改dns 即可

  7. Apache Commons Math3学习笔记(2) - 多项式曲线拟合(转)

    多项式曲线拟合:org.apache.commons.math3.fitting.PolynomialCurveFitter类. 用法示例代码: // ... 创建并初始化输入数据: double[] ...

  8. MyReport报表引擎2.0.0.0新功能

    Web报表引擎:Web上的良好的打印解决方式,WinForm的打印预览体现,报表自己主动化,支持直接打印,页小计,统计,转成金额大写,一维码显示等功能,满足中国式报表的常见功能需求.Web报表编辑器: ...

  9. Objective-C开发编码规范

    Objective-C 编码规范,内容来自苹果.谷歌的文档翻译,自己的编码经验和对其它资料的总结. 概要 Objective-C 是一门面向对象的动态编程语言,主要用于编写 iOS 和 Mac 应用程 ...

  10. ajaxFileUpload+struts2多文件上传(动态添加文件上传框)

    上一篇文章http://blog.csdn.net/itmyhome1990/article/details/36396291介绍了ajaxfileupload实现多文件上传, 但仅仅是固定的文件个数 ...