all,W jaki sposób pthread jest implementowany w jądrze Linux 3.2?
Poniższy kod pochodzi z "Zaawansowanego programowania w środowisku Unix", tworzy nowy wątek i wypisuje identyfikator procesu oraz identyfikator wątku dla głównych i nowych wątków.
W książce napisano, że w systemie Linux wynik tego kodu wskazuje, że dwa wątki mają różne identyfikatory procesowe, ponieważ pthread używa procesu lekkiego do emulowania wątku. Ale kiedy uruchomiłem ten kod w Ubuntu 12.04, ma jądro 3.2, wydrukowano ten sam pid.
tak, czy nowe jądro Linuxa zmienia wewnętrzną implementację 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;
}
Zobacz także: http://stackoverflow.com/questions/5514464/difference-between-pthread-and-fork-on-gnu-linux –
Wikipedia ma przyzwoity artykuł na temat zmiany Linuksa na NPTL w jądrze 2.6: http://en.wikipedia.org/wiki/Native_POSIX_Thread_Library i krótszy artykuł wyjaśniający niektóre z niedociągnięć starszego modelu wątków, LinuxThreads: http://en.wikipedia.org/wiki/LinuxThreads –