当前位置: 首页 > 知识库问答 >
问题:

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

苏星宇
2023-03-14

这可能是一个设置相关的问题。

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

/*
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个答案

殳宸
2023-03-14

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

如果您将进程限制设置为75,这实际上是对用户所有进程的线程总数的限制。所以这是按照它的设计工作的。

进程数的限制可以通过ulimit命令或通过setrlimit()系统调用应用的/etc/limits.conf文档设置

RLIMIT_NPROC这是可以为调用进程的真实用户ID创建的最大进程数(或者更准确地说,在Linux上是线程数)。

 类似资料:
  • 开始使用AWS代码构建。 目标是让docker图像作为最终结果,并在其中运行nodejs、hapi和示例应用程序。 目前我有一个问题:“无法准备上下文:无法在Dockerfile路径中计算符号链接: lstat /tmp/src049302811/src/Dockerfile:没有这样的文件或目录”出现在BUILD阶段。 项目详情: S3存储桶用作源 存储在各自S3存储桶中的ZIP文件包含buil

  • 该项目为 PHP 提供基于 POSIX 的多线程编程机制。可异步执行任何与定义的用户自定义方法和函数。内建支持同步和同步方法。 要求: PHP5.3+ ZTS Enabled ( Thread Safety ) Posix Threads Implementation 示例代码: <?phpclass AsyncOperation extends Thread {  public function

  • 出于某种原因JavaStream生成了更多的值(调用迭代器的hasNext()和Next()方法。 这是合成示例。 我有一个迭代器形式的生成器: 现在,我试图有一个平坦的Stream,它由几个迭代器组成 令人惊讶的是,我的输出如下: 因此,Stream生成的结果比forEach中传递给使用者的结果要多。即使它被显式设置为“parallel = false”。 在我的真实场景中,hasNext()和

  • 问题内容: 我正处于起步阶段,想全神贯注于Go。目前,我正在模拟一个API请求,该请求返回一个包含对象数组的JSON格式的字符串。我正在尝试找出最合适的方法来遍历每个记录并访问每个字段。最终,每个字段都将被写入Excel电子表格,但是现在我只想打印每个字段的键和值。 这就是我所拥有的(我会在Go Playground中提供它,但不支持HTTP请求): 一切工作正常,直到尝试循环访问包含每个记录的属

  • 我正在用RSA私钥加密我的对称密钥(AES),并用我的公钥解密它。 但是,当我加密数据时,字节长度是16字节,然而,当我解密数据时,它抛出了受影响的错误,解密时字节数据的长度是344。

  • 吊舱降到0,但旋转速度不会超过1个吊舱。即使订阅中的代码超过5mg,也会根据建议更新代码。 添加了TriggerAuthentication并使用connectionString作为身份验证。试图发送大约25条消息,但pod仍无法扩展。 只有一个pod在处理所有消息。我需要更新什么吗 获取错误: 运行命令后: apiregistration.k8s.io/v1 166种类:APIService 1