๊ด€๋ฆฌ ๋ฉ”๋‰ด

๋ง๊ฐ๋กœ๊ทธ

ํฌ๋ž˜ํ”„ํ†ค ์ •๊ธ€ WEEK07 DAY56 - PintOS(Alarm Clock - priority) / 7์ฃผ์ฐจ ํ€ด์ฆˆ ๋ฆฌ๋ทฐ ๋ณธ๋ฌธ

Krafton jungle

ํฌ๋ž˜ํ”„ํ†ค ์ •๊ธ€ WEEK07 DAY56 - PintOS(Alarm Clock - priority) / 7์ฃผ์ฐจ ํ€ด์ฆˆ ๋ฆฌ๋ทฐ

habbn 2024. 3. 5. 23:22
728x90
๐Ÿ“†2024.3.5

1. PintOS (Alarm Clock - Priority)
2. 7์ฃผ์ฐจ ํ€ด์ฆˆ
3. ๋ฐฑ์ค€

 

PintOS (Alarm Clock - Priority)

 

์–ด์ œ PintOS Project 1 Alarm Clock Priority ์ œ์™ธํ•˜๊ณ  ๋‹ค ๊ตฌํ˜„ํ•ด์„œ ์˜ค๋Š˜ Priority ๋ถ€๋ถ„์„ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

์šฐ๋ฆฌ๊ฐ€ ์ƒ๊ฐํ•œ Priority์˜ ์กฐ๊ฑด์€ "๋งŒ์ผ ๊ฐ™์€ sleep_time์„ ๊ฐ€์ง„ ์Šค๋ ˆ๋“œ๋“ค์ด sleep_list์— ์žˆ์„ ๋•Œ(๋™์‹œ์— ๊นจ์–ด๋‚˜๋Š” ์Šค๋ ˆ๋“œ๋“ค์ด ์—ฌ๋Ÿฌ ๊ฐœ ์ผ ๋•Œ) ์šฐ์„ ์ˆœ์œ„๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ์Šค๋ ˆ๋“œ ๋จผ์ € ready_list์— ๋„ฃ์–ด์ค€๋‹ค" ๊ณ  ์ƒ๊ฐํ•˜์˜€๋‹ค.

 

๊ทธ๋ž˜์„œ greater_list๋ผ๋Š” ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋‹ด์„ ํ๋ฅผ ๋งŒ๋“ค๊ณ  thread_wakeup ํ•จ์ˆ˜ ๋ถ€๋ถ„์—์„œ ๋จผ์ € sleep_list ๋งจ ์•ž์— ์žˆ๋Š” ์Šค๋ ˆ๋“œ๋ฅผ popํ•œ sleep_pop_front_thread๋ฅผ ๊ตฌ์กฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์คฌ๋‹ค.

์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•ด์•ผํ•˜๋‹ˆ๊นŒ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ฃผ๋Š” priority_greater boolํ˜• ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ •๋ ฌ์‹œํ‚ค๊ณ  

greater_list์— ์‚ฝ์ž…ํ•ด์ฃผ์—ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ greater_list๊ฐ€ ๋น„๊ธฐ ์ „๊นŒ์ง€ ready_list์— ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

 

+ ์ถ”๊ฐ€ํ•œ ์ฝ”๋“œ

void
thread_wakeup(int64_t ticks)
{
	if(list_empty(&sleep_list))
		return;

	enum intr_level old_level;
	
	//sleep_list์˜ ์‹œ์ž‘ ์ง€์ ์— ๋Œ€ํ•œ iterator๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ํ•ด๋‹น ์š”์†Œ๋ฅผ struct thread๋กœ ์บ์ŠคํŒ…
	struct thread *sleep_front_thread = list_entry(list_begin(&sleep_list),struct thread, elem);

	struct thread *sleep_pop_front_thread;

	while(sleep_front_thread->wakeup_tick <= ticks)
	{
		old_level = intr_disable();
		sleep_front_thread->status = THREAD_READY;
        
		/* alarm clock - priority */
		sleep_pop_front_thread = list_entry(list_pop_front(&sleep_list), struct thread, elem);
		list_insert_ordered(&greater_list, &sleep_pop_front_thread->elem , (list_less_func *) &priorty_greater, NULL);
		
		// list_push_back(&ready_list, list_pop_front(&sleep_list));
		sleep_front_thread = list_entry(list_begin(&sleep_list),struct thread, elem);	
		intr_set_level(old_level);
	}

	while(!list_empty(&greater_list))
	{
		old_level = intr_disable();
		list_push_back(&ready_list, list_pop_front(&greater_list));
		intr_set_level(old_level);
	}
}

 

