我做了一个多线程服务器,它有一个指向链表的全局指针,在线程中我试图插入一些数据,但是数据(我插入的)没有保留,有可能在线程中全局值没有保留. 我正在使用以下代码(这是最简单的版本。)
struct node {
int cn; //
struct node *next;
};
/*GLOBAL VARIABLES*/
struct node *start; //Global pointer to Linked List
/* END */
int main(int argc, char *argv[]) {
start = (struct node *)malloc(sizeof(struct node));
start -> cn =0;
int pid;
/* Declaration of Server goes here */
printf("Server Running ...\n");
while (1) {
/* accepting socket*/
pid = fork();
if (pid < 0)
error("ERROR on fork");
if (pid == 0) {
close(serverSocket);
dostuff(childSocket,start);
exit(0);
}
else
close(childSocket);
}
return 0;
}
void dostuff (int sock, struct node *pointer){
returnStatus = read(sock, &requestToJoin, sizeof(int));
if (returnStatus < 0)
error("ERROR reading from socket");
else{
/* Insert Data to global Pointer */
}
}