이 문제는 세마포(semaphore)라는 것을

뮤텍스(mutex)와 test-and-set! 프로시저를 이용하여 구현하는 문제입니다.

 

c6

구현을 하기는 했지만 동작을 시키지 않아 어떤 문제점이 생길지 알 수 없습니다.

그 점이 아쉽습니다.

 

 

참조

해럴드 애빌슨, 김재우 역, <컴퓨터 프로그램의 구조와 해석>, 인사이트, 2007, pp. 407

 

 

; a
(define (make-semaphore n)
  (let ((i 0)
        (mutex (make-mutex)))           
    (define (the-semaphore m)
      (cond ((eq? m 'acquire)
             (set! i (+ i 1)) ; i 증가
             (if (= i 0)
                 (mutex 'acquire))
             (if (>= i n)
                 (mutex 'acquire))) ; retry
            ((eq? m 'release)
             (set! i (- i 1)) ; i 감소
             (if (>= i n)
                 (mutex 'release))
             (if (= i 0)
                 (mutex 'release)))))
    the-semaphore))

; b
(define (make-semaphore n)
  (let ((i 0)
        (cell (list false)))           
    (define (the-semaphore m)
      (cond ((eq? m 'acquire)
             (set! i (+ i 1)) ; i 증가
             (if (>= i n)
                 (if (test-and-set! cell)
                     (the-semaphore 'loop)))) ; retry
            ((eq? m 'release)
             (set! i (- i 1)) ; i 감소
             (if (>= i n)
                 (clear! cell))
             (if (= i 0)
                 (clear! cell)))
            ((eq? m 'loop)
             (if (test-and-set! cell)
                 (the-semaphore 'loop)))))
    the-semaphore))

크리에이티브 커먼즈 라이선스
Creative Commons License

글에 잘못된 점, 다른 점, 부족한 점이 있다면 지적해주세요.
댓글, 트랙백, 메일 모두 고맙습니다.

트랙백 주소 :: http://nosyu.pe.kr/trackback/1605

댓글을 달아 주세요

[로그인][오픈아이디란?]