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

  • Home
  • SEARCH
  • 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 9076123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:02:29+00:00 2026-06-16T19:02:29+00:00

I’m trying to make my program to do this: Take input: nrNodes NrWorkers 3

  • 0

I’m trying to make my program to do this:

  • Take input: nrNodes NrWorkers
  • 3 threads(workers) can only access at a moment the list(read), but
    only 1 can write.
  • when 5 nodes have been done(sqrt value), it should stop and let in
    the cleaner thread which cleans in the linked list all done nodes,
    than it gives the acces back to the remaining threads which have work

    Thread1:
    get semaphoreSlot();
    work;
    send signal if conition acomplished;
    until list parsed;

    Thread2:
    wait signal;
    clean;
    return access to thread1;

    After clean:
    problem encountered;
    stops, without exiting;

Code :

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

#define doneLimit 5

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

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

int done = 0;

sem_t sem;

pthread_mutex_t mutp;   // mutex
pthread_cond_t  condvar;   //condvar

void *deleteDoneNodes(void *n){
    pthread_cond_wait( &condvar, &mutp );
    struct node *root = n;
    struct node *it = root;
    struct node *prev = NULL;

    printf("Cleaning 1's \n");   
        do{
            if(it->value == 1){
                struct node *next = it->next;
                if (prev != NULL) {
                    prev->next = next;
                }
                if (it == root) {
                    root = next;
                }
                free(it);
                it = next;
            }
            else {
                prev = it;
                it = it->next;
            }
        }while(it !=  NULL);
        done = 0;

    pthread_exit(NULL);
}

void * worker( void *data ){
    // get list
    int wFlag;
    struct dataBlock *inData = ( struct dataBlock * ) data;
    struct node *root = inData->root;
    int forIndex = inData->forIndex;
    free(data);

    // parse
    while(1){
        if( sem_wait( &sem )  != 0 ){
            printf( " > waiting...  \n" );
        }
        struct node *it = root;

        printf("    Thread >>>  %d  ---  %lu \n", forIndex, pthread_self() );
        do{
            wFlag = 0;
            pthread_mutex_lock( &mutp );
            if( forIndex == it->worker ){
                if( it->value > 2 ){

                    while( it->value != 1 ){
                        it->value = sqrt(it->value);
                        }
                        printf("! node done\n");
                    pthread_mutex_unlock( &mutp );
                    wFlag += 1;
                    done += 1;
                    if( done == doneLimit ){ // limit 5
                        pthread_cond_signal( &condvar );
                    }
                    break;
                }
            }
            else{
                printf("...parsed done node. \n");
            }
            it = it->next;
            pthread_mutex_unlock( &mutp );
        }while(it !=  NULL);

        sem_post(&sem); 
        sleep(1); // "create" concurrancy envi. 
        if ( wFlag == 0 ){
            break;
        }
    }
    pthread_exit(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");
// init semaphore > keeps max 3 threads working over the list

    if( sem_init(&sem,0,3) < 0){
      perror("semaphore initilization");
      exit(0);
    }

// Create all threads to parse the link list
    int ret;    
    pthread_mutex_init( &mutp,NULL );
    pthread_cond_init( &condvar, 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("Worker creation fail \n");
            exit(2);    
        }   
    } 
// Create Cleaning thread
    pthread_t c_thread;
    ret = pthread_create(&c_thread, NULL, deleteDoneNodes, (void *) root);
    if ( ret ){
        printf("Cleaner cration fail \n");
    }

// Join threads
    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 _  \n", iterator->value, iterator->worker);
        iterator = iterator->next;
    }

    free( root );
    pthread_mutex_destroy( &mutp );
    pthread_cond_destroy( &condvar );
    sem_destroy( &sem );

    return 0;
}

PS:
it works
./s 1 1
./s 1 4
./s 4 4
./s 4 1

It fails after the call of the cleaner:
it fails at ./s x n, when x >= 5

  • 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-16T19:02:31+00:00Added an answer on June 16, 2026 at 7:02 pm

    At least you are not correctly handling the usage of condition variable and mutex.

    The mutex shall be locked by the thread calling pthread_cond_wait() prior to calling the latter.

    Verbatim from man pthread_cond_wait:

    pthread_cond_wait atomically unlocks the mutex (as per pthread_unlock_mutex) and waits for the condition variable cond to be
    signaled. The thread execution is suspended and does not consume any
    CPU time until the condition
    variable is signaled. The mutex must be locked by the calling thread on entrance to pthread_cond_wait. Before returning to the
    calling thread, pthread_cond_wait re-acquires mutex (as per
    pthread_lock_mutex).

    Also the mutex is locked after returning form the call to pthread_cond_wait() and must be unlocked then somewhere sometime.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,

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.