标签:pthread

以下是与标签 “pthread” 相关联的文章

ubuntu16.04 修改 pthread 库的 PTHREAD_KEYS_MAX 宏定义

编译说明

  • pthread 是 glibc 的子库,需要编译整个 glibc。
  • 需要安装编译套件 gcc, make 等工具。

编译 glibc

  • 查看 glibc 当前版本
$ /lib/x86_64-linux-gnu/libc.so.6
GNU C Library (Ubuntu GLIBC 2.23-0ubuntu9) stable release version 2.23, by Roland McGrath et al.
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 5.4.0 20160609.
Available extensions:
        crypt add-on version 2.1 by Michael Glad and others
        GNU Libidn by Simon Josefsson
        Native POSIX Threads Library by Ulrich Drepper et al
        BIND-8.2.3-T5B
libc ABIs: UNIQUE IFUNC
For bug reporting instructions, please see:
<https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs>.
  • 下载 glibc-2.23
apt install axel
axel -n 10 http://ftp.gnu.org/gnu/glibc/glibc-2.23.tar.bz2
tar xjf glibc-2.23.tar.bz2
  • 修改 PTHREAD_KEYS_MAX 宏大小
    • 不能改太大,改成 10240 创建线程时会崩溃
    • 仅修改相关 PC 体系下的头文件,如下是修改完的
$ cd /home/share/glibc-2.23
$ ag "define PTHREAD_KEYS_MAX"
glibc-2.23/sysdeps/unix/sysv/linux/aarch64/bits/local_lim.h
65:#define PTHREAD_KEYS_MAX     2048

glibc-2.23/sysdeps/unix/sysv/linux/bits/local_lim.h
64:#define PTHREAD_KEYS_MAX     2048

glibc-2.23/sysdeps/unix/sysv/linux/alpha/bits/local_lim.h
64:#define PTHREAD_KEYS_MAX     1024

glibc-2.23/sysdeps/unix/sysv/linux/powerpc/bits/local_lim.h
64:#define PTHREAD_KEYS_MAX     1024

glibc-2.23/sysdeps/unix/sysv/linux/mips/bits/local_lim.h
64:#define PTHREAD_KEYS_MAX     1024

glibc-2.23/sysdeps/unix/sysv/linux/ia64/bits/local_lim.h
64:#define PTHREAD_KEYS_MAX     1024

glibc-2.23/sysdeps/unix/sysv/linux/tile/bits/local_lim.h
64:#define PTHREAD_KEYS_MAX     1024

glibc-2.23/sysdeps/unix/sysv/linux/sparc/bits/local_lim.h
64:#define PTHREAD_KEYS_MAX     1024

glibc-2.23/sysdeps/nacl/bits/local_lim.h
29:#define PTHREAD_KEYS_MAX        2048
  • 编译 glibc-2.23
apt install gawk
mkdir build
cd build
../configure CC=gcc --disable-sanity-checks

安装前的准备

  • 查看 pthread 所在目录
$ locate pthread.so
/lib/x86_64-linux-gnu/libpthread.so.0
/usr/lib/x86_64-linux-gnu/libpthread.so
/usr/lib/x86_64-linux-gnu/samba/vfs/aio_pthread.so
  • 查看运行库
$ cd /lib/x86_64-linux-gnu/
$ l *pthread*
-rwxr-xr-x 1 root root 136K Jun 17 04:57 libpthread-2.23.so
lrwxrwxrwx 1 root root   18 Jun 17 04:57 libpthread.so.0 -> libpthread-2.23.so
  • 查看链接库
$ cd /usr/lib/x86_64-linux-gnu
$ l *pthread*
-rw-r--r-- 1 root root 2.4M Jun 17 04:52 libpthread.a
-rw-r--r-- 1 root root 7.6K Jun 17 04:52 libpthread_nonshared.a
-rw-r--r-- 1 root root  252 Jun 17 04:52 libpthread.so
  • 查看头文件位置
$ locate local_lim.h
/usr/include/x86_64-linux-gnu/bits/local_lim.h

替换链接库

  • 备份链接库
cd /usr/lib/x86_64-linux-gnu
mv libpthread.so libpthread.so.bak
mv libpthread.a libpthread.a.bak
mv libpthread_nonshared.a libpthread_nonshared.a.bak
  • 替换链接库 编译完成后记得还原
cd /usr/lib/x86_64-linux-gnu
cp /home/share/glibc-2.23/glibc-2.23/build/nptl/libpthread.so .
cp /home/share/glibc-2.23/glibc-2.23/build/nptl/libpthread.a .
cp /home/share/glibc-2.23/glibc-2.23/build/nptl/libpthread_nonshared.a .

替换运行库

  • 备份运行库

    • 部分 shell 命令依赖 libpthread.so.0,如 cp,mv。
    cd /lib/x86_64-linux-gnu/
    cp libpthread.so.0 libpthread.so.0.bak
    
    • ln 仍然可用,可用于紧急情况下恢复。
    ln -s -f libpthread-2.23.so libpthread.so.0 
    
  • 替换运行库

    • 放到执行目录下
    export LD_LIBRARY_PATH=./
    cd /home/share/glibc-2.23/glibc-2.23/build/nptl
    cp *.so* /var/data/testprogram
    
    • 放到系统目录下 危险,尽量不要使用这种方法
    cd /lib/x86_64-linux-gnu/
    cp /home/share/glibc-2.23/glibc-2.23/build/nptl/libpthread.so libpthread.spider.so
    ln -s -f libpthread.spider.so libpthread.so.0
    
  • 修改头文件 编译完成后记得还原
vim /usr/include/x86_64-linux-gnu/bits/local_lim.h
#define PTHREAD_KEYS_MAX 10240