2015-02-02 7 views
0

심슨 규칙을 사용하여 수치 적 통합을 시도하고 있으므로이 작업을 수행하는 가장 좋은 방법은 쓰레드 3 개, 2는 3 개, 끝 경계는 하나라고 생각했습니다.mthmap 오류가 발생하여 pthread를 사용하려고합니까?

하지만 스레드에 익숙하지 않고 여전히 몇 가지 사항을 배우고 있습니다. 그러나, 나는 이것에 무슨 문제가 있는지 모르겠다. 나는 그 속성을 가진 쓰레드를 생성하고 보냈다. 그런 다음 속성을 해제하고 쓰레드가 사용 가능해 지려고 시도했다. 내가 잘못 했는가? 더 좋은 방법이 있나요?

도움 주셔서 감사합니다.

코드가 부착 ::

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

는 몇 가지 다른 변화와 함께, 해당 오류를 해결하는, 감사합니다, 코드를 반영하기 위해 업데이트되었습니다.

+0

* 무엇이'munmap' 오류를 생성합니까? 당신은'munmap'을 직접 사용하는 것처럼 보이지 않습니다. 또한 왜 fork()와 쓰레드가 필요한지 설명 할 수 있습니까? 그럴 가능성은 희박합니다 (오류의 원인 일 가능성이 높음). – abligh

+0

솔직히 자녀 과정을 통해 부모와 자녀를 맺는 것이 더 나을 것이라고 생각했습니다. 왜 생각했는지 모르겠다. –

+1

쓰레드와'fork()'를 섞는 것은 일반적으로 당신이하는 일을 정확히 알지 않는 한 나쁜 생각이다. – abligh

답변

0

스레드를 한 프로세스에 작성한 다음 다른 프로세스에서 스레드를 작성하려고합니다. fork() 일 때 fork()을 호출하는 스레드 만 자식에 있고 자식에서 만든 스레드는 마술처럼 부모에 나타나지 않습니다.

여기에는 스레드뿐만 아니라 fork()이 필요하지 않습니다. 스레드가 마스터 스레드에서 완료 될 때까지 기다리십시오.

관련 문제