bool
priorty_greater(const struct list_elem *a, const struct list_elem *b, void *aux UNUSED) {
	struct thread *ta = list_entry(a, struct thread, elem);
	struct thread *tb = list_entry(b, struct thread, elem);
	return ta->priority > tb->priority;
}

 

์ด๊ฒŒ ๋˜๋‚˜..?? ํ–ˆ๋Š”๋ฐ pass tests/threads/alarm-priority ...  

 

 

7์ฃผ์ฐจ ํ€ด์ฆˆ 

 

[1] ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•  ๋•Œ multiprocess์™€ multithread ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•˜๋Š” ๊ธฐ์ค€์€ ์–ด๋–ค ๊ฒƒ์ด ์žˆ๋Š”์ง€ ๋ช‡ ๊ฐ€์ง€ ์ œ์‹œํ•˜์„ธ์š”.

 

์•ˆ์ •์„ฑ VS ์ž์› ์‚ฌ์šฉ

์‹œ์Šคํ…œ์˜ ์•ˆ์ •์„ฑ์ด ๋งค์šฐ ์ค‘์š”ํ•œ ๊ฒฝ์šฐ, ๋ฉ€ํ‹ฐํ”„๋กœ์„ธ์Šค๊ฐ€ ์„ ํ˜ธ๋œ๋‹ค. ๋ฆฌ์†Œ์Šค๊ฐ€ ์ œํ•œ์ ์ธ ํ™˜๊ฒฝ์—์„œ๋Š” ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ๊ฐ€ ๋” ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ๋‹ค.

- ๋‹ค์ค‘ ํ”„๋กœ์„ธ์Šค๋Š” ๋…๋ฆฝ์ ์ธ ์ฃผ์†Œ ๊ณต๊ฐ„์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ์นจ๋ฒ” ๋ฐ ๋ฐ์ดํ„ฐ ์˜ค์—ผ์ด ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์ ๋‹ค.

- ๋‹ค์ค‘ ์Šค๋ ˆ๋“œ๋Š” ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๊ฐ€ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋ฉด ํ”„๋กœ์„ธ์Šค ์ „์ฒด๊ฐ€ ์˜ํ–ฅ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์•ˆ์ •์„ฑ์ด ์ƒ๋Œ€์ ์œผ๋กœ ๋‚ฎ๋‹ค.

 

๊ตฌํ˜„์˜ ๋ณต์žก์„ฑ

์Šค๋ ˆ๋“œ๋Š” ๊ณต์œ  ๋ฉ”๋ชจ๋ฆฌ๋กœ ์ธํ•ด ๋™๊ธฐํ™” ๋ฌธ์ œ๊ฐ€ ๋ณต์žกํ•ด์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๊ฐœ๋ฐœ์ž์˜ ๋™์‹œ์„ฑ ์ œ์–ด์— ๋Œ€ํ•œ ์ดํ•ด๋„๊ฐ€ ์ค‘์š”ํ•˜๋‹ค

- ๋ฉ€ํ‹ฐ ํ”„๋กœ์„ธ์Šค์˜ ๊ฐ๊ฐ์˜ ํ”„๋กœ์„ธ์Šค๋Š” ์šด์˜ ์ฒด์ œ์— ์˜ํ•ด ๋ถ„๋ฆฌ๋˜๋ฏ€๋กœ ์‹คํŒจํ•œ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋Š” ๊ฒƒ์ด ๋น„๊ต์  ๊ฐ„๋‹จํ•˜๋‹ค.

 

์‘๋‹ต ์‹œ๊ฐ„

๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ๋Š” ์ปจํ…์ŠคํŠธ ์Šค์œ„์นญ์ด ๋น ๋ฅด๊ธฐ ๋•Œ๋ฌธ์—, ๋” ๋น ๋ฅธ ์‘๋‹ต ์‹œ๊ฐ„์„ ์š”๊ตฌํ•˜๋Š” ๊ฒฝ์šฐ ์œ ๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ”Œ๋žซํผ ๋ฐ ์–ธ์–ด ์ง€์›

์‚ฌ์šฉ ์ค‘์ธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋‚˜ ํ”Œ๋žซํผ์ด ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ ๋˜๋Š” ๋ฉ€ํ‹ฐํ”„๋กœ์„ธ์Šค ์ค‘ ์–ด๋Š ์ชฝ์„ ๋” ์ž˜ ์ง€์›ํ•˜๋Š”์ง€๋„ ์ค‘์š”ํ•œ ์š”์†Œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.

 

-> ์•ˆ์ •์„ฑ์ด ์ค‘์š”ํ•œ ์‹œ์Šคํ…œ์—์„œ๋Š” ๋‹ค์ค‘ ํ”„๋กœ์„ธ์Šค, ๋น ๋ฅธ ์‘๋‹ต ๋ฐ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ์›น ์„œ๋ฒ„์™€ ๊ฐ™์€ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” ๋‹ค์ค‘ ์Šค๋ ˆ๋“œ๊ฐ€ ์ ํ•ฉํ•˜๋‹ค.

 

---> ์™„์ „ํžˆ ํ‹€๋ ค๋ฒ„๋ฆฐ ๋ฌธ์ œ..

 

 

[2]  ๋ฐ๋“œ๋ฝ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์ „๋žต์„ ๋‘ ๊ฐ€์ง€ ์ด์ƒ ์„ค๋ช…ํ•˜์‹œ์˜ค.

 

* ๋ฐ๋“œ๋ฝ : ๋‘ ๊ฐœ ์ด์ƒ์˜ ํ”„๋กœ์„ธ์Šค๋‚˜ ์Šค๋ ˆ๋“œ๊ฐ€ ์„œ๋กœ ์ž์›์„ ์–ป์ง€ ๋ชปํ•˜์—ฌ ๋‹ค์Œ ์ฒ˜๋ฆฌ๋ฅผ ๋ชปํ•˜๋Š” ์ƒํƒœ

 

๋ฐ๋“œ๋ฝ ํ•ด๊ฒฐ ์ „๋žต

 

1. ๋ฐ๋“œ๋ฝ ์˜ˆ๋ฐฉ (Deadlock Prevetion)

์ด ์ ‘๊ทผ๋ฒ•์€ ๋ฐ๋“œ๋ฝ ๋ฐœ์ƒ์„ ์›์ฒœ์ ์œผ๋กœ ์ฐจ๋‹จํ•œ๋‹ค. ๋ฐ๋“œ๋ฝ์ด ๋ฐœ์ƒํ•˜๋Š” ๋„ค ๊ฐ€์ง€ ํ•„์ˆ˜ ์กฐ๊ฑด(์ƒํ˜ธ ๋ฐฐ์ œ, ์ ์œ ์™€ ๋Œ€๊ธฐ, ๋น„์„ ์ , ์ˆœํ™˜ ๋Œ€๊ธฐ) ์ค‘ ์ ์–ด๋„ ํ•˜๋‚˜๋ฅผ ์ œ๊ฑฐํ•จ์œผ๋กœ์จ ๋ฐ๋“œ๋ฝ์„ ๋ฐฉ์ง€ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋น„์„ ์  ์กฐ๊ฑด์„ ์ œ๊ฑฐํ•˜๋ฉด ์–ด๋–ค ๋ฆฌ์†Œ์Šค๋„ ํ•„์š”ํ•  ๋•Œ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์— ์˜ํ•ด ์„ ์ ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Š” ๋ฐ๋“œ๋ฝ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

2. ๋ฐ๋“œ๋ฝ ํšŒํ”ผ (Deadlock Avoidance)

