4-15 OS (thread, user-state, kernel-State, page) database (atomicity, log) JAVA (I/O)

Source: Internet
Author: User
Tags switches

In the Internet services class, the teacher said that Capriccio is the thread in user mode. The 2nd project in Library,os class is to implement a line libraries in user mode. Before using the POSIX library, I know that this is a thread libraries in kernel mode, which means that the kernel creates and dispatches threads. But the kernel is like a black box, and has never understood what it did and how it did it. Modern Operating system has chapters on user space and kernel space control threads.

In user space: All threads are managed in the users area, and the kernel does not know the existence of multithreading. The benefit of implementing in the user area is that you can run multiple threads on an operating system that does not support multithreading, reduce switching between user mode and kernel mode, and use applications for thread scheduling (such as having a garbage collector thread, which we hope will not disturb the execution of other threads). The downside is that if a blocking system call occurs in a thread, the system switches to the kernel state. However, because of thread management in the user state, the kernel does not know the existence of multi-threaded, do not know that in addition to the current blocked thread, there are other threads want to execute, so let the thread block until the result is obtained, blocking the end or the time slice of the process is run out.

What happens in the process of switching between user mode and kernel mode, and why is there a lot of overhead? Start with a system call that happens. Single core CPU can only execute one instruction at any time, when an application is running in user state, it needs to acquire system resources, such as reading data from disk, it must switch to kernel State and execute code in kernel. To call the Read function, first apply the Read function in the C standard Library (library procedure), which in the standard library function has a system call, also called read. In this process, the library function places the number of the system call to occur somewhere, such as a register, and then executes the trap instruction to enter the kernel state from the user state. Traps are similar to normal function calls by pointing a function pointer to another function entry and putting the return address into the stack for a jump. The difference is that after the trap pointer points to the kernel code, the normal function call is another piece of code that points to the user state, and the trap cannot jump to an arbitrary address. Next, the kernel checks the number of system calls that were placed in the register, and jumps to the corresponding handler function by looking at the table. When the system call is complete, return to the user state.

Http://www.cnblogs.com/gtarcoder/articles/5278074.html

What code is part of the kernel? Why do you want to divide the instruction into two levels, user state and kernel state?

In Kenel space: thread invocation in the kernel area, if a thread has a blocking system call, the kernel switches to another thread and resolves the user area problem. In multi-threaded operation, there is a problem of how to handle the signal. The signal is sent to a process, not a single thread. So when a signal is received, which thread will handle it? The book simply describes that different threads register different signals, so a signal is handled by the thread that is interested in it.

Before you know it, what is creating a new thread? Think about what constitutes a thread, and what can a thread be represented by? Of course is the thread state, including function counters, line stacks, some register values. Like a snapshot, you can recover a paused thread from this data. So in the thread library, to save the snapshots of each thread, call up a snapshot, restore the various states of the register, and start execution from where the function counter points.

Most of the operating systems are virtual on storage. The addresses that the code sees are virtual addresses, either user or kernel, and each process has its own page directory and page table, which records the mapping of the virtual address to the physical address. Why use a virtual address? First, the virtual space can be larger than the physical space, or memory size. There may be a problem, because a process uses a virtual address, and if it uses an address space larger than the memory space, how does it work? In fact, the data that needs to be used is divided into many fixed-size pages, and not all pages are used at some point of execution. Only the pages currently in memory need to be saved. When a process requests a page that is not in memory at some point, the operating system will load the corresponding page from the disk into memory, replacing the unwanted page. Fault. In addition, the virtual address provides a contiguous address space for a process.

When does the operating system need to process pages? When you create a process, execute a process, page failure, or end a process.

How does the database guarantee the atomicity of a trade? Atomicity means that a transaction either occurs, or does not occur, all or none. Failure can occur in the course of a transaction, atomicity does not allow the transaction to complete half of the situation, so either proceed, complete the transaction, or fall back to the original state.

One way is to deferred-modification, just like the DS in the second project cache when using Copy-on-write. Here, to update, you first create a copy of the database, update it on the replica, and, to commit, update the pointer to the original database to a pointer to the latest copy. If there is a failure before the update, after the restart or point to the original database, the equivalent of the transaction abort, if the update after the failure, after restarting point to the updated database, transaction success commit. But here it is necessary to ensure that the atomic nature of the pointer update, which can be guaranteed by disk hardware.

One option is to modify the original data directly, but create a log for each modification. According to the log to determine which state a transaction to, if you need to fallback, is to undo the previous operation. Logs need to be written to disk in time to ensure that they do not disappear in the failure.

4-15 OS (thread, user-state, kernel-State, page) database (atomicity, log) JAVA (I/O)

Related Article

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.