【问题标题】:Pi calculator with mutex Synchronization具有互斥量同步的 Pi 计算器
【发布时间】:2015-09-04 12:43:21
【问题描述】:

在这里完成一项任务。让代码工作并计算饼图,但我收到以下错误的随机值除外:

  1. ./piesync 10 3 在 3 个线程中用 10 个项计算的 pi 是 3.14183961892940200045 * `./piesync' 中的错误:free():下一个大小无效(快速):0x0000000001ca3010 *

  2. ./piesync 100 5 * `./piesync' 中的错误:双重释放或损坏(输出):0x0000000000ee5040 *

我知道这可能与数组或互斥锁有关,但不知道是什么。

代码:

//Pini Vaknine
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

//global variables
int N, T;
double gpie = 3.0;
pthread_mutex_t mutex;

//pie function
void* pie_runner(void* arg)
{
    long j = (long)arg;    
    long lower = (N/T)*(j-1)+1;
    long upper = ((N/T)*(j));
    double myPartialSum = 0;
    //printf("lower=%lu upper=%lu\n",lower , upper);

    for(long i = lower; i <= upper; i++)
    {        
        if(i % 2 == 0){
            myPartialSum -= 4.0/((2*i)*(2*i+1)*(2*i+2)); 
            //printf("vsum %lu = %f\n", j, vsum[j]);
                  }
        else{
            myPartialSum += 4.0/((2*i)*(2*i+1)*(2*i+2));
            //printf("vsum %lu = %f\n", j, vsum[j]);
                  }


    }
    pthread_mutex_lock (&mutex);
    gpie = gpie + myPartialSum;
    pthread_mutex_unlock (&mutex);    

    pthread_exit(0);
    //return NULL;
}

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

    if(argc != 3) {
        printf("Error: Must send it 2 parameters, you sent %d\n", argc-1);
        exit(1);
    }
    N = atoi(argv[1]);
    T = atoi(argv[2]); 


    if(N <= T) {
        printf("Error: Number of terms must be greater then number of threads.\n");
        exit(1);    
    }



    //launch threads
    pthread_attr_t attr;
    pthread_t *tids = (pthread_t *) calloc(T, sizeof(pthread_t));
    if(tids == NULL) {
        fprintf(stderr, "Memory allocation problem\n");
        exit(1);
    }


        pthread_mutex_init(&mutex, NULL);
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    for(long i = 1; i<=T; i++)
    {

        int r = pthread_create(&tids[i], &attr, pie_runner, (void*)i);
        if(r<0) {
            printf("ERROR: pthread_create() returned %d\n", r);
            exit(2);
        }
    }

    //wait for threads...
    for(int k = 1; k<=T; k++)
    {
        pthread_join(tids[k], NULL);
    }


    printf("pi computed with %d terms in %d threads is %.20f\n", N, T, gpie);

    pthread_mutex_destroy(&mutex);    
    pthread_attr_destroy(&attr);

    free(tids);

    return 0;
}

【问题讨论】:

  • C 使用零索引循环和数组/指针。

标签: c unix pthreads mutex


【解决方案1】:

您的索引超出了数组的范围。您在此处为 T 元素分配了一个数组

pthread_t *tids = (pthread_t *) calloc(T, sizeof(pthread_t));

但是你索引不正确,这里

for(int k = 1; k<=T; k++)
    {
        pthread_join(tids[k], NULL);
    }

还有其他实例。在 C 中,你从 0 索引一个数组,所以循环应该是

for(int k=0; k<T; k++)

【讨论】:

  • 另外,你在中有一个除以零
【解决方案2】:

你有一个被零除

if(i % 2 == 0){
  myPartialSum -= 4.0/((2*i)*(2*i+1)*(2*i+2));

对于 j=0。

解决这个问题

for(long i = lower; i <= upper; i++)
{        
  if(i % 2 == 0){
    if ( ((2*i)*(2*i+1)*(2*i+2)) == 0)
      myPartialSum = 0.0;
    else 
      myPartialSum -= 4.0/((2*i)*(2*i+1)*(2*i+2)); 
  }
  else{
    myPartialSum += 4.0/((2*i)*(2*i+1)*(2*i+2));
  }
}

更改索引后,程序开箱即用

【讨论】:

  • i == 0 何时在for(long i = lower; i &lt;= upper; i++) 中?
  • 我以一种丑陋的方式修复了它,但现在我得到的 pi 在 24 个线程中用 55 个项计算得出是 3.14159024700620292947
  • 哦,抱歉,当时是 j=0
  • 但事实并非如此。 j 作为最小值传递1,然后在long lower = (N/T)*(j-1)+1; 中使用,所以在所有情况下lower &gt;= 1i &gt;= 1 在你说的表达式中都会导致 div 0 错误。
  • 啊,是的!更正调用循环后,j 可以是 0,但在 N/T == 1 时,lower 只会是 0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-10
  • 2014-05-02
  • 1970-01-01
  • 2012-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多