- 浏览: 608528 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
kangh:
转载的也拿出来 都不试一下 完全错误
Nginx+ffmpeg的HLS开源服务器搭建配置及开发详解 -
wangtxlz:
#cd builders/cmake#cmake .系统提示命 ...
crtmpserver流媒体服务器的介绍与搭建 -
hnraysir:
支持支持支持
手机Android音视频采集与直播推送,实现单兵、移动监控类应用 -
wuent:
把web服务器和php框架绑定到一起?真不建议这样。。。
Swoole(PHP高级Web开发框架) -
wuent:
有更详细的性能比较吗?php,python,java
PHP中的(伪)多线程与多进程
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.
-
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
-
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
- 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
-
Change to the directory
/usr/src/linux
and set up the kernel sources with:make mrproper
-
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
-
Prepare for the kernel build by entering:
make dep make clean
-
Rebuild your kernel with the commands:
make bzImage make modules
-
Backup your old modules directory by entering (for example):
mv /lib/modules/2.2.5-15 /lib/modules/2.2.5-15.old
-
Create a new set of kernel modules with the command:
make modules_install
-
Backup your old kernel by entering (for example):
mv /boot/vmlinuz /boot/vmlinuz.old
-
Copy over your new kernel image with:
cp /usr/src/linux/arch/i386/boot/bzImage /boot/vmlinuz
-
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
-
Generate the new Linux loader configuration by running:
/sbin/lilo
-
Shutdown and reboot with your new kernel by entering:
shutdown -r now
-
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.
-
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. -
Unpack the source archive
/usr/src/redhat/SOURCES/glibc-990416.tar.gz
so that you can modify two of the files. -
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
-
Rebuild the source archive,
/usr/src/redhat/SOURCES/glibc-990416.tar.gz
, to include your changes, replacing the original copy. -
Change to the RPM specification directory,
/usr/src/redhat/SPECS
, and enter the command:rpm -ba glibc-2.1.spec
-
After a few hours, the new LinuxThreads library will be found as
libpthread.so
under:/usr/src/redhat/BUILD/glibc/build-i386-linux/linuxthread
-
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
-
Define the environment variable
LD_LIBRARY_PATH
to include the subdirectory containing the new thread librarylibpthread.so
. -
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/
发表评论
-
select、poll、epoll之间的区别总结
2015-04-02 11:49 0select,poll,epoll都是IO多路复用的机制。I ... -
TCP状态转移图学习总结 (转)
2015-04-02 11:37 0TCP状态转移图学习总结 (转) 这是网络编程的基础,t ... -
(原)CentOS Linux 下安装Samba
2015-01-21 10:18 0一、Samba简介 samba ... -
Java 通过JNI调用C或者CPP代码
2014-10-22 18:20 1225网上搜罗到一个JNI的例子,不错。用cygwin的环境搞了下 ... -
CentOS配置mencoder和ffmpeg flv视频转码总结
2013-11-15 22:04 11981、安装yum的fpmforge源 rpm - ... -
用ffmpeg打造linux转码服务器
2013-11-15 21:54 1384linux在系统资源利用上确实要比windows强很多,所以 ... -
Linux下文件的三种时间标记:访问时间、修改时间、状态改动时间
2012-11-16 16:01 1370在windows下,一个文件有:创建时间、修改时间、访问 ... -
常用awk命令(转)
2012-11-16 13:54 982awk 用法:awk ' pattern {action ... -
php写后端运行程序总结
2012-03-21 23:44 20401、定时执行 就crontab了,li ... -
PHP调用Linux系统的常用函数
2012-03-14 00:10 0PHP调用Linux系统的常用函数 1、exec函数 ... -
删除Linux环境中的默认JDK安装版本
2012-02-29 19:23 1861下面以Fedora8为例: [root@fc8 bin ... -
linux tr命令详解
2012-02-28 22:06 1269通过使用 tr,您可以非常容易地实现 sed 的许多最 ... -
cannot restore segment prot after reloc错误的解决方法
2012-02-21 01:33 1648最近用到gearman。由于C语言的server一直编译不过去 ... -
Linux(CentOS)下更改/转移MySQL数据库目录
2012-02-19 00:16 1075由于MySQL的 ... -
centos安装中文支持
2012-02-15 16:39 3556默认安装为英文的 现在想设置成中文yumgroupinstal ... -
安装及配置eaccelerator-0.9.5加速PHP-5.2.1
2012-02-11 09:37 1204操作系统:Linux ... -
Linux EXT3文件系统下成功恢复误删的mysl数据库
2012-02-09 12:18 1374环境:CentOS 5.3 x86_64下,/dev/sd ... -
Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器[转载]
2012-02-09 12:18 1312[文章作者:张宴 本文版本:v6.3 最后修改:2010. ... -
Centos 开起自动启动设置
2012-02-06 09:08 1453CentOS下添加程序到服务并且设置服务自动启动的两种方法 ... -
linux增加根目录的磁盘空间(LVM)
2012-02-06 09:08 6237第一步:添加磁盘 第二步:使用Linux下的fdis ...
相关推荐
Linux系统提供了 LinuxThreads 库,它是一个符合 POSIX IO03.1c 标准的内核级多线程函数库。在 LinuxThreads 库中提供了一些多线程编程的关键函数,如 pthread_create() 函数,它可以创建一个新的线程,并执行指定...
Linux线程的实现分为用户级实现和核心级实现两种模式: 1. **用户级实现**:在这种模式下,线程的管理和调度完全由用户空间的线程库负责,内核并不感知线程的存在。这意味着,当一个线程执行阻塞性系统调用时,整个...
LinuxThreads 线程库是 Linux 平台上最流行的线程库,它采用线程-进程"一对一"模型,将线程调度交给核心完成。混合线程模型是指核心级线程和用户级线程的结合,既提供核心线程以满足 SMP 系统的需要,也支持用线程...
Linux2.4 内核消除了这个线程个数的限制,并且允许在系统运行中动态地调整进程数上限。 Linux2.6 内核中,进程调度通过重新编写,删除了以前版本中效率不高的算法。内核线程框架也被重新编写,开始使用 NPTL...
标题 "glibc-linuxthreads-2.2.tar.gz" 指的是一个GNU C库(Glibc)的扩展,专门针对Linux系统实现线程支持的源代码包...尽管如此,`linuxthreads`对于理解早期Linux线程实现的历史和多线程编程的基本概念仍然有价值。
### Linux线程实现分析 #### 一、基础知识:线程和进程 - **进程与线程的概念**:根据传统定义,进程是系统进行资源分配和调度的基本单位,而线程则是程序执行的基本单位。进程拥有独立的地址空间和其他资源,线程...
Linux线程实现机制分析主要关注的是Linux平台上的线程模型,特别是LinuxThreads的运作方式。在多线程编程中,Linux面临着兼容性和效率的问题。线程作为程序执行的最小单位,是为了更好地支持对称多处理器(SMP)系统和...
1. **线程描述数据结构及实现限制**:LinuxThreads是早期在Linux系统中实现的线程库,每个线程都有一个与之对应的轻量进程。线程描述符保存了线程的状态、栈信息、调度信息等,但其局限性在于不支持线程组,不完全...
Linux下C开发之线程通信 在Linux平台下,线程通信是C开发中一个非常重要的方面。...Linux平台下提供了多种线程机制,包括LinuxThreads和POSIX线程标准等,可以根据实际情况选择合适的线程机制来实现多线程编程。
总的来说,Linux线程机制是通过轻量级进程和用户级线程库来实现的,虽然没有直接的核心级线程支持,但其优化的进程调度可以补偿这一不足。随着多核处理器的普及,Linux也在不断发展和完善其线程模型,以适应更加复杂...
【Linux线程的基本概念】 线程是操作系统中的一个基本概念,它是程序中的单个顺序控制流,也是进程内的执行单元。在多线程环境中,一个进程可以包含多个并发执行的线程,每个线程都有其独立的执行路径,共享进程的...
【Linux线程技术】在操作系统领域,Linux线程是指在Linux环境下实现的多线程编程技术。虽然在早期的Linux内核中并没有直接支持线程的概念,而是通过轻量级进程(Lightweight Process, LWP)来模拟线程的行为。在...
《glibc-linuxthreads-2.3.3:深入解析Linux线程库的里程碑版本》 在Linux操作系统中,glibc(GNU C Library)是核心的系统库,它为应用程序提供了与操作系统交互的接口。glibc的Linuxthreads是早期实现线程支持的...
Linux 内核只提供了轻量进程的支持,限制了更高效的线程模型的实现,但 Linux 着重优化了进程的调度开销,一定程度上也弥补了这一缺陷。最初的进程定义都包含程序、资源及其执行三部分,其中程序通常指代码,资源在...
Linux操作系统通过`LinuxThreads`库实现了内核级线程,遵循POSIX标准。在编程时,开发者需要包含`pthread.h`头文件,使用`pthread`库提供的函数来创建、管理和控制线程。 **关键库函数详解:** 1. **线程创建和...
Linux操作系统提供了符合POSIX 1003.1c标准的LinuxThreads库,该库支持内核级多线程编程。在进行多线程编程时,通常需要包含`pthread.h`头文件,并链接`libpthread.a`库。 - **线程的创建和终止** - `pthread_...
【Linux线程库的实现机制】深入探讨 Linux操作系统是一个开放源码的Unix-like系统,遵循POSIX(Portable Operating System Interface)标准。在Linux中,线程的实现主要有两种方式:用户级线程和内核级线程。本文将...
虽然Linux内核最初并未直接实现线程模型,而是通过轻量级进程(LWP,Lightweight Process)来模拟线程的行为,但随着技术的发展,Linux引入了线程机制,例如LinuxThreads,以提供更高效、灵活的多线程支持。...
Linux线程技术的发展经历了以下几个阶段: - **Linux 2.2 内核**: 在这个版本之前,Linux并没有真正意义上的线程支持。所谓的线程实际上是通过进程模拟的,因此线程数量受到限制。 - **Linux 2.4 内核**: 这个版本...