◎ Mutual Exclusion Primitive (Ver.1)program versionone;var processnumber : integer;procedure processonebeginwhile true dobeginwhile processnumber = 2 do; // mutual exclusion entry code (processone)criticalsectionone;processnumber := 2; // mutual exclusion exit code ( 〃 )otherstuffoneendend;procedure processtwo;beginwhile true dobeginwhile processnumber = 1 do; // mutual exclusion entry code (processtwo)criticalsectiontwo;processnumber := 1; // mutual exclusion exit code ( 〃 )otherstufftwoendend;beginprocessnumber := 1; // 공유변수(초기값 1로 setting되어 processone procedure가 먼저실행)parbeginprocessone; // parbegin / parend에 의해 processone 과 processtwo 가processtwo // 동시에 수행하게 된다.parendend.▶▶ 프로그램의 실행① processone : while∼do문을 수행하게 되면, processnumber의 초기치가 1이므로, processone이 먼저 Critical - Section(C.S)에 진입한다.② processtwo : 그동안 processnumber가 1인 것을 확인하고, 그 값이 자신의 process number(=2)와 같아질 때while∼do loop을 계속 수행한다.③ processone : C.S 내의 일을 끝내고, processnumber를 2로 치환한다.④ processtwo : while∼do loop을 돌고 있다가, procescode (processone)p1inside := true; // 자신의 변수를 true로 전환 후 C.S내에 진입criticalsectionone;p1inside := false; // mutual exclusion exit code ( 〃 )otherstuffoneendend;procedure processtwo;beginwhile true dobeginwhile p1inside do; // mutual exclusion entry code (processtwo)p2inside := true;criticalsectiontwo;p1inside := false; // mutual exclusion exit code ( 〃 )otherstufftwoendend;beginp1inside := false; // processone이 C.S 내에 있을 때 p1inside=true 가 되고,p2inside := false; // processtwo이 C.S 내에 있을 때 p2inside=true 가 된다.parbeginprocessone; // parbegin / parend에 의해 processone 과 processtwo 가processtwo // 동시에 수행하게 된다.parendend.▶▶ 프로그램의 실행 (가정 : processone이 processtwo보다 실행속도가 빠름)① processone : processtwo가 C.S내에 있는지(=p2inside가 true인지)test 후, 아니면 p1inside variable을 true로 전환하고 C.S내로 진입.② processtwo : p1inside가 true 이므로, while∼do loop을 돌며 대기 (=busy waiting)③ processone : C.S 내의 일을 끝내고, p1inside를 false로 전환④ processtwo : while∼do loop을 돌고 있다가, p1inside가 false임을 확인하고 loop을 빠져나와 C.S내로 진입한다.⑤ processone : p2inside가rocess가 while 검사를 시작할 때 다른 process는 while 검사를 하지 못하게 하는 것이다. 즉, while 검사전에 자신들의 변수(상태)를 바꿈으로써, 자신이 지금 while 검사를 시작한다는 사실을, 그리고 자신이 C.S에 들어가고자 하는 의지를 미리 알리는 것이다. (⇒ Version 3)◎ Mutual Exclusion Primitive (Ver.3)program versionthree;var p1wantstoenter, p2wantstoenter : boolean;procedure processone;beginwhile true dobegin // 자신의 변수(p1wantstoenter)를 true로 전환하므로써p1wantstoenter := true; // 자신이 C.S에 들어가고자 하는 의사 표시함.while p2wantstoenter do; // mutual exclusion entry code (processone)criticalsectionone;p1wantstoenter := false; // mutual exclusion exit code ( 〃 )otherstuffoneendend;procedure processtwo;beginwhile true dobeginp2wantstoenter := true;while p1wantstoenter do; // mutual exclusion entry code (processtwo)criticalsectiontwo;p2wantstoenter := false; // mutual exclusion exit code ( 〃 )otherstufftwoendend;beginp1wantstoenter := false;p2wantstoenter := false;parbeginprocessone; // parbegin / parend에 의해 processone 과 processtwo 가processtwo // 동시에 수행하게 된다.parendend.▶▶ 프로그램의 실행 (가정 : processoness이므로, 동시에 자신의 변수(flag)를 true로 바꾸고, while문에 도달할 수 있다. 이때 두 process는 서로 상대 process의 상태가 true임을 확인하고는 영원히 while∼do loop만을 하게 될 것이 다. 즉, 두 Process가 동시에 수행할 경우, Indefinite postponement 문제와 deadlock이 발생된다.▶▶ SolutionVersion 3의 문제점은 두 process가 무한 loop에 빠질 수 있다는 것이다. 따라서, 이러한 loop으로부터 빠져나오는 방법을 생각해야 한다. 즉, 각 process들이 while∼do loop을 계속하며, 자신의 flag를 얼마동안 계속에서 false로 유지시켜, 상대 process가 C.S내로 들어갈 기회를 제공해 주는 것이다. (⇒ Version 4)◎ Mutual Exclusion Primitive (Ver.4)program versionfour;var p1wantstoenter, p2wantstoenter : boolean;procedure processone;beginwhile true dobeginp1wantstoenter := true;while p2wantstoenter do;begin // Ver 3.를 보완한 부분p1wantstoenter := false; // 얼마동안 flag를 false로 둠으로써 다른 process가delay(random, fewcycles); // 그의 while loop을 빠져나가 되어,p1wantstoenter := ture; // mutual exclusion과 deadlock 문제가 해결된다.end;criticalsectionone;p1wantstoenter := false;otherstuffoneendend;procedure processtwo;beginwhile true dobeginp2wantstoenter := true;while p1wantstoenter do;beginp2wantstoenter := false;delflag를 false로 둠으로써, 다른 process가 그의 while loop을 지나가게 되고, 이로써, mutual exclusion이 보장되고, deadlock이 해결된다.▶▶ 단점두 process가 Parallel process이므로, 동시에 자신의 변수(flag)를 true로 바꾸고, while문에 도달할 수 있다. 이때 두 process는 서로 상대 process의 상태가 true임을 확인하고는 동시에 while∼do loop의 body를 수행하여 flag를 false로 한 얼마후 true로 하고, 다시 while문은 test하게 될 것이다. 이처럼 while을 검사할 때마다 값이 계속 true가 될 수 있다.즉, 두 Process가 동시에 수행할 경우, Indefinite postponement 문제가 여전히 발생된다.▶▶ Solution⇒ Dekker Algorithm◎ Mutual Exclusion Primitive (Dekker Algorithm)program dekkeralgoritm;var favoredprocess :(first, second);p1wantstoenter, p2wantstoenter:boolean;procedure processone;beginwhile true dobeginp1wantstoenter := true;while p2wantstoenter do;if favoredprocess = second thenbegin // Ver 3.를 보완한 부분p1wantstoenter := false; // 얼마동안 flag를 false로 둠으로써 다른 process가while favoredprocess = second do;p1wantstoenter := ture; // mutual exclusion과 deadlock 문제가 해결된다.end;criticalsectionone;favoredprocess := second;p1wantstoenter := false;otherstuffoneendend;procedure process간다.
/**********************************************************************************//* Programming Project # 5.8 (CatchPicture.java) *//* 임의의 위치에 나타나는 그림(dog)을 잡는 프로그램 *//**********************************************************************************/import java.applet.*;import java.awt.*;import java.awt.event.*;import javax.swing.Timer;import java.util.Random;public class CatchPicture extends Applet {CatchMouseListener catchmessage = new CatchMouseListener();Random position = new Random();private final int APPLET_WIDTH = 250;private final int APPLET_HEIGHT = 250;private final int IMAGE_X_SIZE = 25;private final int IMAGE_Y_SIZE = 40;private final int DELAY = 850;private Point point = null;private Timer timer;private Image image;private AudioClip bonk;private int xposition, yposition, catchcount;//-----------------------------------------------------------------------// Animation을 위한 timer, sound를 포함하는 applet을 설정한다.//-----------------------------------------------------------------------public void init() {addMouseListener (new CatchMouseListener());timer = new Timer (DELAY, new CatchActionListener());timer.start();xposition = 0;yposition = 0;catchcount = 0;image = getImage (getCodeBase(), "dog.gif");bonk = getAudioClip (getCodeBase(), "bonk.au");setBackground (Color.white);setSize (APPLET_WIDTH, APPLET_HEIGHT);}//----------------------------------------------------------------------// 현재 mouse를 clicked 한 위치가 image(dog)의 위치에 부합하는지 여부를// 결정하고, mouse로 dog를 catch 한 경우의 수를 count 한다.// catch 하면 일시 정지하고, image를 제외한 applet 부분을 click 하면,// 다시 image가 이동하기 시작한다.//----------------------------------------------------------------------public void catchdog() {if (point.x >= xposition && point.x = yposition && point.y
1. Hamming code와 hamming distance에 관하여 조사하시오.▶ Hamming code(해밍코드)는 code의 전송 시 발생하는 오류를 검출할 수 있을 뿐만이 아니라 오류 code의 수정이 가능한 코드로써 1byte(8bit)에 3∼4개의 검사(check)용 여분의 bit를 두어 이를 이용하여 2bit의 오류를 찾아내거나 1bit의 오류를 정정할 수 있도록 한 code이다.▶ 미국 Bell 연구소의 Richard W. Hamming에 의하여 개발된 오류 정정 코드들 중에서 가장 간 단한 코드이며, "Error 제어 방식"중 순방향 에러정정방식(FEC)) FEC (Forward Error Correction) : 많은 통신시스템에서 자료 형태는 단 방향 채널을 사용하고 역 채널이 없는 경우가 많아 재전송을 요구할 수는 없고 또, 재전송이 가능하더라도 지연시간이 길다. 따라서 오류가 발생하였을 경우 수신측에서 능동적으로 교정할 수 있는 순 방향 오류정정방식으로 FEC가 디지털 전송의 기본이 되고 있다. 이 기술은 디지털 변조기상에서 구현되며 실제 HW는 DSP칩을 이용하여 설계·구현된다에 해당되는 코드이다.▶ 사용방식 : Hamming code는 오류 검출 뿐 만 아니라 오류 정정까지 가능하지만, 오류 정정을 위해서는 많은 Parity bit를 필요로 하게 된다. 또 Parity bit를 포함한 전송 data bit의 구성은 data bit는 parity bit가 아닌 전송하고자 하는 원래의 Data이며, parity bit의 내용은 data bit가 1인 위치의 bit 번호를 Exclusive-OR 함으로써 계산된다. 4bit 단어들에 대해 해밍코드가 사용된 경우 ‥‥‥‥ 벤다이어그램(Venn diagram) 사용① 세 개의 원들을 서로 교차시켜 일곱 개의 구획을 나눈다. ② 내부의 구획들에는 4개(4bit)의 data bit들을 지정한다. ③ 나머지 구획(3bit)들은 parity bit들로 채운다. (짝수parity bit check code 사용) 각 parity bit의 값은 그것이 속한 원내의 1의 개수가 짝수이면, 0을, 홀수이면 1을 선택한다.* venn diagram*************000 ④ 따라서 이 상태에서 data bit들 중의 하나에 오류가 발생하면, 원내의 1의 개수가 짝수개가 아닌 것을 확인함으로써 오류를 쉽게 발견할 수 있고, 해당 구획의 비트를 변경함으로써 오 류를 정정할 수도 있다.▶ Hamming distance는 code의 오류 검출 및 정정 특성을 부여하는 중요한 매개변수로써, n-차원 vector인 부호벡터 C = (C0, C1, ···,Cn-1)에서 부호벡터 C의 Hamming distance는 부호벡터 C 내에서 0(zero)이 아닌 성분(component)의 개수로 정의할 수 있다. 7차원 부호벡터 C = ( 0 1 1 0 0 1 0 )의 Hamming distance는 3이다.한편, 두 개의 n차원 벡터 X = (X0, X1, ···,Xn-1)와 Y = (Y0, Y1, ···,Yn-1)간의 Ham-ming distance는 X와 Y의 Exculsive-OR 결과값에서 0(zero)이 아닌 성분의 개수로 정의할 수 있다. X = ( 1 1 0 1 1 0 0 )Y = ( 1 0 1 1 0 0 1 )Ex-OR = ( 0 1 1 0 1 0 1 ) ∴ X와 Y의 Hamming distance 는 4 이다.2. How to represent the Korean Character?- 조합형 및 완성형 코드의 표현방법을 조사하여 보고서를 작성하되, 자신의 이름을 예로 사용하시오.▶ 한글은 보는 관점에 따라 크게 ①음절문자로 보아 완성형 한글로 사용하는 경우와,②초성+중성+종성을 하나의 글자로 생각하여 조합형 한글로 사용하는 경우가 있다.▶ 2byte 완성형 한글- * 1987년 정부가 한국표준으로 정한 것으로 가장 많이 사용되는 한글 한 음절마다 2byte의 2 진수를 1대 1로 대응하여 표현하는 방법이다. 즉, 한글을 초성, 중성, 종성으로 나누지 않고 이미 한 음절로 완성된 글자 그 자체를 코드화 한 것이다.* 초성, 중성, 종성의 3성의 조합으로 구성된 한글은 자주 사용되지 않는 글자가 거의 70% 가 되고 이러한 점을 이용하여 나온 것이 완성형 한글이다.* 한글 2350자, 한자 4888자, 사용자정의 188자, 특수문자 1128자, 미지정문자 282자를 표현 가능하다.- 장점 : 다른 프로그램 및 통신 등에 이용되는 것을 피해 작성되었기 때문에 통신에는 매우 적합하다.- 단점 : 한글을 초성, 중성, 종성으로 나누지 않고 이미 한 음절로 완성된 글자 그 자체를 코 드화 한 것이므로 한글과 같이 다양하게 결합 가능한 문자에서는 표현 불가능한 글 자가 발생할 수 있다는 단점이 있다.- 표시원리 : 아래의 MSB) MSB(Most Significant Bit) : MSB가 1(즉, 128 이상)이면 한글코드, 0(128이하)이면 영문 코드임을 컴퓨터에 알린다.를 제외한 bit들에 글자를 음절 단위로 할당한다.First ByteSecond Byte*************21011MSB MSB▶ 2byte 조합형 한글- * 한글의 자음과 모음을 각각 code화 하고 이것들이 초성, 중성, 종성으로 합쳐서 한 글자가 된다는 개념으로써 한글의 생성원리와 가장 근접한 코드이다.* 한글을 내부적으로 처리하는 모든 응용 Program에서 주로 사용된다.