일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 시스템프로그래밍
- C언어
- 리눅스커널
- 리눅스
- pwncollege
- 리눅스 커널
- Leviathan
- C++
- 드론
- css
- wargame
- 컴퓨터구조
- 시스템
- pwn.college
- 커널
- 취약점
- 워게임
- kernel
- 시스템해킹
- radare2
- Pwnable.kr
- px4
- 시그널
- Bandit
- write up
- 어셈블리어
- 프로그래밍
- 리버싱
- 알고리즘
- 시스템 프로그래밍
Archives
- Today
- Total
Computer Security
#34 쓰레드 개념, 쓰레드 생성/종료/조인/동기화 본문
반응형
Single-thread vs Multi-thread
멀티 쓰레드의 경우 code, data, files 영역을 3개의 쓰레드가 공유하는 특징을 가지고 있다.
프로그램을 실행하면서 실행 주체별로 필요하기 때문에, stack과 registers는 따로 가지고 있다.
쓰레드를 생성하는 API
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
파라미터
- thread : 생성된 thread ID
- attr : 쓰레드 속성(pthread_attr_init()으로 초기화)
- satrt_routine : thread main function
- arg : thread main function 호출 시 사용할 파라미터
반환 값
- 성공 시 0
- 실패 시 errno를 리턴
쓰레드 종료 API
void pthread_exit(void *retval);
파라미터
- retval : exit status를 저장
쓰레드 조인 API
int pthread_join(pthread_t thread, void **retval);
-해당 쓰레드를 종료 처리
파라미터
- thread : 기다릴 thread ID
- retval : 해당 thread의 exit status를 저장
반환 값
- 성공 시 0
- 실패 시 errno를 리턴
쓰레드 떼어내기
int pthread_detach(pthread_t thread);
-해당 쓰레드는 종료시 자동으로 리소스 해제됨(join이 필요 없음)
파라미터
- thread : 떼어낼 thread ID
반환 값
- 성공 시 0
- 실패 시 errno를 리턴
mutex(mutual Excl) :한명이 사용하면 다른한명이 사용하지못함.
mutex 사용 API
int pthread_mutex_init(pthread_mutex_t *restrict mutex,
const pthread_mutexattr_t *restrict attr);
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
파라미터
- mutex : mutex instance
- attr : mutex 속성
반환 값
- 성공 시 0
- 실패 시 errno를 리턴
생성된 mutex를 destroy하는 API
int pthread_mutex_destroy(pthread_mutex_t *mutex);
파라미터
- mutex : mutex instance
반환 값
- 성공 시 0
- 실패 시 errno를 리턴
mutex를 초기화 했다면, 동기화 하는 API
lock하는 API
int pthread_mutex_lock(pthread_mutex_t *mutex);
lock을 시도하는 API(락x일 경우 바로잡음, 락o일 경우 바로 err리턴)
int pthread_mutex_trylock(pthread_mutex_t *mutex);
unlock 하는 API
int pthread_mutex_unlock(pthread_mutex_t *mutex);
lock() ---> unlock()
파라미터
- mutex : mutex instance
반환 값
- 성공 시 0
- 실패 시 errno를 리턴
반응형
'리눅스 시스템 프로그래밍' 카테고리의 다른 글
#36 쓰레드 개념, 쓰레드 생성/종료/조인/동기화 (실습2) (2) | 2022.10.11 |
---|---|
#35 쓰레드 개념, 쓰레드 생성/종료/조인/동기화 (실습1) (0) | 2022.10.10 |
#33 CPU 구조 (Processor affinity 실습) (0) | 2022.10.08 |
#32 CPU 구조 (2) | 2022.10.07 |
#31 프로그램 실행과 종료처리 (실습) (2) | 2022.10.06 |
Comments