我正在尝试模拟一个银行,其中包含一个由链表表示的客户线,其中一个线程计算客户每单位时间走进的概率(以预设的概率)并将客户添加到结束时当几个出纳员线程从线路的开头移除客户时,我在每次模拟期间在看似不可预测的点处不断出现分段错误。下面是我用来添加和从列表中删除的代码。似乎发生错误是因为客户线程正在尝试在柜员移除时添加。
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);
}
答案 0 :(得分:2)
这里的代码看起来不对:
curr = head -> next;
head = curr -> next;
考虑您的列表如下所示的情况:head -> node -> dummy
。运行上述代码后,head
将指向dummy
,您肯定不需要。尝试将代码更改为:
curr = head;
head = curr -> next;
修改:您获得否定客户的原因是addToQ
,当列表开始为空时(info->inQ
语句的上半部分),您不会增加if
。您总是需要info->inQ++
,就像info->inQ--
总是在removeFromQueue
中一样。