[英]Managing a linked list with pthreads
我正在嘗試模擬一個客戶行,該客戶行由一個鏈表表示,該線程具有一個線程,該線程計算一個客戶每單位時間進入的概率(具有預設的概率),並將該客戶添加到該行的末尾,櫃員線程從該行的開頭刪除了一個客戶,在每次模擬過程中,我總是在看似不可預測的點上遇到分段錯誤。 以下是我用於添加和從列表中刪除的代碼。 該錯誤似乎是由於在刪除櫃員時客戶線程正在嘗試添加而發生的。
void addToQ(int x, void *arg){
printf("ADD ATTEMPT\n");
struct shiftInfo *info = (struct shiftInfo *) arg;
if (head -> ID == -1){ //No clients in line
head -> ID = x;
head -> next = dummy;
tail = head;
}
else{ //clients already in line
tail -> next = (NODE *)malloc(sizeof(NODE));
tail = tail -> next;
tail -> ID = x;
tail -> next = dummy;
info -> inQ++;
}
printf("ADD SUCCESS\n");
}
//Removes node closest to root
//Assumes: root != end
void removeFromQ(void *arg){
printf("REMOVE ATTEMPT\n");
struct shiftInfo *info = (struct shiftInfo *) arg;
if (head -> next == dummy){ //only one element
printf("SCENARIO 1\n");
head -> ID = -1;
}
else{
printf("SCENARIO 2\n");
curr = head -> next;
head = curr -> next;
printf("Halfway\n");
if ((head -> next == NULL) || (head -> next == dummy))
tail = head;
}
info -> inQ--;
printf("REMOVE SUCCESS\n");
}
這里是線程函數:
void *customerT(void *arg){
int custID = 100;
float newCust = 0;
struct shiftInfo *info = (struct shiftInfo *) arg;
float aRate = info -> arrivalRate;
pthread_mutex_lock (&start); //Ensures Customers don't start before Tellers get to work
pthread_mutex_unlock (&start);
while(0<info -> simTime){
newCust = (double)rand() / (double)RAND_MAX;
if (newCust <= aRate){
pthread_mutex_lock (&start);
addToQ(custID,(void *)info);
pthread_mutex_unlock (&start);
custID++;
}
sleep(1/60);
}
pthread_exit(NULL);
}
void *teller(void *arg){
struct shiftInfo *info = (struct shiftInfo *) arg;
int clientID;
int clients = info -> inQ;
pthread_mutex_lock (&start); //Ensures tellers dont start before customers arrive
pthread_mutex_unlock (&start);
while(0<info -> simTime){
pthread_mutex_lock (&accessQ);
clients = info -> inQ;
if(head -> ID > 0){
pthread_mutex_lock (&start);
removeFromQ((void *)info);
pthread_mutex_unlock (&start);
printf("CLIENT OBTAINED\n");
}
printf("In Q %d\n", clients);
pthread_mutex_unlock (&accessQ);
sleep((info -> serviceTime)/60);
}
pthread_exit(NULL);
}
如果相關,則這里是管理器線程函數:
//Creates Customer thread, Teller threads, and timer thread
void *mainThread(void *info){
head = (NODE *)malloc(sizeof(NODE));
head -> ID = -1;
head -> next = dummy;
tail = head;
int status;
struct shiftInfo *I = info;
pthread_mutex_init(&start, NULL);
pthread_mutex_init(&removeID, NULL);
pthread_mutex_init(&accessQ, NULL);
pthread_mutex_lock (&start);
printf("Scheduling Tellers\n");
pthread_t threads[(I -> tellers)];
for (int i = 0; i < (I -> tellers); i++){
I -> threadID = i;
status = pthread_create(&threads[i], NULL, teller, (void *)info);
if (status){
printf("ERROR CODE: %d\n", status);
exit(-1);
}
}
printf("Preparing Customers\n");
pthread_t customer;
status = pthread_create(&customer, NULL, customerT, (void *)info);
if (status){
printf("ERROR CODE: %d\n", status);
exit(-1);
}
printf("Making Timer\n");
pthread_t time;
status = pthread_create(&time, NULL, timer, (void *)info);
if (status){
printf("ERROR CODE: %d\n", status);
exit(-1);
}
pthread_mutex_unlock (&start);
}
這段代碼在這里看起來是錯誤的:
curr = head -> next;
head = curr -> next;
考慮一下您的列表看起來像這樣的情況: head -> node -> dummy
。 運行完上面的代碼后, head
將指向您肯定不想要的dummy
。 嘗試將代碼更改為此:
curr = head;
head = curr -> next;
編輯:獲得否定客戶的原因是在addToQ
,當列表開始為空時( if
語句的上半部分),您不會增加info->inQ
addToQ
。 總是需要info->inQ++
,就像總是在removeFromQueue
有info->inQ--
一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.