How to test the thread limit on Linux

/* compile with: gcc -pthread -o thread-limit thread-limit.c */
/* originally from: http://www.volano.com/linuxnotes.html */

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

#define MAX_THREADS 100000
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);
}

Additional information

If you are an administrator of a server, you can configure various limits in /etc/security/limits.conf

New simulation server: roadrunner

nes-servers

Computation speed for a parallizable numbercrunching problem: red:our new server, blue:two other servers, green:a fast laptop in comparison

The institute has a new number cruncher at your service!

roadrunner.nes.aau.at

It can run up to 24 parallel threads and is now our fastest server.

You can log in with your university LDAP account, the server is only available from NES network (or via VPN). It runs Ubuntu 12.04 LTS and provides the same software set up as feynman.

Reverse the lines of a file

Sometimes one needs things that cannot be done with a built-in function of Word or Libre Office Writer.
I needed to reverse the lines of a file. The IMHO most elegant solution to do this was:

cat -n myfile | sort -nr | cut -c 8- > outfile.txt

Basically it is enumerating the lines of the file, sorting them in reverse order and then cutting away the numbers. Isn’t bash beautiful?

Of course, Linux also has dedicated command to reverse the lines of a file, it is called tac (‘cat’ reversed).