๋ฐ๋“œ๋ฝ ํšŒํ”ผ๋Š” ์‹œ์Šคํ…œ์ด ๋ฐ๋“œ๋ฝ ์ƒํƒœ๋กœ ์ง„์ž…ํ•˜๋Š” ๊ฒƒ์„ ํšŒํ”ผํ•˜๋Š” ์ „๋ ฅ์ด๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์‹œ์Šคํ…œ์€ ๋ฆฌ์†Œ์Šค ํ• ๋‹น ๊ฒฐ์ • ์‹œ ๋ฐ๋“œ๋ฝ์˜ ๊ฐ€๋Šฅ์„ฑ์„ ๊ณ ๋ คํ•œ๋‹ค. ๊ฐ€์žฅ ์œ ๋ช…ํ•œ ์˜ˆ๋Š” ๋ฑ…์ปค์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜(Banker's Algorithm)์œผ๋กœ, ์ด๋Š” ํ”„๋กœ์„ธ์Šค์— ๋ฆฌ์†Œ์Šค๋ฅผ ํ• ๋‹นํ•˜๊ธฐ ์ „์— ์•ˆ์ „ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋งŒ์•ฝ ํ• ๋‹น์œผ๋กœ ์ธํ•ด ๋ฐ๋“œ๋ฝ์ด ๋ฐœ์ƒํ•  ์œ„ํ—˜์ด ์žˆ๋‹ค๋ฉด, ๋ฆฌ์†Œ์Šค๋Š” ํ• ๋‹น๋˜์ง€ ์•Š๋Š”๋‹ค.

 

3. ๋ฐ๋“œ๋ฝ ํƒ์ง€ ๋ฐ ํšŒ๋ณต (Deadlock Detection and Recovery)

์ด ์ „๋žต์€ ์‹œ์Šคํ…œ์ด ๋ฐ๋“œ๋ฝ์„ ํƒ์ง€ํ•˜๊ณ , ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์กฐ์น˜๋ฅผ ์ทจํ•˜๋Š” ๊ฒƒ์„ ํฌํ•จํ•œ๋‹ค. ๋ฐ๋“œ๋ฝ ํƒ์ง€๋Š” ์ฃผ๊ธฐ์ ์œผ๋กœ ๋ฆฌ์†Œ์Šค ํ• ๋‹น ๊ทธ๋ž˜ํ”„๋ฅผ ๊ฒ€์‚ฌํ•˜์—ฌ ์ˆœํ™˜ ๋Œ€๊ธฐ ์กฐ๊ฑด์„ ์ฐพ๋Š” ๊ฒƒ์œผ๋กœ ์ด๋ฃจ์–ด์งˆ ์ˆ˜ ์žˆ๋‹ค. ๋ฐ๋“œ๋ฝ์ด ํƒ์ง€๋˜๋ฉด, ์‹œ์Šคํ…œ์€ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ค‘์ง€ํ•˜๊ฑฐ๋‚˜ ๋ฆฌ์†Œ์Šค ํ• ๋‹น์„ ์ฝœ๋ฐฑํ•˜์—ฌ ๋ฐ๋“œ๋ฝ์„ ํ•ด๊ฒฐํ•œ๋‹ค.

 

4. ์ž์›์˜ ์ƒํ˜ธ ๋ฐฐ์ œ ์ œ๊ฑฐ (Ignoring Mutual Exclusion)

์ผ๋ถ€ ๊ฒฝ์šฐ์—, ๋ฆฌ์†Œ์Šค์˜ ์ƒํ˜ธ ๋ฐฐ์ œ ์กฐ๊ฑด์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ฆฌ์†Œ์Šค๊ฐ€ ๋ณต์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ฑฐ๋‚˜ ๊ณต์œ ๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ, ์—ฌ๋Ÿฌ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋™์‹œ์— ํ•ด๋‹น ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์ƒํ˜ธ ๋ฐฐ์ œ ์กฐ๊ฑด์„ ์ œ๊ฑฐํ•จ์œผ๋กœ์จ ๋ฐ๋“œ๋ฝ ๋ฐœ์ƒ ๊ฐ€๋Šฅ์„ฑ์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

 

---> ์–ด์ œ ํ€ด์ฆˆ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๋ดค๋˜ ๋ถ€๋ถ„์ธ๋ฐ ์ œ๋Œ€๋กœ ์•ˆ๋ด์„œ ๋‹ต์„ ์• ๋งคํ•˜๊ฒŒ ์ ์—ˆ๋‹ค.... ๋‹ค์Œ๋ถ€ํ„ฐ ์ œ๋Œ€๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ๋‹ค ๊ผผ๊ผผํžˆ ๋ด์•ผ๊ฒ ๋‹ค

 

 

[3]  Semphore์™€ Mutex์˜ ํŠน์ง•๊ณผ ์ฃผ์š” ์ฐจ์ด์ ์€ ๋ฌด์—‡์ธ๊ฐ€์š”?

 

Semaphore๋Š” ๊ณต์œ  ์ž์›์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ์ œํ•œํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ด๋Š” ํŠน์ • ์ˆซ์ž๋กœ ์ดˆ๊ธฐํ™”๋œ๋‹ค. ์ด ์ˆซ์ž๋Š” ๋™์‹œ์— ํ•ด๋‹น ์ž์›์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์Šค๋ ˆ๋“œ์˜ ์ตœ๋Œ€ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. Semaphore๋Š” ์Šค๋ ˆ๋“œ๊ฐ€ ์ž์›์„ ์‚ฌ์šฉํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ์†Œํ•˜๊ณ , ์ž์›์„ ํ•ด์ œํ•  ๋•Œ๋งˆ๋‹ค ์ฆ๊ฐ€ํ•œ๋‹ค.

 

Mutex(Mutual Exclusion)๋Š” ๊ณต์œ  ์ž์›์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ๋‹จ์ผ ์Šค๋ ˆ๋“œ์—๊ฒŒ๋งŒ ํ—ˆ์šฉํ•œ๋‹ค. ์ด๋Š” ์ฃผ๋กœ ๋ฐ์ดํ„ฐ์˜ ๋ฌด๊ฒฐ์„ฑ์„ ๋ณดํ˜ธํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋ฉฐ, ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๋งŒ์ด ๊ณต์œ  ์ž์›์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค. Mutex๋Š” ์†Œ์œ ๊ถŒ ๊ฐœ๋…์„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด, ์ž ๊ธˆ์„ ๊ฑด ์Šค๋ ˆ๋“œ๋งŒ์ด ์ž ๊ธˆ์„ ํ•ด์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด ๋‘ ๊ฐ€์ง€ ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ ๋ชจ๋‘ ๋™์‹œ์„ฑ์„ ๊ด€๋ฆฌํ•˜๊ณ  ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ์„ ๋ณด์žฅํ•˜๋Š” ๋ฐ ํ•„์ˆ˜์ ์ด์ง€๋งŒ, ์‚ฌ์šฉ๋˜๋Š” ์ƒํ™ฉ๊ณผ ๋ชฉ์ ์— ๋”ฐ๋ผ ์„ ํƒ๋œ๋‹ค. Mutex๋Š” ๋ณด๋‹ค ์—„๊ฒฉํ•œ ์ œ์–ด๊ฐ€ ํ•„์š”ํ•  ๋•Œ, Semaphore๋Š” ์—ฌ๋Ÿฌ ์ž์›์— ๋Œ€ํ•œ ๋™์‹œ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•  ๋•Œ, ํŠนํžˆ Counting Semaphore๋Š” ์ž์›์˜ ์ˆ˜๋Ÿ‰์ด ์ œํ•œ๋˜์–ด ์žˆ์„ ๋•Œ ์œ ์šฉํ•˜๋‹ค.

 

-> ์„ธ๋งˆํฌ์–ด๋Š” ์ž„๊ณ„์˜์—ญ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๊ฐ€ 1๊ฐœ ์ด์ƒ์ด๊ณ , ๋ฎคํ…์Šค๋Š” ์ž„๊ณ„์˜์—ญ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๊ฐ€ 1๊ฐœ์ด๋‹ค.

 

--> ๋‹ต ์ ์Œ. ์ฝ”์น˜๋‹˜์ด ์›ํ•˜๋Š” ๋‹ต๋ณ€์€ ์„ธ๋งˆํฌ์–ด์™€ ๋ฎคํ…์Šคํ‹‘ ๊ณต์œ  ์ž์›์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ์กฐ์œจํ•˜๊ธฐ ์œ„ํ•œ ๋™๊ธฐํ™” ๊ธฐ๋ฒ•์ธ๋ฐ, ์„ธ๋งˆํฌ์–ด๋Š” ์ž„๊ณ„์˜์—ญ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๊ฐ€ 1๊ฐœ ์ด์ƒ์ด๊ณ , ๋ฎคํ…์Šค๋Š” ์ž„๊ณ„์˜์—ญ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜๊ฐ€ 1๊ฐœ์ด๋‹ค. ์ธ๊ฑฐ ๊ฐ™๋‹ค. ๋น„์Šค๋ฌด๋ฆฌํ•˜๊ฒŒ ์ ์Œ

 

 

[4] ๋‹ค์Œ ANSI C ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ถœ๋ ฅ๋˜๋Š” ๋‚ด์šฉ์€ ๋ฌด์—‡์ธ๊ฐ€?

#include <stdio.h>

int f(int x, int *py, int **ppz)
{
    int y, z;
    **ppz += 1;
    z = **ppz;
    *py += 2;
    y = *py;
    x += 3;
    return x + y + z;
 }
 
 int main()
 {
     int c , *b , **a;
     c = 4;
     b = &c;
     a = &b;
     printf("&d\n", f(c , b, a));
     return 0;
 }

 

c = 4 , b = 4, a = 4

f ํ•จ์ˆ˜ ๋‚ด์—์„œ **ppz (์ฆ‰, c) ๋ฅผ 1์ฆ๊ฐ€ ์‹œํ‚ค๋ฉด c์˜ ๊ฐ’์€ 5๊ฐ€ ๋œ๋‹ค. -> z = 5

*py(์ฆ‰, c)๋ฅผ 2 ์ฆ๊ฐ€์‹œํ‚ค๋ฉด , c์˜ ๊ฐ’์€ 7์ด ๋œ๋‹ค. -> y = 7

x (c์˜ ๋ณต์‚ฌ๋ณธ) ์„ 3 ์ฆ๊ฐ€์‹œํ‚ค๋ฉด, x์˜ ๊ฐ’์€ 7์ด ๋œ๋‹ค.  ->  x = 7

 

7 + 7 + 5 = 19 

 

---> ์ฐธ๊ณ ๋กœ ์ด ๋ถ€๋ถ„์—์„œ ์‹ค์ˆ˜๋ฅผ ํ•ด์„œ 7+ 7+ 7 = 21์˜ ๋‹ต์„ ์ œ์ถœ.... ๋‚˜ ์™œ๊ทธ๋žฌ๋ƒ๊ณ ์˜ค์˜ค 

 

 

[5]  ๋‹ค์Œ C์ฝ”๋“œ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋ฅผ ์ฐพ๊ณ , ํ•ด๊ฒฐ ๋ฐฉ์•ˆ์„ ์ œ์‹œํ•˜์‹œ์˜ค.

#include <stdio.h>
#include <stdlib.h>
typedef struct {
    int data;
    char *description;
} item;

item* create_item(int data, const char *desc) {
    item *new_item = (item *)malloc(sizeof(item));
    if (new_item == NULL) {
    	return NULL;
    }
    new_item->data = data;
    new_item->description = (char *)malloc(strlen(desc) + 1);
    strcpy(new_item->description, desc);
    return new_item;	
}

int main() {
    item *myItem = create_item(5, "Test Item");
    printf("Item: %d, Description: %s\n",
    myItem->data, myItem->description);
    // ๋‹ค๋ฅธ ์ž‘์—… ์ˆ˜ํ–‰
    free(myItem); // ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ
    return 0;	
}

 

create_item ํ•จ์ˆ˜์—์„œ item ๊ตฌ์กฐ์ฒด์™€ description ๋ฌธ์ž์—ด์— ๋Œ€ํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ main ํ•จ์ˆ˜์—์„œ๋Š” ์˜ค์ง item ๊ตฌ์กฐ์ฒด์— ๋Œ€ํ•ด์„œ๋งŒ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•˜๊ณ  ์žˆ๊ณ , description ํ•„๋“œ์— ๋Œ€ํ•œ ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ๊ฐ€ ๋ˆ„๋ฝ๋˜์–ด ์žˆ๋‹ค. ์˜ฌ๋ฐ”๋ฅธ ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ๋ฅผ ์œ„ํ•ด main ํ•จ์ˆ˜์—์„œ free(myItem->description); ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.

 

---> ํ•˜ ์ด ๋ถ€๋ถ„๋„ ํ‹€๋ ค๋ฒ„๋ฆผ ์ œ๋Œ€๋กœ ๋งž์€ ๋ฌธ์ œ๊ฐ€ ์—†๋„ค์š”...๋ฐ˜์„ฑํ•ฉ์‹œ๋‹ค...

 

 

๋ฐฑ์ค€ 1012 ์œ ๊ธฐ๋† ๋ฐฐ์ถ”

 

bfs ๋ฌธ์ œ๋‹ค. ์˜ค๋žœ๋งŒ์— bfs ๋ฌธ์ œ ํ’€์–ด์„œ ๊ฐ์„ ์žƒ์–ด๋ฒ„๋ ธ๋‹ค. ์ƒ๊ฐ๋ณด๋‹ค ์–ด๋ ต์ง€ ์•Š์€ ๋ฌธ์ œ์ธ๋ฐ ๊ฐ์„ ์žƒ์–ด๋ฒ„๋ฆฐ ํƒ“์— ๊ตฌํ˜„์—์„œ ์šฐ๋‹นํƒ•ํƒ• ๊ฒฐ๊ตญ gpt ๋„์›€์œผ๋กœ ์ˆ˜์ •ํ•˜๋ฉด์„œ ํ’€์—ˆ๋‹ค. 

๋‚ด์ผ๋ถ€ํ„ฐ bfs ๋‹ค์‹œ ์‹œ์ž‘์ด๋‹ค..

from collections import deque
import sys
input = sys.stdin.readline

dx = [0, 1, 0, -1]
dy = [1, 0,-1, 0]

def bfs(x,y):
    q = deque()
    q.append((x,y))
    baechu[x][y] = 0
    
    while q:
        x_ , y_ = q.popleft()

        for i in range(4):
            nx = x_ + dx[i]
            ny = y_ + dy[i]
            
            if nx < 0 or nx >= M or ny < 0 or ny >= N or baechu[nx][ny] == 0:
                continue
            
            if baechu[nx][ny] ==1:
                q.append((nx,ny))
                baechu[nx][ny] = 0
     
T = int(input())
for _ in range(T):
    M,N,K = map(int,input().split())
    baechu = [[0] * N for _ in range(M)]
    
    for _ in range(K):
        x,y = map(int,input().split())
        baechu[x][y] = 1
    
    cnt = 0
    for i in range(M):
        for j in range(N):
            if baechu[i][j] == 1:
                  bfs(i,j)
                  cnt +=1
    print(cnt)

 

.

.

.

 

์š”์ฆ˜ ๋„ˆ๋ฌด ํ”ผ๊ณคํ•˜๊ณ  ํ™•์‹คํžˆ PintOS ๋“ค์–ด๊ฐ€๋‹ˆ๊นŒ ๋จธ๋ฆฌ๊ฐ€ ๋„˜ ์•„ํŒŒ์„œ ์ปจ๋””์…˜์ด ์ข‹์ง€ ์•Š์€ ๊ฒƒ ๊ฐ™๋‹ค.

์˜ˆ์ „๊ฐ™์ง€ ์•Š์•„... ์ปจ๋””์…˜ ์กฐ์ ˆ์„ ์ž˜ ํ•ด์•ผ๊ฒ ๋‹คใ… ใ… 

 

728x90