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] ์ต๋ฆฐ ๊ต์๋์ ์ด์์ฒด์ ๊ฐ์๋ฅผ ์๊ฐํ๊ณ ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค. ์๋ชป๋ ๋ด์ฉ์ด ์๋ค๋ฉด ๋๊ธ๋ก ์๋ ค์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค ๐