三,使用glibc-2.3.5
1)、通过调查发现要编译glibc-2.3.5,要求binutils在2.13以上。所以必须安装binutils-2.14
下载binutils-2.14.tar.gz
tar zxpvf binutils-2.14.tar.gz mkdir binutils-build cd binutils-build ../binutils-2.14/configure –prefix=/usr –enable-shared make tooldir=/usr make check make tooldir=/usr install cp ../binutils-2.14/include/libiberty.h /usr/include |
2) 安装glibc-2.3.5
下载 glibc-2.3.5.tar.gz 和 glibc-2.3.5-fix_test-1.patch
tar zxpvf glibc-2.3.5.tar.gz patch –Np1 –i ../glibc-2.3.5-fix_test-1.path mkdir glibc-build cd glibc-build ../glibc-2.3.5/configure –prefix=/usr/local/glibc235 –enable-add-ons=Linuxthreads –enable-kernel=2.6.0 (若安装在/usr目录下,很危险,可能会损坏你的系统) make make check (必须执行的一步) make localedata /install-locales (对应各国文字) mkdir /usr/man/man3(man的安装路径) make –C ../glibc-2.3.5/linuxthreads/man install |
上面的安装完成以后,最好把/usr/local/glibc235/lib和/usr/local/glibc235/include加入到系统的路径中,这样在编译程序时,就不必指定库和header文件的路径了。
四、使用NPTL进行线程编程
#include #include #include #include void thread_fun(void * arg); char message[] = “I am created thread”; int main() { int rnt; pthread_t new_thread; void *thread_result; rnt=pthread_create(&new_thread,NULL, thread_fun, (void*) message); if (rnt != 0) { perrer (“thread creation failed”); exit(EXIT_FAILURE); } printf(“Waiting for other thread to finish… ”); rnt = pthread_join(new_thread, &thread_result); if (rnt != 0) { perrer (“thread join failed”); exit(EXIT_FAILURE); } printf(“Thread join, it returned %s ”, (char*) thread_result); printf(“message now %s ”, message); exit(EXIT_SUCCESS); } void *thread_fun (void * arg) { printf(“the new thread is running. Argument was %s ”,(char*)arg); sleep(3); strcpy(message, “Bye”); pthread_exit(“Thank you for the test”); } |
编译
gcc -D_REENTRANT test_thread.c -o test_thread -lpthread ./test_thread |
成功了。