我也认为主解决方案是不错的选择:
/* Process struct */
typedef struct _process_t {
unsigned long process_id;
struct _process_t *next; /* next process */
struct _process_t *prev; /* previous process */
struct _process_master *master_process; /* Master process */
int (*accepting_socket) (struct _process_t *); /* process accepet function */
char *received_data_buffer; /* the data received over the socket */
} process_t;
/* List of process */
typedef struct _process_list {
process_t *head;
process_t *tail;
int count;
} process_list;
/* The master process */
typedef struct _process_master {
process_list socket_listners; /* All the process listening */
process_list ready_listners; /* Process ready to listen and receive*/
..... /* Complete this struct */
} process_master;
如果您发现带有进程的解决方案很慢,您可以threads
改用(它们共享相同的内存),但代码可能会增加复杂性并且很难跟踪错误。
由于获取互斥体的成本和所有进程之间的上下文切换,第二种解决方案并不比第一种快。