[Bug 282713] Process enters in STOP state and doesn't respond to any signal.

From: <bugzilla-noreply_at_freebsd.org>
Date: Mon, 18 Nov 2024 16:13:24 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=282713

--- 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 = *((int *)arg);
    free(arg); // Free allocated memory for thread argument

    for (int i = 0; i < NUM_ITERATIONS; i++) {
        // Lock and unlock the mutex to simulate contention
        if (pthread_mutex_lock(&lock) != 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) != 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 = pthread_mutexattr_init(&attr);
    if (err != 0)
        handle_error(err, "pthread_mutexattr_init");

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

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

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

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

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

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

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

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