[OS] Competing Process

seunghyunΒ·2023λ…„ 3μ›” 7일
0

πŸ’»

λͺ©λ‘ 보기
3/16

Sharing of global resources can create

  • Need for mutual exclusion (mutex)
  • Deadlock
  • Starvation

Sharing of global data may lead to race condition.

race condition

This occurs when two or more process/threads access shared data and they try to change it at the same time. Because thread/process scheduling algorithm can switch between threads, you don't know which thread will access the shared data first. In this situation, both threads are 'racing' to access/change the data.

Operations upon shared data are critical sections that must be mutually exclusive in order to avoid harmful collision between processes or threads.

critical section

A section of code within a process that requires access to shared resources and that must not be executed while another process is in a corresponding section of code

atomic operation

βœ”οΈ critical section, semaphore 등을 κ΅¬ν˜„ν•˜κΈ° μœ„ν•΄ λͺ¨λ“  CPUκ°€ atomic 연산을 μ œκ³΅ν•œλ‹€.

βœ”οΈ "Atomic" means

  • Indivible, uninterruptable
  • Must be performed atomically, which means either "success" or "failure"
    • Success : successfully change the system state
    • Failure : no effect on the system state

βœ”οΈ Atomic operation

  • A fuction or action implemented as a single instruction or as a sequence of instructions that appears to be indivisible
    • No other processes can see an intermediate state
  • Can be implemented by HW or SW
  • HW-level atomic operations
    • Test-and-set, fetch-and-add, compare-and-swap, load-link, store-conditional
  • SW-level solutions
    • Running a group of instructions in a critical section

πŸ’‘ atomic instruction 을 μ‚¬μš©ν•˜μ—¬ lock λ³€μˆ˜λ₯Ό κ΅¬ν˜„ν•˜λ©΄ process, processor μˆ˜μ— 상관없이 κ³΅μœ μžμ›μ„ μ‚¬μš©ν•  수 μžˆλ‹€. 직관적이고 μ—¬λŸ¬ 개의 critical section 을 κ΅¬ν˜„ν•  수 μžˆλ‹€.
κ·ΈλŸ¬λ‚˜ Busy-waiting ν•΄μ„œ λΉ„νš¨μœ¨μ μ΄κ³ , μŠ€μΌ€μ€„λ§μ— μ˜ν•΄ Starvation, Deadlock 이 κ°€λŠ₯ν•˜λ‹€ λΌλŠ” 단점이 μžˆκΈ°μ— μ’€ 더 논리적인 mutual exclusive 에 λŒ€ν•œ 이상적인 SW적 ν•΄κ²°λ°©μ•ˆμ΄ ν•„μš”ν–ˆλ‹€. κ·Έλž˜μ„œ λ“±μž₯ν•˜λŠ” 것이 semaphore 이닀.

semaphore πŸ”’

μ„Έλ§ˆν¬μ–΄λΌλŠ” μžλ£Œκ΅¬μ‘°κ°€ μžˆλ‹€λ©΄ μš°λ¦¬κ°€ μ›ν•˜λŠ” mutual exclusive critical section 을 κ΅¬ν˜„ν•  수 μžˆμ§€ μ•Šμ„κΉŒ λΌλŠ” κΈ°λŒ€!

βœ”οΈ A variable that provides a simple abstraction for controlling access to a common resource in a programming environment

βœ”οΈ The value if the semaphore variable can be changed by only 2 operations

  • V operation (also known as "signal")
    • Increment the semaphore
  • P operation (also known as "wait")
    • Decrement the semaphore
  • The value of the semaphore S is usually the number of units of the resource that are currently available.

βœ”οΈ Type of semaphores

  • Binary semaphore
    • 0 (locked, unavailable)
    • 1 (unlocked, available)
  • Counting semaphore
    • Can have an arbitrary resource count

βœ”οΈ Definition of Counting semaphore

struct semaphore
{
	int count;
    queueType queue;
};

void semWait(semaphore s) // P
{
	s.count--;
    if(s.count<0)
    {
    	/*place this process in s.queue */
        /*block this queue*/
    }
}

void semSignal(semaphore s) // V 
{
	s.count++;
    if(s.count <=0)
    {
    	/*remove a process P from s.queue */
        /*place process P on ready list*/
    }
}

example

Example of Semaphore Mechanism

Mutual Exclusion (critical section) Using Semaphores

 const int n = number_of_processes;
 semaphore s = 1;
 void P(int i)
 {
 	while(true)
    {
    	semWait(s); // P
    	/*critical section*/
        semSignal(s); // V
        /*remainder*/
    }
 }
 
 void main()
 {
 	parbegin( P(1), P(2), ..., P(N) );
 }

Message Passing

When processes interact with one another, the following actions must be satisfied by the system

  • Mutual exclusion
  • Sychronization
  • Communication

Blocking/Nonblocking Send/Receive

βœ”οΈ Blocking send, blocking receive

  • Both sender and receiver are blocked until the message is delivered
  • Sometimes referred to as a rendezvous

βœ”οΈ Nonblocking send, blocking receive

  • The moust useful combination

βœ”οΈ Nonblocking send, nonblocking receive

  • Neither party is required to wait

Addressing

Direct addressing

Indirect addressing

Synchronization

βœ”οΈ Communication of a message between two processes implies synchronization between the two

  • The receiver cannot receive a message until it has been sent by another process

βœ”οΈ Both sender and receiver can be blocking or nonblocking

  • when a send primitive is executed, there are two possibilities
    • Either the sending process is blocked until the message is received, or it is not
  • When a receive primitive is executed there are also two possibilities

πŸ”— Reference

[KUOCW] 졜린 κ΅μˆ˜λ‹˜μ˜ 운영체제 κ°•μ˜λ₯Ό μˆ˜κ°•ν•˜κ³  μ •λ¦¬ν•œ λ‚΄μš©μž…λ‹ˆλ‹€. 잘λͺ»λœ λ‚΄μš©μ΄ μžˆλ‹€λ©΄ λŒ“κΈ€λ‘œ μ•Œλ €μ£Όμ‹œλ©΄ κ°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€ 😊

0개의 λŒ“κΈ€