20135306 Huang Eighth Chapter study Summary

Source: Internet
Author: User

Eighth Chapter Exception Control Flow

Control Flow: Controls the transfer sequence.

Control transfer: From an instruction to the next instruction.

Exception control Flow: The modern operating system responds to the system state by causing mutations in the control flow, known as abnormal control flows.

8.1 Exception

The anatomy of the exception, as shown in:

Exception handling

    1. Exception table: When the processor detects that an event occurs, it makes an indirect procedure call (exception) to the exception handler through the Jump table.
    2. Exception number: One of the possible types of exceptions in the system is assigned an exception number that is a unique nonnegative integer. The exception number is the index to the exception table.
    3. Exceptions are similar to procedure calls, but there are some important differences:
      (1) When a procedure is called, the processor returns the address to the stack before jumping to the handler, however, depending on the type of the exception, the return address is either the current instruction (the instruction being executed when the event occurs) or the next instruction (an instruction that executes after the current instruction if the event does not occur).
      (2) The processor also pushes some additional processor states into the stack, which is required when the handler returns, restarting the interrupted program. For example, a A32 system will press the EHAGS register containing the current condition code and other content into the stack.
      (3) If control is transferred from one user program to the kernel, then all of these items are pressed into the kernel stack instead of being pressed into the user stack.
      (4) Exception handlers run in kernel mode (see section 824) which means they have full access to all system resources
    • Once the hardware has triggered an exception, the exception handler is completed by the software.

Category of exception

Categories of exceptions-interrupts, traps, failures, and terminations

    1. Interrupt handling: Asynchronous means that a hardware interrupt is not caused by any instruction, but by an event of an external I/O device.
    2. Traps and system calls: System calls are encapsulated functions that are internally implemented by instruction Int N.
      The most important use of traps is to provide system calls. System calls run in kernel mode and can access stacks in the kernel.
      The parameters of the system call are passed through a generic register instead of a stack, such as the%EAX storage system call number,%EBX,%ECX,%EDX,%ESI,%EDI,%EBP stores up to six parameters,%esp is not available, because it is overwritten after entering kernel mode.
    3. Fault
      A classic Failure example is a page fault, when the instruction refers to a virtual address, and the virtual address corresponds to the physical pages that are not in memory and therefore must be removed from the disk when a failure occurs.
    4. Terminate
      Termination is the result of an unrecoverable fatal error, usually a hardware error, such as a parity error that occurs when a dram or SRAM bit is damaged. The terminating handler never returns control to the application. The handler returns control to an abort routine, which terminates the application.

Linux/ia32 exceptions in the system

    1. LINUX/IA32 Failure and termination

    1. LINUX/IA32 system Call


Each system call has a unique integer number that corresponds to the offset from a jump table in the kernel.

8.2 Process

    1. Process (operating system layer): logical control flow, private address space, multitasking, concurrency, parallelism, context, context switching, scheduling.
    2. A process is an executing instance of a program. Each program in the system is running in the context of a process.
    • The key abstraction that a process provides to an application: a) A separate logical control flow; b) a private address space.

Logical Control Flow

The sequence of program counter (PC) values is called logical control flow, or logical flow. As shown, a physical control flow of the processor is divided into three logical streams, one per process.

Concurrent streams

    1. Concurrent flow: Concurrent flow the execution of a logical stream overlaps another stream in time, called a parallel stream
    2. Concurrency: The general phenomenon of concurrent execution of multiple streams is called concurrency.
    3. Multitasking: Multiple processes concurrency is called multitasking.
    4. Parallelism: Concurrent streams on different CPUs or computers, called parallelism.

Private address space

A process provides its own private address space for each program.

User mode and kernel mode

    1. The process that runs the application code is initially in user mode. The only way the process changes from user mode to kernel mode is through exceptions.
    2. Linux provides the/proc file system, which allows the user-mode process to access the contents of the kernel data structure.

