複数のスレッドを使用してファイルからの入力をソートする割り当てを実行しようとしていますが、構造体の配列を使用して、スレッドの後に回復する必要がある情報を格納しようとすると、セグメンテーション違反が発生します。私が持っている情報源によると、なぜそれが障害を引き起こしているのかわかりません。
これが本体ファイル Threads.c です。seg fault は for ループ内にあり、原因行はコメントで指定されています。Sort Method は、私が使用しなかった別の機能です
#include "threads.h"
Threads* threadArray[4];
int linesPerThread;
int extraLines;
main(int argc, char *argv[]){
int n;
if( argc != 4){
printf("Wrong Number of Arguements!\n");
return;
}
n = atoi(argv[1]);
char *inName = argv[2];
*threadArray = (Threads*) (*threadArray, n*sizeof(Threads));
FILE* file = fopen(inName, "r");
if(!file){
printf("invalid file Name \n");
return;}
int lines = 0;
char xyz[5000]; //makes fgets happy
while(fgets(xyz, 5000, file) != NULL){
lines = lines+1;
}
fclose(file);
linesPerThread = lines / n;
extraLines = lines - linesPerThread;
int i =0;
int methodCounter =1;
printf("Right before Loop \n \n");
for(i; i < n; i++){
printf("first part of loop \n");
\\The ling below here Seg Faults.
(*threadArray + i)->id = i;
printf("right after first ThreadArray access \n");
if(methodCounter < 3){
printf("method counter 1\n");
(*threadArray+i)->methodID = methodCounter;
methodCounter++;
}else{
printf("method counter condition 2 \n");
(*threadArray + i)->methodID = 3;
methodCounter = 1;}
if(extraLines > 0){
printf("extra Lines condition 1 \n");
(*threadArray+i)->lines = linesPerThread +1;
extraLines= extraLines -1;
}else{
printf("extraLines condition 2 \n");
(*threadArray+i)->lines = linesPerThread;
}
printf("Right before Thread Creation \n \n");
pthread_t tID;
pthread_create(&tID, NULL, sortMethod, (void*) &((*threadArray+i)->id));
(*threadArray+i)->threadID = tID;
printf("right after thread creation \n \n");
}
printf("right after loop \n \n");
int c=0;
printf("before thread joining \n");
for(c; c< n; c++){
pthread_join( (*threadArray+ c)->threadID, NULL);
}
}
これがヘッダー ファイル Threads.h です。
#include <sys/time.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
int id;
int lines;
pthread_t threadID;
int methodID;
}Threads;
void* sortMethod(void*ptr);
int main(int argc, char *argv[]);
あなたが提供できるどんな助けも大歓迎です。