본문 바로가기
#컴퓨터 과학 [Computer Science]/운영체제 (Operating System)

[OS] 세마포어 (Semaphore)와 뮤텍스 (Mutex)

by cy_mos 2020. 2. 5.
반응형
What is difference between Semaphore and Mutex

🗂 세마포어 (Semaphore)

https://www.keil.com/pack/doc/CMSIS/RTOS2/html/group__CMSIS__RTOS__SemaphoreMgmt.html

 

wait(S)
{   
    while (S<=0);
     S--;
}

signal(S)
{
    S++;
}

 

A semaphore is a signalling mechanism and a thread that is waiting on a semaphore can be signaled by another thread.

 

복수의 작업을 동시에 병행하여 수행하는 운영 체제(또는 프로그래밍)에서 공유 자원에 대한 접속을 제어하기 위하여 사용되는 신호입니다. 병행 내지 병렬로 동작되는 둘 이상의 프로세스 사이에서 마이크로프로세서 시간이나 입출력 접속구(port)와 같은 공유 자원을 동시에 사용할 수 없기 때문에, 한 프로세스가 사용하고 있는 동안에 세마포어를 세워서 다른 프로세스를 대기시키고 사용이 끝나면 해제시키는 방법으로 사용합니다.

 

[네이버 지식백과] 세마포어 [semaphore] (IT용어사전, 한국정보통신기술협회)

🗂 뮤텍스 (Mutex)

https://www.keil.com/pack/doc/CMSIS/RTOS2/html/group__CMSIS__RTOS__MutexMgmt.html

 

wait (mutex);
     …..
Critical Section
     …..
signal (mutex);

 

Mutex is a mutual exclusion object that synchronizes access to a resource. It is created with a unique name at the start of a program. The Mutex is a locking mechanism that makes sure only one thread can acquire the Mutex at a time and enter the critical section. This thread only releases the Mutex when it exits the critical section.

 

스레드들 간에서 공유가 배제되는 객체입니다. 파일과 같은 공유 자원이 수행 중 오직 한 프로그램이나 스레드에게만 소유되어야 할 필요가 있을 때 그 자원에 대한 뮤텍스 객체를 생성시킨다. 뮤텍스가 비신호 상태이면 프로그램은 자원을 점유하여 사용한 후 이를 반환하고, 다른 프로그램 또는 다른 스레드가 자원을 사용 중 즉, 뮤텍스가 신호 상태이면 대기 상태로 들어가 끝나기를 기다린다.

 

[네이버 지식백과] 뮤텍스 [Mutual Exclusion object] (IT용어사전, 한국정보통신기술협회)

🗂 세마포어와 뮤텍스의 차이점 (Difference between binary semaphore and mutex)

구분 세마포어 (Semaphore) 뮤텍스 (Mutex)
Mechanism

It is a type of signaling mechanism.

 

신호 기반의 매커니즘으로 구동합니다.

It is a locking mechanism.

 

자물쇠 기반의 매커니즘으로 구동합니다.

Data Type

Semaphore is an integer variable.

 

세마포어는 정수형의 값을 사용합니다.

Mutex is just an object.

 

뮤텍스는 하나의 객체로서 사용합니다.

Modification

The wait and signal operations can modify a semaphore.

 

세마포어는 wait 및 signal 연산을 통하여 작업을 수행합니다.

It is modified only by the process that may request or release a resource.

 

뮤텍스는 오직 프로세스에 의해서 자원에 대한 요청 및 개방을 통하여 작업을 수행합니다.

Resource management

If no resource is free, then the process requires a resource that should execute wait operation. It should wait until the count of the semaphore is greater than 0.

 

만약 사용가능한 자원이 없는 경우에는 프로세스는 자원에 대하여 wait 연산을 수행할 것을 요구합니다. 이때의 세마포어는 정수형 값이 0 보다 커질 때 까지 대기합니다.

If it is locked, the process has to wait. The process should be kept in a queue. This needs to be accessed only when the mutex is unlocked.

 

만약 잠김 (Locked) 상태인 경우에는 프로세스는 대기합니다. 프로세스는 반드시 큐 내부에서 대기열에 상태를 유지합니다. 그리고 뮤택스가 잠김해제 (Unlocked) 상태가 되었을 경우에만 접근할 수 있습니다.

Thread

You can have multiple program threads.

 

여러개의 프로그램의 쓰레드를 가질 수 있습니다.

You can have multiple program threads in mutex but not simultaneously.

 

뮤텍스는 여러개의 프로그램의 쓰레드를 가질 수는 있지만 동시에 사용할 수 없습니다.

Ownership

Value can be changed by any process releasing or obtaining the resource.

 

세마포어는 자원을 소유하거나 소유를 해제를 한 모든 프로세스에서 값을 변경할 수 있습니다.

Object lock is released only by the process, which has obtained the lock on it.

 

뮤텍스는 잠금 (Lock) 상태를 얻은 프로세스에 의해서만 잠금 (Lock) 상태를 UnLock (잠금해제) 할 수 있습니다.

Types

Types of Semaphore are counting semaphore and binary semaphore.

 

세마포어는 계수 세마포어 (Counting Semaphore)와 이진 세마포어 (Binary Semaphore)의 종류가 있습니다.

Mutex has no subtypes.

 

뮤텍스는 단일 타입입니다.

Operation

Semaphore value is modified using wait () and signal () operation.

 

세마포어 값은 wait() 함수 연산과 signal () 함수 연산을 사용하여 값을 변경할 수 있습니다.

Mutex object is locked or unlocked.

 

뮤텍스 객체는 잠금 (Locked) 또는 잠금해제 (Unlocked)를 수행합니다.

Resources Occupancy

It is occupied if all resources are being used and the process requesting for resource performs wait () operation and blocks itself until semaphore count becomes > 1.

 

모든 자원이 점유되고 있는 상태일 경우에는 프로세스는 자원에 대하여 wait() 함수 수행을 요청하고 있으며 세마포어의 계수가 1 이상인 상태입니다.

In case if the object is already locked, the process requesting resources waits and is queued by the system before lock is released.

 

뮤텍스 객체가 이미 잠금 (Locked) 상태일 경우에는 자원을 요청하는 프로세스는 대기하며 잠금해제 (Unlocked)가 될 때까지 시스템에서 대기합니다.


🚀 REFERENCE

 

Mutex vs Semaphore: What's the Difference?

A virtual assistant or VA is usually self-employed and offers professional, technical, social, or...

www.guru99.com

 

Reference

 

www.keil.com

 

Difference Between Semaphore and Mutex (with Comparison Chart) - Tech Differences

The basic difference between semaphore and mutex is that semaphore is a signalling mechanism i.e. processes perform wait() and signal() operation to indicate whether they are acquiring or releasing the resource, while Mutex is locking mechanism, the proces

techdifferences.com

반응형

댓글