Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 18 Nov 2024 16:13:24 +0000
From:      bugzilla-noreply@freebsd.org
To:        bugs@FreeBSD.org
Subject:   [Bug 282713] Process enters in STOP state and doesn't respond to any signal.
Message-ID:  <bug-282713-227-w4txLmq8pb@https.bugs.freebsd.org/bugzilla/>
In-Reply-To: <bug-282713-227@https.bugs.freebsd.org/bugzilla/>
References:  <bug-282713-227@https.bugs.freebsd.org/bugzilla/>

next in thread | previous in thread | raw e-mail | index | archive | help
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D282713

--- Comment #9 from Rupesh Pilania <rupeshpilania@gmail.com> ---
Running two instances are enough to bring system less responsive.
Running 5 instances will cause system Freeze.
Compiled using cc -lpthread.




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

#define NUM_THREADS 1000   // Number of threads; adjust based on system
capability
#define NUM_ITERATIONS 100000   // Number of iterations per thread

pthread_mutex_t lock;
pthread_mutexattr_t attr;

void handle_error(int err, const char *msg) {
    fprintf(stderr, "%s: %s\n", msg, strerror(err));
    exit(EXIT_FAILURE);
}

void *thread_func(void *arg) {
    int thread_num =3D *((int *)arg);
    free(arg); // Free allocated memory for thread argument

    for (int i =3D 0; i < NUM_ITERATIONS; i++) {
        // Lock and unlock the mutex to simulate contention
        if (pthread_mutex_lock(&lock) !=3D 0) {
            perror("Failed to lock mutex");
        }

        // Simulate some work inside the critical section
        // (we keep it minimal to maximize lock contention)
        if (pthread_mutex_unlock(&lock) !=3D 0) {
            perror("Failed to unlock mutex");
        }

        // Small sleep to prevent the system from just cycling too quickly
        usleep(1);
    }

    printf("Thread %d completed.\n", thread_num);
    return NULL;
}

int keep_create_threads() {
    pthread_t threads[NUM_THREADS];

    //pthread_mutex_init(&lock, NULL);
       int err;

    // Initialize mutex attributes
    err =3D pthread_mutexattr_init(&attr);
    if (err !=3D 0)
        handle_error(err, "pthread_mutexattr_init");

    // Set the mutex as robust
    err =3D pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
    if (err !=3D 0)
        handle_error(err, "pthread_mutexattr_setrobust");

    // Initialize the mutex with the robust attribute
    err =3D pthread_mutex_init(&lock, &attr);
    if (err !=3D 0)
        handle_error(err, "pthread_mutex_init");

    // Create threads
    for (int i =3D 0; i < NUM_THREADS; i++) {
        int *thread_num =3D malloc(sizeof(int));  // Allocate memory for ea=
ch
thread's number
        if (!thread_num) {
            perror("Failed to allocate memory for thread number");
            exit(EXIT_FAILURE);
        }
        *thread_num =3D i;

        if (pthread_create(&threads[i], NULL, thread_func, thread_num) !=3D=
 0) {
            perror("Failed to create thread");
            exit(EXIT_FAILURE);
        }
    }

    // Wait for all threads to finish
    for (int i =3D 0; i < NUM_THREADS; i++) {
        if (pthread_join(threads[i], NULL) !=3D 0) {
            perror("Failed to join thread");
        }
    }

    pthread_mutex_destroy(&lock);
    printf("All threads completed.\n");
    return 0;
}

int main()
{
  while(1){
  keep_create_threads();
}=20
return 0;
}

--=20
You are receiving this mail because:
You are the assignee for the bug.=



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?bug-282713-227-w4txLmq8pb>