모두리눅스 커널 3.2에서 pthread는 어떻게 구현 되었습니까?
아래 코드는 "Unix 환경의 고급 프로그래밍"에서 가져온 것으로 새로운 스레드를 만들고 주 스레드와 새 스레드의 프로세스 ID와 스레드 ID를 인쇄합니다.
리눅스에서이 코드의 출력은 두 스레드가 프로세스 ID가 다르다는 것을 나타냅니다. pthread는 스레드를 에뮬레이트하기 위해 경량 프로세스를 사용하기 때문입니다. 하지만 우분투 12.04에서이 코드를 실행하면 커널 3.2가 나오고 같은 PID를 출력합니다.
그래서 새로운 리눅스 커널이 pthread의 내부 구현을 변경합니까? 리눅스 pthread
에
#include "apue.h"
#include <pthread.h>
pthread_t ntid;
void printids(const char *s) {
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n",
s, (unsigned int)pid, (unsigned int)tid, (unsigned int)tid);
}
void *thread_fn(void* arg) {
printids("new thread: ");
return (void *)0;
}
int main(void) {
int err;
err = pthread_create(&ntid, NULL, thread_fn, NULL);
if (err != 0)
err_quit("can't create thread: %s\n", strerror(err));
printids("main thread: ");
sleep(1);
return 0;
}
또한 참조하십시오 : http://stackoverflow.com/questions/5514464/difference-between-pthread-and-fork-on-gnu-linux –
위키 피 디아는 커널 2.6에서 NPTL을 만든 스위치에 대한 기사를 가지고 있습니다. http://en.wikipedia.org/wiki/Native_POSIX_Thread_Library 및 이전 쓰레딩 모델 인 LinuxThreads의 단점을 설명하는 짧은 기사 : http://en.wikipedia.org/wiki/LinuxThreads –