Philosophers bonus functions

Philosophers๊ณผ์ œ ํ•จ์ˆ˜

memset, printf, malloc, free, write, fork, kill, exit, pthread_create, pthread_detach, pthread_join, usleep, gettimeofday, waitpid, sem_open, sem_close, sem_post, sem_wait, sem_unlink

1. semaphore ํ•จ์ˆ˜

์„ธ๋งˆํฌ์–ด๋ž€ ํ”„๋กœ์„ธ์Šค/์Šค๋ ˆ๋“œ๊ฐ„ ๊ณต์œ ํ•˜๋Š” ๊ณตํ†ต ์ž์›/์˜์—ญ์„ ์ •ํ•ด์ค€ ์ˆ˜์˜ ํ”„๋กœ์„ธ์Šค/์Šค๋ ˆ๋“œ๋งŒ์ด ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ฒ•. ์„ธ๋งˆํฌ์–ด๋Š” ์‹œ์Šคํ…œ ์ž์ฒด์— ๊ฑธ์ณ์žˆ๋Š” ์‹œ์Šคํ…œ์ƒ์˜ ํŒŒ์ผ๋กœ์„œ ์กด์žฌํ•˜๋ฉฐ, ์‹œ์Šคํ…œ์ด ํ•ด์ œํ•˜์ง€ ์•Š์œผ๋ฉด ์‹œ์Šคํ…œ์ด ๋‚จ์•„์žˆ๋Š”ํ•œ ์‚ฌ๋ผ์ง€์ง€ ์•Š๋Š”๋‹ค.

๋ฎคํ…์Šค์™€์˜ ์ฐจ์ด์ ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ์„ธ๋งˆํฌ์–ด๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค/์Šค๋ ˆ๋“œ๊ฐ„ ๊ณต์œ ํ•˜๋Š” ์ž์›์„ ๋ณดํ˜ธํ•œ๋‹ค. ๋ฎคํ…์Šค๋Š” ํ•œ ํ”„๋กœ์„ธ์Šค ๋‚ด์˜ ์Šค๋ ˆ๋“œ๊ฐ„์— ๊ณต์œ ํ•˜๋Š” ์ž์›/์˜์—ญ์„ ๋ณดํ˜ธํ•œ๋‹ค.
  2. ์„ธ๋งˆํฌ์–ด๋Š” ์ž„๊ณ„๊ตฌ์—ญ/๊ณต์œ ์ž์›์— ์ ‘๊ทผํ•˜๋Š” ํ”„๋กœ์„ธ์Šค/์Šค๋ ˆ๋“œ์˜ ์ˆ˜(1~n)๋ฅผ ์ œํ•œํ•œ์ˆ˜ ์žˆ๋‹ค. ๋ฎคํ…์Šค๋Š” ์ž„๊ณ„๊ตฌ์—ญ/๊ณต์œ ์ž์›์— ๋™์‹œ ์ ‘๊ทผํ• ์ˆ˜ ์žˆ๋Š” ์Šค๋ ˆ๋“œ ์ˆ˜๋Š” ์˜ค์ง ํ•˜๋‚˜์ด๋‹ค.
  3. ์œ„์™€ ๊ฐ™์€ ์ด์œ ๋กœ ์„ธ๋งˆํฌ์–ด๋ฅผ ๋ฎคํ…์Šค์ฒ˜๋Ÿผ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋‹ค(ํฌํ•จ๊ฐœ๋…์€ ์•„๋‹ˆ๋ผ๊ณ  ํ•œ๋‹คโ€ฆ).

1.1 sem_open

#include <semaphore.h>
sem_t *sem_open(const char *name, int oflag, ...);

