Segmentation fault (core dumped) at pthread_join

I'm trying to run this file but it gives out this fault. Getting a segmentation fault (core dumped). Segmentation fault (core dumped) at pthread_join . Tried running printf at all the places and felt that this must be the error.

enter image description here

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <math.h>
#include <pthread.h>

float etime();
char *buffer;
void *foo(int kb)
{
    printf("Allocating memory\n");
    buffer = calloc(kb,sizeof(char));
    printf("Memory allocated\n");
    int i;
    printf("After i\n");
    for(i=0;i<kb;i++)
    {
        buffer[i]='z';
    }
    printf("End of for loop\n");
}
    
void et(int size)  
{
    
    int thrr;
    pthread_t num;
    etime();
    printf("Inside et \n");
    printf("Before calling create thread \n");
    thrr=pthread_create(&num,NULL,foo(size),NULL);
    printf("thrr = %d\n",thrr);
    if(thrr!=0)
    {
        printf("Cannot create thread!\n");
    
    }
    else
    {
        printf("Thread created successfully\n");
    
    }
    pthread_join(num,NULL);
    
    printf("Time taken = %f\n",etime());
}
    



Read more here: https://stackoverflow.com/questions/66267568/segmentation-fault-core-dumped-at-pthread-join

Content Attribution

This content was originally published by AnmolSingh at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: