I am trying to work with multi-threaded programs, and I am getting an error with the pthread_join function. The output from this code is:
after pthread_create
Segmentation fault (core dumped)
And here is the code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *myfunc1()
{
// code segments
pthread_exit(sum1);
}
void *myfunc2()
{
// code segments
pthread_exit(sum2);
}
int main(int argc, char *argv[])
{
void *sum1, *sum2;
pthread_t thread_id1,thread_id2;
pthread_create(&thread_id1,NULL,myfunc1,NULL);
pthread_create(&thread_id2,NULL,myfunc2,NULL);
printf("after pthread_create\n");
pthread_join(thread_id1, &sum2);
pthread_join(thread_id2, &sum1);
printf("after pthread_join\n");
float firstSum = *((float *)sum1);
float secondSum = *((float *)sum2);
printf("Sum is %.5f\n\n", firstSum+secondSum);
return 0;
}
sum1 and sum2 are not initialised. So these lines
float firstSum = *((float *)sum1);
float secondSum = *((float *)sum2);
dereference undefined pointers.
Your thread functions should pass a pointer back (to something that survives the exit of the function) which can then be used by pthread_join e.g.
void *myfunc1()
{
float *ret = malloc(sizof *ret);
*ret = 3.14;
// code segments
pthread_exit(ret);
}
Then in main
float *sum1;
// run the thread
pthread_join(thread_id2, (void**)&sum1);
float result = *sum1;
free(sum1);
Your segmentation fault happens in one of your threads. pthread_create() creates and launches your thread, pthread_join() makes your main thread wait for the end of the other threads. Your main thread keeps running and starts waiting for your other threads to end, but one of them creates a segmentation fault so your main thread does not display "after pthread_join". So the segmentation fault does not come from pthread_join().
Related
when i try to compile this on ubuntu 20.04 lts it throws a segmentation error ,anyone could tell me how to fix this please?
#include<stdio.h>
void* stars (void * arg0)
{ for(int i=0;i<100;i++)
printf("*");
return NULL;
}
void* sharp(void* arg0)
{for(int i=0;i<100;i++)
printf("#");
return NULL;
}
int main(){
pthread_t thread0,thread1;
pthread_join(thread0,NULL);
pthread_join(thread1,NULL);
pthread_create(&thread0,NULL,stars,NULL);
pthread_create(&thread1,NULL,sharp,NULL);
}```
The problem is that you assign the thread to the respective variables after trying to join. Essentially, the pthread library is trying to dereference a variable with a garbage value, because you did not assign NULL to them on creation. The fix this this:
int main(){
pthread_t thread0,thread1;
pthread_create(&thread0,NULL,stars,NULL);
pthread_create(&thread1,NULL,sharp,NULL);
pthread_join(thread0,NULL);
pthread_join(thread1,NULL);
}
I am using the GMP. My program can build successfully, But run failed. The following is error things:
a=1231231231231231
res^n != a
Segment fault
All codes in my program is:
#include <gmp.h>
#include <stdio.h>
int main()
{
mpz_t a,res;
unsigned long int n = 123;
char str1[] = "1231231231231231";
mpz_init_set_str(a, str1, 10);
gmp_printf("a=%Zd\n",a);
mpz_init(res);
if(mpz_root(res, a, n)){
printf("res^n == a\n");
}
else{
printf("res^n != a\n");
}
mpz_clears(a,res);
return 0;
}
You have to call mpz_clears() like:
mpz_clears(a,res, NULL);
Here's what the documentation says:
Function: void mpz_clears (mpz_t x, ...)
Free the space occupied by a NULL-terminated list of mpz_t variables.
I have a little C program.
#include <stdlib.h>
#include <stdio.h>
void counter(int n);
int main(int argc, char *argv[])
{
int n = atoi(argv[1]);
counter(n);
return 0;
}
void counter(int n)
{
printf("%d\n", n);
if(n == 0) {
return;
}
counter(n - 1);
}
When I enter 200000 as the command line argument it works well. But with 300000 it says: 17623 segmentation fault (core dumped).
With gdb, the error is: Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a6f198 in _IO_new_file_write (f=0x7ffff7dd0760 <_IO_2_1_stdout_>, data=0x555555756260, n=6) at fileops.c:1196 1196 fileops.c: No such file or directory.
I don't know what is wrong. Should I allocate memory or something like that?
I'm trying to pass pointers to struct lower_hyper_id from a thread to the main thread, by the means of pthread_exit() function, that would compare and output the value in the struct. However, i receive an error (Segmentation fault) when i am trying to use the returned value and cast it to the struct.
thread that creates and returns the struct:
void *compute(void *arg){
lower_hyper_id *data = (lower_hyper_id *)malloc(sizeof(lower_hyper_id));
//some code
//i debug the program, and at this point, the struct i want
//to return has the values i want.
pthread_exit((void *)data);
}
in the main:
lower_hyper_id l_hyper_id;
int main(){
void *ap_state;
lower_hyper_id values;
void *ret;
//some code
for (int i = 0; i < NUMBER_OF_FILTERING_THREADS; i++)
{
s = pthread_join(filtering_threads[i], (void *)&ret);
//some error checking
values = *((lower_hyper_id *)ret); //this is where i receive the error
if (values.lowest_cost <= l_hyper_id.lowest_cost)
{
l_hyper_id.hyper_id = values.hyper_id;
l_hyper_id.lowest_cost = values.lowest_cost;
}
free(ret);
}
I have already looked at answers in the stackoverflow such as this question, but it hasn't helped me resolving this. I actually changed the code to be exactly equal to the code in this answer, but still it gives me an error.
You're not testing if malloc returned NULL. That could be an issue if you're allocing a large chunk and the allocation can fail.
Other than that, I don't think the problem is in the return value passing.
pthread_exit()ing with a mallocd pointer should work just fine.
A minimial working example:
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *compute (void *arg)
{
printf("thread=%llx\n", (unsigned long long)pthread_self());
size_t sz = strlen("hello world")+1;
char *ret = malloc(sz+1);
if(ret) memcpy(ret, "hello world", sz+1);
return ret;
}
int main()
{
printf("thread=%llx\n", (unsigned long long)pthread_self());
pthread_t ptid;
int er;
if((er=pthread_create(&ptid,0,compute,0))) return errno=er,perror(0),1;
void *retval;
if((er=pthread_join(ptid,&retval))) return errno=er,perror(0),1;
printf("thread returned: %s\n", (char*)retval);
free(retval);
}
I'm trying to multiply two matrices using multithreading. But the code is giving segmentation fault. I am passing the row number and column number using a structure. The matrices a and b are made global. This is not entirely correct way to do it, but I'm just trying to understand how multithreading stuff works.
#include <pthread.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int b[3][2]={{1,2},{3,4},{5,6}};
int c[3][2];
int k =3;
struct thread_data{
int m;
int n;
};
void* do_loop(void* threadarg)
{
int p,q;
struct thread_data *my_data;
my_data = (struct thread_data *) threadarg;
int i=my_data->m;
int j=my_data->n;
c[i][j]=0;
for(q=0;q<k;q++)
{
c[i][j]=c[i][j]+a[i][q]*b[q][j];
}
pthread_exit(NULL);
}
int main(int argc, char* argv[])
{
int i,j,k;
struct thread_data td[6];
int thr_id;
pthread_t p_thread[6];
int count=0;
for(i=0;i<3;i++)
for(j=0;j<2;j++)
{
td[count].m=i;
td[count].n=j;
thr_id = pthread_create(&p_thread[count], NULL, do_loop, (void*)&td[count]);
// pthread_join(p_thread[count],NULL);
count++;
}
return 0;
}
How can I fix the segmentation fault?
First thing, you need to wait for all the threads to finish (in main):
for (i = 0; i < count; ++i) {
pthread_join(p_thread[i],NULL);
}
Failure to do so will crash your app as the thread continue to work why the application is being destroyed.
You need to call pthread_join after you create all the threads.
If you create a thread and immediately call pthread_join you execution is serial as one thread is active at any given time.
Explanation:
"join" means: "wait for thread to finish execution". A thread finishes execution when either it returns from it's entry point function (function passed to pthread_create) or it calls pthread_exit.