Context Switches

    1. Context switching: The operating system kernel uses exception control flows called context switches to achieve multitasking.
    2. Context Switch: A) Saves the context of the current process; b) Restores the context in which a previously preempted process was saved; c) passes control to the newly restored process
    3. Dispatch: The scheduler in the kernel implements scheduling.
    4. Context switching may occur when the kernel performs context switching on behalf of the user. If a system call is blocked, the kernel can let the current process hibernate and switch to another process, such as a read system call, or sleep will display a request for the calling process to hibernate. In general, the kernel can also determine context switching, rather than returning control to the calling process, even if the system call is not blocked.
    • Interrupts can also cause context switching. For example, the timer is interrupted.

8.3 system invoke error handling

System invoke error handling

When UNIX system-level functions encounter errors, they typically return-1, and set the global integer variable errno to indicate an error.
See Appendix A For more details on learning.

8.4 Process Control get process ID

Creating and terminating processes

The exit function terminates the process with the status exit state.

The parent process creates a new run child process by calling the fork function. The newly created child process is almost but not exactly the same as the parent process. When the parent process calls fork, the child process can read and write to any file that is open in the parent process. However, there is a different PID between the parent and child processes.
The fork function is detailed:
is called once and returns two times.
Concurrent execution.
The same but separate address space.
Share files.

Reclaim Child processes

When a process terminates for some reason, it is saved in a state that has been terminated until it is reclaimed by his parent process. When the parent process recycles a child process that has already been terminated, the kernel passes the child process's exit state to the parent process and discards the terminated process, which does not exist.
Zombie Process : a process that terminates but is not recycled.
A process can wait for his child process to terminate or stop by calling the Waitpid function.

Process hibernation

The sleep function suspends a process for a specified amount of time.

The pause function lets the calling function hibernate until the process receives a signal.

Load and run the program

The EXECVE function loads and runs a new program in the context of the current process.

8.5 Signal

    1. A higher-level form of software exception, called a UNIX signal, allows a process to interrupt other processes.
    2. Low-level hardware exceptions are handled by the kernel exception handlers, which are normally not visible to the user process. The signal provides a mechanism to inform the user that the process has occurred with these exceptions.

Signal terminology

    1. Sending a signal to the destination process is made up of two steps
      1. Send a signal. The kernel sends (delivers) a signal to the destination process by updating a state in the context of the destination process.
      There are two possible reasons for sending a signal:
      1) The kernel detects a system event.
      2) A process calls the Kill function, which explicitly requires the kernel to send a signal to the destination process, and a process can send a signal to itself.
      2. Receive the signal. The destination process receives a signal when the destination process is forced by the kernel to respond in some way. The process can ignore this signal, terminate it, or do not live by executing a user layer function called a signal handler.
    2. A signal that is only emitted and not received is called a pending signal. At any one time, a type can have at most one pending signal.
    3. A process can selectively block the receipt of certain signals. When a signal is blocked, he can still be sent, but the resulting pending signal is not received until the process cancels blocking the signal.
    4. A pending signal can be received at most once.

Send Signal

1. Process Group

    • The process group. Each process belongs to only one process group, and the process group is identified by a positive integer process group ID. A child process and its parent process belong to a process group, and a process group can change itself or a process group of other processes by using the Setpgid function.
      2. Send a signal using the/bin/kill program
    • Use the/bin/kill program to send an arbitrary signal to another process.
      3. Send a signal from the keyboard
    • Send signal shell from keyboard Create a separate process group for each job.
      4. Send a signal using the Kill function
    • Processes send signals to other processes (including themselves) by calling the Kill function.
      5. Send a signal using the alarm function
    • A process can send a SIGALRM signal to himself by calling the alarm function.

