2013-04-01 117 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:这是一个很好的观点。我很高兴看到您在单独的答案,作为我的编辑或这些评论中展开这一点。 –