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);
}
If you read
mallocman pages you can see that doesn’t initialize allocated memory to 0. So, youmallocmemory inp4,p5aandp5bfunctions. After this, you invoquef4function with no initialized LIST contens.In
p4function you check for a valid pointer withif(L1->head == NULL)but it maybe be not null. So you don’t alloc memory forL1->headand, after this function, inf5bfunction you are freeing a non allocated pointer.Advices:
mallocreturned pointers