そのため、pthreads を使用して問題を並行して解決するプログラムに取り組んでいます。現在、関数で次のコードを実行すると、セグ フォールトが発生します: average_power.
これは、エラーがどこかにあると確信しているコードの関連部分です。
struct args {
signal *sigs;
int filter_orders;
double *filter_coeffss;
signal *outputs;
int threadIDs;
int bandwidths;
int bandNumbers;
};
double *band_power;
pthread_t *tid; // array of thread ids
int numThread;
int numProc;
void *worker(void *arg){
struct args *currentArgs = (struct args*) arg;
int i;
int blocksize = currentArgs->bandNumbers / numThread; // note: floor
int mystart, myend;
int currentID = currentArgs->threadIDs;
int band;
int bandwidth = currentArgs->bandwidths;
mystart = currentID*blocksize;
if (currentID==(numThread-1)) { // last processor
// the last processor will take care of the leftover
// elements of the vector, in case num_threads doesn't
// divide vector_len
myend = currentArgs->bandNumbers;
} else {
myend = (currentID+1) * blocksize;
}
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(currentID%numProc,&set);
if (sched_setaffinity(0,numProc,&set)<0) { // do it
perror("Can't setaffinity"); // hopefully doesn't fail
exit(-1);
}
//THIS LOOP WILL BE THE NEW WORKER PROCESS TO BE SPLIT UP VIA BANDS
for (band=mystart;band<myend;band++) {
// Make the filter
generate_band_pass(currentArgs->sigs->Fs,
band*bandwidth+0.0001, // keep within limits
(band+1)*bandwidth-0.0001,
currentArgs->filter_orders,
currentArgs->filter_coeffss);
hamming_window(currentArgs->filter_orders,currentArgs->filter_coeffss);
// Convolve
convolve(currentArgs->sigs->num_samples,
currentArgs->sigs->data,
currentArgs->filter_orders,
currentArgs->filter_coeffss,
currentArgs->outputs->data);
// Capture characteristics
band_power[band] = avg_power(currentArgs->outputs->data, currentArgs->outputs->num_samples);
}
pthread_exit(NULL);
}
これはワーカー関数と、スレッドが初期化されて命令が与えられる別の関数のこのセクションから渡された構造体の引数です。
band_power = (double *) malloc(sizeof(double)*numThread);
tid = (pthread_t *) malloc(sizeof(pthread_t)*numThread);
///create all structs and initialize
struct args *curargs;
int p;
int num_started;
for(p=0;p<numThread;p++){
outputNew = (struct signal *) malloc(sizeof(struct signal)); //THIS IS THE LINE THAT
outputNew = allocate_signal(sig->num_samples, sig->Fs, 0);
curargs = (struct args *) malloc(sizeof(struct args));
curargs->sigs = sig;
curargs->filter_orders = filter_order;
curargs->filter_coeffss = filter_coeffs;
curargs->outputs = outputNew;
curargs->threadIDs = p;
curargs->bandwidths = bandwidth;
curargs->bandNumbers = num_bands;
rc=pthread_create( &(tid[p]), // thread id gets put here
NULL, // use default attributes
worker, // thread will begin in this function
curargs // we'll give it i as the argument
);
if (rc==0) {
printf("Started thread %ld, tid %lu\n",p,tid[p]);
num_started++;
} else {
printf("Failed to start thread %ld\n",p);
perror("Failed to start thread");
tid[p]=0xdeadbeef;
}
だから私が得ているエラーは、 *worker の最後に average_power が呼び出されたときのいずれかのスレッド内の segfault であるか (average_power は正しいことを証明しました)、または次の行のコメントを外すとエラーが発生します配列を初期化する私のループ。
outputNew = (struct signal *) malloc(sizeof(struct signal));
outputNew が arg 構造体に入れられる前に、上書きされないように独自のスペースがあることを確認したかったのですが、この行はコンパイルを防ぎます。私はそれを正しくしていませんか?それ以外の場合、 average_power は args 構造体からこの変数 (出力) にアクセスし、それが発生すると segfault が発生します。これは、どこかのメモリが正しく使用されていないことを意味すると思いますか? これが非常に長い場合は申し訳ありませんが、十分に表現していない場合は、部分を明確にすることができます. 他のすべての関数または呼び出しは私のプロジェクトに関連しており、それらが正しいことはわかっています.pthreadの実装からエラーが発生し、どこかにデータを渡しています。