Receive Signal

    1. When the kernel returns from an exception handler and prepares to pass control to process p, he examines the set of non-blocked processing signals for process p. If the set is empty, then the kernel controls the next instruction in the logical control flow to P, and if the collection is nonempty, the kernel selects a signal K (usually the smallest K0) in the collection and forces p to receive the signal K. Receiving this signal triggers some kind of behavior in the process. Once the process has completed this behavior, the control passes back to the next instruction in the logical control flow of P.
    2. Each signal type has a predetermined default behavior:
      (1) Process termination
      (2) Process termination and dump memory
      (3) process stops until the Sigcont model is restarted
      (4) process ignores the signal
    3. The signal function can change the behavior associated with the signal signum by one of the following three methods:
      (1) If handler is sig_ign, then the signal type Signum is ignored.
      (2) If handler is SIG_DFL, then the signal behavior of type Signum reverts to the default behavior
      (3) Otherwise, handler is the address of the user-defined function, which becomes the signal handler, so long as the process receives a signal of type Signum, it will call this program, by passing the address of the handler to the signal function to change the default behavior, This is called setting the signal handler.
    4. But when a process does not live a signal of type K, the handler set for the signal K is called, and an integer parameter is set to K. This parameter allows the same processing function to capture different types of signals.
    5. Execution of the signal handler interrupts the execution of the main C function, similar to how the underlying exception handler interrupts the current application's control flow, because the signal handler's logical control flow overlaps the logical control flow of the main function, and the signal handler and main function run concurrently.

Signal processing problems

    1. When a program captures multiple signals, some minor problems arise.
      (1) The pending signal is blocked. UNIX signal handlers typically block pending signals of the type being processed by the current handler.
      (2) The pending signal is not queued for processing. Any type at most has only one pending signal. Therefore, if a signal of two type K is transmitted to a destination process, and because the destination process is currently executing a signal K handler, so the signal K is blocked, then the second and the signal is simply discarded, he will not wait in line.
      (3) system call can be interrupted. System calls such as read, wait, and accept can potentially block a process for a long time, called a slow system call. In some systems, when the handler captures a signal, the interrupted slow system call no longer resumes when the signal handler returns, but immediately returns to the user an error condition and sets the errno to Eintr.
    2. It is not possible to use signals to treat events that occur in other processes.

Portable Signal Processing

The signal processing semantics of the signal handler set by the Signal wrapper function:
(1) Only the type of signal that the handler is currently processing is blocked
(2) As with all signal implementations, the signal will not wait in line
(3) If possible, the interrupted system call will be restarted automatically.
(4) Once the signal processing program is set, it will remain, knowing that signal with handler parameter is Sig_ign or SIG_DFL is called.

Synchronizing streams to avoid annoying concurrency errors

    1. In general, the number of flows that may be interleaved is exponentially related to the number of instructions.
    2. Synchronize and communicate in some way to get the largest possible staggered set, with the right results for every possible interleaving.
    3. The question of how to write concurrent streaming programs that read and write to the same storage location has plagued generations of computer scientists. For example, the issue of competition.

8.6 non-local jump

    1. The C language provides a form of user-level exception control flow called a local jump. Provided through the setjmp and LONGJMP functions.
    2. The SETJMP function is called only once, but is returned multiple times: once when setjmp is first called and the calling environment is saved in the buffer env, once for each corresponding longjmp call. On the other hand, longjmp is called only once, but never returned. The sig-function is a version of the setjmp and LONGJMP functions that can be used by a signal handler.
    3. An important application of non-local jumps is to allow immediate return from a deeply nested function call, usually caused by an error condition being detected.
    4. Another important application of non-local jumps is to have a signal handler branch to a special code location instead of returning to the location of the command that was interrupted.

8.7 tools for manipulating processes

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

    • STRACE: Prints the trajectory of each system call that is being called by a running program and its child processes. For the curious tool. Compiling your program with-static can be uploaded to a cleaner, non-student-like trajectory that is fascinating to have a lot of output associated with shared libraries.
    • PS: Lists the processes in the current system (including zombie processes)
    • TOP: Prints out information about the current process resource usage.
    • PMAP: Displays the memory mappings for the process. Proc: A virtual file system that outputs the contents of a large number of kernel data structures in ASCII text format, the user program can be cat 2/proc/load avg ", observing the average load on Linux systems.

Resources

    1. Teaching Material: Eighth chapter, detailed study instruction: http://group.cnblogs.com/topic/73069.html
    2. Course Information: https://www.shiyanlou.com/courses/413 Experiment 10, Course invitation code: W7FQKW4Y
    3. The code in the textbook run, think about, read the Code learning method: Http://www.cnblogs.com/rocedu/p/4837092.html.

20135306 Huang Eighth Chapter study Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.