我對C相對比較陌生,我試圖讓我的銀行家算法在下面工作。邏輯是堅實的,但我收到編譯錯誤,我不能解決。主要的是處理我的結構,該結構保存算法的所有消費者數據。它讀取:C銀行家算法中的' - >'結構無效類型參數
"error: invalid type argument of ‘->’ (have ‘struct threadData’) t->allocation[t->customer_num][i] = t->allocation[t->customer_num][i] - t->requestOrRelease[i];"
這是許多類似的錯誤之一。
我知道你可能會發現更多,因爲我還沒有完成,所以如果你這樣做,就喊出來。
我一直在努力工作,我被困住了。任何幫助都是極好的。請記住,我知道有些事情我可能沒有做到儘可能好,但我希望我的代碼能夠工作,然後我會優化。
我知道代碼很長,但我非常感謝任何幫助。謝謝!
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <stdbool.h>
#define cust 5
#define reso 3
struct threadData
{
int available[reso];
int maximum[cust][reso];
int allocation[cust][reso];
int need[cust][reso];
int requestOrRelease[reso];
int customer_num;
} ;
void request_resources(struct threadData *t);
void release_resources(struct threadData *t);
int isSafeState(int work[],int need[][cust],int allocation[][cust]);
int sumVector(int input[]);
int sumRowCol(int input[][cust],int index,char c);
void fillRequestRelease(int * array,int max0,int max1,int max2);
int main(int argc, const char **argv)
{
int available[reso]; //number of resources available of each type
int maximum[cust][reso]; //maximum demand of each process
int allocation[cust][reso]; //number of resources of each type currently allocated
int need[cust][reso]; //remaining resouce needs of each process : maximum-allocation
pthread_mutex_t mutex;
pthread_mutex_init(&mutex,NULL); //Create mutex lock
bool end = false;
//===========================================
srand(time(NULL));
int i,j,r;
for(i = 0;i < argc;i++) //Initialize values in available array
{
r = atoi(argv[i]);
available[i] = r;
}
for(i = 0;i < cust;i++) //Initialize values in maximum array
{
for(j = 0;j < reso;j++)
{
r = rand()%8; //Random int between 0 and 7
reso[i][j] = r;
}
}
for(i = 0;i < cust;i++) //Initialize values in allocation/need arrays
{
for(j = 0;j < reso;j++)
{
allocation[i][j] = 0;
need[i][j] = 0;
}
}
//===========================================
threadData data;
data.available = available;
data.maximum = maximum;
data.allocation = allocation;
data.need = need;
data.requestOrRelease = requestOrRelease;
data.customer_num = 0;
pthread_t custRequest0,custRequest1,custRequest2,custRequest3,custRequest4;
pthread_t custRelease0,custRelease1,custRelease2,custRelease3,custRelease4;
while(true)
{
data.customer_num = 0;
fillRequestRelease(data.requestOrRelease,data.need[0][0],data.need[0][1],data.need[0][2]);
pthread_create(&custRequest0,NULL,(void *)data);
data.customer_num = 1;
fillRequestRelease(data.requestOrRelease,data.need[1][0],data.need[1][1],data.need[1][2]);
pthread_create(&custRequest1,NULL,(void *)data);
data.customer_num = 2;
fillRequestRelease(data.requestOrRelease,data.need[2][0],data.need[2][1],data.need[2][2]);
pthread_create(&custRequest2,NULL,(void *)data);
data.customer_num = 3;
fillRequestRelease(data.requestOrRelease,data.need[3][0],data.need[3][1],data.need[3][2]);
pthread_create(&custRequest3,NULL,(void *)data);
data.customer_num = 4;
fillRequestRelease(data.requestOrRelease,data.need[4][0],data.need[4][1],data.need[4][2]);
pthread_create(&custRequest4,NULL,(void *)data);
pthread_join(custRequest0,NULL);
pthread_join(custRequest1,NULL);
pthread_join(custRequest2,NULL);
pthread_join(custRequest3,NULL);
pthread_join(custRequest4,NULL);
data.customer_num = 0;
fillRequestRelease(data.requestOrRelease,data.allocation[0][0],data.allocation[0][1],data.allocation[0][2]);
pthread_create(&custRelease0,NULL,(void *)data);
data.customer_num = 1;
fillRequestRelease(data.requestOrRelease,data.allocation[1][0],data.allocation[1][1],data.allocation[1][2]);
pthread_create(&custRelease1,NULL,(void *)data);
data.customer_num = 2;
fillRequestRelease(data.requestOrRelease,data.allocation[2][0],data.need[2][1],data.need[2][2]);
pthread_create(&custRelease2,NULL,(void *)data);
data.customer_num = 3;
fillRequestRelease(data.requestOrRelease,data.allocation[3][0],data.allocation[3][1],data.allocation[3][2]);
pthread_create(&custRelease3,NULL,(void *)data);
data.customer_num = 4;
fillRequestRelease(data.requestOrRelease,data.allocation[4][0],data.allocation[4][1],data.allocation[4][2]);
pthread_create(&custRelease4,NULL,(void *)data);
pthread_join(custRelease0,NULL);
pthread_join(custRelease1,NULL);
pthread_join(custRelease2,NULL);
pthread_join(custRelease3,NULL);
pthread_join(custRelease4,NULL);
int nonZeroCount = 0;
for(i = 0;i < cust;i++)
{
for(j = 0;j < reso;j++)
{
if(need[i][j] > 0)
nonZeroCount++;
}
}
if(nonZeroCount == 0)
break;
}
printf("Program done!")
}
//Determines if system is in safe state
//Returns 0 if in safe state, else -1
int isSafeState(int work[],int need[][cust],int allocation[][cust]) //work = available
{
int i,j;
int temp1,temp2,trueCount,notSafeCount;
bool finish[sizeof(need)/sizeof(int)];
for (i = 0;i < sizeof(need)/sizeof(int);i++)
finish[i] = false;
trueCount = 0;
bool iexists = false;
while(true)
{
for(i = 0;i < sizeof(finish)/sizeof(int);i++)
{
temp1 = sumRowCol(need,i,'r');
temp2 = sumRowCol(work,0,'r');
if(finish[i] == false && (temp1 <= temp2))
{
finish[i] = true;
for(j = 0;j < reso;j++)
work[j] = work[j] + allocation[i][j];
trueCount++;
iexists = true;
}
}
if(trueCount == sizeof(finish)/sizeof(int))
return 0;
else if(iexists == false)
return -1;
}
}
// Returns sum of given vector
int sumVector(int input[])
{
int sum = 0;
int i;
for(i = 0;i < sizeof(input)/sizeof(int);i++)
sum += input[i];
return sum;
}
//Returns sum of given input array row/column
int sumRowCol(int input[][cust],int index,char c)
{
int sum = 0;
int i;
if(c == 'r')
{
for(i = 0;i < sizeof(input)/sizeof(int);i++)
sum += input[index][i];
}
else
{
for(i = 0;i < sizeof(input)/sizeof(int);i++)
sum += input[i][index];=
}
return sum;
}
void request_resources(struct threadData t)
{
int i,j,safeState,custNum;
int tempAvailable[reso];
int tempMaximum[cust][reso];
int tempAllocation[cust][reso];
int tempNeed[cust][reso];
int tempRequest[reso];
bool end = false;
if(t->requestOrRelease[0] == 0 && t->requestOrRelease[1] == 0 && t->requestOrRelease[2] == 0)
return;
while(true)
{
custNum = t->customer_num;
for(int i = 0;i < reso;i++)
{
if(t->requestOrRelease[i] > t->need[custNum][i])
{
printf("Error: Request is greater than need. Aborting request to bank.");
pthread_mutex_unlock(&mutex);
return;
}
}
int passCount;
while(true)
{
passCount = 0;
for(int i = 0;i < reso;i++)
{
if(t->requestOrRelease[i] > t->available[i])
{
passCount--;
while(true)
{
if(t->requestOrRelease[i] <= t->available[i])
break;
}
}
passCount++;
}
if(passCount = reso)
break;
}
pthread_mutex_lock(&mutex); //get mutex lock
//Filling temporary arrays with current data
//===========================================
for(i = 0;i < cust;i++)
{
for(j = 0;j < reso;j++)
{
tempMaximum[i][j] = t->maximum[i][j];
tempAllocation[i][j] = t->allocation[i][j];
tempNeed[i][j] = t->need[i][j];
}
tempAvailable[i] = t->available[i];
tempRequest[i] = t->requestOrRelease[i];
}
//===========================================
//"Pretend" to allocate resources
for(i = 0;i < reso;i++)
{
tempAvailable[i] = tempAvailable[i] - tempRequest[i];
tempAllocation[custNum][i] = tempAllocation[custNum][i] + tempRequest[i];
tempNeed[custNum][i] = tempNeed[custNum][i] - tempRequest[i];
}
safeState = isSafeState(tempAvailable,tempNeed,tempAllocation);
if(safeState == 0)
{
for(i = 0;i < reso;i++)
{
t->available[i] = t->available[i] - tempRequest[i];
t->allocation[custNum][i] = t->allocation[custNum][i] + tempRequest[i];
t->need[custNum][i] = t->need[custNum][i] - tempRequest[i];
}
pthread_mutex_unlock(&mutex); //release mutex lock
end = true;
}
if(end == true)
break;
pthread_mutex_unlock(&mutex); //release mutex lock
}
printf("Request resources granted to consumer: %d",custNum);
}
void release_resources(struct threadData t)
{
int i;
pthread_mutex_lock(&mutex); //get mutex lock
for(i = 0;i < reso;i++)
{
t->available[i] = t->available[i] + t->requestOrRelease[i];
t->allocation[t->customer_num][i] = t->allocation[t->customer_num][i] - t->requestOrRelease[i];
}
pthread_mutex_unlock(&mutex); //release mutex lock
}
//Generates random integers for request and release vectors
void fillRequestRelease(int * array,int max0,int max1,int max2)
{
srand(time(NULL));
int r;
r = rand()%(max0+1);
array[0] = r;
r = rand()%(max1+1);
array[1] = r;
r = rand()%(max2+1);
array[2] = r;
}
編譯器應該給你受影響的行。請看看那條線並嘗試識別結構。你應該使用'。'用結構和' - >'指向struct。 – Mabus
你有'struct threadData t'而不是'struct threadData * t'來定義你的一些函數。你得到的錯誤信息是告訴你' - >'的類型是錯誤的,並且有警告會告訴你你的原型與你的定義不同。 – Dmitri