简体   繁体   English

尝试使用pthreads,获得munmap错误?

[英]Trying to use pthreads, getting munmap error?

I am trying to do a numerical integration using simpsons rule, so I thought the best way to do this is in three threads, one for 2's one for 3's and then the end bounds. 我正在尝试使用辛普森规则进行数值积分,因此我认为最好的方法是在三个线程中进行操作,一个线程为2的线程,一个线程为3的线程,然后是结束边界。

But I am new to threads and am still learning some things. 但是我对线程不熟悉,仍然在学习一些东西。 However, I don't really know whats wrong with this, I create the thread with its attributes and sent it off, then I free the attributes and try to join the threads as they become available, did I do this wrong? 但是,我真的不知道这有什么问题,我创建了带有其属性的线程并将其发送出去,然后释放这些属性并尝试在线程可用时加入它们,这是不是做错了? Is there a better way to do it? 有更好的方法吗?

Thank you for any help! 感谢您的任何帮助!

Code attached:: 附带的代码::

#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;
}

Thank you, that solved that error, along with a few other changes, the code has been updated to reflect. 谢谢,解决了该错误,并进行了其他一些更改,代码已更新以反映出来。

You are creating the threads in one process, then destroying them in a different process. 您在一个进程中创建线程,然后在另一个进程中销毁它们。 When you fork() , only the thread that calls fork() will be present in the child, and threads created in the child will not magically appear in the parent. 当您fork() ,只有调用fork()的线程才会出现在子代中,而在子代中创建的线程将不会神奇地出现在父代中。

You don't need fork() here as well as threads. 您不需要这里的fork()和线程。 Just wait for the threads to complete in the master thread. 只需等待主线程中的线程完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM