0
我正在實現使用POSIX共享內存和未命名信號量的客戶端服務器。期望服務器同時處理多個客戶端。該代碼適用於單個客戶端,但不適用於多個客戶端。 POSIX的操作與管理,posix爲多個客戶端共享內存
enum { MAX_MSG = 256 };
enum { CLIENT_SEM, // semaphore is 1 if server is available for use by client
MSG_FOR_DAEMON_SEM, // semaphore is 1 if shm contains msg for daemon
MSG_FOR_CLIENT_SEM, // semaphore is 1 if shm contains msg for client
MSG_FOR_SERVER_SEM, // semaphore is 1 if shm contains msg for server
N_SEMS };
typedef struct {
sem_t sems[N_SEMS]; // semaphore sent for sync
pid_t clientPid; // pid of current client
char msg[MAX_MSG]; // current message being sent
int max_matrix_size; //max rows a square matrix can have
}Comm;
// server calls setup_comm with doCreate=1 and creates shared mem of size max_clients * sizeof(Comm)
// client calls setup_comm with doCreate=0 and in return gets the mmap pointer to the shared memory created by the server
Comm* setup_comm(const char *shmPosixName, int doCreate, int max_clients);
的問題是,要處理多個客戶端,我們需要保持通訊結構的陣列;那就是Comm[max_clients]
而不是我目前使用的(單個Comm結構)?對於每個客戶端,服務器都需要管理Comm陣列並將適當的元素從該陣列返回給客戶端。客戶端反過來將使用該塊來同步Comm元素中信號量的操作?或者多個客戶端可以使用單個Comm結構進行處理?