我正在使用共享内存进行客户端/服务器通信系统。我正在向客户端写入服务器将读取的PID。问题是,当我尝试读取该值时,它只读取最不重要的8位。这是代码的一大块:
int receive(void* data, void * connection) {
connection_t * c = (connection_t *)connection;
printf("PRINTING: %d \n", *(c->address));
return 0;
}
void notify_server() {
int mem_id = shmget(PUBLIC_KEY, PAGE_SIZE, PERMISSION_FLAGS);
int * address = shmat(mem_id, NULL, 0);
int sem_id = semget(PUBLIC_KEY, 1, PERMISSION_FLAGS);
*address = (pid_t)PRIVATE_KEY;
printf("FROM CLIENT, PRINT MY PID --- %d\n", *address);
}
结构是:
typedef struct {
int id;
char * address;
struct shmid_ds data;
int sem_id;
struct sembuf sb;
} connection_t;
答案 0 :(得分:1)
address
字段为char
:char * address;
当然*(c->address)
会给出一个8位的char
。