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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:24:30+00:00 2026-05-31T00:24:30+00:00

This is the first time I’ve used pthreads. I’m having trouble because some times

  • 0

This is the first time I’ve used pthreads. I’m having trouble because some times my program seg faults and sometimes it does not. I have a few functions in my program that perform some basic tasks (written in C) like creating a linked list, adding an item to a list, and deleting an item from a list. Each function creates it’s own copy of a list so I don’t think they interact with each other and hence don’t need mutexes. Anyway below is my code if anyone has any ideas or if there are any “common” beginner pthread mistakes.

I run each function 1000 times in parallel, some times seg faulting, some times not. I notice it only happens with a combination of these 3 functions.

The process goes like this:
– create thread
– run threads in parallel
– each thread calls dummy function to perform a task a given number of times
– that dummy function also calls other functions

I think it might have to do with memory usage/allocation because all of these functions have to do with creating/deleting linked list nodes. Thanks a lot.

Here are the creates and joins:

 pthread_create(&t7, NULL, (void*)&p4, (void*)var);
 pthread_create(&t8, NULL, (void*)&p5a, (void*)var);
 pthread_create(&t9, NULL, (void*)&p5b, (void*)var);
 pthread_join(t7, NULL);
 pthread_join(t8, NULL);
 pthread_join(t9, NULL);

Here are the dummy functions:

void p4(int *nptr){
  int n = *nptr;
  // Get current time
  struct timeval t0, t1;
  gettimeofday(&t0,0);

  int i = 0;
  LIST *list = (LIST*)malloc(sizeof(LIST));
      for(i=0;i<n;i++){
    f4(list);
    deleteList(list);
  }
  // Get current time and find time elapsed
  gettimeofday(&t1,0);
  float elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + t1.tv_usec-t0.tv_usec;
      printf("Successful execution of p4 in %f microseconds.\n", elapsed);
  free(list);
}
void p5a(int *nptr){
  int n = *nptr;
  LIST *list = (LIST*)malloc(sizeof(LIST));
  f4(list);
  // Get current time
  struct timeval t0, t1;
  gettimeofday(&t0,0);

  int i = 0;
      for(i=0;i<n;i++){
    f5a(list);
  }
  // Get current time and find time elapsed
  gettimeofday(&t1,0);
  float elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + t1.tv_usec-t0.tv_usec;
  printf("Successful execution of p5a in %f microseconds.\n", elapsed);
}
void p5b(int *nptr){
  int n = *nptr;
  LIST *list = (LIST*)malloc(sizeof(LIST));
  f4(list);
  int i = 0;
      for(i=0;i<n;i++){
    f5a(list);
  }
  // Get current time
  struct timeval t0, t1;
  gettimeofday(&t0,0);
  for(i=0;i<n;i++){
    f5b(list);
  }
  // Get current time and find time elapsed
  gettimeofday(&t1,0);
  float elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + t1.tv_usec-t0.tv_usec;
  printf("Successful execution of p5b in %f microseconds.\n", elapsed);
}

Here are the functions used that perform regular tasks:

// FUNCTION: initialize a linked list with pointers and insert a last element
void f4(LIST *L1){
  // initialize an empty linked list if L1 = null
  if(L1->head == NULL){
    NODE *n = (NODE *)malloc(sizeof(NODE));
    L1->head = n;
    L1->tail = n;
    L1->tail->next = NULL;
    n->data = 1;
  }
  // traverse the linked list to the end
  NODE *iter = L1->head;
  while(iter->next != NULL)
    iter = iter->next;
  // insert a new 2 element
  NODE *new = (NODE *)malloc(sizeof(NODE));
  new->data = 2; // arbitrary for testing
  new->next = NULL;
  iter->next = new;
  L1->tail = new;
}

// FUNCTION: add an item to the end of a list (queue)
void f5a(LIST *list){
  NODE *new = (NODE *)malloc(sizeof(NODE));
  new->data = 999;
  new->next = NULL;
  list->tail->next = new;
  list->tail = new;
}

// FUNCTION: remove an item from the beginning of a list (queue)
void f5b(LIST *list){
  NODE *remove = list->head;
  list->head = list->head->next;
  free(remove);
}
  • 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-05-31T00:24:32+00:00Added an answer on May 31, 2026 at 12:24 am

    If you read malloc man pages you can see that doesn’t initialize allocated memory to 0. So, you malloc memory in p4, p5a and p5b functions. After this, you invoque f4 function with no initialized LIST contens.

    In p4 function you check for a valid pointer with if(L1->head == NULL) but it maybe be not null. So you don’t alloc memory for L1->head and, after this function, in f5b function you are freeing a non allocated pointer.

    Advices:

    • Always initialize allocated regions.
    • Always check malloc returned pointers
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing this first time so I need some help. I am using
This is my first time attempting to call an ASP.NET page method from jQuery.
This is my first time using joomla. I don't know if I'm using the
This is my first time with Web services. I have to develop web services
this is my first time posting here, I have a question which I have
This is my first time on StackOverflow though I read Coding Horror quite often.
This is the first time I'm creating an open-source project, and I've decided (based
This is the first time I'm trying random numbers with C (I miss C#).
This is my first time making a hash table. I'm trying to associate strings
This is the first time ever I'm using AJAX, and I want to do

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.