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 8930641
In Process

The Archive Base Latest Questions

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

Each linked list node contains a value which is has to be worked on,

  • 0

Each linked list node contains a value which is has to be worked on, and a worker which contains the number of the thread which ahs to work on the list. Nr of list elements and threads which are supposed to work on the list are command line paramethers(argv[1] and argv[2]). But somehow in the hole process my list’s last node doesn’t get evaluated / worked. A fix I considerd is to use in the thread function the do{}while(); structure in order to get it evaluated but it didn’t work.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <math.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 ){
    int j;
    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->next !=  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);
    }
    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;
}

EDIT valgrind -v:

bogdan@bogdan-VirtualBox:~/dos/threads$ valgrind -v ./s 2 2
==7205== Memcheck, a memory error detector
==7205== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==7205== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==7205== Command: ./s 2 2
==7205== 
--7205-- Valgrind options:
--7205--    --suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp
--7205--    -v
--7205-- Contents of /proc/version:
--7205--   Linux version 3.2.0-34-generic-pae (buildd@roseapple) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #53-Ubuntu SMP Thu Nov 15 11:11:12 UTC 2012
--7205-- Arch and hwcaps: X86, x86-sse1-sse2
--7205-- Page sizes: currently 4096, max supported 4096
--7205-- Valgrind library directory: /usr/lib/valgrind
--7205-- Reading syms from /lib/i386-linux-gnu/ld-2.15.so (0x4000000)
--7205--   Considering /lib/i386-linux-gnu/ld-2.15.so ..
--7205--   .. CRC mismatch (computed 405891ab wanted 9a1d2f37)
--7205--   Considering /usr/lib/debug/lib/i386-linux-gnu/ld-2.15.so ..
--7205--   .. CRC is valid
--7205-- Reading syms from /home/bogdan/dos/threads/s (0x8048000)
--7205-- Reading syms from /usr/lib/valgrind/memcheck-x86-linux (0x38000000)
--7205--   Considering /usr/lib/valgrind/memcheck-x86-linux ..
--7205--   .. CRC mismatch (computed 169814b3 wanted aeba1395)
--7205--    object doesn't have a symbol table
--7205--    object doesn't have a dynamic symbol table
--7205-- Reading suppressions file: /usr/lib/valgrind/debian-libc6-dbg.supp
--7205-- Reading suppressions file: /usr/lib/valgrind/default.supp
==7205== embedded gdbserver: reading from /tmp/vgdb-pipe-from-vgdb-to-7205-by-bogdan-on-???
==7205== embedded gdbserver: writing to   /tmp/vgdb-pipe-to-vgdb-from-7205-by-bogdan-on-???
==7205== embedded gdbserver: shared mem   /tmp/vgdb-pipe-shared-mem-vgdb-7205-by-bogdan-on-???
==7205== 
==7205== TO CONTROL THIS PROCESS USING vgdb (which you probably
==7205== don't want to do, unless you know exactly what you're doing,
==7205== or are doing some strange experiment):
==7205==   /usr/lib/valgrind/../../bin/vgdb --pid=7205 ...command...
==7205== 
==7205== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==7205==   /path/to/gdb ./s
==7205== and then give GDB the following command
==7205==   target remote | /usr/lib/valgrind/../../bin/vgdb --pid=7205
==7205== --pid is optional if only one valgrind process is running
==7205== 
--7205-- REDIR: 0x40182d0 (strlen) redirected to 0x38056b04 (???)
--7205-- REDIR: 0x4018050 (index) redirected to 0x38056adf (???)
--7205-- Reading syms from /usr/lib/valgrind/vgpreload_core-x86-linux.so (0x4025000)
--7205--   Considering /usr/lib/valgrind/vgpreload_core-x86-linux.so ..
--7205--   .. CRC mismatch (computed 79fe1296 wanted 94e37e32)
--7205--    object doesn't have a symbol table
--7205-- Reading syms from /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so (0x4028000)
--7205--   Considering /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so ..
--7205--   .. CRC mismatch (computed 85cd0163 wanted d1841711)
--7205--    object doesn't have a symbol table
--7205-- Reading syms from /lib/i386-linux-gnu/libm-2.15.so (0x4047000)
--7205--   Considering /lib/i386-linux-gnu/libm-2.15.so ..
--7205--   .. CRC mismatch (computed 6ee9373d wanted 98815fe2)
--7205--   Considering /usr/lib/debug/lib/i386-linux-gnu/libm-2.15.so ..
--7205--   .. CRC is valid
--7205-- Reading syms from /lib/i386-linux-gnu/libpthread-2.15.so (0x4073000)
--7205--   Considering /lib/i386-linux-gnu/libpthread-2.15.so ..
--7205--   .. CRC mismatch (computed e3e38d22 wanted 86ccdb6e)
--7205--   Considering /usr/lib/debug/lib/i386-linux-gnu/libpthread-2.15.so ..
--7205--   .. CRC is valid
--7205-- Reading syms from /lib/i386-linux-gnu/libc-2.15.so (0x408e000)
--7205--   Considering /lib/i386-linux-gnu/libc-2.15.so ..
--7205--   .. CRC mismatch (computed 75ebd58c wanted bfe81264)
--7205--   Considering /usr/lib/debug/lib/i386-linux-gnu/libc-2.15.so ..
--7205--   .. CRC is valid
--7205-- REDIR: 0x410bf70 (strnlen) redirected to 0x40254a0 (_vgnU_ifunc_wrapper)
--7205-- REDIR: 0x410d4c0 (strncasecmp) redirected to 0x40254a0 (_vgnU_ifunc_wrapper)
--7205-- REDIR: 0x4124fb0 (strstr) redirected to 0x40254a0 (_vgnU_ifunc_wrapper)
--7205-- REDIR: 0x4124bf0 (__GI_strstr) redirected to 0x402f030 (strstr)
--7205-- REDIR: 0x410c110 (__GI_strrchr) redirected to 0x402c1b0 (__GI_strrchr)
--7205-- REDIR: 0x4107d70 (malloc) redirected to 0x402be00 (malloc)
--7205-- REDIR: 0x410e7e0 (strchrnul) redirected to 0x402ebe0 (strchrnul)
node #1 worker: 1  value: 86
--7205-- REDIR: 0x4108250 (free) redirected to 0x402b000 (free)
--7205-- REDIR: 0x410beb0 (__GI_strlen) redirected to 0x402c670 (__GI_strlen)
? List got populated
workersInput: 2
--7205-- REDIR: 0x4108d30 (calloc) redirected to 0x402a540 (calloc)
in for, ret= 0
*    Thread id: 82025280    forID:  0  workerNode: 
in for, ret= 0
*    Thread id: 90417984    forID:  1  workerNode: 
_ _ _ _forID_ 1
->val: 86   
valid for sqrt  forIndex 1 == it->worker 1
==7205== Thread 3:
==7205== Invalid read of size 4
==7205==    at 0x804892E: worker (in /home/bogdan/dos/threads/s)
==7205==    by 0x4079D4B: start_thread (pthread_create.c:308)
==7205==    by 0x417CD3D: clone (clone.S:130)
==7205==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==7205== 
==7205== 
==7205== Process terminating with default action of signal 11 (SIGSEGV)
==7205==  Access not within mapped region at address 0x0
==7205==    at 0x804892E: worker (in /home/bogdan/dos/threads/s)
==7205==    by 0x4079D4B: start_thread (pthread_create.c:308)
==7205==    by 0x417CD3D: clone (clone.S:130)
==7205==  If you believe this happened as a result of a stack
==7205==  overflow in your program's main thread (unlikely but
==7205==  possible), you can try to increase the size of the
==7205==  main thread stack using the --main-stacksize= flag.
==7205==  The main thread stack size used in this run was 8388608.
==7205== 
==7205== HEAP SUMMARY:
==7205==     in use at exit: 304 bytes in 5 blocks
==7205==   total heap usage: 7 allocs, 2 frees, 328 bytes allocated
==7205== 
==7205== Searching for pointers to 5 not-freed blocks
==7205== Checked 16,847,500 bytes
==7205== 
==7205== LEAK SUMMARY:
==7205==    definitely lost: 0 bytes in 0 blocks
==7205==    indirectly lost: 0 bytes in 0 blocks
==7205==      possibly lost: 272 bytes in 2 blocks
==7205==    still reachable: 32 bytes in 3 blocks
==7205==         suppressed: 0 bytes in 0 blocks
==7205== Rerun with --leak-check=full to see details of leaked memory
==7205== 
==7205== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==7205== 
==7205== 1 errors in context 1 of 1:
==7205== Invalid read of size 4
==7205==    at 0x804892E: worker (in /home/bogdan/dos/threads/s)
==7205==    by 0x4079D4B: start_thread (pthread_create.c:308)
==7205==    by 0x417CD3D: clone (clone.S:130)
==7205==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==7205== 
==7205== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Killed
  • 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:56:07+00:00Added an answer on June 15, 2026 at 8:56 am

    The problem is your exit condition for the worker thread loop

    while(it->next !=  NULL);
    

    will exit when you’re about to process the last list item. Things should work if you change this to

    while(it !=  NULL);
    

    instead.

    I think there might also be an error in setup of the list. Does the last element ever have its next member initialised? There are a number of ways in which you could address this, the least invasive being to set iterator->next to NULL at the end of the list setup:

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a singly-linked list containing 1 value in each element. struct listElement {
I have a vector of Linked list pointers. Each LinkedList has a head pointer
I had a doubly linked list capable of holding characters in each node. This
I have a linked list of Nodes, each Node is defined as: struct Node
I have a list, and each item is linked, is there a way I
i have two tables products and reviews each product has several reviews linked by
Each key in dictionary has list of MANY integers. I need to iterate through
I wrote the code for reversing a doubly linked list containing words in each
I'm writing a linked list and I want a struct's destructor (a Node struct)
I have a linear linked list that consists of nodes: class Node{ Object data;

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.