多线程:我有一个逻辑错误的代码,导致__segmentaion fault__错误,但是我找不到它

时间:2019-05-19 00:36:39

标签: c linux multithreading pthreads

我编写了一个代码,使用pthreads计算向量中的平方和。我现在正在尝试使程序针对不同数量的线程运行相同的算法,然后比较每个线程数所需的时间并将其写在文件中。

程序正在编译,但运行不正常。

此循环背后的想法是,我从1个线程开始循环,然后为2、4、8个线程等重复该过程。我将其速度记录在一个文件中,以便以后进行比较,但结果是出现分段错误错误。

似乎在循环中甚至没有进展,第一次陷入线程数为1的情况

我仍然是C语言的新手,我无法自己解决这个问题,需要您的帮助。

#include <stdio.h>                                      //comments written in english coz greek is broken
#include <semaphore.h>
#include <pthread.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

struct thread_arg {                                     //struct creation in order two pass multiple parameters within the pthread creation
        int *table;
        int k;
};

void *calculation(void *);

pthread_mutex_t sum_mutex = PTHREAD_MUTEX_INITIALIZER;
int  maxp, n, p, *sum = 0;

int main()
{
        int i, j, k=0, check, *table;
        clock_t t;
        double time_taken;
        FILE *f=fopen ("C:\\tmp\\resultsLS.txt","w");                                                       
        if (f==NULL){                                                                                               
            printf("\nError opening file!\n");
            exit(1);
        }

        printf("Give the max number of pthreads used\n");
        scanf("%d", &maxp);
        printf("Give the number of elements of the table\n");
        do{
            printf("The number of elements must be an integral multiple of the number of threads\n");
            scanf("%d",&n);
            check=n%maxp;
            if(check!=0){
                printf("Jesus how hard is it?\nTry one more time\nGive the number of elements of the table\n");
            }
            }while(check!=0);

        table = (int*) malloc(n * sizeof(int));
        if(table == NULL){
            printf("Error! Memory not allocated.\n");
            exit(0);
        }

        srand(time(NULL));
        for (i=0; i<n; i++){
            table[i]=(rand()%200);                                                                              
        }


        printf("\n--------------------------------\n");
        printf("\nArray elements: ");
        for (i=0; i<n; i++) {
            printf(" %d ", table[i]);
        }

        for(p=1;p<=maxp;p*2){                               
        fprintf(f, "\t#Number of threads\t%d", p);
        pthread_t threads[p];
        printf("\n--------------------------------\n");

        struct thread_arg th_args[p];                                           //table creation to store arguments of each pthread;

        t=clock();
        for(i=0;i<p;i++){                                                       //thread creation
                th_args[i].table = table;
                th_args[i].k = k;
                pthread_create(&threads[i], NULL, calculation, &th_args[i]);    
                k++;                                                            //k is a variable used to seperate table, explained later
        }
        t=clock()-t;
        time_taken=((double)t)*1000/CLOCKS_PER_SEC;
        fprintf(f,"\nExecution time of function with %d threads: %d ms\n", i, (int)time_taken);

        for(i=0;i<p;i++){
                pthread_join(threads[i],NULL);
        }

        printf("--------------------------------\n");
        printf("Sum of vector= %d\n", *sum);
        *sum=0;                                                                 //reset of sum
        }

        fclose(f);
        free(table);
        exit(0);
        return 0;
}

void *calculation(void *data){
        int i;
        int local_sum=0;
        int *table;
        int k;

        struct thread_arg *th_args = data;
        k = th_args->k;
        table = th_args->table;

        printf("--------------------------------\n");
        for(i=(n/p)*k;i<(n/p)*(k+1);i++)                                        //this algorithm seperates the table into equivelant pieces and
        {                                                                       //every pthread is calculating its given piece, then stores that value in its local variable sum
                if((n/p)>n){                                                    //and eventually it is updating the global variable
                        pthread_exit(NULL);
                }
                local_sum+=pow(table[i], 2);
                printf("Thread's %lu calculation is %d\n", pthread_self(), local_sum);
        }

        pthread_mutex_lock(&sum_mutex);                                         //mutex used here to protect the critical code
        *sum += local_sum;
        pthread_mutex_unlock(&sum_mutex);

        return NULL;
}

循环应在显示sum of the vector之后结束,但程序将在其之前停止其流程。

0 个答案:

没有答案