기본 콘텐츠로 건너뛰기

Context Switching


Motivation

 When changed from state to another state or interrupted, operating system(OS) must maintain current context for OS to fulfill the consistency.
In particular, we have to save hardware context which involve in CPU registers and I/O registers.


Intuition

- All of the change of context is made of occurring Interrupt. Moreover, Context Switching is a kind of Interrupt Service Routine. Whenever Interrupt occurs, we have to save all of the registers known as a state vector which involve a program counter, stack pointer and so on, to PCB of the current process.
- There is a context switch function in kernel memory, that can allocate CPU resource.
- In Linux system, context switch function is known as schedule function and When calling system call such as read, wait, exit it invokes schedule function.


Review

- Interrupt occurs in anytime, even if system is in the smallest process step such as ADD instruction. Because after finishing every instruction, in hardware mechanism, system checks the interrupt bit that can know whether interrupt occurs or not.
- Every program needs two stacks, Kernel Stack which involve PCB and User Stack used for function. To call a kernel function like system call, a kernel stack always resides in the the memory of kernel space.
- Process State(context) consists of Memory Context which has data, stack and heap segment and Hardware Context which has CPU registers, I/O registers and Kernel Context(System Context)
- Hardware Context is preserved by Context Switching and Memory Context is preserved by Swapping which take it from memory to disk when the degree of multiprogramming is up to the maximum.


Mechanism

based on MIPS.

Step 1
- OSPCBCur : the global variable which operating system has, points the current process of PCB.
- StkPtr(OS-PCB) : the perspective of Operating System.
- CPU's SP : the perspective of CPU.
- MEM : Kernel Stack.

Step 2
Interrupt Occurrence!
As soon as occurring, in hardware mechanism, system pushes PSW(Program Status Word) and Return Address made of SEG task and OFF task. Because of Segmented Addressing for Intel Processor, there are two words.
Before running Interrupt Service Routine, hardware components have a minimum support that they push PSW and Return Address to Kernel Stack(PCB) in order for Operating System to run a dispatcher function smoothly.

Step 3
Interrupt Service Routine is executed!
By using PUSH instruction, the rest of all registers are pushed. As far as here, it is the completion of context save.

Step 4
Allocate StkPtr!
Now, Stack Pointer is saved at PCB involved in Operating System.

Step 5
If interrupt is a timer, after saving context, operating system invokes scheduler which points PCT of the next process.
OSPCBCur points next StkPtr!

Step 6
Allocate CPU's SP!
Now, CPU understands where stack pointer is and then CPU executes POP instruction that can move all registers from kernel stack to CPU.

Step 7

At the same time as popping Return Address, the new process can run.
Return Address points User Stack, exactly points the next one from the instruction executed in last time.


Extra Step

In order to have the structure like that, all of the process executes at least one more ISR. When OS creates process, it make process to have a fake stack like that, as if the process is given Context Switching only once.  


Logical View

하드웨어와 소프트웨어의 조합이 인상적이다. 빠른 시간이 요구되는 매커니즘은 하드웨어가 지원해주고, 2가지 패러다임(하드웨어,소프트웨어)을 상호보완적으로 사용하면서 복잡한 컴퓨터 신호의 흐름들을 원활히 제어한다. 컴퓨터 시스템은 가격과 속도 측면에서 하드웨어와 소프트웨어가 가장 잘 조합된 구조라고 생각한다.



Reference
[1] 운영체제의 기초 (the basic of operating system)

댓글

이 블로그의 인기 게시물

Pattern Discovery in Data Mining

Coursera Illinois at Urbana-Champaign by Jiawei Han 2015.03.19 CONTENT 1. A brief Introduction to Data Mining 2. Pattern Discovery : Basic Concepts 3. Efficient Pattern Mining Methods 4. Pattern Evaluation 5. Mining Diverse Patterns 6. Constraint-Based Pattern Mining 7. Sequential Pattern Mining 8. Graph Pattern Mining 9. Pattern-Based Classification 10. Exploring Pattern Mining Applications Lecture 1 : A brief Introduction to Data Mining - We'are drowning in data but starving for knowledge ( a lot of data are unstructured ) - Data mining : a misnomer ! -> Knowledge mining from data - Extraction of interesting patterns (non-trivial, implicit, previously unknown and potentially useful) or knowledge from massive data. - Data mining is a interdisciplinary field (machine learning, pattern recognition, statistics, databases, big data, business intelligence..) Knowledge Discovery (KDD) Process Methodology View: Confluence of Multiple Disciplines Lecture 2 : Pattern Discovery : Ba

Vector Space Model

Motivation When you want to find some information by using Search Engines, you have to make a query used for search. Unfortunately, since you don't know exactly what it means, your query will be ambiguous and not accurate. Therefore, Search Engines give you the information in a ranked list rather than the right position. Intuition In order to make a ranked list, you need to calculate the similarity between the query and documents based on terms or words. One of the calculation of similarity is dot product on a vector space. In the vector space, there are many documents with respect to word dimensions The first to rank is d2, because to see with eyes it's the most similarity with the query. Problem How do we plot those vectors wonderfully and nicely and very very fairly ? - How do we define the dimension ? - How do we place a document vector ? - How do we place a query vector ? - How do we match a similarity ? Consideration 1. The frequency of each word of Query. First, Score in

Text Retrieval and Search Engines

by ChengXiang "Cheng" Zhai CONTENT 1. Natural Language Content Analysis 2. Text Access 3. Text Retrieval Problem 4. Text Retrieval Methods 5. Vector Space Model (Retrieval Model l) 6. System Implementation 7. Evaluation 8. Probabilistic Model (Retrieval Model ll) 9. Feedback 10. Web Search Harnessing Big Text Data: Text Retrieval + Text Mining Course Summary 1. Natural Language Content Analysis 1.1 Natural Language Processing (NLP) Natural Language Processing (NLP) is a field of computer science, artificial intelligence, and computational linguisitc concerned with the interactions between computers and human (natural) languages. As such, NLP is related to the area of human-computer interaction. Many challenges in NLP involve natural language understanding, that is, enabling computers to derive meaning from human or natural language input, and others involve natural language generation. Computers can understand human language like that Koreans understand English. but, it's