๊ถ๊ธํ์
์ Trap์ ๋๊ธฐ์ , Interrupt๋ฅผ ๋น๋๊ธฐ์ ์ด๋ผ๋ ํํ์ ์ผ์๊น
User Mode์ ํ๋ก์ธ์ค๋ ๋์ฒด์ ์ผ๋ก ์ฌ์ฉ์์ ์ํด ๊ตฌ๋๋ ์ดํ๋ฆฌ์ผ์ด์
์ด๋ผ๊ณ ํ๋๋ฐ, ๊ทธ ์๊ธฐ๋ ์ดํ๋ฆฌ์ผ์ด์
ํ๋ก์ธ์ค๋ฅผ ์์ฑํ๋ ๊ฑฐ ์์ฒด๋ Kernel Mode์์ ์ด๋ฃจ์ด์ง๊ณ ๊ทธ๋ ๊ฒ ์์ ๋ ์ดํ๋ฆฌ์ผ์ด์
์์์ ์ผ์ด๋๋ ์ผ๋ค์ด User mode๋ก ์ฒ๋ฆฌ๋๋ค๋๊ฑด๊ฐ?
์ฒ ํ์ ๋ฌธ์ ํด๊ฒฐ ๊ฐ์ ์
/*
** i ๋ฒ์งธ ์ฒ ํ์๊ฐ ์์ฌ๋ฅผ ํ ์ค๋น๊ฐ ๋์๋์ง ํ์ธํ๋ค.
** ์ ์์ ์ฒ ํ์๊ฐ ์์ฌ๋ฅผ ํ๊ณ ์์ง ์์ ์ ๊ฐ๋ฝ์ ๋ชจ๋ ์ด์ฉํ ์ ์๋ค๋ฉด, ์ฒ ํ์๊ฐ take_chopsticks์์ waitํ์ง ์๋๋ก signal์ ๋ณด๋ธ๋ค.
** ์ฃผ์ด์ง ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๊ฒฝ์ฐ์๋ philo[i]์ ๊ฐ์ด 1์ด๋ฏ๋ก take_chopsticks์์ Block๋์ง ์๊ณ EATING ํ ์ ์๋ค.
*/
check(int i)
{
if (state[i] == THINKING && state[LEFT] != EATING && state[RIGHT] != EATING)
{
state[i] = EATING;
signal(philo[i]);
}
}
/*
** mutex๋ฅผ ํตํด i ๋ฒ์งธ ์ฒ ํ์์ ์ํ๋ฅผ ๋ณ๊ฒฝํ๋ค.
** check๋ฅผ ํตํด ์์ชฝ ์ฒ ํ์์ ์ํ๋ฅผ ํ์ธํ๋ค.
** i ๋ฒ์งธ ์ฒ ํ์๋ ์์ ์ด ์์ฌ๋ฅผ ํ ์ ์์ ๋๊น์ง ๊ธฐ๋ค๋ฆฐ๋ค.
*/
take_chopsticks(int i)
{
wait(mutex);
state[i] = THINKING;
check(i);
signal(mutex);
wait(philo[i]);
}
/*
** mutex๋ฅผ ํตํด i ๋ฒ์งธ ์ฒ ํ์์ ์ํ๋ฅผ ๋ณ๊ฒฝํ๋ค.
** check๋ฅผ ํตํด ์ผ์ชฝ ์ฒ ํ์์ ์ค๋ฅธ์ชฝ ์ฒ ํ์์ ์ ์์ ํ์ธํ๋ค.
** ๋ ์ค ์์ฌ๊ฐ ๊ฐ๋ฅํ ์ฒ ํ์์๊ฒ check ๋ด๋ถ์์ signal์ ๋ณด๋ธ๋ค.
*/
put_chopsticks(int i)
{
wait(mutex);
state[i] = SLEEPING;
check(LEFT);
check(RIGHT);
signal(mutex);
}
/*
** Solution
*/
do
{
...
THINKING
...
take_chopsticks(i)
...
EATING
...
put_chopsticks(i);
...
SLEEPING
...
} while (1);
C
๋ณต์ฌ
ํ์ฌ ์ํฉ
โข
์ฒ ํ์1(sleeping) ์ฒ ํ์2(Eating) ์ฒ ํ์3 ์ฒ ํ์4(sleeping) ์ฒ ํ์5(sleeping)
โข
์ฒ ํ์3๊ฐ ์์ฌํ ์ ์๋์ง์ ๋ํ ํ๋จ๊ณผ์ ์ ํ์ธ
๊ณผ์
1.
์ฒ ํ์3๊ฐ take_chopsticksํจ์๋ก ๋ค์ด๊ฐ
take_chopsticks(int i)
{
wait(mutex);
state[์ฒ ํ์3] = THINKING;
check(์ฒ ํ์3);
signal(mutex);
wait(philo[์ฒ ํ์3]);
}
C
๋ณต์ฌ
โข
wait(mutex)
์ฒ ํ์3 ๋ง๊ณ ๋ค๋ฅธ ์ฒ ํ์์ ๋ํ ์ํ ์ ๊ทผ ๋ถ๊ฐ
โข
state[์ฒ ํ์3] = THINKING;
๋ฐฅ์ ๋จน๊ธฐ ์ ์ THINKING
โข
check(์ฒ ํ์3)
check(int i)
{
if (state[์ฒ ํ์3] == THINKING && state[์ฒ ํ์2] != EATING && state[์ฒ ํ์4] != EATING)
{
state[i] = EATING;
signal(philo[i]);
}
}
C
๋ณต์ฌ
์ฒ ํ์2๊ฐ Eating์ด๊ธฐ ๋๋ฌธ์ ์กฐ๊ฑด๋ฌธ ์์ ํ์ฑํ๋์ง ์๊ณ ๋์ด๊ฐ
โข
signal(mutex)
์ฒ ํ์3๊ฐ ์์ฌ๋ฅผ ํ ์ ์๊ธฐ ์ํด์๋ ์ฒ ํ์2๊ฐ ์์ฌ๊ฐ ๋์ด ๋์ผํ๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ์ฒ ํ์๋ค์ ์ํ์ ๊ทผ์ ํ์ฉํด์ค
โข
wait(philo[์ฒ ํ์3])
์ฒ ํ์2๊ฐ ์์ฌ๋ฅผ ๋ง์น ๋๊น์ง ๊ธฐ๋ค๋ฆฐ๋ค
2.
์ฒ ํ์2๊ฐ put_chopsticksํจ์๋ก ๋ค์ด๊ฐ
put_chopsticks(int i)
{
wait(mutex);
state[์ฒ ํ์2] = SLEEPING;
check(์ฒ ํ์1);
check(์ฒ ํ์3);
signal(mutex);
}
C
๋ณต์ฌ
โข
wait(mutex)
๋ค๋ฅธ ์ฒ ํ์ ์ํ์ ์ ๊ทผํ์ง ๋ชปํ๊ฒ guard
โข
state[์ฒ ํ์2] = SLEEPING;
์ฒ ํ์2์ ์ํ๋ฅผ sleeping
โข
check(์ฒ ํ์1)
์ฒ ํ์1์ด sleeping์ด๊ธฐ ๋๋ฌธ์ ๊ทธ๋ฅ ๋์ด๊ฐ
โข
check(์ฒ ํ์3)
check(int i)
{
if (state[์ฒ ํ์3] == THINKING && state[์ฒ ํ์2] != EATING && state[์ฒ ํ์4] != EATING)
{
state[i] = EATING;
signal(philo[i]);
}
}
C
๋ณต์ฌ
์ด๋ ๊ฒ ๋ค์ ์ฒ ํ์3์ ์ํ๋ฅผ ํ์ธํ๊ฒ ๋๊ณ ์ฒ ํ์2๊ฐ Eating์์ sleeping์ด ๋์๊ธฐ ๋๋ฌธ์ ์ด์ ์ฒ ํ์3๋ ์์ฌ๋ฅผ ํ ์ ์๊ฒ ๋๋ค
context switch
โข
ํ๋์ ์ฝ์ด์๋ ํ๋์ ์์
๋ง ์กด์ฌ
โข
๋ชจ๋ ํ๋ก์ธ์ค๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด์ ํ๋์ ํ๋ก์ธ์ค๋ฅผ ์ฒ๋ฆฌํ๊ณ ๊ทธ ๋ค์ ํ๋ก์ธ์ค๊ฐ ์ฒ๋ฆฌํ๋ ์์ ํ๋ก์ธ์ค ๊ฐ์ ์ ํ
โข
Kernel์ Dispatcher๋ผ๋ ๊ณณ์์ ์ผ์ด๋๋ฉฐ PCB๋ผ๋ ์๋ฃ๊ตฌ์กฐ๊ฐ ํ์
โข
Time Quantum์ด ๋ชจ๋ ์์ง๋๊ฑฐ๋, Interrupt์ ์ํด ๋ฐ์
Time Quantum์ ํ๋ก์ธ์ค๊ฐ ํ ๋ฒ์ ์ฒ๋ฆฌ๋ ์ ์๋ ์๊ฐ ์ด๋์ ์๋ฏธํ๋ค. ์ผ๋ฐ์ ์ผ๋ก ํ๋ก์ธ์ค์๊ฒ ํ ๋น๋๋ Time Quantum์ ์ฌ์ฉ์๊ฐ ์ฒด๊ฐํ์ง ๋ชปํ ์ ๋๋ก ์๋ค. ๋๋ถ์ ํ ๋ฒ์ ํ๋์ ํ๋ก์ธ์ค ๋ฐ์ ์ฒ๋ฆฌํ์ง ๋ชปํ๋ ์ํฉ์์๋ ์ฌ์ฉ์๋ ๋ชจ๋ ํ๋ก์ธ์ค๊ฐ ๋์์ ์ฒ๋ฆฌ๋๋ ๊ฒ์ฒ๋ผ ๋๋ผ๊ฒ ๋๋ค.
โข
์์
ํ๋ ๋ด์ฉ์ PCB์ ์ ์ฅํ๊ณ ์์
ํ๊ณ ์ถ์ ๋ด์ฉ์ PCB๋ก ๋ถํฐ ์ฝ์ด๋ค์ธ๋ค
Context Switch๋ Interrupt์ ์ํด ๋ฐ์ํ์ง๋ง ํ๋ก์ธ์ค์ ๊ธฐ๋ก ์์ฒด๋ ์์คํ
์ฝ์ ๊ธฐ๋ฐํ๋ ๊ฒ์ ์ ์ ์๋ค.
Context Switch ์์ฒด๋ ์์์ ์ธ๊ธ๋ ๊ฒ์ฒ๋ผ Dispatcher์ ์ํด์ ์ฒ๋ฆฌ๋๋๋ฐ, ์ด๋ ๊ณง Dispatcher์ ํธ์ถ๋ก๋ถํฐ ์์๋๋ค. Dispatcher์ ํธ์ถ์ Interrupt์ ์ํด ๋ฐ์ํ๋ค. Dispatcher๋ฅผ ํธ์ถํ๋ Interrupt๋ Preemptive Scheduling๊ณผ Non-Preemptive Scheduling์ผ๋ก ๋๋๋ค. Time Quantum์ ๋ชจ๋ ์์งํ์ฌ ์ด์์ฒด์ ๊ถํ์ผ๋ก ํ๋ก์ธ์ค์ ๊ถํ์ ๋บ์ผ๋ฉด์ ๋ฐ์ํ๋ Interrupt๊ฐ Preemptive Scheduling์ด๊ณ , I/O ํธ์ถ๊ณผ ๊ฐ์ด ํ๋ก์ธ์ค๊ฐ ์ค์ค๋ก CPU ์ ์ ๋ฅผ ํฌ๊ธฐํ๋ฉด์ ๋ฐ์ํ๋ Interrupt๊ฐ Non-Preemptive Scheduling์ด๋ค.
Preemptive / Non-preemptive
โฆ
๋น์ ์ ํ ์ค์ผ์ค๋ง
์ด๋ค ํ๋ก์ธ์ค๊ฐ CPU๋ฅผ ํ ๋น ๋ฐ์ผ๋ฉด ๊ทธ ํ๋ก์ธ์ค๊ฐ ์ข
๋ฃ๋๊ฑฐ๋ IO request๊ฐ ๋ฐ์ํ์ฌ ์๋ฐ์ ์ผ๋ก ๋๊ธฐ ์ํ๋ก ๋ค์ด๊ฐ ๋๊น์ง ๊ณ์ ์คํ
์ด๋ค ํ๋ก์ธ์ค๊ฐ ์์
์ ๋ง์น๊ณ ์๋ฐ์ ์ผ๋ก ๋๊ธฐ ์ํ๋ก ๋ค์ด๊ฐ๊ฑฐ๋ ์ข
๋ฃ๋๋ ๊ฒฝ์ฐ ๋ค๋ฅธ ํ๋ก์ธ์ค๊ฐ ์คํ
โฆ
์ ์ ํ ์ค์ผ์ค๋ง
์ด๋ค ํ๋ก์ธ์ค๊ฐ ์คํ๋๋ค time slice๋ฅผ ๋ชจ๋ ์ฌ์ฉํด time-out๋๊ฑฐ๋, IO๊ฐ ๋ฐ์ํ๊ฑฐ๋, event๋ฅผ ๊ธฐ๋ค๋ ค์ผ ํ๋ ์ํฉ์ด๋ผ๋ฉด ๋ค๋ฅธ ํ๋ก์ธ์ค์๊ฒ CPU ์ฌ์ฉ์ ์๋ณด
ํ๋ก์ธ์ค๋ฅผ ์ซ์ ๋ด๊ณ CPU์์์ ์ ์ ํ ์ ์๋ค๋ ๋ป
ํ์ฌ OS๋ ๋๋ถ๋ถ ์๋ถํ ์ ์ ํ ์ค์ผ์ฅด๋ง์ ์ฌ์ฉ. ๋น์ ์ ํ์ ํด๋น ์์
์ด ๋๋ ๋ ๊น์ง ๊ณ์ ์คํ๋๊ธฐ ๋๋ฌธ์ ๋ฉํฐ ํ๋ก์ธ์ค ํ๊ฒฝ์์ ์๋ต์ฑ์ ๊ธฐ๋ํ ์ ์๋ค
User mode & kernel mode
ํ๋ก์ธ์ค์์ instruction์ ์ํํ ๋ usermode ์ kernelmode์ ๋ฐ๋ผ์ cpu ์คํ์ด ๋ฌ๋ผ์ง
User Mode์ ํ๋ก์ธ์ค๋ ๋์ฒด์ ์ผ๋ก ์ฌ์ฉ์์ ์ํด ๊ตฌ๋๋ ์ดํ๋ฆฌ์ผ์ด์
์ด๋ฉฐ, Instruction์ ๋ํด ๋ฒ์ฉ์ ์ธ ๊ถํ์ ๊ฐ๊ณ ๋ ์์ง๋ง Kernel Mode ๋งํผ์ ๊ถํ์ ๊ฐ์ง๋ ๋ชปํ๋ค.
trap & interrupt
1.
trap
โข
๋๊ธฐ์ ์ด๋ฒคํธ ์ฒ๋ฆฌ
โข
ํ์ฌ ์ฒ๋ฆฌํ๊ณ ์๋ ํ๋ก๊ทธ๋จ์ ์ํด ๋ฐ์
โข
์ฃผ๋ก ์์คํ
์ฝ ์ฒ๋ฆฌ
โข
trap handler์ ์ํด ์ฒ๋ฆฌ
โข
trap์ด ๋ฐ์ํ๊ฒ ๋๋ฉด ๊ทธ ๋
์์ด ๋ค ์ฒ๋ฆฌ๋๊ณ ๋์ผ ๋ค๋ฅธ ํ๋ก๊ทธ๋จ์ ์ฒ๋ฆฌํ๊ธฐ ๋๋ฌธ์ context๋ฅผ ์ ์ฅํ ํ์๋ ์๊ณ sp์ pc๋ง์ ์ด์ฉํ์ฌ ๊ธฐ์กด์ ์ํํ๋ ๋์์ผ๋ก ๋ณต๊ท ๊ฐ๋ฅ
2.
interrupt
โข
๋น๋๊ธฐ์ ์ด๋ฒคํธ ์ฒ๋ฆฌ
โข
๋คํธ์ํฌ ํจํท ๋์ฐฉ ํน์ I/O์ ๋ํ ์ด๋ฒคํธ ์ฒ๋ฆฌ
โข
ํ๋์จ์ด์ ์ํด ๋ฐ์. ํ๋์จ์ด์ ์ฐ์ ์์์ ๋ฐ๋ผ ์ฐ์ ์์ ๊ฒฐ์
โข
ํ๋์จ์ด๋ค์ด ๋์์ interrupt๋ฅผ ๊ฑธ์ด๋ ์ฒ๋ฆฌ ๊ฐ๋ฅ
โข
interrupt handler์ ์ํด ์ฒ๋ฆฌ
โข
interrupt๋ฅผ ์ฒ๋ฆฌํ ๋ค ๋ณต๊ทํด์ผ ํ๋ ํ๋ก์ธ์ค๊ฐ ํ์ฌ ํ๋ก์ธ์ค๊ฐ ์๋ ์ ์๊ธฐ ๋๋ฌธ์ context์ ๋ํ ๊ธฐ๋ก์ด ์๊ตฌ๋จ
3.
ํน์ง
Trap์ ๊ฒฝ์ฐ์๋ Trap Service Routine ๋ด์์ ์ฒ๋ฆฌ ๋์ค์ Interrupt๋ฅผ ๋ฐ์ ์๋ ์์ง๋ง, Interrupt์ ๊ฒฝ์ฐ์๋ Interrupt Service Routine ๋ด์์ ์ฒ๋ฆฌ ๋์ค์ Inetrrupt๋ฅผ ๋ฐ์ ์ ์๊ฒ ๋์ด ์๋ค. ์ด๋ Interrupt์ Depth๊ฐ ๊น์ด์ง์ ๋ฐ๋ผ ํ๋์ Interrupt๊ฐ ๋๋๋๋ฐ ๊ธด ์๊ฐ์ด ์๊ตฌ๋๋ ๊ตฌ์กฐ๊ฐ ๋ ์ ์๊ธฐ ๋๋ฌธ์ด๋ค. ๋ฐ๋ผ์ Interrupt๋ฅผ ์ฒ๋ฆฌํ ๋ ๋ค๋ฅธ Interrupt๋ค๋ ์ฒ๋ฆฌํ ์ ์๋๋ก Interrupt Serivce Routine์ ์ต๋ํ ์งง๊ฒ ์ํ๋๋๋ก ์ค๊ณ ๋์ด ์๋ค.
Trap์ ํ๋ก์ธ์ค์ Context๋ฅผ ์ ์ฅํ์ง ์์๋ ๋๋ค๋ ๋ฉด์์ Interrupt๋ณด๋ค ๊ฐ๋ณ์ง๋ง Trap์ ๋ํ ์ฒ๋ฆฌ๊ฐ ์๋ฃ๋ ๋๊น์ง Block๋๋ค๋ ํน์ง์ด ์๋ค. Interrupt๋ Trap์ ๋ฐ๋์ ํน์ง์ ๊ฐ๊ณ ์๋ค๊ณ ๋ณด๋ฉด ๋๋ค.
๋ง์ผ Interrupt๊ฐ ๋์์ ๋ฐ์ํ๋ฉด ์ฐ์ ์์์ ๋ฐ๋ผ ์ฒ๋ฆฌํ๋ค๊ณ ํ๋๋ฐ, ๋์ผํ ์ฐ์ ์์๋ฅผ ๊ฐ์ง Interrupt๋ฅผ ๋ฐ๊ฒ ๋๋ฉด ์ด์์ฒด์ ์ ํ๋์จ์ด์ ์ํด ํ๋์ Interrupt๋ฅผ ์ฒ๋ฆฌํ๊ณ ๋๋จธ์ง Interrupt๋ ์ ์ฅํด๋๊ฑฐ๋ ๋ฌด์ํ๊ฒ ๋๋ค.
๊ฐ๋ฐ์ผ์ง
์ ๋ ฅ๊ฐ
./philo number_of_philosophers time_to_die time_to_eat time_to_sleep {number_of_times_each_philosopher_must_eat}
Shell
๋ณต์ฌ
number_of_philosophers: ์ฒ ํ์์ ์์ ํฌํฌ์ ์์ด๋ค.
time_to_die: ๋ฐ๋ฆฌ ์ด ๋จ์์ด๋ฉฐ, ๋ง์ฝ ์ฒ ํ์๊ฐ ๋ง์ง๋ง ์์ฌ๋ฅผ ์์ํ๊ฑฐ๋ ์๋ฎฌ๋ ์ด์
์ ์์ํ ํ
'time_to_die' miliseconds๋ฅผ ๋จน๊ธฐ ์์ํ์ง ์๋๋ค๋ฉด ์ฃฝ๋๋ค.
time_to_eat: ๋ฐ๋ฆฌ ์ด ๋จ์์ด๋ฉฐ, ์ฒ ํ์๊ฐ ์์ฌํ๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ด๋ค. ๊ทธ ์๊ฐ ๋์ ๊ทธ๋ ํฌํฌ ๋๊ฐ๋ฅผ ์
์งํด์ผ ํ ๊ฒ์ด๋ค.
time_to_sleep: ๋ฐ๋ฆฌ ์ด ๋จ์์ด๋ฉฐ, ์ฒ ํ์๊ฐ ์ ์ ์๋ ๋ฐ ์ฐ๋ ์๊ฐ์ด๋ค.
number_of_times_each_philosopher_must_eat: ๋
ผ์์ ์ ํ์ฌํญ์ด๋ฉฐ, ๋ง์ฝ ๋ชจ๋
์ฒ ํ์๋ค์ด ์ ์ด๋ 'number_of_times_each_philosopher_must_eat'์ ๋จน๋๋ค๋ฉด, ์๋ฎฌ๋ ์ด์
์
์ค๋จ๋ ๊ฒ์ด๋ค. ๊ตฌ์ฒด์ ์ผ๋ก ๋ช
์ํ์ง ์์ผ๋ฉด, ์๋ฎฌ๋ ์ด์
์ ์ฒ ํ์๊ฐ ์ฌ๋ง ํ ๋๋ง ์ค๋จ๋ ๊ฒ์
๋๋ค.
Pseudo code
์ธ์๊ฐ ๊ตฌ์กฐ์ฒด์ ์ด๊ธฐํ()
{
์ธ์๊ฐ atoi()
์ฒ ํ์ ์๋งํผ mutex์์ฑ()
}
์ธ์๊ฐ๊ตฌ์กฐ์ฒด๋ฅผ ์ฒ ํ์ ๊ตฌ์กฐ์ฒด์ ์ด๊ธฐํ()
{
int main()
{
์ธ์๊ฐ์ ์ ํจ์ฑ์ฒดํฌ()
์ธ์๊ฐ ๊ตฌ์กฐ์ฒด์ ์ด๊ธฐํ()
์ธ์๊ฐ๊ตฌ์กฐ์ฒด๋ฅผ ์ฒ ํ์ ๊ตฌ์กฐ์ฒด์ ์ด๊ธฐํ()
C
๋ณต์ฌ
mutex ๊ฑธ์ด์ผ ํ๋ ์ด์
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
pthread_mutex_t m;
void *t_function(void *data)
{
pid_t pid; // process id
pthread_t tid; // thread id
pid = getpid();
tid = pthread_self();
char* thread_name = (char*)data;
int i = 0;
pthread_mutex_lock(&pm);
while (i < 5) // 0,1,2 ๊น์ง๋ง loop ๋๋ฆฝ๋๋ค.
{
// ๋๊ฒจ๋ฐ์ ์ฐ๋ ๋ ์ด๋ฆ๊ณผ
// ํ์ฌ process id ์ thread id ๋ฅผ ํจ๊ป ์ถ๋ ฅ
printf("[%s] pid:%u, tid:%x --- %d\n",
thread_name, (unsigned int)pid, (unsigned int)tid, i);
i++;
sleep(1); // 1์ด๊ฐ ๋๊ธฐ
}
pthread_mutex_unlock(&m);
}
int main()
{
pthread_t p_thread[2];
int thr_id;
int status;
char p1[] = "thread_1";
char p2[] = "thread_2";
char pM[] = "thread_m";
sleep(1);
pthread_mutex_init(&m, NULL);
thr_id = pthread_create(&p_thread[0], NULL, t_function, (void *)p1);
if (thr_id < 0)
{
perror("thread create error : ");
exit(0);
}
thr_id = pthread_create(&p_thread[1], NULL, t_function, (void *)p2);
if (thr_id < 0)
{
perror("thread create error : ");
exit(0);
}
while(1)
;
pthread_join(p_thread[0], (void **)&status);
pthread_join(p_thread[1], (void **)&status);
printf("์ธ์ ์ข
๋ฃ ๋ ๊น์?\n");
return (0);
}
C
๋ณต์ฌ
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
pthread_mutex_t m;
void *t_function(void *data)
{
pid_t pid; // process id
pthread_t tid; // thread id
pid = getpid();
tid = pthread_self();
char* thread_name = (char*)data;
int i = 0;
// pthread_mutex_lock(&m);
while (i < 5) // 0,1,2 ๊น์ง๋ง loop ๋๋ฆฝ๋๋ค.
{
// ๋๊ฒจ๋ฐ์ ์ฐ๋ ๋ ์ด๋ฆ๊ณผ
// ํ์ฌ process id ์ thread id ๋ฅผ ํจ๊ป ์ถ๋ ฅ
printf("[%s] pid:%u, tid:%x --- %d\n",
thread_name, (unsigned int)pid, (unsigned int)tid, i);
i++;
sleep(1); // 1์ด๊ฐ ๋๊ธฐ
}
// pthread_mutex_unlock(&m);
}
int main()
{
pthread_t p_thread[2];
int thr_id;
int status;
char p1[] = "thread_1";
char p2[] = "thread_2";
char pM[] = "thread_m";
sleep(1);
pthread_mutex_init(&m, NULL);
thr_id = pthread_create(&p_thread[0], NULL, t_function, (void *)p1);
//pthread_detach(p_thread[0]);
if (thr_id < 0)
{
perror("thread create error : ");
exit(0);
}
thr_id = pthread_create(&p_thread[1], NULL, t_function, (void *)p2);
//pthread_detach(p_thread[1]);
if (thr_id < 0)
{
perror("thread create error : ");
exit(0);
}
while(1)
;
pthread_join(p_thread[0], (void **)&status);
pthread_join(p_thread[1], (void **)&status);
printf("์ธ์ ์ข
๋ฃ ๋ ๊น์?\n");
return (0);
}
C
๋ณต์ฌ
โข
mutex๋ฅผ ๊ฑธ์ด์ฃผ๋ฉด thread1์ด ๋จผ์ ๋ค ๋๊ณ thread2๊ฐ ๋๋ ๊ฒ์ ์ ์ ์๋ค
์ฐ๋ ๋๋?
pthread_detach์ pthread_join์ ์ฐจ์ด
๋๋ค ํจ์๊ฐ ๋์ด๋๋ฉด ์์ํด์ ๋ฅผ ํด์ค. thread์์์ ํ์ผ๋ฉด ์์ํด์ ๋ฅผ ๋ฌด์กฐ๊ฑด ํด์ค์ผํจ
join
โข
join์ ๊ทธ ํด๋น ์ฐ๋ ๋๊ฐ ๋๋ ๋๊น์ง ๋๊ธฐ. ๋ฆฌํด๊ฐ์ ๋ฐ์์ด
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
// ์ฐ๋ ๋ ํจ์
// 1์ด๋ฅผ ๊ธฐ๋ค๋ฆฐํ (๋งค๊ฐ๋ณ์*๋งค๊ฐ๋ณ์)๋ฅผ ๋ฆฌํดํฉ๋๋ค
void *t_function(void *data)
{
int num = *((int *)data);
printf("num %d\n", num);
sleep(1);
num *= num;
printf("์ฐ๋ ๋ ํจ์ ์ข
๋ฃ ํฉ๋๋ค\n");
return (void *)(num); // warning ๋ฐ์
}
int main()
{
pthread_t p_thread;
int thr_id;
int result;
int a = 200;
thr_id = pthread_create(&p_thread, NULL, t_function, (void *)&a);
if (thr_id < 0)
{
perror("thread create error : ");
exit(0);
}
// ์ฐ๋ ๋ ์๋ณ์ p_thread ๊ฐ ์ข
๋ฃ๋๊ธธ ๊ธฐ๋ค๋ ธ๋ค๊ฐ
// ์ข
๋ฃํ์ ๋ฆฌํด๊ฐ์ ๋ฐ์์ต๋๋ค.
//pthread_join(p_thread, (void *)&result);
printf("thread join : %d\n", result);
printf("main() ์ข
๋ฃ\n");
return 0;
}
C
๋ณต์ฌ
pthread_join x
pthread_join o
detach๋ ๋ฉ์ธ ์ฐ๋ ๋์์ ๋ถ๋ฆฌ๋์ด ๋ฉ์ธ ์ฐ๋ ๋๊ฐ ๋๋๋ฉด ์ข
๋ฃ. ์ด๊ฒ์ ๋ง๊ธฐ ์ํด ์ ์ ํ mutex lock ํ์. join๊ณผ ๋ค๋ฅด๊ฒ ๋ฐ๋ก ๋ค์ ๋ช
๋ น์ด ์คํ














