Philosophers functions

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

memset, printf, malloc, free, write, usleep, gettimeofday, pthread_create, pthread_detach, pthread_join, pthread_mutex_init, pthread_mutex_destroy, pthread_mutex_lock, pthread_mutex_unlock

1. thread ํ•จ์ˆ˜

pthread.h : POSIX tread.

pthread_t์™€ opaque type pthread_t ์ž๋ฃŒ๊ตฌ์กฐ opaque type : ํ•ด๋‹น ํƒ€์ž…์— ์ €์žฅ๋˜๋Š” ์‹ค์ œ ๋ฐ์ดํ„ฐ๋Š” ์‹œ์Šคํ…œ์˜ ๊ณ ์œ  ์ •๋ณด์ด๋ฉฐ, ๊ทธ ์•ˆ์˜ ๋ฐ์ดํ„ฐ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ pthread_t๋ฅผ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” API๊ฐ€ ์ œ๊ณตํ•ด์ฃผ๋Š” thread๋ฅผ ๋‹ค๋ฃจ๋Š” ํ•จ์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค. ์˜์ƒ ์ฐธ๊ณ  : ๋ณ„์ค€์ฝ”๋”ฉ, bigpel66.oopy.io

1.1 pthread_create

#include <pthread.h>
int pthread_create(pthread_t *thread,
                    const pthread_attr_t *attr,
                    void *(*start_routine)(void *),
                    void *arg);

์Šค๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜.

  1. ์ธ์ž
    • pthread_t *thread : ์Šค๋ ˆ๋“œ ์‹๋ณ„์ž. ์ƒ์„ฑ๋œ ์Šค๋ ˆ๋“œ์˜ ์ •๋ณด๋ฅผ ๋‹ด๊ณ ์žˆ๋Š” pthread_t ํฌ์ธํ„ฐ. ๋‹ค๋งŒ, ์ด ๊ฐ’์€ ์šด์˜์ฒด์žฌ๋งˆ๋‹ค ๋‹ค๋ฅด๊ฒŒ ์ •์˜ํ•œ๋‹ค.
    • const pthread_attr_t *attr : ์“ฐ๋ ˆ๋“œ ํŠน์„ฑ ์ง€์ •ํ•˜๋Š” ์ธ์ž. ๋””ํดํŠธ ์‚ฌ์šฉ์‹œ NULL ์ „๋‹ฌ.
    • void *(*start_routine)(void *) : ๋ถ„๊ธฐ์‹œ์ผœ์„œ ์‹คํ–‰์‹œํ‚ฌ ์Šค๋ ˆ๋“œ ํ•จ์ˆ˜.
    • void *arg : start_routine์˜ ์ธ์ž.
  2. ๋ฐ˜ํ™˜๊ฐ’
    • ์„ฑ๊ณต์‹œ 0, ์‹คํŒจ์‹œ error number ๋ฐ˜ํ™˜

1.2 pthread_detach

#include <pthread.h>
int pthread_detach(pthread_t thread);

์ž…๋ ฅ๋œ ์Šค๋ ˆ๋“œ๋ฅผ ๋ฐ”๋กœ ์ข…๋ฃŒ์‹œํ‚ค๋Š” ํ•จ์ˆ˜. ์ƒ์„ฑ๋œ ์Šค๋ ˆ๋“œ์˜ ์ž์›์€ ์Šค๋ ˆ๋“œ ์ข…๋ฃŒ์‚ฌ ์ง€๋™์œผ๋กœ ํ•ด์ œ๋˜์ง€ ์•Š๊ณ , pthread_detach๋‚˜ pthread_join์„ ํ†ตํ•ด์„œ ํ•ด์ œ์‹œ์ผœ์•ผํ•œ๋‹ค.

  1. ์ธ์ž
    • pthread_t thread : ์Šค๋ ˆ๋“œ
  2. ๋ฐ˜ํ™˜
    • ์„ฑ๊ณต์‹œ 0, ์‹คํŒจ์‹œ error number ๋ฐ˜ํ™˜

1.3 pthread_join

#include <pthread.h>
int pthread_join(pthread_t thread, void **value_ptr);

์ž…๋ ฅ๋œ ์Šค๋ ˆ๋“œ๊ฐ€ ์ข…๋ฃŒ๋ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋Š” ํ•จ์ˆ˜. ํ•ด๋‹น ์Šค๋ ˆ๋“œ์˜ ๋ฆฌํ„ด๊ฐ’์„ ๋ฐ›์•„์˜ฌ์ˆ˜ ์žˆ๋‹ค.

  1. ์ธ์ž
    • pthread_t thread : ์Šค๋ ˆ๋“œ
    • void **value_ptr : ์Šค๋ ˆ๋“œ์˜ ๋ฆฌํ„ด๊ฐ’(void *)์„ ๋ฐ›์•„์˜ฌ ๋ณ€์ˆ˜.
  2. ๋ฐ˜ํ™˜
    • ์„ฑ๊ณต์‹œ 0, ์‹คํŒจ์‹œ error number ๋ฐ˜ํ™˜

