I made a multi threaded server, it has a global pointer to a linked list, in thread I am trying to insert some data, but that data (which I insert) not retaining, is that possible that in threads that global value is not retaining. I am using the following code (it's a simplest version.)
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 */
}
}