C结构无效的类型参数' - >'在银行家算法

时间:2015-11-13 17:44:04

标签: c pointers struct

我对C比较陌生,我试图让我的Bankers算法在下面工作。逻辑是坚实的,但我得到编译错误,我可以很好地解决。主要是处理我的结构,该结构包含算法的所有消费者数据。它写着:

  

"错误:' - >'的无效类型参数(包含'struct threadData')      t->分配[t-> customer_num] [i] = t->分配[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;
}

3 个答案:

答案 0 :(得分:1)

据我所知,在request_resources()函数中,t不是指针变量。所以,而不是

t->requestOrRelease[0]

你应该使用

t.requestOrRelease[0]

等等。

答案 1 :(得分:1)

t不是指针,因此您必须编写t.field而不是t->field

答案 2 :(得分:0)

错误消息中的重要线索是“(有'struct threadData')”。

这意味着您在结构上使用指针解引用运算符->,而不是在结构指针上。请改为使用成员解除引用运算符.