2. ๋ฎคํ…์Šค

๋ฎคํ…์Šค๋Š” ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ๊ฐ€ ๊ณต์œ ํ•˜๋Š” ์ž์›์„ ๋ณดํ˜ธํ•˜๋Š” ์ผ์ข…์˜ ๋ฐ์ดํ„ฐ ์ž ๊ธˆ์žฅ์น˜์ด๋‹ค. ์ด ๋ฎคํ…์Šค๋กœ ๋ณดํ˜ธ๋˜๋Š” ์˜์—ญ์—๋Š” ํ•˜๋‚˜์˜ ์Šค๋ ˆ๋“œ๋งŒ ์ ‘๊ทผํ• ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด ์˜์—ญ์„ ์ž„๊ณ„๊ตฌ์—ญ์ด๋ผ๊ณ ํ•œ๋‹ค.

2.1 pthread_mutex_init

#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *mutex,
                        const pthread_mutexattr_t *attr);

์ž ๊ทธ๊ณ  ์‹ถ์€ ์˜์—ญ ํ•˜๋‚˜๋‹ค ํ•˜๋‚˜์˜ ์ž๋ฌผ์‡ ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ด๋•Œ ์ด ์ž๋ฌผ์‡ ์˜ ์—ญํ• ์„ ํ•˜๋Š”๊ฒŒ ๋ฎคํ…์Šค ๊ฐ์ฒด?๊ตฌ์กฐ์ฒด(pthread_mutex_t)์ด๋ฏ€๋กœ ์ž„๊ณ„๊ตฌ์—ญ๊ฐœ์ˆ˜๋งŒํผ ๋ฎคํ…์Šค ๊ฐ์ฒด ๋˜ํ•œ ์ดˆ๊ธฐํ™”์‹œ์ผœ์ค˜์•ผํ•œ๋‹ค.

  1. ์ธ์ž
    • pthread_mutex_t *mutex : ์ดˆ๊ธฐํ™” ์‹œํ‚ฌ ๋ฎคํ…์Šค ๊ตฌ์กฐ์ฒด
    • const pthread_mutexattr_t *attr : ๋ฎคํ…์Šค์˜ ํŠน์„ฑ์„ ์ง€์ •ํ•˜๋Š” ์ธ์ž. ๋””ํดํŠธ ์‚ฌ์šฉ์‹œ NULL ์ „๋‹ฌ.
  2. ๋ฐ˜ํ™˜
    • ์„ฑ๊ณต์‹œ 0, ์‹คํŒจ์‹œ error number ๋ฐ˜ํ™˜

2.2 pthread_mutex_destroy

#include <pthread.h>
int pthread_mutex_destroy(pthread_mutex_t *mutex);
  1. ์ธ์ž
    • pthread_mutex_t *mutex : ํŒŒ๊ดดํ•  ๋ฎคํ…์Šค ๊ตฌ์กฐ์ฒด(์‚ฌ์šฉ์ด ๋๋‚จ)
  2. ๋ฐ˜ํ™˜
    • ์„ฑ๊ณต์‹œ 0, ์‹คํŒจ์‹œ error number ๋ฐ˜ํ™˜

2.3 pthread_mutex_lock

#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
  1. ์ธ์ž
    • pthread_mutex_t *mutex : ์ž„๊ณ„๊ตฌ์—ญ์„ ์ž ๊ทธ๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•  ๋ฎคํ…์Šค ๊ตฌ์กฐ์ฒด
  2. ๋ฐ˜ํ™˜
    • ์„ฑ๊ณต์‹œ 0, ์‹คํŒจ์‹œ error number ๋ฐ˜ํ™˜

2.4 pthread_mutex_unlock

#include <pthread.h>
int pthread_mutex_unlock(pthread_mutex_t *mutex);
  1. ์ธ์ž
    • pthread_mutex_t *mutex : ํ•ด๋‹น ์ž„๊ณ„๊ตฌ์—ญ์„ ์ž ๊ทธ๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•œ ๋ฎคํ…์Šค ๊ตฌ์กฐ์ฒด
  2. ๋ฐ˜ํ™˜
    • ์„ฑ๊ณต์‹œ 0, ์‹คํŒจ์‹œ error number ๋ฐ˜ํ™˜