所以我正在使用pthreads來並行解決問題的程序。現在,當我在函數:average_power中運行以下代碼時,我遇到了seg故障。分段錯誤或SizeOf使用不正確
這是代碼的相關部分,我敢肯定,錯誤的是什麼地方:
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);
}
所以這是工人函數從另一個函數,其中的這部分傳遞給它的結構ARGS沿線程被初始化並作出批示:
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;
}
所以我得到的錯誤或者是內部的一個線程段錯誤時average_power被稱爲在*工人結束(average_power是正確的,我已經證明那forsure)或者我得到一個e當我在我的循環中取消註釋下面一行來初始化數組時,會出現rror。
outputNew = (struct signal *) malloc(sizeof(struct signal));
我想確保outputNew,然後才放入ARG結構有自己的空間,所以它不會被覆蓋,但此行防止它編譯。我不正確嗎?否則,average_power從args結構中訪問這個變量(輸出),當它發生時它會得到一個段錯誤,我認爲這意味着某處的某個內存沒有被正確使用?對不起,如果這麼長時間,我可以澄清任何部分,如果我沒有足夠好的話。所有其他函數或調用與我的項目相關,我知道這些都是正確的,我的錯誤來自我的pthread實現並將數據傳遞到某處。
您是否嘗試過通過調試器中運行您的程序?像[valgrind](http://valgrind.org/info/tools.html)這樣的東西可以幫助你確定你的錯誤。看到[這個問題](http://stackoverflow.com/questions/981011/c-programming-debugging-with-pthreads)多一點建議調試pthreads程序。 – charleyc