首页 > 其他分享 >CS:APP--Chapter08 : exceptional control flow

CS:APP--Chapter08 : exceptional control flow

时间:2023-01-30 01:33:05浏览次数:40  
标签:control exception exceptional -- flow system process program

CS:APP--Chapter08 : exceptional control flow

标签(空格分隔): CS:APP

目录


prologue

Back to the previous chapters, An introduction on the connection between our program and hardware. From this point forward, we are ganna explore how processor connects program and Operation system.
TO make it connected, exception control flow builds an bridge between program and Operation system to implement the access to system service and concurrency deatiled later.

Overview

We can assume that processor sets up a sequence of value:

\([I_0,I_1...I_k,I_{k+1}]\)

each element indicates each instruction in the sequence formed from the time we apply time on computer to the time we shout it off.

one control flow consist of a sequence of control transitions formed by the adjacent two instructions may without adjacent address.

the simplest control flow is a linear control flow from a of index 0 to a of index k with each abutting pair of instruction having adjacent addresses.

there're some changes in control flow as a result of procedure call by call/ret pair and our program handles it by pushing the state of previous procedure into user-program stack and then popping them after change getting finished.

In practice, there are some change different from the case above, they are highly related to the state of processor. These abrupt changes are called exeception here.

The processing of exception is partly different from procedure depending on the type of exception.

1. Exception

From the perspective of a novice programmer, exception is just the abrupt change transferring control to the destination program, and this abrupt change is called event which corresponds to some kind of change in processor's state. And for each type of event, it is detected by hardware which can make an indirect procedure call, through one entry in exceptional table , to an subroutine called exception handler.

1.1 exception handling

when the exception is detected, the hardware will look up one particular entry in exception table where the address of specified exception handler is here.

One thing need notice that combining the index in the entry corresonding to exception number k and the value in the exception table base register can produce the actual address of the specified exception handler.

此处输入图片的描述
problem: the figure on my book demonstrates the factor mulitplied to entry index is 8 instead of 84 here.

return address: depending on the type of exception, the returning address varies among them. And in order to restart the returning function, some necessary processor states should be pushed into stack.

user-mode and kernel-mode: kernel mode provides the service of exposing the invisible data to user which should invisbile to them.

1.2 classes of exception

  1. interrupts : caused by I/O devices by signal (asynchronously)
  2. trap : used for system call like the procedure-interface between user program and OS(synochronously)
  3. fault : test whether the exception can be fixed immediately(synochronously)
  4. abort : because of some fata errors, terminate the specified program usually after fault(synochronously)

Examples:(index correspond to the case above)

index description the return instruction
1 ctrl+c \(I_{next}\)
2 access file in computer system \(I_{next}\)
3 page fault(fail first time leading to load data from disk to memory and access data second time) if successful \(I_current\)
4 invoke the kill function nothing

2.process

Oen important notation in computer science, process refers to an instance of an running program. the current computer system provides a mechanism that the processor executes these programs one by one without any interruption as if each of program makes an exclusibe use of computer resources. All this illusion is bulit on the concept process

To cut to the chase, the key abstractions on it are:

  1. An independent logical control flow -> exclusive use of process
  2. A private address apace -> exclusive use of memory

2.1 logical control flow

The main reason why it can achieve the exclusive use of process is that all processes take turns using the processor
此处输入图片的描述

2.2 concurrent flows

A control flow whose execution overlaps in time with another control flow is called concurrent flow.

some ideas like multitasking, time slice and time slicing will be detailed later.

2.3 private memory space

A process provides each program with its own private memory space.

2.4 user and kernel mode

processor typically provide this capacity with a mode bit in some control register that characterizes the privileges that the process currently enjoys.

2.5 context switches

The operating system will pause one process and switch to the next for a variety of reasons:

  1. A process must wait for some resources or signals, such as read and sleep, to arrive due to a system call
  2. The clock cycle allocated to the current process is used up, and the received clock is interrupted

In the process of execution, a lot of temporary data will be generated, such as register data, page table, process table file table, various state information and so on, which is called the context information of the process. Therefore, when the operating system suspends the process to switch to the next process, it must properly deal with these context information:

  1. Saves the context of the current process
  2. Restores the context of the process to be executed
  3. Transfer control to the process that is about to execute

此处输入图片的描述

3. system call error handling

pid_t Fork(void) 
{
  pid_t pid;
  if ((pid=fork()) < 0) {
    fprintf(stderr, "fork error: %s\n", strerror(errno));// simple error-handling function
    exit(0);
  }
  return pid;
}

4. process control

so complicated that I need some time to sum them up

5. signal

标签:control,exception,exceptional,--,flow,system,process,program
From: https://www.cnblogs.com/UQ-44636346/p/17074195.html

相关文章