Sharing of global resources can create
Sharing of global data may lead to 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.
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
βοΈ critical section, semaphore λ±μ ꡬννκΈ° μν΄ λͺ¨λ CPUκ° atomic μ°μ°μ μ 곡νλ€.
βοΈ "Atomic" means
βοΈ Atomic operation
π‘ atomic instruction μ μ¬μ©νμ¬ lock λ³μλ₯Ό ꡬννλ©΄ process, processor μμ μκ΄μμ΄ κ³΅μ μμμ μ¬μ©ν μ μλ€. μ§κ΄μ μ΄κ³ μ¬λ¬ κ°μ critical section μ ꡬνν μ μλ€.
κ·Έλ¬λ Busy-waiting ν΄μ λΉν¨μ¨μ μ΄κ³ , μ€μΌμ€λ§μ μν΄ Starvation, Deadlock μ΄ κ°λ₯νλ€ λΌλ λ¨μ μ΄ μκΈ°μ μ’ λ λ Όλ¦¬μ μΈ mutual exclusive μ λν μ΄μμ μΈ SWμ ν΄κ²°λ°©μμ΄ νμνλ€. κ·Έλμ λ±μ₯νλ κ²μ΄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
βοΈ Type of semaphores
βοΈ 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 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) );
}
When processes interact with one another, the following actions must be satisfied by the system
βοΈ Blocking send, blocking receive
βοΈ Nonblocking send, blocking receive
βοΈ Nonblocking send, nonblocking receive
Direct addressing
Indirect addressing
βοΈ Communication of a message between two processes implies synchronization between the two
βοΈ Both sender and receiver can be blocking or nonblocking
[KUOCW] μ΅λ¦° κ΅μλμ μ΄μ체μ κ°μλ₯Ό μκ°νκ³ μ 리ν λ΄μ©μ λλ€. μλͺ»λ λ΄μ©μ΄ μλ€λ©΄ λκΈλ‘ μλ €μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€ π