This is likely a setting related matter.
Problem: Unable to generate more than 70 pthreads on our public Redhat environments. Expectation is several thousand.
The attached program is designed to demonstrate to students the use of mutex semaphores for controlling access to shared data while also showing the ability of a system to handle many more pthreads than processes. It’s part of a teaching unit on creating a network server program.
Unfortunately the program fails at about 70 pthreads, which indicates the same limit as the number of concurrent processes allowed on our systems, which is set to a default of 75. This happens on both our two Enterprise student environments – one is Redhat 5.11 and the other is Redhat 6.8 . These are our shared environments which we use to give demos and students do assignments.
On a downloaded version of Redhat 7 as well as on Ubuntu 15.04, both running under VMWare I'm able to generate several thousand threads, which is what I'd expect.
The problem, when it occurs, becomes evident in the initial for loop in main.
/*
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;
}