尝试使用 pthreads,出现 munmap 错误?

Trying to use pthreads, getting munmap error?

我正在尝试使用辛普森规则进行数值积分,所以我认为最好的方法是在三个线程中,一个用于 2,一个用于 3,然后是结束边界。

但我是线程的新手,仍在学习一些东西。 但是,我真的不知道这有什么问题,我用它的属性创建线程并将其发送,然后我释放属性并尝试在它们可用时加入线程,我做错了吗? 有更好的方法吗?

感谢您的帮助!

附加代码::

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


#define PI 3.1415926535897932384626433832795028841971693993751058

float h_val, x_i, a_bound, b_bound, n_slices;
long double temp_vals[4];
int first, second = 0;

void * threes (void *t)
{
    long double a_n;
    long double seq_threes;
    long double x_i;
    long tid = (long)t;
    long double threes_sum = 0;
    for(int i = 1; i < n_slices; i++)
    {
        a_n = .25*((6*i)-(pow((-1),i))-3);
        x_i = a_n * (PI / n_slices);
        printf("Value of i :: %d  Value of x_i :: %Lf \n", i, x_i);
        seq_threes = 3*pow(((sin(PI*sin(x_i)))/(PI*sin(x_i))),2);
        threes_sum = threes_sum + seq_threes;
    }

    temp_vals[0] = threes_sum;
    pthread_exit((void*) t);
    return EXIT_SUCCESS;
}

void * twos (void *t)
{
    long double a_n;
    long double seq_twos;
    long double x_i;
    long double twos_sum = 0;
    long tid = (long)t;
    for(int i = 1; i < n_slices; i++)
    {
        a_n = 3*i;
        x_i = a_n * (PI / n_slices);
        printf("Value of i :: %d  Value of x_i :: %Lf \n", i, x_i);
        seq_twos = 2*pow(((sin(PI*sin(x_i)))/(PI*sin(x_i))),2);
        twos_sum = twos_sum + seq_twos;
    }

    temp_vals[1] = twos_sum;
    pthread_exit((void*) t);
    return EXIT_SUCCESS;
}

void* ends(void *t)
{
    long double a_n, a_0;
    long double x_n = n_slices;
    long double x_0 = 0;
    long tid = (long)t;
    pid_t end_to_end;
    printf("End to ends:: x_n ==%Lf \n",x_n);

    end_to_end = fork();
    if (end_to_end == 0)
    {
        x_0 = 0 * (PI / n_slices);
        a_0 = 1; 
        printf("a_0 :: %Lf :: x_0 :: %Lf\n",a_0, x_0);
        exit(1);
    }
    else
    {
        x_n = n_slices * (PI / n_slices);
        a_n = pow(((sin(PI*sin(x_n)))/(PI*sin(x_n))),2);
        printf("a_n :: %Lf || x_n :: %Lf\n",a_n, x_n);
        wait(NULL);
    }

    temp_vals[2] = a_0 + a_n;
    pthread_exit((void*) t);
    return EXIT_SUCCESS;
}


int main(int argc, char *argv[])
{

    pid_t id;
    pthread_t ends_thread, twos_and_threes[3];
    pthread_mutex_t thread_locker;
    void * status;
    long t;

    n_slices =(atof(argv[1])) ;
    n_slices = ((n_slices + 3 - 1) / 3) * 3 ;

    printf("Running with slice size :: %lf (must be multiple of 3)\n", n_slices);
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    pthread_mutex_init(&thread_locker, NULL);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    //Child Process
    if(pthread_create(&twos_and_threes[0], &attr,ends,(void*) t))
        return EXIT_FAILURE;
    if(pthread_create(&twos_and_threes[1],&attr,twos,(void*)t))
        return EXIT_FAILURE;
    if(pthread_create(&twos_and_threes[2],&attr,threes,(void*)t))
        return EXIT_FAILURE;

    wait(NULL);
    pthread_attr_destroy(&attr);
    for(int i = 0 ; i < 3 ; i++)
        pthread_join(twos_and_threes[i], &status);

    /*
    twos();
    threes();
    ends(); 
    */

    temp_vals[3] = temp_vals[0]+temp_vals[1] +temp_vals[2];

    printf( "Temp vals :: %Lf :: %Lf :: %Lf :: %Lf\n",\
            temp_vals[0],temp_vals[1],temp_vals[2],temp_vals[3]);

    wait(NULL);

    long double result  =((3*(PI/ n_slices))/8)*(temp_vals[3]) ;
    printf("Result == %Lf \n",result);




    pthread_exit(NULL);

    return EXIT_SUCCESS;
}

谢谢,解决了那个错误,并进行了一些其他更改,代码已更新以反映出来。

您正在一个进程中创建线程,然后在另一个进程中销毁它们。当你 fork() 时,只有调用 fork() 的线程会出现在子线程中,而在子线程中创建的线程不会神奇地出现在父线程中。

这里不需要 fork() 也不需要线程。只需等待主线程中的线程完成即可。