Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8930323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:52:04+00:00 2026-06-15T08:52:04+00:00

I got stuck at thSegmentation fault (core dumped) is error and I’m running out

  • 0

I got stuck at thSegmentation fault (core dumped)
is error and I’m running out of ideas what it may be. Please help me detect what is the problem.

code:

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

#define workTime 5
#define workersLimitNr 3

struct dataBlock{
    struct node *root;
    int listSize;
    int forIndex;
};

struct node { // std linked list node
    int value;
    int worker;
    struct node *next;
};

int slots = 0; // only 3 threads are allowed to access the list
int availableCheck(){   // check if thread can acces the list
    if(slots < 3) return 0;
    else return -1;
}

pthread_mutex_t mutp = PTHREAD_MUTEX_INITIALIZER;   //condvar mutex
pthread_cond_t  condvar = PTHREAD_COND_INITIALIZER;   //condvar

void * worker( void *data ){
    struct dataBlock *inData = (struct dataBlock *) data;
    struct node *root = inData->root;
    int listSize =  inData->listSize;
    int forIndex = inData ->forIndex;
    free(data);
    printf( "*    Thread id: %lu    forID:  %d  workerNode: \n",pthread_self(),forIndex); 

    pthread_mutex_lock( &mutp );
    if(availableCheck() < 0){
        printf( " ^^^ List not available yet... \n" ); 
        pthread_cond_wait( &condvar, &mutp );
    } 
    struct node *it = root;

    printf( "_ _ _ _forID_ %d\n", forIndex );
    do{
        if(forIndex == it->worker){
            printf("valid for sqrt  forIndex %d == it->worker %d\n",forIndex, it->worker );
            if(it->value > 2){
                while(it->value != 1)
                it->value = sqrt(it->value);
                // it->value = it->value - 1;
            }
        }
        it = it->next;
        printf("->val: %d   \n", it->value);
    }while(it !=  NULL);

    pthread_cond_signal( &condvar ); // 
    pthread_mutex_unlock( &mutp ); 
    return NULL;
}

int main( int argc, char *argv[] ){
    if ( argc != 3 ){
        printf( "Programm must be called with \n NR of elements and NR of workers! \n " );
        exit( 1 );
    }

    int i;
    struct node *root;
    struct node *iterator;  

//prepare list for task
    int listSize = atoi(argv[1]);
    int nrWorkers = atoi(argv[2]);
    root = malloc(sizeof( struct node) );

    root->value = rand() % 100;
    root->worker = 0;
    iterator = root;

    for( i=1; i<listSize; i++ ){
        iterator->next = malloc(sizeof(struct node));
        iterator = iterator->next;
        iterator->value = rand() % 100;
        iterator->worker = i % nrWorkers;
        printf("node #%d worker: %d  value: %d\n", i, iterator->worker,iterator->value);
    }
    iterator->next = NULL;
    printf("? List got populated\n");

// Create all threads to parse the link list
    int ret;    
    printf("workersInput: %d\n",nrWorkers);
    pthread_mutex_init(&mutp,NULL);

    pthread_t w_thread;
    pthread_t* w_threads = malloc(nrWorkers * sizeof(w_thread));


    for( i=0; i < nrWorkers; i++ ){         
        struct dataBlock *data = malloc(sizeof(struct dataBlock));
        data->root = root;
        data->listSize = listSize;
        data->forIndex = i;
        ret = pthread_create ( &w_threads[i], NULL, worker, (void *) data );
        if( ret ) {
            perror("Thread creation fail");
            exit(2);    
        }   
        printf("in for, ret= %d\n",ret);

    } 

    for ( i = 0; i < nrWorkers; i++){
        pthread_join(w_threads[i],NULL);
    }
    iterator = root;
    for ( i = 0; i < listSize; i++){
        printf("val: %d  worker: %d _  ", iterator->value, iterator->worker);
        iterator = iterator->next;
    }

    free(root);
    free(iterator);
    return 0;
}

terminal compile + program run:

bogdan@bogdan-VirtualBox:~/dos/threads$ gcc -Wall -g  s.c -o s -pthread -lm
s.c: In function ‘worker’:
s.c:35:6: warning: unused variable ‘listSize’ [-Wunused-variable]
bogdan@bogdan-VirtualBox:~/dos/threads$ ./s 4 4
node #1 worker: 1  value: 86
node #2 worker: 2  value: 77
node #3 worker: 3  value: 15
? List got populated
workersInput: 4
in for, ret= 0
in for, ret= 0
in for, ret= 0
in for, ret= 0
*    Thread id: 3050646336    forID:  3  workerNode: 
_ _ _ _forID_ 3
->val: 86   
->val: 77   
->val: 15   
valid for sqrt  forIndex 3 == it->worker 3
Segmentation fault (core dumped)

PS: The error poped up after changing the the while condition in the thread worker function from while(iterator->next != NULL) and in the list creation iterator->next == NULL;

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:52:06+00:00Added an answer on June 15, 2026 at 8:52 am

    Your printf crashes the whole thing:

    do {
        // ...
        it = it->next;
        printf("->val: %d   \n", it->value);
    } while(it !=  NULL);
    

    At some point, right before your exit condition has been reached, it is NULL, so you’re dereferencing a null pointer by trying to print the value.

    Removing the printf should make the program run normally. Also, don’t forget to free().

    Pro-tip: Use Valgrind to detect invalid memory accesses.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning WF4 and got stuck at the following place. Please help.Thanks. 1)
Hi i got stuck with this problem, i can't find out how to get
I'm following the Django tutorial and got stuck with an error at part 4
I've been testing my application and got stuck into error that does not seem
Got stuck and need some help, This should be relatively simple but it's been
I'm trying out expression trees in C# and just got stuck with something that
I got stuck on trying to detect the pair of name and value of
I got stuck with a NaN error in this section of code: void Robot::updatePos(int
I've got stuck with strange Oracle connection error on Windows Server 2003 x64 (IIS
I got stuck by this error message because for three days I didn't find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.