現在、デッドロックとコア ダンプの両方が発生しています。
メインの外には次のものがあります
char buffer[SLOTCOUNT][SLOTSIZE];
int isEmpty[SLOTCOUNT];
FILE *myInFile;
FILE *myOutFile;
sem_t buf_lock, slot_avail, item_avail;
私が持っているメインの中に
int main(int argc, const char * argv[]){
//Create the basics of threads and intilize the sem
pthread_t consumer_t, producer_t;
sem_init(&buf_lock, 0, 1);
sem_init(&slot_avail, 0, 4);
sem_init(&item_avail, 0, 0);
/*Clear and clean the buffer*/
for(int i=0; i <= SLOTCOUNT; ++i){
isEmpty[i] = 0;
for(int j=0; j <= SLOTSIZE; ++j){
buffer[i][j] = NULL;
}
}
/*Check to make sure that the correct number of inputs have been provided*/
checkStart(argc);
/*Get the locations to the files and save it into a local variable. */
char inFile[sizeof(argv[1])];
char outFile[sizeof(argv[2])];
strcpy(inFile, argv[1]);
strcpy(outFile, argv[2]);
/*Load the file to read from and create the file to write to*/
fileReady(inFile, outFile);
/*Get the threads ready to return*/
pthread_create(&producer_t, NULL, producer, NULL);
pthread_create(&consumer_t, NULL, consumer, NULL);
pthread_join(producer_t, NULL);
pthread_join(consumer_t, NULL);
return 0;
}
そして最後に、プロデューサー関数
void* producer(){
/*Critical Section*/
sem_wait(&slot_avail);
printf("gets here\n");
sem_wait(&buf_lock);
printf("Never gets here\n");
for (int i = 0; i <= SLOTCOUNT; ++i){
if(buffer[i][0] == NULL) fread(buffer[i], sizeof(char), READSIZE, myInFile);
}
sem_post(&buf_lock);
sem_post(&item_avail);
/*Critical Section*/
}
現在、printf("Never gets here") は印刷されません。
私はそれを修正したいと思います。
ただし、をコメントアウトするとsem_wait(&buf_lock)
、出力されますが、コアダンプが発生します。
私が間違っていること、またはコアダンプをデバッグする方法についてのアイデア
編集:これが問題の最善の解決策ではないことは理解していますが、これはセマフォの理解を示すためのものです。
編集: コードの残りの部分は独立してテストされており、動作します。IE char のサイズ ... など
私も試してみました
int errno = sem_wait(&buf_lock);
printf("%s", strerror(errno));
何が起こっているのかを確認しますが、それでもロックされ、予想どおり印刷されません。