Question:
Segmentation fault(core dumped)?
?
2013-04-05 00:39:38 UTC
I keep getting a segmentation fault when I run my code with 1 thread and 10 as the second input. I have two print flags (lines 37 and 39) however, only one of them prints, which leads me to believe it has to do with the lock. Am I using the mutex lock correctly, or am I making a mistake with my arrays?

http://pastie.org/private/yyb72oowctdfjcwuxh5fw

#include
#include
#include

int* numbermarker = NULL;
int* buffer = NULL;
int pullposition = 0;
int placeposition = 1;
pthread_mutex_t *lock;
int ceiling;

/*This method places one of the primes in the buffer. It
offers a safe way to manage where the next value will be placed*/
void placevalue(int value)
{
buffer[placeposition] = value;
placeposition++;
}

/*This method pulls the next prime and increments to the next prime in the list*/
int takevalue()
{
pullposition++;
return buffer[pullposition-1];
}


void* threadmethod()
{
int k;
int l;
int firstval;
while(1)
{
while(numbermarker[buffer[pullposition]-1]==0)
{
printf("flag1 \n");
pthread_mutex_lock(lock);
printf("flag2 \n");
numbermarker[buffer[pullposition]-1] = 1;
l = takevalue();
pthread_mutex_unlock(lock);
firstval = 1;
for(k=l+1;k<=ceiling;k++)
{
if(k%l != 0)
{
if(firstval)
{
placevalue(k);
firstval = 0;
}
}
else
{
numbermarker[k-1] = 1;
}
}
}
}
}


int main()
{
int numthreads;
int i;

printf("Enter number of threads: \n");
scanf("%d", &numthreads);

printf("Enter the highest value to check \n");
scanf("%d", &ceiling);

/* This will hold 1's and 0's.
1 = number has been checked or is
confirmed not to be a prime
0 = number is a possible prime

The idea behind these values is that the next
prime can always be identified by the 0 with
the lowest index*/
numbermarker = (int*)malloc(sizeof(int)*(ceiling));

/*This will hold the primes as they are found*/
buffer = (int*)malloc(sizeof(int)*(ceiling));

for(i=0; i {
if(i<1)
{
numbermarker[i] = 1;
}
else
{
numbermarker[i] = 0;
}

buffer[i]=0;
printf("%d \n",numbermarker[i]);
}

placevalue(2);

pthread_t **tid = (pthread_t **) malloc(sizeof(pthread_t *) * numthreads);


for(i=0;i {

tid[i] = (pthread_t *) malloc(sizeof(pthread_t));
}

for(i=0;i {

if(pthread_create(tid[i],
NULL,
threadmethod,
NULL))
{

printf("Could not create thread \n");
exit(-1);
}
}


int not_done = 1;
int sum;
while(not_done)
{
sum = 0;
for(i=0; i {
sum += numbermarker[i];
}
if(sum == ceiling)
not_done = 0;
}
for(i=0;i {
if(pthread_join(*tid[i], NULL))
{
printf("Error Joining with thread \n");
}
free(tid[i]);
}
free(tid);




for(i=0;i {
if(buffer[i] != 0);
printf("%d \n", i);
}
free(buffer);
free(numbermarker);
buffer=NULL;
numbermarker=NULL;


return(0);
}
Three answers:
AnalProgrammer
2013-04-05 03:12:29 UTC
This block of code appears to be an endless loop that perhaps you should investigate.

int not_done = 1;

int sum;

while(not_done)

{

sum = 0;

for(i=0; i
{

sum += numbermarker[i];

}

if(sum == ceiling)

not_done = 0;

}



Have fun.
Andrew
2013-04-05 23:15:17 UTC
I have no experience with mutex but segmentation faults are normally caused by attempting to use invalid pointers or reading outside the range of an array.

My first recommendation would be to verify that you aren't trying to use an invalid index on the array, maybe print the sizes allocated for the arrays and the indexes being used, possibly even an if statement to catch an invalid value.



Or run a debugger and see what it tells you ;-)
satish
2013-04-05 07:45:38 UTC
i think the mistake is with arrays.



can you use gdb and print the output?



looks like the code in pastebin is different. It is stuck when iam executing it.

can you check whether you have the same code in the paste bin??



iam guessing the sigsegv is due to running free on alreafy freed pointer



free(tid);#153



remove this and run again


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...