[ CODE ] Pthread

38A·2023년 4월 22일
1

Operating System

목록 보기
12/14
post-thumbnail

[ POSIX ] Pthreads

✔️ pthread_create()

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
tid를 변수에 저장하고 return 0, if fail return 에러코드 값

➕ Parameter

thread : tid 저장하는 변수
attr : NULL = default
start_routine : functuin pointer ➡️ function name
= 시작 address를 저장하기 위한 pointer (th는 이 함수부터 시작)
arg : argument ( parameter )

pthread_create(&tid, NULL, runner, argv[1]);

✔️ pthread_attr_init()

int pthread_attr_init(pthread_attr_t *attr);
return 0, if fail return # of error

pthread_attr_init(&attr);

✔️ pthread_join()

int pthread_join(pthread_t th, void **thread_return);
return 0, if fail return # of error

pthread_join(tid, NULL);

✔️ pthread_exit()

: Terminates thread

void pthread_exit(void *retval);

➕ Parameter

retval : 같은 프로세스 내의 다른 스레드가 pthread_join을 통해서 받기 가능

pthread_exit(0);

✔️ pthread_cancel()

: Terminating a thread before it has completed

int pthread_cancel(pthread_t thread);
return 0, if fail return # of error

→ 바로 죽이는 명령 x

✔️ pthread_setcanceltype()

int pthread_setcanceltype(int type, int *oldtype);
return 0, if fail return # of error

➕ Parameter

type : cancellation type

  • PTHREAD_CANCEL_ASYNCHRONOUS : 즉시 종료
  • PTHREAD_CANCEL_DEFERRED : pthread_testcancel()에서 종료
    ➡️ 안전한 곳에만 둬야함, Safer than asynchronous cancellation

oldtype : 이전 type을 oldtype가 가리키는 버퍼로 반환

pthread_t pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);

✔️ pthread_setcancelstate()

int pthread_setcancelstate(int state, int *oldstate);
return 0, if fail return # of error

➕ Parameter

state : cancellation state

  • PTHREAD_CANCEL_ENABLE
  • PTHREAD_CANCEL_DISABLE : cancellation remains pending until thread enables it
pthread_t pthread_setcanceltype(PTHREAD_CANCEL_ENABLE, &oldstate);

✔️ Thread-local storage(TLS)

__thread int tls

: Each thread has its own ‘int tls’ variable

[ POSIX ] Pthread Scheduling

  • PCS(PTHREAD_SCOPE_PROCESS):
    • In many-to-one, only PCS is possible
  • SCS(PTHREAD_SCOPE_SYSTEM):
    • In one-to-one model, only SCS is possible

✔️ pthread_attr_setscope()

pthread_attr_setscope(pthread_attr_t *attr, int scope);

pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_create(&tid, &attr, runner, NULL);

✔️ pthread_attr_getscope()

pthread_attr_getscope(pthread_attr_t *attr, int *scope);

int scope = 0;
pthread_attr_getscope(&attr, &scope);

[ POSIX ] Real-Time Sechduling

  • SCHED_FIFO : FCFS policy
  • SCHED_RR : Round-Robin policy
  • SCHED_OthER : system-specific (user define)

✔️ pthread_attr_setschedpolicy()

pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
return 0, if fail return # of error

if(pthread_attr_setschedpolicy(&attr, SCHED_FIFO) != 0)
	fprinf(stderr, "Unable to set policy\n");

for(i=0; i<NUM_THREAD; i++) 
  	pthread_create(&tid[i], &attr, runner, NULL);

✔️ pthread_attr_getschedpolicy()

pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy);
return 0, if fail return # of error

int policy = 0;
if(pthread_attr_getschedpolicy(&attr, &policy) != 0)
  	fprintf(stderr, "Unable to get policy\n");
profile
HGU - 개인 공부 기록용 블로그

0개의 댓글