`

Linux2.2 修改LinuxThreads线程限制

阅读更多

This document describes how to modify the per-process file descriptor limit and task limit of the Linux version 2.2 kernel to increase the number of sockets and threads that a Java server application can create. In particular, it describes the socket and thread requirements of the VOLANO® chat server, but similar requirements are likely to exist in any server application written for the Java platform. Note that the current Linux kernel version is 2.6, so this information is unlikely to apply to any recent Linux distribution.

File descriptors and tasks

When running on the Linux 2.2 kernel, each process is limited by default to 1,024 file descriptors. Since the VolanoChat server requires one file descriptor for each person chatting through a VolanoChat applet, you are limited by default to a maximum of 1,024 simultaneous users when using the Linux 2.2 kernel.

Some Java virtual machines, such as the IBM Developer Kit for Linux, Java Technology Edition, use native threads on Linux which require one task for each Java thread. Since VolanoChat requires two Java threads for each connection, you may need to increase the maximum number of tasks per user for such virtual machines. The default maximum number of tasks in a Linux 2.2 system is 512, with a maximum of 256 tasks per user. So by default, a VolanoChat server running under IBM JDK 1.1.6 for Linux can support a maximum of 128 people chatting at the same time.

You can increase the limits testing the the number of file descriptors and the number of tasks in the Linux 2.2 kernel by following the steps below. These steps were performed on a Red Hat Linux 6.0 system using Linux kernel 2.2.5-15. The instructions may be different for your Linux system.

To increase the maximum file descriptors per process to 4,096 (from 1,024) and the maximum tasks per user to 4,090 (from 256) on Linux 2.2, do the following steps.

  1. Change the following C header files.

    In /usr/include/linux/tasks.h, change:

    NR_TASKS            512          to 4090
    MAX_TASKS_PER_USER  (NR_TASKS/2) to NR_TASKS
    

    In /usr/include/linux/limits.h, change:

    NR_OPEN             1024 to 4096
    OPEN_MAX             256 to 4096
    

    In /usr/include/linux/posix_types.h, change:

    __FD_SETSIZE        1024 to 4096
    

    In /usr/include/bits/types.h, change:

    __FD_SETSIZE        1024 to 4096
    
  2. To allow users to increase their file descriptor limits, change the following configuration files:

    In /etc/security/limits.conf, add the lines:

    *       soft    nofile  1024
    *       hard    nofile  4096
    

    In /etc/pam.d/login, add:

    session required /lib/security/pam_limits.so
    
  3. To increase the system-wide file descriptor limit, add the following three lines to the /etc/rc.d/rc.local startup script:
    # Increase system-wide file descriptor limit.
    echo  8192 > /proc/sys/fs/file-max
    echo 24576 > /proc/sys/fs/inode-max
    
  4. Change to the directory /usr/src/linux and set up the kernel sources with:

    make mrproper
    
  5. Make any other kernel configuration changes, such as setting the processor family type, by entering the command below. Save your changes and exit the configuration panel when you are done.

    make xconfig
    
  6. Prepare for the kernel build by entering:

    make dep
    make clean
    
  7. Rebuild your kernel with the commands:

    make bzImage
    make modules
    
  8. Backup your old modules directory by entering (for example):

    mv /lib/modules/2.2.5-15 /lib/modules/2.2.5-15.old
    
  9. Create a new set of kernel modules with the command:

    make modules_install
    
  10. Backup your old kernel by entering (for example):

    mv /boot/vmlinuz /boot/vmlinuz.old
    
  11. Copy over your new kernel image with:

    cp /usr/src/linux/arch/i386/boot/bzImage /boot/vmlinuz
    
  12. Edit the Linux loader configuration in /etc/lilo.conf so that your new kernel is the default and your old kernel is still available from the boot prompt, as in the example shown below:

    boot=/dev/hda
    map=/boot/map
    install=/boot/boot.b
    prompt
    timeout=50
    image=/boot/vmlinuz
            label=linux
            root=/dev/hda1
            read-only
    image=/boot/vmlinuz.old
            label=linux.old
            root=/dev/hda1
            read-only
    
  13. Generate the new Linux loader configuration by running:

    /sbin/lilo
    
  14. Shutdown and reboot with your new kernel by entering:

    shutdown -r now
    
  15. After rebooting, verify that the changes were effective as shown in the sections below.

LinuxThreads 0.7 limits

Even after you rebuild your Linux 2.2 kernel to increase the maximum number of threads per Linux user from 256 to 4,090, there are still definitions in the thread library itself which limit the number of threads per process to less than 1,000. The VolanoChat server requires two threads for each person chatting through a VolanoChat applet, so the LinuxThreads implementation creates a limit of 500 people to a single VolanoChat server.

You can increase this limit to 8,192 threads with the following steps. Even though the thread library will allow 8,192 threads per process after these changes, you'll still be limited by the Linux 2.2 kernel to a maximum of 4,090 threads for the Linux user account running the VolanoChat server after the changes in the previous section. The steps below are shown for a Red Hat Linux 6.0 system, so they may be different for your Linux distribution.

  1. Install the Glibc 2.1.1 source package from /mnt/cdrom/SRPMS/glibc-2.1.1-6.src.rpm on the Red Hat Linux CD-ROM #2.

  2. Unpack the source archive /usr/src/redhat/SOURCES/glibc-990416.tar.gz so that you can modify two of the files.

  3. Change the following C header files.

    In /usr/src/redhat/SOURCES/glibc/linuxthreads/internals.h, change the size of the thread stack reserve from 2 megabytes down to 256 kilobytes (with a page size of 4,096 bytes):

    STACK_SIZE  (2 * 1024 * 1024) -> (64 * PAGE_SIZE)
    

    In sysdeps/unix/sysv/linux/bits/local_lim.h under /usr/src/redhat/SOURCES/glibc/linuxthreads, change the Posix thread implementation limit from 1,024 per process to 8,192 per process:

    PTHREAD_THREADS_MAX  1024 -> 8192
    
  4. Rebuild the source archive, /usr/src/redhat/SOURCES/glibc-990416.tar.gz, to include your changes, replacing the original copy.

  5. Change to the RPM specification directory, /usr/src/redhat/SPECS, and enter the command:

    rpm -ba glibc-2.1.spec
    
  6. After a few hours, the new LinuxThreads library will be found as libpthread.so under:

    /usr/src/redhat/BUILD/glibc/build-i386-linux/linuxthread
    
  7. Copy the new thread library into your own lib subdirectory:

    cd /usr/src/redhat/BUILD/glibc/build-i386-linux
    cp linuxthread/libpthread.so ~/lib
    cd ~/lib
    ln -s libpthread.so libpthread.so.0
    
  8. Define the environment variable LD_LIBRARY_PATH to include the subdirectory containing the new thread library libpthread.so.

  9. Verify the new thread limits as instructed below.

Verifying the new file descriptor limit

Increase your new limits to their maximum by entering "ulimit -n 4096" for the Bash shell or "unlimit" for the C shell. You can then check whether your changes were effective by running the following C program:

#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>

void main(void) {
    int k = 3;
    while (dup(0) != -1)
        k++;
    printf("Opens = %d Fdsize = %d\n", k, sizeof(fd_set) * 8);
}

Both numbers printed should give your new file descriptor limit.

Verifying the new thread limit

You can use the following program to verify your new thread limits in the modified Linux 2.2 kernel and in the modified LinuxThreads library.

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

#define MAX_THREADS 10000
int i;

void run(void) {
    char c;
    if (i < 10)
        printf("Address of c = %u KB\n", (unsigned int) &c / 1024);
    sleep(60 * 60);
}

int main(int argc, char *argv[]) {
    int rc = 0;
    pthread_t thread[MAX_THREADS];
    printf("Creating threads ...\n");
    for (i = 0; i < MAX_THREADS && rc == 0; i++) {
        rc = pthread_create(&(thread[i]), NULL, (void *) &run, NULL);
        if (rc == 0) {
            pthread_detach(thread[i]);
            if ((i + 1) % 1000 == 0)
                printf("%i threads so far ...\n", i + 1);
        }
        else
            printf("Failed with return code %i creating thread %i.\n",
                rc, i + 1);
    }
    exit(0);
}

Compile the program with:

gcc -lpthread -o count count.c

Running with the original LinuxThreads library will print:

blue:/home/john> count
Creating threads ...
Address of c = 3137535 KB
Address of c = 3135487 KB
Address of c = 3133439 KB
Address of c = 3131391 KB
Address of c = 3129343 KB
Address of c = 3127295 KB
Address of c = 3125247 KB
Address of c = 3123199 KB
Address of c = 3121151 KB
1000 threads so far ...
Failed with return code 11 creating thread 1023.

while running with the modified LinuxThreads library will print:

blue:/home/john> count
Creating threads ...
Address of c = 3144703 KB
Address of c = 3144447 KB
Address of c = 3144191 KB
Address of c = 3143935 KB
Address of c = 3143679 KB
Address of c = 3143423 KB
Address of c = 3143167 KB
Address of c = 3142911 KB
Address of c = 3142655 KB
1000 threads so far ...
2000 threads so far ...
3000 threads so far ...
4000 threads so far ...

You may need to enter Ctrl-C to exit the program.

Notice that the thread stack addresses are spaced apart by 256 kilobytes instead of the earlier reserve of 2 megabytes per stack. Also notice that the program can create threads right up to the Linux 2.2 kernel limit of 4,090 for the system rather than the earlier LinuxThreads limit of 1,024 per process.

 

http://www.volano.org/articles/linux-2.2-sockets-threads/

分享到:
评论

相关推荐

    Linux下的多线程编程.pdf

    Linux系统提供了 LinuxThreads 库,它是一个符合 POSIX IO03.1c 标准的内核级多线程函数库。在 LinuxThreads 库中提供了一些多线程编程的关键函数,如 pthread_create() 函数,它可以创建一个新的线程,并执行指定...

    Linux下的线程

    Linux线程的实现分为用户级实现和核心级实现两种模式: 1. **用户级实现**:在这种模式下,线程的管理和调度完全由用户空间的线程库负责,内核并不感知线程的存在。这意味着,当一个线程执行阻塞性系统调用时,整个...

    Linux_C语言线程实现机制分析.pdf

    LinuxThreads 线程库是 Linux 平台上最流行的线程库,它采用线程-进程"一对一"模型,将线程调度交给核心完成。混合线程模型是指核心级线程和用户级线程的结合,既提供核心线程以满足 SMP 系统的需要,也支持用线程...

    《嵌入式Linux应用程序开发详解》之多线程编程

    Linux2.4 内核消除了这个线程个数的限制,并且允许在系统运行中动态地调整进程数上限。 Linux2.6 内核中,进程调度通过重新编写,删除了以前版本中效率不高的算法。内核线程框架也被重新编写,开始使用 NPTL...

    glibc-linuxthreads-2.2.tar.gz

    标题 "glibc-linuxthreads-2.2.tar.gz" 指的是一个GNU C库(Glibc)的扩展,专门针对Linux系统实现线程支持的源代码包...尽管如此,`linuxthreads`对于理解早期Linux线程实现的历史和多线程编程的基本概念仍然有价值。

    Linux 线程实现分析

    ### Linux线程实现分析 #### 一、基础知识:线程和进程 - **进程与线程的概念**:根据传统定义,进程是系统进行资源分配和调度的基本单位,而线程则是程序执行的基本单位。进程拥有独立的地址空间和其他资源,线程...

    Linux 线程实现机制分析

    Linux线程实现机制分析主要关注的是Linux平台上的线程模型,特别是LinuxThreads的运作方式。在多线程编程中,Linux面临着兼容性和效率的问题。线程作为程序执行的最小单位,是为了更好地支持对称多处理器(SMP)系统和...

    \嵌入式linux开发教程之进程与线程--千锋培训

    1. **线程描述数据结构及实现限制**:LinuxThreads是早期在Linux系统中实现的线程库,每个线程都有一个与之对应的轻量进程。线程描述符保存了线程的状态、栈信息、调度信息等,但其局限性在于不支持线程组,不完全...

    Linux下C开发之线程通信

    Linux下C开发之线程通信 在Linux平台下,线程通信是C开发中一个非常重要的方面。...Linux平台下提供了多种线程机制,包括LinuxThreads和POSIX线程标准等,可以根据实际情况选择合适的线程机制来实现多线程编程。

    linux系统线程的结构分析

    总的来说,Linux线程机制是通过轻量级进程和用户级线程库来实现的,虽然没有直接的核心级线程支持,但其优化的进程调度可以补偿这一不足。随着多核处理器的普及,Linux也在不断发展和完善其线程模型,以适应更加复杂...

    linux线程的基本概念

    【Linux线程的基本概念】 线程是操作系统中的一个基本概念,它是程序中的单个顺序控制流,也是进程内的执行单元。在多线程环境中,一个进程可以包含多个并发执行的线程,每个线程都有其独立的执行路径,共享进程的...

    linux线程技术

    【Linux线程技术】在操作系统领域,Linux线程是指在Linux环境下实现的多线程编程技术。虽然在早期的Linux内核中并没有直接支持线程的概念,而是通过轻量级进程(Lightweight Process, LWP)来模拟线程的行为。在...

    glibc-linuxthreads-2.3.3.tar.gz

    《glibc-linuxthreads-2.3.3:深入解析Linux线程库的里程碑版本》 在Linux操作系统中,glibc(GNU C Library)是核心的系统库,它为应用程序提供了与操作系统交互的接口。glibc的Linuxthreads是早期实现线程支持的...

    Linux 线程实现机制分析POSIX线程编程

    Linux 内核只提供了轻量进程的支持,限制了更高效的线程模型的实现,但 Linux 着重优化了进程的调度开销,一定程度上也弥补了这一缺陷。最初的进程定义都包含程序、资源及其执行三部分,其中程序通常指代码,资源在...

    Linux下的多线程机制的分析与实现.pdf

    Linux操作系统通过`LinuxThreads`库实现了内核级线程,遵循POSIX标准。在编程时,开发者需要包含`pthread.h`头文件,使用`pthread`库提供的函数来创建、管理和控制线程。 **关键库函数详解:** 1. **线程创建和...

    Linux下的多线程编程

    Linux操作系统提供了符合POSIX 1003.1c标准的LinuxThreads库,该库支持内核级多线程编程。在进行多线程编程时,通常需要包含`pthread.h`头文件,并链接`libpthread.a`库。 - **线程的创建和终止** - `pthread_...

    Linux线程库的实现机制.pdf

    【Linux线程库的实现机制】深入探讨 Linux操作系统是一个开放源码的Unix-like系统,遵循POSIX(Portable Operating System Interface)标准。在Linux中,线程的实现主要有两种方式:用户级线程和内核级线程。本文将...

    Linux开发之线程通信

    虽然Linux内核最初并未直接实现线程模型,而是通过轻量级进程(LWP,Lightweight Process)来模拟线程的行为,但随着技术的发展,Linux引入了线程机制,例如LinuxThreads,以提供更高效、灵活的多线程支持。...

    嵌入式Linux应用程序开发详解-第9章(多线程编程)

    Linux线程技术的发展经历了以下几个阶段: - **Linux 2.2 内核**: 在这个版本之前,Linux并没有真正意义上的线程支持。所谓的线程实际上是通过进程模拟的,因此线程数量受到限制。 - **Linux 2.4 内核**: 这个版本...

Global site tag (gtag.js) - Google Analytics