pthreads:在Redhat 6.8下不能生成70多个

时间:2017-08-08 20:22:26

标签: c linux multithreading pthreads redhat

这可能与设定有关。

问题:无法在我们的公共Redhat环境中生成超过70个pthread。期望是几千。

附加程序旨在向学生演示使用互斥信号量来控制对共享数据的访问,同时还显示系统处理比进程多得多的pthread的能力。它是创建网络服务器程序的教学单元的一部分。

不幸的是,程序在大约70个pthread上失败,这表示与我们系统上允许的并发进程数相同的限制,默认设置为75.这在我们的两个Enterprise学生环境中都会发生 - 一个是Redhat 5.11和另一个是Redhat 6.8。这些是我们用于演示和学生分配的共享环境。

在Redhat 7以及Ubuntu 15.04的下载版本上,两者都在VMWare下运行,我能够生成数千个线程,这是我所期望的。

问题发生时,在主要 循环中变得明显。

/*
File: pthreadDemo2.c
Course: CENG320
Author: Prof. Leon King,J230,x4200
Date: Friday Jun 16, 2017   14:26 PM

In this demo we list the status of threads and selectively cancel them.
The commands are:  list  (list)
kill  #  kill a thread by number
inc      have threads increment the counter
INC   #  increment a specific counter  
quit     end the program
show     show the counter

Compile with the thread library -pthread
See also:  http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
 */


#include <pthread.h>
#include <bits/local_lim.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
/* We have no predefined limit on the number of threads.  */
#define NUMBER_OF_THREADS 4000 

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER; //Warning! This is a shared global variable!
long counter=0;

void incrementCounter(int signum)
{
    //pthread_mutex_lock(&mutex); 
    int c2;
    c2=counter;
    c2=c2+1;
    counter=c2;
    //fprintf(stdout,"Changing a counter\n"); 
    /*FILE *log=fopen("threadSafeLog.dat","a");
      fprintf(log,"Logging event for thread %u\n",(int) pthread_self()); 
      fprintf(log,"Logging event 2nd write for thread %u\n",(int) pthread_self()); 
      fclose(log);
     */
    //pthread_mutex_unlock(&mutex);
}

void *threadProcedure(void * arg)
{
    int myThreadInfo=(long) arg,
    threadID=pthread_self();
    while(1) pause();  
    pthread_exit(0);  //calling exit() would end all threads & main

}

int main(int argc, char * argv[],char * envp[])
{
    pthread_t threadList[NUMBER_OF_THREADS]={0};
    char cmd[20];

    pthread_mutex_init(&mutex,NULL); 
    pthread_attr_t threadAttributes; 
    pthread_attr_init(&threadAttributes);

    int err1=pthread_attr_setstacksize(&threadAttributes, 16384);
    if(err1) fprintf(stdout,"Error in setting the stack size: %d\n", err1);

    signal(SIGUSR1,incrementCounter);  
    fprintf(stdout,"Hello World. I'm %d\n",(int) getpid()); 
    //
    //Spawn a group of threads
    //THIS IS THE CORE OF THE PROBLEM
    //IT CAN GENERATE OVER 6K threads on a Ubuntu 15.04 Release
    //BUT only 70 threads on Redhat 5.11 and 6.8 Enterprise
    //The focus question is: How does one increase the limit on the number of threads in Redhat 
    for(int i=0;i<NUMBER_OF_THREADS;i++) 
    {
    if(!pthread_create(&threadList[i],&threadAttributes,threadProcedure,(void *)i))
        {
            pthread_detach(threadList[i]); 
         }
     else   perror("Failure to create a thread");

    }

    while(1)
    {
        fprintf(stdout,"Commands: list, show, increment, quit\n");
    fscanf(stdin,"%s",cmd);
    switch(cmd[0])
    {

        case 'l': for(int i=0;i<NUMBER_OF_THREADS;i++)
              fprintf(stdout,"thread id of threadlist[%d]: %u\n",i,(int) threadList[i]); 
              break;

        case 's':  fprintf(stdout,"Counter value: %lu\n",counter);
               break;

               break;

        case 'i': fprintf(stdout,"Initial value of counter: %lu\n",counter);     
              for(int i=0;i<100;i++)
              {
                  int error= pthread_kill(threadList[i],SIGUSR1);
                  if(error) {perror("Failed signal"); errno=0;}

              }
              fprintf(stdout,"Final value of Counter? %lu\n",counter);  
              break;

        case 'q':  exit(0); 

    }

    }

    return 0;

}

1 个答案:

答案 0 :(得分:1)

是的,在linux下,线程和进程的处理方式非常相似。

如果您将进程限制设置为75,那么对用户的所有进程的线程总数实际上是一个限制。所以这就像它的设计一样。

使用ulimit命令或/etc/limits.conf设置进​​程数限制 这是应用setrlimit()系统调用, 来自documentation

  

<强> RLIMIT_NPROC                 这是最大的进程数(或者更确切地说是                 可以为真实用户ID创建的Linux(线程)                 呼叫过程。

相关问题