2013-04-01 82 views
0

OS-sim.hÇ訪問數組正確

typedef enum { 
    PROCESS_NEW = 0, 
    PROCESS_READY, 
    PROCESS_RUNNING, 
    PROCESS_WAITING, 
    PROCESS_TERMINATED 
} process_state_t; 

typedef struct _pcb_t { 
    const unsigned int pid; 
    const char *name; 
    const unsigned int static_priority; 
    process_state_t state;    <<---Trying to access this 
    op_t *pc; 
    struct _pcb_t *next; 
} pcb_t; 

file1.c中

static pcb_t **current; 

extern void yield(unsigned int cpu_id) 
{ 
    /* FIX ME */ 
    while (1) 
    { 
    pthread_mutex_lock(&current_mutex); 
    current[cpu_id].state = PROCESS_WAITING; ///<-------ERROR HERE 
    pthread_mutex_unlock(&current_mutex); 
    break; 
    } 
    schedule(cpu_id); 
} 

in main method(): 
current = malloc(sizeof(pcb_t*) * 10); 

我有錯誤在這行current[cpu_id].state = PROCESS_WAITING;

error: request for member ‘state’ in something not a structure or union 

這個錯誤是什麼意思?
這是不是正確的方式來訪問當前數組持有pcb_t?
如果是這樣,我該如何訪問當前數組?和狀態字段?

回答

5

你很可能在尋找:

current[cpu_id]->state = PROCESS_WAITING; 

類型的currentpcb_t **。所以current[cpu_id]的類型是pcb_t *

+0

所以你使用 - >當它是**和你使用。當它是*? – ealeon

+2

@sharth等待,如果他只爲10個指針malloc(sizeof(pcb_t *)* 10)分配內存,那麼它是如何工作的?他不能在'狀態'中存儲任何東西,因爲空間還沒有分配(?) – 2013-04-01 22:18:16

+0

@Armin:這是一個很好的觀點。我很高興看到您在單獨的答案,作爲我的編輯或這些評論中展開這一點。 –