์ด๋ฆ„์žˆ๋Š” ์„ธ๋งˆํฌ์–ด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ์—ฌ๋Š” ํ•จ์ˆ˜.

  1. ์ธ์ž
    • const char *name : ์„ธ๋งˆํฌ์–ด ๊ฐ์ฒด(? c์ธ๋ฐ?)์— ์ง€์ •ํ•  ์ด๋ฆ„. - name์ด /๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ฒฝ์šฐ(/name) ์„ธ๋งˆํฌ์–ด๋Š” /dev/sem/์— ์ƒ์„ฑ๋œ๋‹ค(/dev/sem/name).
    • name์ด /๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ(name) ์„ธ๋งˆํฌ์–ด๋Š” /dev/sem/์ž‘์—…ํด๋”๋ช…/์— ์ƒ์„ฑ๋œ๋‹ค(/dev/sem/์ž‘์—…ํด๋”๋ช…/name). - int oflag : ์„ธ๋งˆํฌ์–ด ์ƒ์„ฑ์‹œ ์ ์šฉํ•  ํ”Œ๋ž˜๊ทธ. - O_CREATํ”Œ๋ž˜๊ทธ ๋‹จ๋…: ํ•ด๋‹น ์ด๋ฆ„์˜ ์„ธ๋งˆํฌ์–ด๊ฐ€ ์—†์œผ๋ฉด ์ƒ์„ฑํ•œ๋‹ค.
    • O_CREAT | O_EXCLํ”Œ๋ž˜๊ทธ : ํ•ด๋‹น ์ด๋ฆ„์˜ ์„ธ๋งˆํฌ์–ด๊ฐ€ ์žˆ๋Š”๊ฒฝ์šฐ ์—๋Ÿฌ ๋ฐœ์ƒ.
    • O_EXCLํ”Œ๋ž˜๊ทธ ๋‹จ๋…: undefind behavior - ... : O_CREATํ”Œ๋ž˜๊ทธ๋ฅผ ์‚ฌ์šฉํ•œ๊ฒฝ์šฐ mode_t mode, const unsigned int value๋ฅผ ๊ธฐ์ž…ํ•œ๋‹ค(sem_t *sem_open(const char *name, O_CREAT |... , mode_t mode, unsigned int value)).
  2. ๋ฐ˜ํ™˜๊ฐ’
    • ์„ธ๋งˆํฌ์–ด ๋””์Šคํฌ๋ฆฝํ„ฐ(sem_t *) ๋ฐ˜ํ™˜. ์ด ๋””์Šคํฌ๋ฆฝํ„ฐ๊ฐ’์„ ์ด์šฉํ•ด์„œ set_ํ•จ์ˆ˜๋“ค์„ ์ด์šฉํ• ์ˆ˜ ์žˆ๋‹ค. ์‹คํŒจ์‹œ ~๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  errno์„ค์ •.
  3. ์‹ค์ œ ์‚ฌ์šฉ : sem_t *sem_open("name", O_CREAT | O_EXCL, 0644, ์ดˆ๊ธฐ๊ฐ’); 3๋ฒˆ์งธ ์ธ์ž๋Š” mode๊ฐ’์ด๋ฉฐ, ํ•ด๋‹น ์„ธ๋งˆํฌ์–ด์˜ ์ ‘๊ทผ๊ถŒํ•œ์„ ์„ค์ •ํ•œ๋‹ค. 0644์˜ต์…˜์„ ์ฃผ๋ฃŒ ์‚ฌ์šฉ.
    • 6์€ 0b110(0brwx)์œผ๋กœ, ๊ฐ™์€ ์œ ์ €์—๊ฒŒ read & write๊ถŒํ•œ์„ ๋ถ€์—ฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
    • 4์€ 0b100์œผ๋กœ, ๊ทธ๋ฃน์—๊ฒŒ read ๊ถŒํ•œ์„ ๋ถ€์—ฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
    • 4์€ 0b100์œผ๋กœ, ๊ทธ ์™ธ์—๊ฒŒ read ๊ถŒํ•œ์„ ๋ถ€์—ฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
    • ์ฐธ๊ณ  : c์—์„œ 0์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ˆซ์ž๋Š” ํ‘œ๊ธฐ๋ฒ•(notation)์„ ์˜๋ฏธํ•˜๊ณ , 0x์™€ ๊ฐ™๋‹ค.

์ฐธ๊ณ ํ•œ ๊ธ€ : stackoverflow

1.2 sem_close

#include <semaphore.h>
int	sem_close(sem_t *sem);

์ด๋ฆ„์žˆ๋Š” ์„ธ๋งˆํฌ์–ด์— ํ• ๋‹น๋œ ์ž์›๋“ค์„ ํ•ด์ œํ•˜๊ณ , ์„ธ๋งˆํฌ์–ด ๋””์Šคํฌ๋ฆฝํ„ฐ๋ฅผ ์˜๋ฏธ์—†๊ฒŒ ํ•˜๋Š”(์—†์• ๋Š”)ํ•จ์ˆ˜. ์ด๋ฆ„์—†๋Š” ์„ธ๋งˆํฌ์–ด๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ undefine behavior

  1. ์ธ์ž
    • sem_t *sem : ์„ธ๋งˆํฌ์–ด ๋””์Šคํฌ๋ฆฝํ„ฐ
  2. ๋ฐ˜ํ™˜๊ฐ’
    • ์„ฑ๊ณต์‹œ 0, ์‹คํŒจ์‹œ -1 ๋ฐ˜ํ™˜ํ•˜๊ณ  errno์„ค์ •.

1.3 sem_wait

#include <semaphore.h>
int	sem_wait(sem_t *sem);

