Thread
- Threads use and exist within these process resources, yet are able to be scheduled by the O/S and run as independent entities within a process
- Process안에 속하는 더 작은 수행 단위
- 독립적인 control flow, stack, PC 를 가짐
- Text, data, heap은 다른 thread와 공유함.
- Lightweight Process(LWP)
- It may be managed by the O/S or by a user application
-Basic Pthreads API
= Pthread_create : thread 생성
= Pthread_exit : thread 종료
= Pthread_join : thread수행이 종료되기를 기다림
= Pthread_kill : thread에게 신호를 보냄
= Pthread_detach : thread가 자원을 해제하도록 설정
= Pthread_equal : 두 thread ID가 동일한지 검사
= Pthread_self : 자신의 thread ID를 얻음
= Pthread_cancel : 다른 thread의 수행을 취소
<간단한 쓰레드 프로그램>
#include <pthread.h> //스레드 프로그래밍을 위한 헤더파일
#define NUM_THREADS 5 //스레드의 갯수 : 5개
void *PrintHello(void *threadid) //생성된 스레드가 수행할 함수
{
printf("\n%d: Hello World!\n", threadid);
pthread_exit(NULL); //Hello World와 Thread id를 출력시킨뒤 종료
}
int main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS]; //5개의 쓰레드 생성을 위한 선언
int rc, t;
for(t=0;t<NUM_THREADS;t++){
printf("Creating thread %d\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void*)t);
//스레드 생성
if(rc){
printf("ERROR : return code from pthread_create() is %d\n",rc);
exit(-1);
}
}
pthread_exit(NULL);
}
<결과화면>
'Backend > OS★' 카테고리의 다른 글
쓰레드 동기화 (Synchronization) (0) | 2011.05.26 |
---|---|
Process 생성 (0) | 2011.05.26 |
프로세스 (Process) (0) | 2011.05.26 |
인터럽트 (0) | 2011.05.26 |
주 상태(Major State) (0) | 2011.05.26 |