user2040136
user2040136

Reputation: 31

setrlimit() does not change values

My computer accepts 380 threads per process. I try to increase to a larger number, using settlimit () but I have the expected result.

How I can increase the number of process?

The following code does not work properly:



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

    #include <sys/time.h>
    #include <sys/resource.h>
    #include <errno.h>

    void *doSomeThing()
    {
        pthread_detach(pthread_self());
        sleep(1);
        pthread_exit(NULL);
    }

    int main(void)
    {
        struct rlimit rlim;
        pthread_t tid;
        int i;

        if (getrlimit(RLIMIT_NPROC, &rlim) != 0) {
            printf("Can't call getrlimit(): [%s]\n", strerror(errno));
            exit(0);
        }

        rlim.rlim_cur = 1000;
        rlim.rlim_max = 1200;
        if (setrlimit64(RLIMIT_NPROC, &rlim) != 0) {
            printf("Error: getrlimit()\n");
            exit(0);
        }

        /* Create threads */
        for (i=0; i<385; i++)  {
            if (pthread_create(&tid, NULL, doSomeThing, NULL) != 0)
                printf("Can't create thread %d:[%s]\n", i, strerror(errno));
        }
    }

Upvotes: 1

Views: 276

Answers (1)

Gearoid Murphy
Gearoid Murphy

Reputation: 12116

Decrease the size of the pthread stack, this should allow you to fit more threads on your system.

pthread_attr_init(&attr);
assert(pthread_attr_setstacksize(&attr, 1<<16) == 0);
for (i=0; i<2048; i++) 
    assert(pthread_create(&tid, &attr, doSomeThing, NULL) == 0);

Alternatively, decrease your stack size using setrlimit

rlim.rlim_cur=4096;
rlim.rlim_max=4096;
setrlimit64(RLIMIT_NPROC, &rlim);

Upvotes: 1

Related Questions