์„ธ๋งˆํฌ์–ด(sem)์˜ ๊ฐ’(๊ฐ€์šฉ ์ž์›)์ด 0 ์ด์ƒ๋ผ๋ฉด ๊ฐ’์„ 1๋‚ฎ์ถ”๊ณ  ์ž„๊ณ„ ์˜์—ญ ๋‚ด๋กœ ์ง„์ž…์‹œํ‚จ๋‹ค. ๋งŒ์•ฝ 0์ด๋ผ๋ฉด blockํ•˜์—ฌ ๋Œ€๊ธฐํ์—์„œ ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๊ฒŒ ํ•œ๋‹ค. ๊ฐ€์šฉํ•œ ์ž์›์ด 1์ด์ƒ์ด ๋˜๋ฉด ์ธํ„ฐ๋Ÿฝ์Šค ์‹œ๊ทธ๋„์— ์˜ํ•ด ๋Œ€๊ธฐํ์— ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค์ค‘ ํ•˜๋‚˜๊ฐ€ ์ž„๊ณ„๊ตฌ์—ญ ๋‚ด๋กœ ์ง„์ž…ํ•˜๊ฒŒ ๋œ๋‹ค.

  1. ์ธ์ž
    • sem_t *sem : ์„ธ๋งˆํฌ์–ด ๋””์Šคํฌ๋ฆฝํ„ฐ
  2. ๋ฐ˜ํ™˜๊ฐ’
    • ์„ฑ๊ณต์‹œ 0, ์‹คํŒจ์‹œ -1 ๋ฐ˜ํ™˜ํ•˜๊ณ  errno์„ค์ •ํ•˜๋ฉฐ ์„ธ๋งˆํฌ์–ด์˜ ์ƒํƒœ๋Š” ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.

1.4 sem_post

#include <semaphore.h>
int	sem_post(sem_t *sem);

ํŠน์ • ํ”„๋กœ์„ธ์Šค/์Šค๋ ˆ๋“œ์˜ ์ž„๊ณ„์˜์—ญ ์‚ฌ์šฉ์ด ๋๋‚œ ํ›„, ์„ธ๋งˆํฌ์–ด(sem)์˜ ๊ฐ’์„ ์ฆ๊ฐ€์‹œ์ผœ ๋‹ค์Œ ํ”„๋กœ์„ธ์Šค/์Šค๋ ˆ๋“œ๊ฐ€ ๊ฐ€์šฉํ•˜๊ฒŒ ํ•˜๋Š” ํ•จ์ˆ˜.

  1. ์ธ์ž
    • sem_t *sem : ์„ธ๋งˆํฌ์–ด ๋””์Šคํฌ๋ฆฝํ„ฐ
  2. ๋ฐ˜ํ™˜๊ฐ’
    • ์„ฑ๊ณต์‹œ 0, ์‹คํŒจ์‹œ -1 ๋ฐ˜ํ™˜ํ•˜๊ณ  errno์„ค์ •.
#include <semaphore.h>
sem_t *	sem_unlink(const char *name);

์ด๋ฆ„์žˆ๋Š” ์„ธ๋งˆํฌ์–ด์˜ ์ด๋ฆ„์„ ์ œ๊ฑฐํ•˜๋Š” ํ•จ์ˆ˜. sem_closeํ•˜์—ฌ ์ž์›๋“ค ๋ฐ˜ํ™˜ํ›„ sem_unkinkํ•˜์—ฌ ์ด๋ฆ„์„ ์—†์• ์•ผํ•œ๋‹ค(์ˆœ์„œ ์ง€์ผœ์•ผํ•จ).

  1. ์ธ์ž
    • const char *name :
  2. ๋ฐ˜ํ™˜๊ฐ’
    • ์„ฑ๊ณต์‹œ 0, ์‹คํŒจ์‹œ -1 ๋ฐ˜ํ™˜ํ•˜๊ณ  errno์„ค์ •ํ•˜๋ฉฐ ์„ธ๋งˆํฌ์–ด์˜ ์ƒํƒœ๋Š” ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.

sem_unlink๋ฅผ ํ†ตํ•ด ํŠน์ • ์„ธ๋งˆํฌ์–ด๋ฅผ ์‚ญ์ œํ•œ๋‹ค๊ณ  ํ–ˆ์„๋•Œ, ํŠน์ • ํ”„๋ก์„ธ์Šค๊ฐ€ ๊ทธ ์„ธ๋งˆํฌ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ณ , sem_close๊ฐ€ ํ˜ธ์ถœ๋ ๋•Œ๊นŒ์ง€ ์‚ญ์ œ๊ฐ€ ์ง€์—ฐ๋œ๋‹ค(= sem_close๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์‚ญ์ œ๋œ๋‹ค). sem_close๋˜ํ•œ, sem_unlink์—†์ด ํ˜ธ์ถœ๋˜์—ˆ๋‹ค๋ฉด ๋‹จ๋…์œผ๋กœ ์„ธ๋งˆํฌ์–ด๋ฅผ ์ œ๊ฑฐํ•˜์ง„ ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹คโ€ฆ

์ฐธ๊ณ ํ•œ ๊ณณ : HYโ€™s TIL blog


์ „์ฒด์ ์ธ ์ฐธ๊ณ  : Jseo Doodle ๋ธ”๋กœ๊ทธ, yechoi tistory, JongeunKeum-Dev vlog